http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FileConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FileConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FileConverterTest.java index 5282b84..858fa06 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FileConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FileConverterTest.java @@ -41,11 +41,14 @@ public class FileConverterTest { FileConverter instance = new FileConverter(); File result; - assertThat(instance.convert(null, context)).isNull(); + assertThat(instance.convert(null)).isNull(); URL testfileUrl = getClass().getResource("/testfile.properties"); System.out.println(testfileUrl.toString()); - result = instance.convert(testfileUrl.toString(), context); + ConversionContext.set(context); + result = instance.convert(testfileUrl.toString()); + ConversionContext.reset(); + assertThat(result).isNotNull(); assertThat(context.getSupportedFormats().contains("<File> (FileConverter)")).isTrue(); }
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java index c5c4866..11fd864 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java @@ -20,7 +20,6 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; import static org.assertj.core.api.Assertions.assertThat; @@ -39,7 +38,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_Decimal() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.decimal", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(1.23456789f).isCloseTo(valueRead, within(0.0f)); @@ -52,7 +51,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_DecimalNegative() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.decimalNegative", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(-1.23456789f).isCloseTo(valueRead, within(0.0f)); @@ -65,7 +64,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_Integer() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.integer", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(100f).isCloseTo(valueRead, within(0.0f)); @@ -78,7 +77,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_Hex1() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.hex1", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(255f).isCloseTo(valueRead, within(0.0f)); @@ -91,7 +90,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_Hex2() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.hex2", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(-255f).isCloseTo(valueRead, within(0.0f)); @@ -104,7 +103,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_Hex3() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.hex3", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(255f).isCloseTo(valueRead, within(0.0f)); @@ -117,7 +116,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_MinValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.min", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(valueRead).isCloseTo(Float.MIN_VALUE, within(0.0f)); @@ -130,7 +129,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_MaxValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.max", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(valueRead).isCloseTo(Float.MAX_VALUE, within(0.0f)); @@ -143,7 +142,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_NaNValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.nan", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(valueRead).isCloseTo(Float.NaN, within(0.0f)); @@ -156,7 +155,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_PositiveInfinityValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.pi", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(valueRead).isCloseTo(Float.POSITIVE_INFINITY, within(0.0f)); @@ -169,7 +168,7 @@ public class FloatConverterTest { */ @Test public void testConvert_Float_NegativeInfinityValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Float valueRead = config.get("tests.converter.float.ni", Float.class); assertThat(valueRead!=null).isTrue(); assertThat(valueRead).isCloseTo(Float.NEGATIVE_INFINITY, within(0.0f)); @@ -177,16 +176,17 @@ public class FloatConverterTest { @Test(expected = ConfigException.class) public void testConvert_FloatInvalid() throws ConfigException { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); config.get("tests.converter.float.invalid", Float.class); } @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Float.class)).build(); + ConversionContext.set(context); FloatConverter converter = new FloatConverter(); - converter.convert("", context); - + converter.convert(""); + ConversionContext.reset(); assertThat(context.getSupportedFormats().contains("<float> (FloatConverter)")).isTrue(); assertThat(context.getSupportedFormats().contains("MIN_VALUE (FloatConverter)")).isTrue(); assertThat(context.getSupportedFormats().contains("MAX_VALUE (FloatConverter)")).isTrue(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/InstantConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/InstantConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/InstantConverterTest.java index 40257c3..50c3c7c 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/InstantConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/InstantConverterTest.java @@ -41,9 +41,9 @@ public class InstantConverterTest { @Test public void convert() throws Exception { InstantConverter conv = new InstantConverter(); - Instant value = conv.convert("2007-12-03T10:15:30.00Z", context); + Instant value = conv.convert("2007-12-03T10:15:30.00Z"); assertThat(Instant.parse("2007-12-03T10:15:30.00Z")).isEqualTo(value); - value = conv.convert("foo", context); + value = conv.convert("foo"); assertThat(value).isNull(); } @@ -59,11 +59,13 @@ public class InstantConverterTest { @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(Instant.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Instant.class)).build(); + ConversionContext.set(context); InstantConverter converter = new InstantConverter(); - converter.convert("", localcontext); + converter.convert(""); + ConversionContext.reset(); - assertThat(localcontext.getSupportedFormats().toString().contains(" (InstantConverter)")).isTrue(); + assertThat(context.getSupportedFormats().toString().contains(" (InstantConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java index 72b4eee..d6b5081 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java @@ -20,7 +20,6 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; import org.junit.Test; @@ -39,7 +38,7 @@ public class IntegerConverterTest { */ @Test public void testConvert_Integer_Decimal() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Integer valueRead = config.get("tests.converter.integer.decimal", Integer.class); assertThat(valueRead != null).isTrue(); assertThat(101).isEqualTo(valueRead.intValue()); @@ -52,7 +51,7 @@ public class IntegerConverterTest { */ @Test public void testConvert_Integer_Octal() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Integer valueRead = config.get("tests.converter.integer.octal", Integer.class); assertThat(valueRead != null).isTrue(); assertThat(Integer.decode("02").intValue()).isEqualTo(valueRead.intValue()); @@ -65,7 +64,7 @@ public class IntegerConverterTest { */ @Test public void testConvert_Integer_Hex() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Integer valueRead = config.get("tests.converter.integer.hex.lowerX", Integer.class); assertThat(valueRead != null).isTrue(); assertThat(Integer.decode("0x2F").intValue()).isEqualTo(valueRead.intValue()); @@ -81,7 +80,7 @@ public class IntegerConverterTest { */ @Test public void testConvert_NotPresent() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Integer valueRead = config.get("tests.converter.integer.foo", Integer.class); assertThat(valueRead != null).isFalse(); } @@ -93,7 +92,7 @@ public class IntegerConverterTest { */ @Test public void testConvert_Integer_MinValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Integer valueRead = config.get("tests.converter.integer.min", Integer.class); assertThat(valueRead != null).isTrue(); assertThat(valueRead.intValue()).isEqualTo(Integer.MIN_VALUE); @@ -106,7 +105,7 @@ public class IntegerConverterTest { */ @Test public void testConvert_Integer_MaxValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Integer valueRead = config.get("tests.converter.integer.max", Integer.class); assertThat(valueRead != null).isTrue(); assertThat(valueRead.intValue()).isEqualTo(Integer.MAX_VALUE); @@ -114,15 +113,17 @@ public class IntegerConverterTest { @Test(expected = ConfigException.class) public void testConvert_IntegerInvalid() throws ConfigException { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); config.get("tests.converter.integer.invalid", Integer.class); } @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Integer.class)).build(); + ConversionContext.set(context); IntegerConverter converter = new IntegerConverter(); - converter.convert("", context); + converter.convert(""); + ConversionContext.reset(); assertThat(context.getSupportedFormats().contains("<int> (IntegerConverter)")).isTrue(); assertThat(context.getSupportedFormats().contains("MIN_VALUE (IntegerConverter)")).isTrue(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateConverterTest.java index 341254b..57a8ea5 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateConverterTest.java @@ -41,9 +41,9 @@ public class LocalDateConverterTest { @Test public void convert() throws Exception { LocalDateConverter conv = new LocalDateConverter(); - LocalDate value = conv.convert("2007-12-03", context); + LocalDate value = conv.convert("2007-12-03"); assertThat(LocalDate.parse("2007-12-03")).isEqualTo(value); - value = conv.convert("foo", context); + value = conv.convert("foo"); assertThat(value).isNull(); } @@ -59,11 +59,13 @@ public class LocalDateConverterTest { @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(LocalDate.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(LocalDate.class)).build(); + ConversionContext.set(context); LocalDateConverter converter = new LocalDateConverter(); - converter.convert("", localcontext); + converter.convert(""); + ConversionContext.reset(); - assertThat(localcontext.getSupportedFormats().toString().contains(" (LocalDateConverter)")).isTrue(); + assertThat(context.getSupportedFormats().toString().contains(" (LocalDateConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverterTest.java index b92d6fa..5fbe82c 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverterTest.java @@ -41,9 +41,9 @@ public class LocalDateTimeConverterTest { @Test public void convert() throws Exception { LocalDateTimeConverter conv = new LocalDateTimeConverter(); - LocalDateTime value = conv.convert("2007-12-03T10:15:30", context); + LocalDateTime value = conv.convert("2007-12-03T10:15:30"); assertThat(LocalDateTime.parse("2007-12-03T10:15:30")).isEqualTo(value); - value = conv.convert("foo", context); + value = conv.convert("foo"); assertThat(value).isNull(); } @@ -59,11 +59,13 @@ public class LocalDateTimeConverterTest { @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(LocalDateTime.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(LocalDateTime.class)).build(); + ConversionContext.set(context); LocalDateTimeConverter converter = new LocalDateTimeConverter(); - converter.convert("", localcontext); + converter.convert(""); + ConversionContext.reset(); - assertThat(localcontext.getSupportedFormats().toString().contains(" (LocalDateTimeConverter)")).isTrue(); + assertThat(context.getSupportedFormats().toString().contains(" (LocalDateTimeConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalTimeConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalTimeConverterTest.java index 6b8707d..876f3fb 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalTimeConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LocalTimeConverterTest.java @@ -41,9 +41,9 @@ public class LocalTimeConverterTest { @Test public void convert() throws Exception { LocalTimeConverter conv = new LocalTimeConverter(); - LocalTime value = conv.convert("10:15:30", context); + LocalTime value = conv.convert("10:15:30"); assertThat(LocalTime.parse("10:15:30")).isEqualTo(value); - value = conv.convert("foo", context); + value = conv.convert("foo"); assertThat(value).isNull(); } @@ -59,11 +59,13 @@ public class LocalTimeConverterTest { @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(LocalTime.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(LocalTime.class)).build(); + ConversionContext.set(context); LocalTimeConverter converter = new LocalTimeConverter(); - converter.convert("", localcontext); + converter.convert(""); + ConversionContext.reset(); - assertThat(localcontext.getSupportedFormats().toString().contains(" (LocalTimeConverter)")).isTrue(); + assertThat(context.getSupportedFormats().toString().contains(" (LocalTimeConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java index 06e62a7..bfab1c9 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java @@ -20,7 +20,6 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; import org.junit.Test; @@ -39,7 +38,7 @@ public class LongConverterTest { */ @Test public void testConvert_Long_Decimal() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Long valueRead = config.get("tests.converter.long.decimal", Long.class); assertThat(valueRead != null).isTrue(); assertThat(101).isEqualTo(valueRead.intValue()); @@ -52,7 +51,7 @@ public class LongConverterTest { */ @Test public void testConvert_Long_Octal() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Long valueRead = config.get("tests.converter.long.octal", Long.class); assertThat(valueRead != null).isTrue(); assertThat(Long.decode("02").intValue()).isEqualTo(valueRead.intValue()); @@ -65,7 +64,7 @@ public class LongConverterTest { */ @Test public void testConvert_Long_Hex() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Long valueRead = config.get("tests.converter.long.hex.lowerX", Long.class); assertThat(valueRead != null).isTrue(); assertThat(Long.decode("0x2F").intValue()).isEqualTo(valueRead.intValue()); @@ -81,7 +80,7 @@ public class LongConverterTest { */ @Test public void testConvert_NotPresent() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Long valueRead = config.get("tests.converter.long.foo", Long.class); assertThat(valueRead != null).isFalse(); } @@ -93,7 +92,7 @@ public class LongConverterTest { */ @Test public void testConvert_Long_MinValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Long valueRead = config.get("tests.converter.long.min", Long.class); assertThat(valueRead != null).isTrue(); assertThat(valueRead.longValue()).isEqualTo(Long.MIN_VALUE); @@ -106,7 +105,7 @@ public class LongConverterTest { */ @Test public void testConvert_Long_MaxValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Long valueRead = config.get("tests.converter.long.max", Long.class); assertThat(valueRead != null).isTrue(); assertThat(valueRead.longValue()).isEqualTo(Long.MAX_VALUE); @@ -114,15 +113,17 @@ public class LongConverterTest { @Test(expected = ConfigException.class) public void testConvert_LongInvalid() throws ConfigException { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); config.get("tests.converter.long.invalid", Long.class); } @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Long.class)).build(); + ConversionContext.set(context); LongConverter converter = new LongConverter(); - converter.convert("", context); + converter.convert(""); + ConversionContext.reset(); assertThat(context.getSupportedFormats().contains("<long> (LongConverter)")).isTrue(); assertThat(context.getSupportedFormats().contains("MIN_VALUE (LongConverter)")).isTrue(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java index 76bc778..41427ab 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java @@ -19,7 +19,6 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; import org.junit.Test; import java.math.BigDecimal; @@ -41,7 +40,7 @@ public class NumberConverterTest { */ @Test public void testConvert_Decimal() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Number valueRead = config.get("tests.converter.bd.decimal", Number.class); assertThat(valueRead).isNotNull(); assertThat(101L).isEqualTo(valueRead); @@ -55,7 +54,7 @@ public class NumberConverterTest { */ @Test public void testConvert_Hex() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Number valueRead = config.get("tests.converter.bd.hex.lowerX", Number.class); assertThat(valueRead).isNotNull(); assertThat(Long.valueOf("47")).isEqualTo(valueRead); @@ -71,7 +70,7 @@ public class NumberConverterTest { */ @Test public void testConvert_NotPresent() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Number valueRead = config.get("tests.converter.bd.foo", Number.class); assertThat(valueRead).isNull(); } @@ -83,7 +82,7 @@ public class NumberConverterTest { */ @Test public void testConvert_BigValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Number valueRead = config.get("tests.converter.bd.big", Number.class); assertThat(valueRead).isNotNull(); assertThat(new BigDecimal("101666666666666662333337263723628763821638923628193612983618293628763")) @@ -97,7 +96,7 @@ public class NumberConverterTest { */ @Test public void testConvert_BigFloatValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Number valueRead = config.get("tests.converter.bd.bigFloat", Number.class); assertThat(valueRead).isNotNull(); assertThat(new BigDecimal("1016666666666666623333372637236287638216389293628763.1016666666666666623333372" + @@ -112,7 +111,7 @@ public class NumberConverterTest { */ @Test public void testConvert_PositiveInfinityValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Number valueRead = config.get("tests.converter.double.pi", Number.class); assertThat(valueRead).isNotNull(); assertThat(valueRead.doubleValue()).isCloseTo(Double.POSITIVE_INFINITY, within(0.0d)); @@ -125,7 +124,7 @@ public class NumberConverterTest { */ @Test public void testConvert_NegativeInfinityValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Number valueRead = config.get("tests.converter.double.ni", Number.class); assertThat(valueRead).isNotNull(); assertThat(valueRead.doubleValue()).isCloseTo(Double.NEGATIVE_INFINITY, within(0.0d)); @@ -138,7 +137,7 @@ public class NumberConverterTest { */ @Test public void testConvert_NaNValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Number valueRead = config.get("tests.converter.double.nan", Number.class); assertThat(valueRead).isNotNull(); assertThat(valueRead.doubleValue()).isCloseTo(Double.NaN, within(0.0d)); @@ -146,15 +145,17 @@ public class NumberConverterTest { @Test(expected = ConfigException.class) public void testConvert_NumberInvalid() throws ConfigException { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); config.get("tests.converter.bd.invalid", Number.class); } @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Number.class)).build(); + ConversionContext.set(context); NumberConverter converter = new NumberConverter(); - converter.convert("", context); + converter.convert(""); + ConversionContext.reset(); assertThat(context.getSupportedFormats().contains("<double>, <long> (NumberConverter)")).isTrue(); assertThat(context.getSupportedFormats().contains("POSITIVE_INFINITY (NumberConverter)")).isTrue(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverterTest.java index 3daf49f..9d6c966 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverterTest.java @@ -18,7 +18,6 @@ */ package org.apache.tamaya.core.internal.converters; -import java.time.LocalDate; import org.apache.tamaya.spi.ConversionContext; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,9 +41,9 @@ public class OffsetDateTimeConverterTest { @Test public void convert() throws Exception { OffsetDateTimeConverter conv = new OffsetDateTimeConverter(); - OffsetDateTime value = conv.convert("2007-12-03T10:15:30+01:00", context); + OffsetDateTime value = conv.convert("2007-12-03T10:15:30+01:00"); assertThat(OffsetDateTime.parse("2007-12-03T10:15:30+01:00")).isEqualTo(value); - value = conv.convert("foo", context); + value = conv.convert("foo"); assertThat(value).isNull(); } @@ -60,11 +59,13 @@ public class OffsetDateTimeConverterTest { @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(OffsetDateTime.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(OffsetDateTime.class)).build(); + ConversionContext.set(context); OffsetDateTimeConverter converter = new OffsetDateTimeConverter(); - converter.convert("", localcontext); + converter.convert(""); + ConversionContext.reset(); - assertThat(localcontext.getSupportedFormats().toString().contains(" (OffsetDateTimeConverter)")).isTrue(); + assertThat(context.getSupportedFormats().toString().contains(" (OffsetDateTimeConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverterTest.java index dc21c85..daf3476 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverterTest.java @@ -41,9 +41,9 @@ public class OffsetTimeConverterTest { @Test public void convert() throws Exception { OffsetTimeConverter conv = new OffsetTimeConverter(); - OffsetTime value = conv.convert("10:15:30+01:00", context); + OffsetTime value = conv.convert("10:15:30+01:00"); assertThat(OffsetTime.parse("10:15:30+01:00")).isEqualTo(value); - value = conv.convert("foo", context); + value = conv.convert("foo"); assertThat(value).isNull(); } @@ -59,11 +59,13 @@ public class OffsetTimeConverterTest { @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(OffsetTime.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(OffsetTime.class)).build(); + ConversionContext.set(context); OffsetTimeConverter converter = new OffsetTimeConverter(); - converter.convert("", localcontext); + converter.convert(""); + ConversionContext.reset(); - assertThat(localcontext.getSupportedFormats().toString().contains(" (OffsetTimeConverter)")).isTrue(); + assertThat(context.getSupportedFormats().toString().contains(" (OffsetTimeConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OptionalConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OptionalConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OptionalConverterTest.java index e928a94..80c8cda 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OptionalConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/OptionalConverterTest.java @@ -20,10 +20,11 @@ package org.apache.tamaya.core.internal.converters; import java.util.List; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.Configuration; import org.junit.Test; import java.util.Optional; -import org.apache.tamaya.ConfigurationProvider; + import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; import static org.assertj.core.api.Assertions.*; @@ -32,23 +33,25 @@ public class OptionalConverterTest { @Test public void nullConversionYieldsEmptyOptional() { - final Optional<?> result = new OptionalConverter().convert(null, null); + final Optional<?> result = new OptionalConverter().convert(null); assertThat(result).isNotNull(); assertThat(result.isPresent()).isFalse(); } @Test(expected = ConfigException.class) public void emulateExceptionWhenGivenContextIsNull() { - new OptionalConverter().convert("JustATestValueThatIsIgnored", null); + new OptionalConverter().convert("JustATestValueThatIsIgnored"); } @Test public void testOptionalString() { TypeLiteral<List<String>> listOfStringTypeLiteral = new TypeLiteral<List<String>>() { }; - ConversionContext ctx = new ConversionContext.Builder("testOptionalString", listOfStringTypeLiteral).build(); + ConversionContext context = new ConversionContext.Builder("testOptionalString", listOfStringTypeLiteral).build(); + ConversionContext.set(context); + final Optional<String> result = new OptionalConverter().convert("astring"); + ConversionContext.reset(); - final Optional<String> result = new OptionalConverter().convert("astring", ctx); assertThat(result).isNotNull(); assertThat(result.isPresent()).isTrue(); assertThat(result.get()).isEqualTo("astring"); @@ -58,11 +61,14 @@ public class OptionalConverterTest { public void testOptionalInteger() { TypeLiteral<List<Integer>> listOfIntegerTypeLiteral = new TypeLiteral<List<Integer>>() { }; - ConversionContext ctx = new ConversionContext.Builder("testOptionalInteger", listOfIntegerTypeLiteral) - .setConfiguration(ConfigurationProvider.getConfiguration()) + ConversionContext context = new ConversionContext.Builder("testOptionalInteger", listOfIntegerTypeLiteral) + .setConfiguration(Configuration.current()) .build(); + ConversionContext.set(context); + + final Optional<Integer> result = new OptionalConverter().convert("11"); + ConversionContext.reset(); - final Optional<Integer> result = new OptionalConverter().convert("11", ctx); assertThat(result).isNotNull(); assertThat(result.isPresent()).isTrue(); assertThat(result.get().intValue()).isEqualTo(11); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/PathConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/PathConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/PathConverterTest.java index 4e2f153..0ecafcb 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/PathConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/PathConverterTest.java @@ -42,25 +42,25 @@ public class PathConverterTest { @Test public void convert() throws Exception { PathConverter conv = new PathConverter(); - Path value = conv.convert("testRoot", context); + Path value = conv.convert("testRoot"); assertThat(Paths.get("testRoot")).isEqualTo(value); - value = conv.convert("foo", context); + value = conv.convert("foo"); assertThat(value).isNotNull(); } @Test public void convertNull() throws Exception { PathConverter conv = new PathConverter(); - Path value = conv.convert(null, context); + Path value = conv.convert(null); assertThat(value).isNull(); - value = conv.convert("", context); + value = conv.convert(""); assertThat(value).isNull(); } @Test public void convertInvalidPath() throws Exception { PathConverter conv = new PathConverter(); - Path value = conv.convert("/invalid:/\u0000", context); + Path value = conv.convert("/invalid:/\u0000"); assertThat(value).isNull(); } @@ -76,10 +76,13 @@ public class PathConverterTest { @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(Path.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Path.class)).build(); + ConversionContext.set(context); PathConverter converter = new PathConverter(); - converter.convert("notempty", localcontext); - assertThat(localcontext.getSupportedFormats().contains("<File> (PathConverter)")).isTrue(); + converter.convert("notempty"); + ConversionContext.reset(); + + assertThat(context.getSupportedFormats().contains("<File> (PathConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java index 9f9ba72..fc54aa8 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java @@ -20,7 +20,6 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; import org.junit.Test; @@ -39,7 +38,7 @@ public class ShortConverterTest { */ @Test public void testConvert_Short_Decimal() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Short valueRead = config.get("tests.converter.short.decimal", Short.class); assertThat(valueRead != null).isTrue(); assertThat(101).isEqualTo(valueRead.intValue()); @@ -52,7 +51,7 @@ public class ShortConverterTest { */ @Test public void testConvert_Short_Octal() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Short valueRead = config.get("tests.converter.short.octal", Short.class); assertThat(valueRead != null).isTrue(); assertThat(Short.decode("02").intValue()).isEqualTo(valueRead.intValue()); @@ -65,7 +64,7 @@ public class ShortConverterTest { */ @Test public void testConvert_Short_Hex() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Short valueRead = config.get("tests.converter.short.hex.lowerX", Short.class); assertThat(valueRead != null).isTrue(); assertThat(Short.decode("0x2F").intValue()).isEqualTo(valueRead.intValue()); @@ -81,7 +80,7 @@ public class ShortConverterTest { */ @Test public void testConvert_NotPresent() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Short valueRead = config.get("tests.converter.short.foo", Short.class); assertThat(valueRead != null).isFalse(); } @@ -93,7 +92,7 @@ public class ShortConverterTest { */ @Test public void testConvert_Short_MinValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Short valueRead = config.get("tests.converter.short.min", Short.class); assertThat(valueRead != null).isTrue(); assertThat(valueRead.intValue()).isEqualTo(Short.MIN_VALUE); @@ -106,7 +105,7 @@ public class ShortConverterTest { */ @Test public void testConvert_Short_MaxValue() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); Short valueRead = config.get("tests.converter.short.max", Short.class); assertThat(valueRead != null).isTrue(); assertThat(valueRead.intValue()).isEqualTo(Short.MAX_VALUE); @@ -115,15 +114,17 @@ public class ShortConverterTest { @Test(expected = ConfigException.class) public void testConvert_ShortInvalid() throws ConfigException { - Configuration config = ConfigurationProvider.getConfiguration(); + Configuration config = Configuration.current(); config.get("tests.converter.short.invalid", Short.class); } @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Short.class)).build(); + ConversionContext.set(context); ShortConverter converter = new ShortConverter(); - converter.convert("", context); + converter.convert(""); + ConversionContext.reset(); assertThat(context.getSupportedFormats().contains("short (ShortConverter)")).isTrue(); assertThat(context.getSupportedFormats().contains("MIN_VALUE (ShortConverter)")).isTrue(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/SupplierConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/SupplierConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/SupplierConverterTest.java index 3d52333..0fd0585 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/SupplierConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/SupplierConverterTest.java @@ -44,14 +44,16 @@ public class SupplierConverterTest { SupplierConverter instance = new SupplierConverter(); Supplier<String> stringResult; TypeLiteral listStringTypeLiteral = new TypeLiteral<List<String>> () {}; - ConversionContext stringContext = new ConversionContext.Builder(listStringTypeLiteral).build(); - - stringResult = instance.convert(null, stringContext); + ConversionContext context = new ConversionContext.Builder(listStringTypeLiteral).build(); + ConversionContext.set(context); + stringResult = instance.convert(null); assertThat(stringResult.get()).isNull(); - stringResult = instance.convert("aString", stringContext); + stringResult = instance.convert("aString"); assertThat(stringResult.get()).isEqualTo("aString"); - + + ConversionContext.reset(); + Supplier<InetAddress> addressResult; Configuration mockConfig = Mockito.mock(Configuration.class); @@ -61,9 +63,11 @@ public class SupplierConverterTest { ConversionContext myConverterContext = new ConversionContext.Builder(myConverterTypeLiteral) .setConfiguration(mockConfig) .build(); + ConversionContext.set(myConverterContext); + addressResult = instance.convert("someKey"); - addressResult = instance.convert("someKey", myConverterContext); assertThat(addressResult.get() instanceof InetAddress).isTrue(); + ConversionContext.reset(); } @@ -85,7 +89,7 @@ public class SupplierConverterTest { private class MyConverter<T extends InetAddress> implements PropertyConverter<InetAddress> { @Override - public InetAddress convert(String value, ConversionContext context) { + public InetAddress convert(String value) { return Mockito.mock(InetAddress.class); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java index 61f84fa..7839233 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java @@ -38,47 +38,49 @@ public class URIConverterTest { @Test public void testConvert_URI() throws Exception { URIConverter converter = new URIConverter(); - assertThat(new URI("test:path")).isEqualTo(converter.convert("test:path", context)); + assertThat(new URI("test:path")).isEqualTo(converter.convert("test:path")); } @Test public void testConvert_URI_WithSpaces() throws Exception { URIConverter converter = new URIConverter(); - assertThat(new URI("test:path")).isEqualTo(converter.convert(" test:path\t", context)); + assertThat(new URI("test:path")).isEqualTo(converter.convert(" test:path\t")); } @Test public void testConvert_URI_WithSpacesBefore() throws Exception { URIConverter converter = new URIConverter(); - assertThat(new URI("test:path")).isEqualTo(converter.convert(" test:path", context)); + assertThat(new URI("test:path")).isEqualTo(converter.convert(" test:path")); } @Test public void testConvert_URI_WithSpacesAfter() throws Exception { URIConverter converter = new URIConverter(); - assertThat(new URI("test:path")).isEqualTo(converter.convert("test:path ", context)); + assertThat(new URI("test:path")).isEqualTo(converter.convert("test:path ")); } @Test public void testConvert_NotPresent() throws Exception { URIConverter converter = new URIConverter(); - assertThat(converter.convert("", context)).isNull(); - assertThat(converter.convert(null, context)).isNull(); + assertThat(converter.convert("")).isNull(); + assertThat(converter.convert(null)).isNull(); } @Test public void testConvert_URIInvalid() throws ConfigException { URIConverter converter = new URIConverter(); - assertThat(converter.convert("not a uri", context)).isNull(); + assertThat(converter.convert("not a uri")).isNull(); } @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(URI.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(URI.class)).build(); + ConversionContext.set(context); URIConverter converter = new URIConverter(); - converter.convert("test:path", localcontext); + converter.convert("test:path"); + ConversionContext.reset(); - assertThat(localcontext.getSupportedFormats().contains("<uri> -> new URI(uri) (URIConverter)")).isTrue(); + assertThat(context.getSupportedFormats().contains("<uri> -> new URI(uri) (URIConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java index f2def82..dcea40d 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java @@ -39,47 +39,49 @@ public class URLConverterTest { @Test public void testConvert_URL() throws Exception { URLConverter converter = new URLConverter(); - assertThat(new URL("http://apache.org:4000/path")).isEqualTo(converter.convert("http://apache.org:4000/path", context)); + assertThat(new URL("http://apache.org:4000/path")).isEqualTo(converter.convert("http://apache.org:4000/path")); } @Test public void testConvert_URL_WithSpaces() throws Exception { URLConverter converter = new URLConverter(); - assertThat(new URL("http://apache.org:4000/path")).isEqualTo(converter.convert(" http://apache.org:4000/path\t", context)); + assertThat(new URL("http://apache.org:4000/path")).isEqualTo(converter.convert(" http://apache.org:4000/path\t")); } @Test public void testConvert_URL_WithSpacesBefore() throws Exception { URLConverter converter = new URLConverter(); - assertThat(new URL("http://apache.org:4000/path")).isEqualTo(converter.convert(" http://apache.org:4000/path", context)); + assertThat(new URL("http://apache.org:4000/path")).isEqualTo(converter.convert(" http://apache.org:4000/path")); } @Test public void testConvert_URL_WithSpacesAfter() throws Exception { URLConverter converter = new URLConverter(); - assertThat(new URL("http://apache.org:4000/path")).isEqualTo(converter.convert("http://apache.org:4000/path ", context)); + assertThat(new URL("http://apache.org:4000/path")).isEqualTo(converter.convert("http://apache.org:4000/path ")); } @Test public void testConvert_NotPresent() throws Exception { URLConverter converter = new URLConverter(); - assertThat(converter.convert("", context)).isNull(); - assertThat(converter.convert(null, context)).isNull(); + assertThat(converter.convert("")).isNull(); + assertThat(converter.convert(null)).isNull(); } @Test public void testConvert_URLInvalid() throws ConfigException { URLConverter converter = new URLConverter(); - assertThat(converter.convert("not a url", context)).isNull(); + assertThat(converter.convert("not a url")).isNull(); } @Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { - ConversionContext localcontext = new ConversionContext.Builder(TypeLiteral.of(URL.class)).build(); + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(URL.class)).build(); + ConversionContext.set(context); URLConverter converter = new URLConverter(); - converter.convert("http://localhost", localcontext); + converter.convert("http://localhost"); + ConversionContext.reset(); - assertThat(localcontext.getSupportedFormats().contains("<URL> (URLConverter)")).isTrue(); + assertThat(context.getSupportedFormats().contains("<URL> (URLConverter)")).isTrue(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java index c315ff6..65d69dd 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java +++ b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyDefaultSource.java @@ -34,7 +34,7 @@ public class TestPropertyDefaultSource extends BasePropertySource{ public TestPropertyDefaultSource() { super(100); - properties.put("name",PropertyValue.of("name", "Anatole", "test")); + properties.put("name", PropertyValue.of("name", "Anatole", "test")); properties.put("name2",PropertyValue.of("name2", "Sabine", "test")); properties = Collections.unmodifiableMap(properties); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java index 96f80a6..aea67f6 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java +++ b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java @@ -18,23 +18,24 @@ */ package org.apache.tamaya.core.testdata; -import org.apache.tamaya.spi.FilterContext; import org.apache.tamaya.spi.PropertyFilter; import org.apache.tamaya.spi.PropertyValue; import javax.annotation.Priority; +import java.util.concurrent.atomic.AtomicInteger; /** * Simple PropertyFilter that filters exact one value, registered using ServiceLoader. */ @Priority(100) public class TestPropertyFilter implements PropertyFilter{ + + private AtomicInteger counter = new AtomicInteger(); + @Override - public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context) { - if("name4".equals(context.getProperty().getKey())){ - return valueToBeFiltered.toBuilder() - .setValue(valueToBeFiltered.getValue() + "(filtered)") - .build(); + public PropertyValue filterProperty(PropertyValue valueToBeFiltered) { + if("name4".equals(valueToBeFiltered.getKey())){ + return valueToBeFiltered.mutable().setValue(valueToBeFiltered.getValue() + "(filtered"+counter.incrementAndGet()+")"); } return valueToBeFiltered; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java index 488ea0b..223e197 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java +++ b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java @@ -18,8 +18,7 @@ */ package org.apache.tamaya.core.testdata; -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.spi.FilterContext; +import org.apache.tamaya.Configuration; import org.apache.tamaya.spi.PropertyFilter; import org.apache.tamaya.spi.PropertyValue; @@ -31,14 +30,13 @@ import javax.annotation.Priority; @Priority(200) public class TestRemovingPropertyFilter implements PropertyFilter{ @Override - public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context) { - if("name5".equals(context.getProperty().getKey())){ + public PropertyValue filterProperty(PropertyValue valueToBeFiltered) { + if("name5".equals(valueToBeFiltered.getKey())){ return null; } - else if("name3".equals(context.getProperty().getKey())){ - return valueToBeFiltered.toBuilder().setValue( - "Mapped to name: " + ConfigurationProvider.getConfiguration().get("name")) - .build(); + else if("name3".equals(valueToBeFiltered.getKey())){ + return valueToBeFiltered.setValue( + "Mapped to name: " + Configuration.current().get("name")); } return valueToBeFiltered; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java index b0a4069..72c8972 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java @@ -19,9 +19,12 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; -import java.util.Map; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; /** @@ -36,13 +39,59 @@ public interface ConfigValueEvaluator { * @param context the context, not null. * @return the value, or null. */ - PropertyValue evaluteRawValue(String key, ConfigurationContext context); + default PropertyValue evaluteRawValue(String key, ConfigurationContext context){ + List<PropertyValue> values = evaluteAllValues(key, context); + if(values.isEmpty()){ + return null; + } + return values.get(0); + } + + /** + * Evaluates all values using a {@link ConfigurationContext}. + * @param key the config key, not null. + * @param context the context, not null. + * @return the value, or null. + */ + default List<PropertyValue> evaluteAllValues(String key, ConfigurationContext context){ + List<PropertyValue> result = new ArrayList<>(); + for(PropertySource ps:context.getPropertySources()){ + try{ + PropertyValue val = ps.get(key); + if(val!=null){ + result.add(val); + } + }catch(Exception e){ + Logger.getLogger(getClass().getName()) + .log(Level.WARNING, "Failed to access '"+key+"' from PropertySource: " + ps.getName(), e); + } + } + // Ensure returning values found in order of precedence. + Collections.reverse(result); + return result; + } /** * Evaluates all property values from a {@link ConfigurationContext}. * @param context the context, not null. * @return the value, or null. */ - Map<String, PropertyValue> evaluateRawValues(ConfigurationContext context); + default Map<String, PropertyValue> evaluateRawValues(ConfigurationContext context){ + Map<String, PropertyValue> result = new HashMap<>(); + List<PropertySource> propertySources = context.getPropertySources(); + Collections.reverse(propertySources); + for(PropertySource ps:propertySources){ + try{ + Map<String,PropertyValue> val = ps.getProperties(); + if(val!=null){ + result.putAll(val); + } + }catch(Exception e){ + Logger.getLogger(getClass().getName()) + .log(Level.WARNING, "Failed to access properties from PropertySource: " + ps.getName(), e); + } + } + return result; + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java deleted file mode 100644 index fb456e6..0000000 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java +++ /dev/null @@ -1,334 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.spisupport; - - - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.*; - -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Map; - -/** - * A builder for creating new or adapting instances of {@link Configuration}. - * Builders can be obtained in exactly two ways: - * <ol> - * <li>By accessing a preinitialized builder from an existing {@link Configuration}, - * by calling {@link org.apache.tamaya.Configuration#toBuilder()}.</li> - * <li>By accessing an empty builder instance from - * {@link org.apache.tamaya.ConfigurationProvider#getConfigurationBuilder()}.</li> - * </ol> - */ -public interface ConfigurationBuilder { - - /** - * Init this builder instance with the given {@link ConfigurationContext} instance. This - * method will use any existing property sources, filters, converters and the combination - * policy of the given {@link ConfigurationContext} and initialize the current builder - * with them. - * - * @param context the {@link ConfigurationContext} instance to be used, not {@code null}. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder setContext(ConfigurationContext context); - - /** - * This method can be used for adding {@link PropertySource}s. - * Hereby the property source is added to the tail of property sources with - * lowest priority regardless of its current ordinal value. To sort the property - * sources based on their ordinals call {@link #sortPropertySources}. - * - * @param propertySources the {@link PropertySource}s to add - * @return this builder, for chaining, never null. - * @throws IllegalArgumentException If a property source with a given name already - * exists. - */ - ConfigurationBuilder addPropertySources(PropertySource... propertySources); - - /** - * This method can be used for programmatically adding {@link PropertySource}s. - * Hereby the property source is added to the tail of property sources with - * lowest priority regardless of its current ordinal value. To sort the property - * sources based on their ordinals call {@link #sortPropertySources}. - * - * @param propertySources the PropertySources to add - * @return this builder, for chaining, never null. - * @throws IllegalArgumentException If a property source with a given name already - * exists. - */ - ConfigurationBuilder addPropertySources(Collection<PropertySource> propertySources); - - /** - * Add all registered (default) property sources to the context built. The sources are ordered - * based on their ordinal values and added to the chain of property sources with - * higher priority. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addDefaultPropertySources(); - - /** - * Removes the given property sources, if existing. The existing order of property - * sources is preserved. - * - * @param propertySources the property sources to remove, not {@code null}. - * @return the builder for chaining. - */ - ConfigurationBuilder removePropertySources(PropertySource... propertySources); - - /** - * Removes the given property sources, if existing. The existing order of property - * sources is preserved. - * - * @param propertySources the property sources to remove, not {@code null}. - * @return the builder for chaining. - */ - ConfigurationBuilder removePropertySources(Collection<PropertySource> propertySources); - - /** - * Access the current chain of property sources. Items at the end of the list have - * precedence/more significance. - * - * @return the property source chain, never {@code null}. - */ - List<PropertySource> getPropertySources(); - - /** - * Access the current chain of property filters. Items at the end of the list have - * precedence/more significance. - * - * @return the property source chain, never {@code null}. - */ - List<PropertyFilter> getPropertyFilters(); - - /** - * Access the current registered property converters. - * - * @return the current registered property converters. - */ - Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter(); - - /** - * Increases the priority of the given property source, by moving it towards the end - * of the chain of property sources. If the property source given is already at the end - * this method has no effect. This operation does not change any ordinal values. - * - * @param propertySource the property source to be incresed regarding its significance. - * @return the builder for chaining. - * @throws IllegalArgumentException If no such property source exists in the current - * chain. - */ - ConfigurationBuilder increasePriority(PropertySource propertySource); - - /** - * Decreases the priority of the given property source, by moving it towards the start - * of the chain of property sources. If the property source given is already the first - * this method has no effect. This operation does not change any ordinal values. - * - * @param propertySource the property source to be decresed regarding its significance. - * @return the builder for chaining. - * @throws IllegalArgumentException If no such property source exists in the current - * chain. - */ - ConfigurationBuilder decreasePriority(PropertySource propertySource); - - /** - * Increases the priority of the given property source to be maximal, by moving it to - * the tail of the of property source chain. If the property source given is - * already the last item this method has no effect. This operation does not change - * any ordinal values. - * - * @param propertySource the property source to be maximized regarding its significance. - * @return the builder for chaining. - * @throws IllegalArgumentException If no such property source exists in the current - * chain. - */ - ConfigurationBuilder highestPriority(PropertySource propertySource); - - /** - * Decreases the priority of the given property source to be minimal, by moving it to - * the start of the chain of property source chain. If the property source given is - * already the first item this method has no effect. This operation does not change - * any ordinal values. - * - * @param propertySource the property source to be minimized regarding its significance. - * @return the builder for chaining. - * @throws IllegalArgumentException If no such property source exists in the current - * chain. - */ - ConfigurationBuilder lowestPriority(PropertySource propertySource); - - /** - * Adds the given {@link PropertyFilter} instances, hereby the instances are added - * to the end of the list with highest priority. The ordering of existing - * property filters remains unchanged. To sort the property - * filters call {@link #sortPropertyFilter}. - * - * @param filters the filters to add - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addPropertyFilters(PropertyFilter... filters); - - /** - * Adds the given {@link PropertyFilter} instances, hereby the instances are added - * to the end of the list with highest priority. The ordering of existing - * property filters remains unchanged. To sort the property - * filters call {@link #sortPropertyFilter}. - * - * @param filters the filters to add - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> filters); - - /** - * Add all registered (default) property filters to the context built. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addDefaultPropertyFilters(); - - - /** - * Removes the given {@link PropertyFilter} instances, if existing. The order of the remaining - * filters is preserved. - * - * @param filters the filter to remove - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder removePropertyFilters(PropertyFilter... filters); - - /** - * Removes the given {@link PropertyFilter} instances, if existing. The order of the remaining - * filters is preserved. - * - * @param filters the filter to remove - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder removePropertyFilters(Collection<PropertyFilter> filters); - - /** - * This method can be used for adding {@link PropertyConverter}s. - * Converters are added at the end after any existing converters. - * For converters already registered for the current target type the - * method has no effect. - * - * @param typeToConvert the type for which the converter is used - * @param propertyConverters the {@link PropertyConverter}s to add for this type - * @param <T> the target type. - * @return this builder, for chaining, never null. - */ - <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> typeToConvert, - PropertyConverter<T>... propertyConverters); - - /** - * This method can be used for adding {@link PropertyConverter}s. - * Converters are added at the end after any existing converters. - * For converters already registered for the current target type the - * method has no effect. - * - * @param typeToConvert the type for which the converter is used - * @param propertyConverters the {@link PropertyConverter}s to add for this type - * @param <T> the target type. - * @return this builder, for chaining, never null. - */ - <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> typeToConvert, - Collection<PropertyConverter<T>> propertyConverters); - - /** - * Add all registered (default) property converters to the context built. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addDefaultPropertyConverters(); - - /** - * Removes the given {@link PropertyConverter} instances for the given type, - * if existing. - * - * @param typeToConvert the type for which the converter is used - * @param propertyConverters the converters to remove - * @param <T> the target type. - * @return this builder, for chaining, never null. - */ - <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - PropertyConverter<T>... propertyConverters); - - /** - * Removes the given {@link PropertyConverter} instances for the given type, - * if existing. - * - * @param typeToConvert the type for which the converter is used - * @param propertyConverters the converters to remove - * @param <T> the target type. - * @return this builder, for chaining, never null. - */ - <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - Collection<PropertyConverter<T>> propertyConverters); - - /** - * Removes all converters for the given type, which actually renders a given type - * unsupported for type conversion. - * - * @param typeToConvert the type for which the converter is used - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder removePropertyConverters(TypeLiteral<?> typeToConvert); - - /** - * Sorts the current registered property sources using the given comparator. - * - * NOTE: property sources at the beginning have minimal significance. - * - * @param comparator the comparator to be used, not {@code null}. - * @return this instance for chaining. - */ - ConfigurationBuilder sortPropertySources(Comparator<PropertySource> comparator); - - /** - * Sorts the current registered property filters using the given comparator. - * - * NOTE: property filters at the beginning have minimal significance. - * - * @param comparator the comparator to be used, not {@code null}. - * @return this instance for chaining. - */ - ConfigurationBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator); - - /** - * Sets the {@link PropertyValueCombinationPolicy} used to evaluate the final - * property values. - * - * @param policy the {@link PropertyValueCombinationPolicy} used, not {@code null}. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy); - - /** - * Builds a new {@link Configuration} based on the data in this builder. The ordering of property - * sources and property filters is not changed, regardless of their ordinals. For ensure a certain - * ordering/significance call {@link #sortPropertyFilter(Comparator)} and/or {@link #sortPropertySources(Comparator)} - * before building the context. - * - * @return the final configuration. - */ - Configuration build(); - -} - http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e45effd2/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java index b6f979a..3767efd 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java @@ -23,20 +23,9 @@ import org.apache.tamaya.ConfigOperator; import org.apache.tamaya.ConfigQuery; import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spi.PropertyValueCombinationPolicy; -import org.apache.tamaya.spi.ServiceContextManager; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import org.apache.tamaya.spi.*; + +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -59,12 +48,12 @@ public class DefaultConfiguration implements Configuration { /** * EvaluationStrategy */ - private ConfigValueEvaluator configEvaluator = loadConfigValueEvaluator(); + private ConfigValueEvaluator configEvaluator; private ConfigValueEvaluator loadConfigValueEvaluator() { ConfigValueEvaluator eval = null; try{ - eval = ServiceContextManager.getServiceContext() + eval = configurationContext.getServiceContext() .getService(ConfigValueEvaluator.class); }catch(Exception e){ LOG.log(Level.WARNING, "Failed to load ConfigValueEvaluator from ServiceContext, using default.", e); @@ -82,6 +71,7 @@ public class DefaultConfiguration implements Configuration { */ public DefaultConfiguration(ConfigurationContext configurationContext){ this.configurationContext = Objects.requireNonNull(configurationContext); + this.configEvaluator = loadConfigValueEvaluator(); } /** @@ -105,6 +95,26 @@ public class DefaultConfiguration implements Configuration { } /** + * Get a given value, filtered with the context's filters as needed. + * @param key the property's key, not null. + * @return the filtered value, or null. + */ + public List<PropertyValue> getValues(String key) { + Objects.requireNonNull(key, "Key must not be null."); + + List<PropertyValue> value = configEvaluator.evaluteAllValues(key, configurationContext); + if(value==null || value.isEmpty()){ + return Collections.emptyList(); + } + value = PropertyFiltering.applyFilters(value, configurationContext); + if(value!=null){ + return value; + } + return null; + } + + + /** * Evaluates the raw value using the context's {@link PropertyValueCombinationPolicy}. * @param key the key, not null. * @return the value, before filtering is applied. @@ -160,7 +170,6 @@ public class DefaultConfiguration implements Configuration { if(val.getValue()!=null) { result.put(val.getKey(), val.getValue()); // TODO: Discuss metadata handling... - result.putAll(val.getMetaEntries()); } } return result; @@ -199,32 +208,39 @@ public class DefaultConfiguration implements Configuration { Objects.requireNonNull(key, "Key must not be null."); Objects.requireNonNull(type, "Target type must not be null"); - return convertValue(key, get(key), type); + return convertValue(key, getValues(key), type); } @SuppressWarnings("unchecked") - protected <T> T convertValue(String key, String value, TypeLiteral<T> type) { - if (value != null) { + protected <T> T convertValue(String key, List<PropertyValue> values, TypeLiteral<T> type) { + if (values != null && !values.isEmpty()) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); - ConversionContext context = new ConversionContext.Builder(this, this.configurationContext, key, type) + ConversionContext context = new ConversionContext.Builder(this, key, type) + .setValues(values) .build(); - for (PropertyConverter<T> converter : converters) { - try { - T t = converter.convert(value, context); - if (t != null) { - return t; + try { + String value = values.get(0).getValue(); + ConversionContext.set(context); + for (PropertyConverter<T> converter : converters) { + try { + T t = converter.convert(value); + if (t != null) { + return t; + } + } catch (Exception e) { + LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert value: " + value, e); } - } catch (Exception e) { - LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert value: " + value, e); } + // if the target type is a String, we can return the value, no conversion required. + if (type.equals(TypeLiteral.of(String.class))) { + return (T) value; + } + // unsupported type, throw an exception + throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + + ", supported formats: " + context.getSupportedFormats()); + }finally{ + ConversionContext.reset(); } - // if the target type is a String, we can return the value, no conversion required. - if(type.equals(TypeLiteral.of(String.class))){ - return (T)value; - } - // unsupported type, throw an exception - throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + - ", supported formats: " + context.getSupportedFormats()); } return null; }
