Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getConverter method to Config #495

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/src/main/java/org/eclipse/microprofile/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Optional;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;

/**
* <p>
Expand Down Expand Up @@ -161,4 +162,15 @@ public interface Config {
* @return the configuration sources
*/
Iterable<ConfigSource> getConfigSources();

/**
* Return the {@link Converter} used by this instance to produce instances of the specified type from string values.
*
* @param <T>
* 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
*/
<T> Optional<Converter<T>> getConverter(Class<T> forType);
emattheis marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.booleanvalues",
Expand All @@ -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() {
Expand Down Expand Up @@ -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<String[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.stringvalues",
Expand Down Expand Up @@ -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<Integer[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.integervalues",
Expand All @@ -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);
Expand Down Expand Up @@ -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<Long[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.longvalues",
Expand All @@ -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);
Expand Down Expand Up @@ -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<Float[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.floatvalues",
Expand All @@ -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);
Expand Down Expand Up @@ -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<Double[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.doublevalues",
Expand All @@ -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);
Expand Down Expand Up @@ -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<Duration[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.durationvalues",
Expand Down Expand Up @@ -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<LocalTime[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.localtimevalues",
Expand Down Expand Up @@ -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<LocalDate[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.localdatevalues",
Expand Down Expand Up @@ -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<LocalDateTime[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.localdatetimevalues",
Expand Down Expand Up @@ -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<OffsetDateTime[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.offsetdatetimevalues",
Expand Down Expand Up @@ -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<OffsetTime[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.offsettimevalues",
Expand Down Expand Up @@ -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<Instant[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.instantvalues",
Expand Down Expand Up @@ -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<URL[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.urlvalues", URL[].class);
Expand Down Expand Up @@ -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<URI[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.urlvalues", URI[].class);
Expand Down Expand Up @@ -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<Pizza[]> optionalValue = config.getOptionalValue("tck.config.test.javaconfig.converter.array.pizza",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading