diff --git a/api/src/main/java/org/eclipse/microprofile/config/Config.java b/api/src/main/java/org/eclipse/microprofile/config/Config.java
index 273e0f9d..351377c9 100644
--- a/api/src/main/java/org/eclipse/microprofile/config/Config.java
+++ b/api/src/main/java/org/eclipse/microprofile/config/Config.java
@@ -37,6 +37,7 @@
import java.util.Optional;
import org.eclipse.microprofile.config.spi.ConfigSource;
+import org.eclipse.microprofile.config.spi.Converter;
/**
*
@@ -161,4 +162,15 @@ public interface Config {
* @return the configuration sources
*/
Iterable getConfigSources();
+
+ /**
+ * Return the {@link Converter} used by this instance to produce instances of the specified type from string values.
+ *
+ * @param
+ * the conversion type
+ * @param forType
+ * the type to be produced by the converter
+ * @return an {@link Optional} containing the converter, or empty if no converter is available for the specified type
+ */
+ Optional> getConverter(Class forType);
}
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/ArrayConverterTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/ArrayConverterTest.java
index 8655eb20..7dfe6787 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/ArrayConverterTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/ArrayConverterTest.java
@@ -90,6 +90,14 @@ public void testBooleanLookupProgrammatically() {
Assert.assertEquals(value, new Boolean[]{true, false, true});
}
+ @Test
+ public void testGetBooleanArrayConverter() {
+ Boolean[] value = config.getConverter(Boolean[].class).get().convert("true,false,true");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 3);
+ Assert.assertEquals(value, new Boolean[]{true, false, true});
+ }
+
@Test
public void testOptionalBooleanLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.booleanvalues",
@@ -107,7 +115,15 @@ public void testBooleanArrayInjection() {
Assert.assertEquals(converterBean.getMyBooleans(), new Boolean[]{true, false, true});
}
- //test bool[] support
+ //test boolean[] support
+
+ @Test
+ public void testGetbooleanArrayConverter() {
+ boolean[] value = config.getConverter(boolean[].class).get().convert("true,false,true");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 3);
+ Assert.assertEquals(value, new Boolean[]{true, false, true});
+ }
@Test
public void testbooleanArrayInjection() {
@@ -138,6 +154,14 @@ public void testStringLookupProgrammatically() {
Assert.assertEquals(value, new String[]{"microservice", "microprofile", "m,f", "microservice"});
}
+ @Test
+ public void testGetStringArrayConverter() {
+ String[] value = config.getConverter(String[].class).get().convert("microservice,microprofile,m\\,f,microservice");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 4);
+ Assert.assertEquals(value, new String[]{"microservice", "microprofile", "m,f", "microservice"});
+ }
+
@Test
public void testOptionalStringLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.stringvalues",
@@ -181,6 +205,14 @@ public void testIntLookupProgrammatically() {
Assert.assertEquals(value, new Integer[]{1234, 9999});
}
+ @Test
+ public void testGetIntegerArrayConverter() {
+ Integer[] value = config.getConverter(Integer[].class).get().convert("1234,9999");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals(value, new Integer[]{1234, 9999});
+ }
+
@Test
public void testOptionalIntLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.integervalues",
@@ -200,6 +232,14 @@ public void testIntArrayInjection() {
//test int[] support
+ @Test
+ public void testGetIntArrayConverter() {
+ int[] value = config.getConverter(int[].class).get().convert("1234,9999");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals(value, new Integer[]{1234, 9999});
+ }
+
@Test
public void testintArrayInjection() {
Assert.assertEquals(converterBean.getMyints().length, 2);
@@ -231,6 +271,14 @@ public void testLongLookupProgrammatically() {
Assert.assertEquals(value, new Long[] {1234567890L, 1999999999L});
}
+ @Test
+ public void testGetLongArrayCoverter() {
+ Long[] value = config.getConverter(Long[].class).get().convert("1234567890,1999999999");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals(value, new Long[] {1234567890L, 1999999999L});
+ }
+
@Test
public void testOptionalLongLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.longvalues",
@@ -250,6 +298,14 @@ public void testLongArrayInjection() {
//test long[] support
+ @Test
+ public void testGetlongArrayCoverter() {
+ long[] value = config.getConverter(long[].class).get().convert("1234567890,1999999999");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals(value, new Long[] {1234567890L, 1999999999L});
+ }
+
@Test
public void testlongArrayInjection() {
Assert.assertEquals(converterBean.getMylongs().length, 2);
@@ -281,6 +337,14 @@ public void testFloatLookupProgrammatically() {
Assert.assertEquals(value, new Float[]{12.34f, 99.99f});
}
+ @Test
+ public void testGetFloatArrayConverter() {
+ Float[] value = config.getConverter(Float[].class).get().convert("12.34,99.99");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals(value, new Float[]{12.34f, 99.99f});
+ }
+
@Test
public void testOptionalFloatLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.floatvalues",
@@ -300,6 +364,14 @@ public void testFloatArrayInjection() {
//test float[] support
+ @Test
+ public void testGetfloatArrayConverter() {
+ float[] value = config.getConverter(float[].class).get().convert("12.34,99.99");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals(value, new Float[]{12.34f, 99.99f});
+ }
+
@Test
public void testfloatArrayInjection() {
Assert.assertEquals(converterBean.getMyfloats().length, 2);
@@ -331,6 +403,14 @@ public void testDoubleLookupProgrammatically() {
Assert.assertEquals( value, new Double[]{12.34d,99.9999d});
}
+ @Test
+ public void testGetDoubleArrayConverter() {
+ Double[] value = config.getConverter(Double[].class).get().convert("12.34,99.9999");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals( value, new Double[]{12.34d,99.9999d});
+ }
+
@Test
public void testOptionalDoubleLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.doublevalues",
@@ -350,6 +430,14 @@ public void testDoubleArrayInjection() {
//test double[] support
+ @Test
+ public void testGetdoubleArrayConverter() {
+ double[] value = config.getConverter(double[].class).get().convert("12.34,99.9999");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals( value, new Double[]{12.34d,99.9999d});
+ }
+
@Test
public void testdoubleArrayInjection() {
Assert.assertEquals(converterBean.getMydoubles().length, 2);
@@ -383,6 +471,16 @@ public void testDurationLookupProgrammatically() {
Duration.parse("PT20M")});
}
+ @Test
+ public void testGetDurationArrayConverter() {
+ Duration[] value = config.getConverter(Duration[].class).get().convert("PT15M,PT20M");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals( value, new Duration[]{
+ Duration.parse("PT15M"),
+ Duration.parse("PT20M")});
+ }
+
@Test
public void testOptionalDurationLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.durationvalues",
@@ -435,6 +533,16 @@ public void testLocalTimeLookupProgrammatically() {
LocalTime.parse("11:44")});
}
+ @Test
+ public void testGetLocalTimeArrayConverter() {
+ LocalTime[] value = config.getConverter(LocalTime[].class).get().convert("10:37,11:44");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals( value, new LocalTime[]{
+ LocalTime.parse("10:37"),
+ LocalTime.parse("11:44")});
+ }
+
@Test
public void testOptionalLocalTimeLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.localtimevalues",
@@ -486,6 +594,16 @@ public void testLocalDateLookupProgrammatically() {
LocalDate.parse("2017-11-29")});
}
+ @Test
+ public void testGetLocalDateArrayConverter() {
+ LocalDate[] value = config.getConverter(LocalDate[].class).get().convert("2017-12-24,2017-11-29");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals(value, new LocalDate[]{
+ LocalDate.parse("2017-12-24"),
+ LocalDate.parse("2017-11-29")});
+ }
+
@Test
public void testOptionalLocalDateLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.localdatevalues",
@@ -537,6 +655,16 @@ public void testLocalDateTimeLookupProgrammatically() {
LocalDateTime.parse("2017-12-24T10:25:33")});
}
+ @Test
+ public void testGetLocalDateTimeArrayConverter() {
+ LocalDateTime[] value = config.getConverter(LocalDateTime[].class).get().convert("2017-12-24T10:25:30,2017-12-24T10:25:33");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals( value, new LocalDateTime[]{
+ LocalDateTime.parse("2017-12-24T10:25:30"),
+ LocalDateTime.parse("2017-12-24T10:25:33")});
+ }
+
@Test
public void testOptionalLocalDateTimeLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.localdatetimevalues",
@@ -588,6 +716,16 @@ public void testOffsetDateTimeLookupProgrammatically() {
OffsetDateTime.parse("2007-12-03T10:15:30+02:00")});
}
+ @Test
+ public void testGetOffsetDateTimeArrayConverter() {
+ OffsetDateTime[] value = config.getConverter(OffsetDateTime[].class).get().convert("2007-12-03T10:15:30+01:00,2007-12-03T10:15:30+02:00");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals( value, new OffsetDateTime[] {
+ OffsetDateTime.parse("2007-12-03T10:15:30+01:00"),
+ OffsetDateTime.parse("2007-12-03T10:15:30+02:00")});
+ }
+
@Test
public void testOptionalOffsetDateTimeLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.offsetdatetimevalues",
@@ -640,6 +778,16 @@ public void testOffsetTimeLookupProgrammatically() {
OffsetTime.parse("13:45:30.123456789+03:00")});
}
+ @Test
+ public void testGetOffsetTimeArrayConverter() {
+ OffsetTime[] value = config.getConverter(OffsetTime[].class).get().convert("13:45:30.123456789+02:00,13:45:30.123456789+03:00");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals( value, new OffsetTime[]{
+ OffsetTime.parse("13:45:30.123456789+02:00"),
+ OffsetTime.parse("13:45:30.123456789+03:00")});
+ }
+
@Test
public void testOptionalOffsetTimeLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.offsettimevalues",
@@ -692,6 +840,16 @@ public void testInstantLookupProgrammatically() {
Instant.parse("2017-06-02T21:34:33.616Z")});
}
+ @Test
+ public void testGetInstantArrayConverter() {
+ Instant[] value = config.getConverter(Instant[].class).get().convert("2015-06-02T21:34:33.616Z,2017-06-02T21:34:33.616Z");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 2);
+ Assert.assertEquals(value, new Instant[]{
+ Instant.parse("2015-06-02T21:34:33.616Z"),
+ Instant.parse("2017-06-02T21:34:33.616Z")});
+ }
+
@Test
public void testOptionalInstantLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.instantvalues",
@@ -744,6 +902,17 @@ public void testUrlLookupProgrammatically() throws MalformedURLException {
new URL("http://microprofile.io")});
}
+ @Test
+ public void testGetUrlArrayConverter() throws MalformedURLException {
+ URL[] value = config.getConverter(URL[].class).get().convert("http://microprofile.io,http://openliberty.io,http://microprofile.io");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 3);
+ Assert.assertEquals(value, new URL[]{
+ new URL("http://microprofile.io"),
+ new URL("http://openliberty.io"),
+ new URL("http://microprofile.io")});
+ }
+
@Test
public void testOptionalUrlLookupProgrammatically() throws MalformedURLException {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.urlvalues", URL[].class);
@@ -797,6 +966,17 @@ public void testUriLookupProgrammatically() {
URI.create("http://microprofile.io")});
}
+ @Test
+ public void testGetUriArrayConverter() {
+ URI[] value = config.getConverter(URI[].class).get().convert("http://microprofile.io,http://openliberty.io,http://microprofile.io");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 3);
+ Assert.assertEquals(value, new URI[]{
+ URI.create("http://microprofile.io"),
+ URI.create("http://openliberty.io"),
+ URI.create("http://microprofile.io")});
+ }
+
@Test
public void testOptionalUriLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.urlvalues", URI[].class);
@@ -853,6 +1033,17 @@ public void testCustomTypeArrayLookupProgrammatically() {
new Pizza("pepperoni", "small")});
}
+ @Test
+ public void testGetCustomTypeArrayConverter() {
+ Pizza[] value = config.getConverter(Pizza[].class).get().convert("large:cheese\\,mushroom,medium:chicken,small:pepperoni");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.length, 3);
+ Assert.assertEquals(value, new Pizza[]{
+ new Pizza("cheese,mushroom", "large"),
+ new Pizza("chicken", "medium"),
+ new Pizza("pepperoni", "small")});
+ }
+
@Test
public void testOptionalCustomTypeArrayLookupProgrammatically() {
Optional optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.array.pizza",
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/ClassConverterTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/ClassConverterTest.java
index d402ed11..233504c7 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/ClassConverterTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/ClassConverterTest.java
@@ -75,6 +75,17 @@ public void testClassConverterWithLookup() {
assertEquals(testClasses, new Class[]{ClassConverterTest.class, String.class});
}
+ @Test
+ public void testGetClassConverter() {
+ Class> testClass = config.getConverter(Class.class).get()
+ .convert("org.eclipse.microprofile.config.tck.ClassConverterTest");
+ assertEquals(testClass, ClassConverterTest.class);
+ Class>[] testClasses = config.getConverter(Class[].class).get()
+ .convert("org.eclipse.microprofile.config.tck.ClassConverterTest,java.lang.String");
+ assertEquals(testClasses.length, 2);
+ assertEquals(testClasses, new Class[]{ClassConverterTest.class, String.class});
+ }
+
@Test
public void testConverterForClassLoadedInBean() {
assertEquals(classConverterBean.getTestClass(), ClassConverterTest.class);
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/ConverterTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/ConverterTest.java
index 8debc048..1847866d 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/ConverterTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/ConverterTest.java
@@ -101,6 +101,11 @@ public void testDonaldNotConvertedByDefault() {
config.getValue("tck.config.test.javaconfig.converter.donaldname", Donald.class);
}
+ @Test
+ public void testNoDonaldConverterByDefault() {
+ Assert.assertFalse(config.getConverter(Donald.class).isPresent());
+ }
+
@Test
public void testDonaldConversionWithLambdaConverter() {
Config newConfig = ConfigProviderResolver.instance().getBuilder().addDefaultSources()
@@ -111,6 +116,16 @@ public void testDonaldConversionWithLambdaConverter() {
Assert.assertEquals(donald.getName(), "Duck");
}
+ @Test
+ public void testGetDonaldConverterWithLambdaConverter() {
+ Config newConfig = ConfigProviderResolver.instance().getBuilder().addDefaultSources()
+ .withConverter(Donald.class, 100, (s) -> Donald.iLikeDonald(s))
+ .build();
+ Donald donald = newConfig.getConverter(Donald.class).get().convert("Duck");
+ Assert.assertNotNull(donald);
+ Assert.assertEquals(donald.getName(), "Duck");
+ }
+
@Test
public void testDonaldConversionWithMultipleLambdaConverters() {
// defines 2 config with the lambda converters defined in different orders.
@@ -124,6 +139,29 @@ public void testDonaldConversionWithMultipleLambdaConverters() {
.withConverter(Donald.class, 101, (s) -> Donald.iLikeDonald(s.toUpperCase()))
.build();
+ Donald donald = config1.getConverter(Donald.class).get().convert("Duck");
+ Assert.assertNotNull(donald);
+ Assert.assertEquals(donald.getName(), "DUCK",
+ "The converter with the highest priority (using upper case) must be used.");
+ donald = config2.getConverter(Donald.class).get().convert("Duck");
+ Assert.assertNotNull(donald);
+ Assert.assertEquals(donald.getName(), "DUCK",
+ "The converter with the highest priority (using upper case) must be used.");
+ }
+
+ @Test
+ public void testGetDonaldConverterWithMultipleLambdaConverters() {
+ // defines 2 config with the lambda converters defined in different orders.
+ // Order must not matter, the lambda with the upper case must always be used as it has the highest priority
+ Config config1 = ConfigProviderResolver.instance().getBuilder().addDefaultSources()
+ .withConverter(Donald.class, 101, (s) -> Donald.iLikeDonald(s.toUpperCase()))
+ .withConverter(Donald.class, 100, (s) -> Donald.iLikeDonald(s))
+ .build();
+ Config config2 = ConfigProviderResolver.instance().getBuilder().addDefaultSources()
+ .withConverter(Donald.class, 100, (s) -> Donald.iLikeDonald(s))
+ .withConverter(Donald.class, 101, (s) -> Donald.iLikeDonald(s.toUpperCase()))
+ .build();
+
Donald donald = config1.getValue("tck.config.test.javaconfig.converter.donaldname", Donald.class);
Assert.assertNotNull(donald);
Assert.assertEquals(donald.getName(), "DUCK",
@@ -148,7 +186,24 @@ public void testbyte() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testByte_Broken() {
- Byte value = config.getValue("tck.config.test.javaconfig.converter.bytevalue.broken", Byte.class);
+ config.getValue("tck.config.test.javaconfig.converter.bytevalue.broken", Byte.class);
+ }
+
+ @Test
+ public void testGetByteConverter() {
+ Byte value = config.getConverter(Byte.class).get().convert("123");
+ Assert.assertEquals(value, Byte.valueOf((byte)123));
+ }
+
+ @Test
+ public void testGetbyteConverter() {
+ byte value = config.getConverter(byte.class).get().convert("123");
+ Assert.assertEquals(value, (byte)123);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetByteConverter_Broken() {
+ config.getConverter(Byte.class).get().convert("xxx");
}
@Test
@@ -165,7 +220,24 @@ public void testshort() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testShort_Broken() {
- Short value = config.getValue("tck.config.test.javaconfig.converter.shortvalue.broken", Short.class);
+ config.getValue("tck.config.test.javaconfig.converter.shortvalue.broken", Short.class);
+ }
+
+ @Test
+ public void testGetShortConverter() {
+ Short value = config.getConverter(Short.class).get().convert("1234");
+ Assert.assertEquals(value, Short.valueOf((short)1234));
+ }
+
+ @Test
+ public void testGetshortConverter() {
+ short value = config.getConverter(short.class).get().convert("1234");
+ Assert.assertEquals(value, (short)1234);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetShortConverter_Broken() {
+ config.getConverter(Short.class).get().convert("xxx");
}
@Test
@@ -182,7 +254,24 @@ public void testInt() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testInteger_Broken() {
- Integer value = config.getValue("tck.config.test.javaconfig.converter.integervalue.broken", Integer.class);
+ config.getValue("tck.config.test.javaconfig.converter.integervalue.broken", Integer.class);
+ }
+
+ @Test
+ public void testGetIntegerConverter() {
+ Integer value = config.getConverter(Integer.class).get().convert("1234");
+ Assert.assertEquals(value, Integer.valueOf(1234));
+ }
+
+ @Test
+ public void testGetIntConverter() {
+ int value = config.getConverter(int.class).get().convert("1234");
+ Assert.assertEquals(value, 1234);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetIntegerConverter_Broken() {
+ config.getConverter(Integer.class).get().convert("xxx");
}
@Test
@@ -202,6 +291,23 @@ public void testLong_Broken() {
config.getValue("tck.config.test.javaconfig.converter.longvalue.broken", Long.class);
}
+ @Test
+ public void testGetLongConverter() {
+ Long value = config.getConverter(Long.class).get().convert("1234567890");
+ Assert.assertEquals(value, Long.valueOf(1234567890));
+ }
+
+ @Test
+ public void testGetlongConverter() {
+ long primitiveValue = config.getConverter(long.class).get().convert("1234567890");
+ Assert.assertEquals(primitiveValue, 1234567890L);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetLongConverter_Broken() {
+ config.getConverter(Long.class).get().convert("alfasdf");
+ }
+
@Test
public void testFloat() {
Float value = config.getValue("tck.config.test.javaconfig.converter.floatvalue", Float.class);
@@ -219,6 +325,23 @@ public void testFloat_Broken() {
config.getValue("tck.config.test.javaconfig.converter.floatvalue.broken", Float.class);
}
+ @Test
+ public void testGetFloatConverter() {
+ Float value = config.getConverter(Float.class).get().convert("12.34");
+ Assert.assertEquals(value, 12.34f);
+ }
+
+ @Test
+ public void testGetfloatConverter() {
+ float value = config.getConverter(float.class).get().convert("12.34");
+ Assert.assertEquals(value, 12.34f);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetFloatConverter_Broken() {
+ config.getConverter(Float.class).get().convert("alfasdf");
+ }
+
@Test
public void testDouble() {
Double value = config.getValue("tck.config.test.javaconfig.converter.doublevalue", Double.class);
@@ -233,7 +356,24 @@ public void testdouble() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testDouble_Broken() {
- Double value = config.getValue("tck.config.test.javaconfig.converter.doublevalue.broken", Double.class);
+ config.getValue("tck.config.test.javaconfig.converter.doublevalue.broken", Double.class);
+ }
+
+ @Test
+ public void testGetDoubleConverter() {
+ Double value = config.getConverter(Double.class).get().convert("12.34");
+ Assert.assertEquals(value, 12.34d);
+ }
+
+ @Test
+ public void testGetdoubleConverter() {
+ double value = config.getConverter(double.class).get().convert("12.34");
+ Assert.assertEquals(value,12.34d);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetDoubleConverter_Broken() {
+ config.getConverter(Double.class).get().convert("alfasdf");
}
@Test
@@ -250,7 +390,24 @@ public void testchar() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testChar_Broken() {
- Character value = config.getValue("tck.config.test.javaconfig.converter.charvalue.broken", Character.class);
+ config.getValue("tck.config.test.javaconfig.converter.charvalue.broken", Character.class);
+ }
+
+ @Test
+ public void testGetCharConverter() {
+ Character value = config.getConverter(Character.class).get().convert("c");
+ Assert.assertEquals(value, Character.valueOf('c'));
+ }
+
+ @Test
+ public void testGetcharConverter() {
+ char value = config.getConverter(char.class).get().convert("c");
+ Assert.assertEquals(value, 'c');
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetCharConverter_Broken() {
+ config.getConverter(Character.class).get().convert("xxx");
}
@Test
@@ -261,7 +418,18 @@ public void testDuration() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testDuration_Broken() {
- Duration value = config.getValue("tck.config.test.javaconfig.converter.durationvalue.broken", Duration.class);
+ config.getValue("tck.config.test.javaconfig.converter.durationvalue.broken", Duration.class);
+ }
+
+ @Test
+ public void testGetDurationCoverter() {
+ Duration value = config.getConverter(Duration.class).get().convert("PT15M");
+ Assert.assertEquals(value, Duration.parse("PT15M"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetDurationConverter_Broken() {
+ config.getConverter(Duration.class).get().convert("alfasdf");
}
@Test
@@ -272,7 +440,18 @@ public void testLocalTime() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testLocalTime_Broken() {
- LocalTime value = config.getValue("tck.config.test.javaconfig.converter.localtimevalue.broken", LocalTime.class);
+ config.getValue("tck.config.test.javaconfig.converter.localtimevalue.broken", LocalTime.class);
+ }
+
+ @Test
+ public void testGetLocalTimeConverter() {
+ LocalTime value = config.getConverter(LocalTime.class).get().convert("10:37");
+ Assert.assertEquals(value, LocalTime.parse("10:37"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetLocalTimeConverter_Broken() {
+ config.getConverter(LocalTime.class).get().convert("alfasdf");
}
@Test
@@ -283,7 +462,18 @@ public void testLocalDate() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testLocalDate_Broken() {
- LocalDate value = config.getValue("tck.config.test.javaconfig.converter.localdatevalue.broken", LocalDate.class);
+ config.getValue("tck.config.test.javaconfig.converter.localdatevalue.broken", LocalDate.class);
+ }
+
+ @Test
+ public void testGetLocalDateConverter() {
+ LocalDate value = config.getConverter(LocalDate.class).get().convert("2017-12-24");
+ Assert.assertEquals(value, LocalDate.parse("2017-12-24"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetLocalDateConverter_Broken() {
+ config.getConverter(LocalDate.class).get().convert("alfasdf");
}
@Test
@@ -294,7 +484,18 @@ public void testLocalDateTime() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testLocalDateTime_Broken() {
- LocalDateTime value = config.getValue("tck.config.test.javaconfig.converter.localdatetimevalue.broken", LocalDateTime.class);
+ config.getValue("tck.config.test.javaconfig.converter.localdatetimevalue.broken", LocalDateTime.class);
+ }
+
+ @Test
+ public void testGetLocalDateTimeConverter() {
+ LocalDateTime value = config.getConverter(LocalDateTime.class).get().convert("2017-12-24T10:25:30");
+ Assert.assertEquals(value, LocalDateTime.parse("2017-12-24T10:25:30"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetLocalDateTimeConverter_Broken() {
+ config.getConverter(LocalDateTime.class).get().convert("alfasdf");
}
@Test
@@ -305,7 +506,18 @@ public void testOffsetDateTime() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testOffsetDateTime_Broken() {
- OffsetDateTime value = config.getValue("tck.config.test.javaconfig.converter.offsetdatetimevalue.broken", OffsetDateTime.class);
+ config.getValue("tck.config.test.javaconfig.converter.offsetdatetimevalue.broken", OffsetDateTime.class);
+ }
+
+ @Test
+ public void testGetOffsetDateTimeConverter() {
+ OffsetDateTime value = config.getConverter(OffsetDateTime.class).get().convert("2007-12-03T10:15:30+01:00");
+ Assert.assertEquals(value, OffsetDateTime.parse("2007-12-03T10:15:30+01:00"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetOffsetDateTimeConverter_Broken() {
+ config.getConverter(OffsetDateTime.class).get().convert("alfasdf");
}
@Test
@@ -315,11 +527,21 @@ public void testOffsetTime() {
Assert.assertEquals(value, parsed);
}
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testOffsetTime_Broken() {
+ config.getValue("tck.config.test.javaconfig.converter.offsettimevalue.broken", OffsetTime.class);
+ }
+ @Test
+ public void testGetOffsetTimeConverter() {
+ OffsetTime value = config.getConverter(OffsetTime.class).get().convert("13:45:30.123456789+02:00");
+ OffsetTime parsed = OffsetTime.parse("13:45:30.123456789+02:00");
+ Assert.assertEquals(value, parsed);
+ }
@Test(expectedExceptions = IllegalArgumentException.class)
- public void testOffsetTime_Broken() {
- OffsetTime value = config.getValue("tck.config.test.javaconfig.converter.offsettimevalue.broken", OffsetTime.class);
+ public void testGetOffsetTimeConverter_Broken() {
+ config.getConverter(OffsetTime.class).get().convert("alfasdf");
}
@Test
@@ -331,7 +553,19 @@ public void testZoneOffset() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testZoneOffset_Broken() {
- ZoneOffset value = config.getValue("tck.config.test.javaconfig.converter.zoneoffsetvalue.broken", ZoneOffset.class);
+ config.getValue("tck.config.test.javaconfig.converter.zoneoffsetvalue.broken", ZoneOffset.class);
+ }
+
+ @Test
+ public void testGetZoneOffsetConverter() {
+ ZoneOffset value = config.getConverter(ZoneOffset.class).get().convert("+02:00");
+ ZoneOffset parsed = ZoneOffset.of("+02:00");
+ Assert.assertEquals(value, parsed);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetZoneOffsetConverter_Broken() {
+ config.getConverter(ZoneOffset.class).get().convert("alfasdf");
}
@Test
@@ -342,7 +576,18 @@ public void testInstant() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testInstant_Broken() {
- Instant value = config.getValue("tck.config.test.javaconfig.converter.instantvalue.broken", Instant.class);
+ config.getValue("tck.config.test.javaconfig.converter.instantvalue.broken", Instant.class);
+ }
+
+ @Test
+ public void testGetInstantConverter() {
+ Instant value = config.getConverter(Instant.class).get().convert("2015-06-02T21:34:33.616Z");
+ Assert.assertEquals(value, Instant.parse("2015-06-02T21:34:33.616Z"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetInstantConverter_Broken() {
+ config.getConverter(Instant.class).get().convert("alfasdf");
}
@Test
@@ -373,11 +618,47 @@ public void testBoolean() {
Assert.assertFalse(config.getValue("tck.config.test.javaconfig.configvalue.boolean.off", boolean.class));
}
+ @Test
+ public void testGetBooleanConverter() {
+ Converter converter = config.getConverter(Boolean.class).get();
+ Converter primitiveConverter = config.getConverter(boolean.class).get();
+
+ Assert.assertTrue(converter.convert("true"));
+ Assert.assertTrue(primitiveConverter.convert("true"));
+ Assert.assertTrue(converter.convert("TRUE"));
+ Assert.assertTrue(converter.convert("TruE"));
+ Assert.assertFalse(converter.convert("false"));
+
+ Assert.assertTrue(converter.convert("1"));
+ Assert.assertFalse(converter.convert("0"));
+ Assert.assertFalse(converter.convert("17"));
+
+ Assert.assertTrue(converter.convert("yes"));
+ Assert.assertTrue(converter.convert("YES"));
+ Assert.assertTrue(converter.convert("Yes"));
+ Assert.assertFalse(converter.convert("no"));
+
+ Assert.assertTrue(converter.convert("y"));
+ Assert.assertTrue(converter.convert("Y"));
+ Assert.assertFalse(converter.convert("n"));
+
+ Assert.assertTrue(converter.convert("on"));
+ Assert.assertTrue(converter.convert("ON"));
+ Assert.assertTrue(converter.convert("oN"));
+ Assert.assertFalse(converter.convert("off"));
+ Assert.assertFalse(primitiveConverter.convert("off"));
+ }
+
@Test
public void testCustomConverter() {
Assert.assertEquals(namedDuck.getName(), "Hannelore");
}
+ @Test
+ public void testGetCustomConverter() {
+ Assert.assertEquals(config.getConverter(Duck.class).get().convert("Hannelore").getName(), "Hannelore");
+ }
+
@Test
public void testDuckConversionWithMultipleConverters() {
// defines 2 config with the converters defined in different orders.
@@ -401,6 +682,29 @@ public void testDuckConversionWithMultipleConverters() {
"The converter with the highest priority (UpperCaseDuckConverter) must be used.");
}
+ @Test
+ public void testGetDuckConverterWithMultipleConverters() {
+ // defines 2 config with the converters defined in different orders.
+ // Order must not matter, the UpperCaseDuckConverter must always be used as it has the highest priority
+ Config config1 = ConfigProviderResolver.instance().getBuilder().addDefaultSources()
+ .withConverters(new UpperCaseDuckConverter(), new DuckConverter())
+ .build();
+ Config config2 = ConfigProviderResolver.instance().getBuilder().addDefaultSources()
+ .withConverters(new DuckConverter(), new UpperCaseDuckConverter())
+ .build();
+
+ Duck duck = config1.getConverter(Duck.class).get().convert("Hannelore");
+ Assert.assertNotNull(duck);
+ Assert.assertEquals(duck.getName(), "HANNELORE",
+ "The converter with the highest priority (UpperCaseDuckConverter) must be used.");
+
+ duck = config2.getConverter(Duck.class).get().convert("Hannelore");
+ Assert.assertNotNull(duck);
+ // the UpperCaseDuckConverter has highest priority
+ Assert.assertEquals(duck.getName(), "HANNELORE",
+ "The converter with the highest priority (UpperCaseDuckConverter) must be used.");
+ }
+
@Test
public void testURLConverter() throws MalformedURLException {
URL url = config.getValue("tck.config.test.javaconfig.converter.urlvalue", URL.class);
@@ -409,7 +713,18 @@ public void testURLConverter() throws MalformedURLException {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testURLConverterBroken() throws Exception {
- URL ignored = config.getValue("tck.config.test.javaconfig.converter.urlvalue.broken", URL.class);
+ config.getValue("tck.config.test.javaconfig.converter.urlvalue.broken", URL.class);
+ }
+
+ @Test
+ public void testGetURLConverter() throws MalformedURLException {
+ URL url = config.getConverter(URL.class).get().convert("http://microprofile.io");
+ Assert.assertEquals(url, new URL("http://microprofile.io"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetURLConverterBroken() throws Exception {
+ config.getConverter(URL.class).get().convert("tt:--location");
}
@Test
@@ -420,7 +735,18 @@ public void testURIConverter() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testURIConverterBroken() throws Exception {
- URI ignored = config.getValue("tck.config.test.javaconfig.converter.urivalue.broken", URI.class);
+ config.getValue("tck.config.test.javaconfig.converter.urivalue.broken", URI.class);
+ }
+
+ @Test
+ public void testGetURIConverter() {
+ URI uri = config.getConverter(URI.class).get().convert("http://microprofile.io");
+ Assert.assertEquals(uri, URI.create("http://microprofile.io"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testGetURIConverterBroken() throws Exception {
+ config.getConverter(URI.class).get().convert("space is an illegal uri character");
}
@Test
@@ -438,8 +764,27 @@ public void testConverterSerialization() {
} catch (IOException | ClassNotFoundException ex) {
Assert.fail("Converter instance should be serializable, but could not deserialize a previously serialized instance", ex);
}
- Assert.assertEquals(((DuckConverter) ((Converter) readObject)).convert("Donald").getName(),
+ Assert.assertEquals(((DuckConverter) ((Converter>) readObject)).convert("Donald").getName(),
original.convert("Donald").getName(),"Converted values to be equal");
+ }
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testGetConverterSerialization() {
+ Converter original = config.getConverter(Duck.class).get();
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ try (ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream)) {
+ out.writeObject(original);
+ } catch (IOException ex) {
+ Assert.fail("Converter instance should be serializable, but could not serialize it", ex);
+ }
+ Object readObject = null;
+ try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))) {
+ readObject = in.readObject();
+ } catch (IOException | ClassNotFoundException ex) {
+ Assert.fail("Converter instance should be serializable, but could not deserialize a previously serialized instance", ex);
+ }
+ Assert.assertEquals(((Converter) readObject).convert("Donald").getName(),
+ original.convert("Donald").getName(),"Converted values to be equal");
}
}
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CustomConverterTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CustomConverterTest.java
index 76cf6314..47ce8ede 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CustomConverterTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CustomConverterTest.java
@@ -121,4 +121,64 @@ public void testCharPrimitive() {
char value = config.getValue("tck.config.test.javaconfig.custom.converter.charvalue", char.class);
Assert.assertEquals(value, 'r');
}
+
+ @Test
+ public void testGetIntegerConverter() {
+ Integer value = config.getConverter(Integer.class).get().convert("1");
+ Assert.assertEquals(value, Integer.valueOf(999));
+ }
+
+ @Test
+ public void testGetIntPrimitiveConverter() {
+ int value = config.getConverter(int.class).get().convert("1");
+ Assert.assertEquals(value, 999);
+ }
+
+ @Test
+ public void testGetLongConverter() {
+ Long value = config.getConverter(Long.class).get().convert("1");
+ Assert.assertEquals(value, Long.valueOf(999));
+ }
+
+ @Test
+ public void testGetLongPrimitiveConverter() {
+ long value = config.getConverter(long.class).get().convert("1");
+ Assert.assertEquals(value, 999);
+ }
+
+ @Test
+ public void testGetDoubleConverter() {
+ Double value = config.getConverter(Double.class).get().convert("1.0");
+ Assert.assertEquals(value, 999.9);
+ }
+
+ @Test
+ public void testGetDoublePrimitiveConverter() {
+ double value = config.getConverter(double.class).get().convert("1.0");
+ Assert.assertEquals(value, 999.9);
+ }
+
+ @Test
+ public void testGetBooleanConverter() {
+ Boolean value = config.getConverter(Boolean.class).get().convert("false");
+ Assert.assertEquals(value, Boolean.TRUE);
+ }
+
+ @Test
+ public void testGetBooleanPrimitiveConverter() {
+ boolean value = config.getConverter(boolean.class).get().convert("false");
+ Assert.assertTrue(value);
+ }
+
+ @Test
+ public void testGetCharacterConverter() {
+ Character value = config.getConverter(Character.class).get().convert("c");
+ Assert.assertEquals(value, Character.valueOf('r'));
+ }
+
+ @Test
+ public void testGetCharPrimitiveConverter() {
+ char value = config.getConverter(char.class).get().convert("c");
+ Assert.assertEquals(value, 'r');
+ }
}
diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/ImplicitConverterTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/ImplicitConverterTest.java
index e3261762..bb3e7740 100644
--- a/tck/src/main/java/org/eclipse/microprofile/config/tck/ImplicitConverterTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/ImplicitConverterTest.java
@@ -154,5 +154,69 @@ public void testImplicitConverterSquenceParseBeforeConstructor() {
Assert.assertEquals(value.getVal(), "parseBeforeConstructor");
}
+ @Test
+ public void testGetImplicitConverterStringCtConverter() {
+ ConvTestTypeWStringCt value = config.getConverter(ConvTestTypeWStringCt.class).get().convert("stringCt");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.getVal(), "stringCt");
+ }
+ @Test
+ public void testGetImplicitConverterStringValueOfConverter() {
+ ConvTestTypeWStringValueOf value = config.getConverter(ConvTestTypeWStringValueOf.class).get()
+ .convert("stringValueOf");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.getVal(), "stringValueOf");
+ }
+ @Test
+ public void testGetImplicitConverterStringOfConverter() {
+ ConvTestTypeWStringOf value = config.getConverter(ConvTestTypeWStringOf.class).get().convert("stringOf");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.getVal(), "stringOf");
+ }
+
+ @Test
+ public void testGetImplicitConverterCharSequenceParseConverter() {
+ ConvTestTypeWCharSequenceParse value = config.getConverter(ConvTestTypeWCharSequenceParse.class).get()
+ .convert("charSequenceParse");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.getVal(), "charSequenceParse");
+ }
+ @Test
+ public void testGetImplicitConverterCharSequenceParseJavaTimeConverter() {
+ YearMonth value = config.getConverter(YearMonth.class).get().convert("2017-12");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value, YearMonth.parse("2017-12"));
+ }
+
+ @Test
+ public void testGetImplicitConverterEnumValueOfConverter() {
+ SomeEnumToConvert value = config.getConverter(SomeEnumToConvert.class).get().convert("BAZ");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value, SomeEnumToConvert.BAZ);
+ Assert.assertEquals(value.name(), "BAZ");
+ }
+
+ @Test
+ public void testGetImplicitConverterSquenceOfBeforeValueOfConverter() {
+ ConvTestSequenceOfBeforeValueOf value = config.getConverter(ConvTestSequenceOfBeforeValueOf.class).get()
+ .convert("ofBeforeValueOf");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.getVal(), "ofBeforeValueOf");
+ }
+
+ @Test
+ public void testGetImplicitConverterSquenceValueOfBeforeParseConverter() {
+ ConvTestSequenceValueOfBeforeParse value = config.getConverter(ConvTestSequenceValueOfBeforeParse.class).get()
+ .convert("valueOfBeforeParse");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.getVal(), "valueOfBeforeParse");
+ }
+ @Test
+ public void testGetImplicitConverterSquenceParseBeforeConstructorConverter() {
+ ConvTestSequenceParseBeforeConstructor value = config.getConverter(ConvTestSequenceParseBeforeConstructor.class).get()
+ .convert("parseBeforeConstructor");
+ Assert.assertNotNull(value);
+ Assert.assertEquals(value.getVal(), "parseBeforeConstructor");
+ }
}