http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java index b91cb59..2983f5c 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java @@ -18,6 +18,9 @@ */ package org.apache.tamaya.spisupport; +import java.util.HashMap; +import java.util.Map; +import org.apache.tamaya.spi.PropertyValue; import org.apache.tamaya.spisupport.propertysource.BuildablePropertySource; import org.junit.Test; @@ -46,7 +49,7 @@ public class BuildablePropertySourceTest { .withSimpleProperty("a", "b").build(); assertEquals("b", ps1.get("a").getValue()); } - + @Test public void getProperties() throws Exception { BuildablePropertySource ps1 = BuildablePropertySource.builder() @@ -55,6 +58,61 @@ public class BuildablePropertySourceTest { assertEquals(1, ps1.getProperties().size()); assertEquals("b", ps1.getProperties().get("a").getValue()); } + + @Test + public void testScannable() { + BuildablePropertySource bps = BuildablePropertySource.builder().build(); + assertTrue(bps.isScannable()); + } + + @Test + public void testSource() { + BuildablePropertySource bps = BuildablePropertySource.builder() + .withSource("fakeSource") + .withSimpleProperty("defaultSourceKey", "defaultSourceValue") + .withSimpleProperty("namedSourceKey", "namedSourceValue", "namedSource") + .build(); + + assertEquals("fakeSource", bps.get("defaultSourceKey").getSource()); + assertEquals("namedSource", bps.get("namedSourceKey").getSource()); + } + + @Test + public void testWithMaps() { + Map<String, String> propertyFirst = new HashMap<>(); + propertyFirst.put("firstKey", "firstValue"); + Map<String, String> propertySecond = new HashMap<>(); + propertySecond.put("secondKey", "secondValue"); + + Map<String, PropertyValue> propertyThird = new HashMap<>(); + propertyThird.put("thirdKey", PropertyValue.of("thirdPVKey", "thirdValue", "thirdSource")); + + //This seems wrong + BuildablePropertySource bps = BuildablePropertySource.builder() + .withSimpleProperties(propertyFirst) + .withProperties(propertySecond, "secondSource") + .withProperties(propertyThird) + .build(); + + assertNull(bps.get("firstKey")); + assertNull(bps.get("secondKey")); + assertEquals("thirdValue", bps.get("thirdKey").getValue()); + assertEquals("thirdSource", bps.get("thirdKey").getSource()); + assertNull(bps.get("thirdPVKey")); + + bps = BuildablePropertySource.builder() + .withProperties(propertyThird) + .withSimpleProperties(propertyFirst) + .withProperties(propertySecond, "secondSource") + .build(); + + assertEquals("firstValue", bps.get("firstKey").getValue()); + assertEquals("secondSource", bps.get("secondKey").getSource()); + assertEquals("secondValue", bps.get("secondKey").getValue()); + assertEquals("thirdValue", bps.get("thirdKey").getValue()); + assertEquals("thirdSource", bps.get("thirdKey").getSource()); + assertNull(bps.get("thirdPVKey")); + } @Test public void equals() throws Exception { @@ -66,6 +124,8 @@ public class BuildablePropertySourceTest { ps2 = BuildablePropertySource.builder() .withName("test2").build(); assertNotEquals(ps1, ps2); + assertNotEquals(ps2, null); + assertNotEquals(ps1, "aString"); } @Test @@ -83,6 +143,7 @@ public class BuildablePropertySourceTest { @Test public void builder() throws Exception { assertNotNull(BuildablePropertySource.builder()); + assertNotNull(BuildablePropertySource.builder().but()); assertNotEquals(BuildablePropertySource.builder(), BuildablePropertySource.builder()); }
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java new file mode 100644 index 0000000..e77b99f --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluatorTest.java @@ -0,0 +1,61 @@ +/* + * 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 java.util.Map; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.spi.PropertyValue; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author William.Lieurance 2018-02-10 + */ +public class DefaultConfigValueEvaluatorTest { + + + /** + * Test of evaluteRawValue method, of class DefaultConfigValueEvaluator. + */ + @Test + public void testEvaluteRawValue() { + Configuration config = ConfigurationProvider.getConfiguration(); + DefaultConfigValueEvaluator instance = new DefaultConfigValueEvaluator(); + PropertyValue result = instance.evaluteRawValue("confkey1", config.getContext()); + assertEquals("javaconf-value1", result.getValue()); + result = instance.evaluteRawValue("missing", config.getContext()); + assertNull(result); + } + + /** + * Test of evaluateRawValues method, of class DefaultConfigValueEvaluator. + */ + @Test + public void testEvaluateRawValues() { + Configuration config = ConfigurationProvider.getConfiguration(); + DefaultConfigValueEvaluator instance = new DefaultConfigValueEvaluator(); + Map<String, PropertyValue> result = instance.evaluateRawValues(config.getContext()); + assertTrue(result.containsKey("confkey1")); + assertEquals("javaconf-value1", result.get("confkey1").getValue()); + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java index c586969..a7f4088 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilderTest.java @@ -18,6 +18,8 @@ */ package org.apache.tamaya.spisupport; +import java.util.Arrays; +import java.util.Collection; import org.apache.tamaya.Configuration; import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; @@ -26,6 +28,7 @@ import org.apache.tamaya.spi.*; import org.junit.Test; import java.util.Collections; +import java.util.Comparator; import java.util.Map; import static org.junit.Assert.*; @@ -35,7 +38,8 @@ import static org.junit.Assert.*; */ public class DefaultConfigurationBuilderTest { - private TestPropertySource testPropertySource = new TestPropertySource(){}; + private TestPropertySource testPropertySource = new TestPropertySource() { + }; @Test public void setContext() throws Exception { @@ -54,8 +58,8 @@ public class DefaultConfigurationBuilderTest { } @Test - public void addPropertySources_Array() throws Exception { - PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); + public void addRemovePropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addRemovePropertySources_Array"); ConfigurationBuilder b = new DefaultConfigurationBuilder() .addPropertySources(testPropertySource, testPS2); Configuration cfg = b.build(); @@ -63,22 +67,30 @@ public class DefaultConfigurationBuilderTest { assertEquals(2, ctx.getPropertySources().size()); assertTrue(ctx.getPropertySources().contains(testPropertySource)); assertTrue(ctx.getPropertySources().contains(testPS2)); + + b = new DefaultConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + cfg = b.removePropertySources(testPropertySource).build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertySources().size()); + assertFalse(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); } @Test - public void removePropertySources_Array() throws Exception { - PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); + public void addRemovePropertySources_Collection() throws Exception { + PropertySource testPS2 = new TestPropertySource("addRemovePropertySources_Collection"); ConfigurationBuilder b = new DefaultConfigurationBuilder() - .addPropertySources(testPropertySource, testPS2); + .addPropertySources(Arrays.asList(testPropertySource, testPS2)); Configuration cfg = b.build(); ConfigurationContext ctx = cfg.getContext(); assertEquals(2, ctx.getPropertySources().size()); assertTrue(ctx.getPropertySources().contains(testPropertySource)); assertTrue(ctx.getPropertySources().contains(testPS2)); + b = new DefaultConfigurationBuilder() .addPropertySources(testPropertySource, testPS2); - b.removePropertySources(testPropertySource); - cfg = b.build(); + cfg = b.removePropertySources(Arrays.asList(testPropertySource)).build(); ctx = cfg.getContext(); assertEquals(1, ctx.getPropertySources().size()); assertFalse(ctx.getPropertySources().contains(testPropertySource)); @@ -86,75 +98,277 @@ public class DefaultConfigurationBuilderTest { } @Test - public void addPropertyFilters_Array() throws Exception { + public void addRemovePropertyFilters_Array() throws Exception { PropertyFilter filter1 = (value, context) -> value; PropertyFilter filter2 = (value, context) -> value; DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); - b.addPropertyFilters(filter1, filter2); - Configuration cfg = b.build(); + Configuration cfg = b.addPropertyFilters(filter1, filter2).build(); ConfigurationContext ctx = cfg.getContext(); assertTrue(ctx.getPropertyFilters().contains(filter1)); assertTrue(ctx.getPropertyFilters().contains(filter2)); assertEquals(2, ctx.getPropertyFilters().size()); + b = new DefaultConfigurationBuilder(); b.addPropertyFilters(filter1, filter2); - b.addPropertyFilters(filter1, filter2); + cfg = b.addPropertyFilters(filter1, filter2).build(); + ctx = cfg.getContext(); assertEquals(2, ctx.getPropertyFilters().size()); + + b = new DefaultConfigurationBuilder(); + b.addPropertyFilters(filter1, filter2); + cfg = b.removePropertyFilters(filter1).build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } @Test - public void removePropertyFilters_Array() throws Exception { + public void addRemovePropertyFilters_Collection() throws Exception { PropertyFilter filter1 = (value, context) -> value; PropertyFilter filter2 = (value, context) -> value; - ConfigurationBuilder b = new DefaultConfigurationBuilder() - .addPropertyFilters(filter1, filter2); - Configuration cfg = b.build(); + DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); + Configuration cfg = b.addPropertyFilters(Arrays.asList(filter1, filter2)).build(); ConfigurationContext ctx = cfg.getContext(); assertTrue(ctx.getPropertyFilters().contains(filter1)); assertTrue(ctx.getPropertyFilters().contains(filter2)); assertEquals(2, ctx.getPropertyFilters().size()); - b = new DefaultConfigurationBuilder() - .addPropertyFilters(filter1, filter2); - b.removePropertyFilters(filter1); - cfg = b.build(); + + b = new DefaultConfigurationBuilder(); + b.addPropertyFilters(Arrays.asList(filter1, filter2, filter1)); + cfg = b.addPropertyFilters(Arrays.asList(filter1, filter2)).build(); + ctx = cfg.getContext(); + assertEquals(2, ctx.getPropertyFilters().size()); + + b = new DefaultConfigurationBuilder(); + b.addPropertyFilters(Arrays.asList(filter1, filter2)); + cfg = b.removePropertyFilters(Arrays.asList(filter1)).build(); ctx = cfg.getContext(); assertEquals(1, ctx.getPropertyFilters().size()); assertFalse(ctx.getPropertyFilters().contains(filter1)); assertTrue(ctx.getPropertyFilters().contains(filter2)); + + } + + @Test + public void increasePriority() { + DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + b.increasePriority(propertySources[propertySources.length - 1]); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.increasePriority(propertySources[propertySources.length - 2]).build(); + for (int i = 0; i < propertySources.length - 2; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[propertySources.length - 1], b.getPropertySources().get(propertySources.length - 2)); + assertEquals(propertySources[propertySources.length - 2], b.getPropertySources().get(propertySources.length - 1)); + boolean caughtAlreadyBuilt = false; + try { + b.increasePriority(propertySources[propertySources.length - 2]); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void decreasePriority() { + DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + b.decreasePriority(propertySources[0]); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.decreasePriority(propertySources[1]).build(); + for (int i = 2; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[0], b.getPropertySources().get(1)); + assertEquals(propertySources[1], b.getPropertySources().get(0)); + boolean caughtAlreadyBuilt = false; + try { + b.decreasePriority(propertySources[1]); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void lowestPriority() { + // setup + DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + // test + b.lowestPriority(propertySources[0]); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.lowestPriority(propertySources[1]); + for (int i = 2; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[0], b.getPropertySources().get(1)); + assertEquals(propertySources[1], b.getPropertySources().get(0)); + b.lowestPriority(propertySources[5]).build(); + assertEquals(propertySources[5], b.getPropertySources().get(0)); + boolean caughtAlreadyBuilt = false; + try { + b.lowestPriority(propertySources[5]); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void highestPriority() { + // setup + DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + // test + b.highestPriority(propertySources[propertySources.length - 1]); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.highestPriority(propertySources[propertySources.length - 2]); + for (int i = 0; i < propertySources.length - 2; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[propertySources.length - 2], b.getPropertySources().get(propertySources.length - 1)); + assertEquals(propertySources[propertySources.length - 1], b.getPropertySources().get(propertySources.length - 2)); + b.highestPriority(propertySources[5]).build(); + assertEquals(propertySources[5], b.getPropertySources().get(propertySources.length - 1)); + boolean caughtAlreadyBuilt = false; + try { + b.highestPriority(propertySources[5]); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void sortPropertySources() { + // setup + DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + Comparator<PropertySource> psComp = (o1, o2) -> o1.toString().compareTo(o2.toString()); + // test + assertEquals(DefaultConfigurationBuilder.class, b.sortPropertySources(psComp).getClass()); + Arrays.sort(propertySources, psComp); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + } + + @Test + public void sortPropertyFilter() { + // setup + DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(); + PropertyFilter[] propertyFilters = new PropertyFilter[10]; + for (int i = 0; i < propertyFilters.length; i++) { + propertyFilters[i] = (value, context) -> value.toBuilder().setValue(toString() + " - ").build(); + } + + b.addPropertyFilters(propertyFilters); + Comparator<PropertyFilter> pfComp = (o1, o2) -> o1.toString().compareTo(o2.toString()); + //test + assertEquals(DefaultConfigurationBuilder.class, b.sortPropertyFilter(pfComp).getClass()); + Arrays.sort(propertyFilters, pfComp); + for (int i = 0; i < propertyFilters.length; i++) { + assertEquals(propertyFilters[i], b.getPropertyFilters().get(i)); + } } @Test - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void addPropertyConverter() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); - ConfigurationBuilder b = new DefaultConfigurationBuilder() - .addPropertyConverters(TypeLiteral.of(String.class), converter); + public void addRemovePropertyConverter_Array() throws Exception { + PropertyConverter converter1 = (value, context) -> value.toLowerCase(); + PropertyConverter converter2 = (value, context) -> value.toUpperCase(); + ConfigurationBuilder b = new DefaultConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter1, converter2); Configuration cfg = b.build(); ConfigurationContext ctx = cfg.getContext(); - assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> buildConverters = b.getPropertyConverter(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter1)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter2)); assertEquals(1, ctx.getPropertyConverters().size()); + assertEquals(2, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + assertTrue(buildConverters.get(TypeLiteral.of(String.class)).containsAll( + ctx.getPropertyConverters().get(TypeLiteral.of(String.class)))); + b = new DefaultConfigurationBuilder() - .addPropertyConverters(TypeLiteral.of(String.class), converter); - b.addPropertyConverters(TypeLiteral.of(String.class), converter); - assertEquals(1, ctx.getPropertyConverters().size()); + .addPropertyConverters(TypeLiteral.of(String.class), converter1); + cfg = b.addPropertyConverters(TypeLiteral.of(String.class), converter1).build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + + b = new DefaultConfigurationBuilder().addPropertyConverters(TypeLiteral.of(String.class), converter1, converter2); + cfg = b.removePropertyConverters(TypeLiteral.of(String.class), converter1).build(); + ctx = cfg.getContext(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter1)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter2)); + + b = new DefaultConfigurationBuilder().addPropertyConverters(TypeLiteral.of(String.class), converter1, converter2); + cfg = b.removePropertyConverters(TypeLiteral.of(String.class)).build(); + ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); } @Test - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void removePropertyConverters_Array() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); + public void addRemovePropertyConverter_Collection() throws Exception { + PropertyConverter converter1 = (value, context) -> value.toLowerCase(); + PropertyConverter converter2 = (value, context) -> value.toUpperCase(); ConfigurationBuilder b = new DefaultConfigurationBuilder() - .addPropertyConverters(TypeLiteral.of(String.class), converter); + .addPropertyConverters(TypeLiteral.of(String.class), Arrays.asList(converter1, converter2)); Configuration cfg = b.build(); ConfigurationContext ctx = cfg.getContext(); - assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); - assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> buildConverters = b.getPropertyConverter(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter1)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter2)); + assertEquals(1, ctx.getPropertyConverters().size()); + assertEquals(2, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + assertTrue(buildConverters.get(TypeLiteral.of(String.class)).containsAll( + ctx.getPropertyConverters().get(TypeLiteral.of(String.class)))); + b = new DefaultConfigurationBuilder() - .addPropertyConverters(TypeLiteral.of(String.class), converter); - b.removePropertyConverters(TypeLiteral.of(String.class), converter); - cfg = b.build(); + .addPropertyConverters(TypeLiteral.of(String.class), Arrays.asList(converter1)); + cfg = b.addPropertyConverters(TypeLiteral.of(String.class), Arrays.asList(converter1)).build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + + b = new DefaultConfigurationBuilder().addPropertyConverters(TypeLiteral.of(String.class), Arrays.asList(converter1, converter2)); + cfg = b.removePropertyConverters(TypeLiteral.of(String.class), Arrays.asList(converter1)).build(); + ctx = cfg.getContext(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter1)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter2)); + + b = new DefaultConfigurationBuilder().addPropertyConverters(TypeLiteral.of(String.class), Arrays.asList(converter1, converter2)); + cfg = b.removePropertyConverters(TypeLiteral.of(String.class)).build(); ctx = cfg.getContext(); - assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); } @@ -179,15 +393,15 @@ public class DefaultConfigurationBuilderTest { builder.addDefaultPropertyConverters(); } - private static class TestPropertySource implements PropertySource{ + private static class TestPropertySource implements PropertySource { private String id; - public TestPropertySource(){ + public TestPropertySource() { this(null); } - public TestPropertySource(String id){ + public TestPropertySource(String id) { this.id = id; } @@ -198,7 +412,7 @@ public class DefaultConfigurationBuilderTest { @Override public String getName() { - return id!=null?id:"TestPropertySource"; + return id != null ? id : "TestPropertySource"; } @Override @@ -217,4 +431,4 @@ public class DefaultConfigurationBuilderTest { } } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java new file mode 100644 index 0000000..b783877 --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextBuilderTest.java @@ -0,0 +1,675 @@ +/* + * 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.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValueCombinationPolicy; +import static org.junit.Assert.*; + +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.ArrayList; + +/** + * + * @author William.Lieurance 2018.02.17 + * + * This class is an almost exact copy of + * com.tamaya.core.internal.CoreConfigurationBuilderTest, which itself uses + * DefaultConfigurationContextBuilder under the covers. + * + */ +public class DefaultConfigurationContextBuilderTest { + + private MockedPropertySource testPropertySource = new MockedPropertySource() { + }; + + @Test + public void setContext() throws Exception { + ConfigurationContext context = ConfigurationProvider.getConfiguration().getContext(); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .setContext(context); + assertEquals(context, b.build()); + boolean caughtAlreadyBuilt = false; + try { + b.setContext(context); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + b = new DefaultConfigurationContextBuilder(context); + assertEquals(context, b.build()); + } + + @Test + public void addPropertySources_Array() throws Exception { + PropertySource testPS2 = new MockedPropertySource("addPropertySources_Array", 1); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ConfigurationContext ctx = b.build(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPropertySource.getName())); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPS2.getName())); + // Ensure no sorting happens during add, so switch ordinals! + testPS2 = new MockedPropertySource("addPropertySources_Array", 1); + b = new DefaultConfigurationContextBuilder(); + ctx = b.addPropertySources(testPS2, testPropertySource).build(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPropertySource.getName())); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPS2.getName())); + assertEquals(ctx.getPropertySources().get(1).getName(), "MockedPropertySource"); + assertEquals(ctx.getPropertySources().get(0).getName(), "addPropertySources_Array"); + } + + @Test + public void addPropertySources_Collection() throws Exception { + PropertySource testPS2 = new MockedPropertySource("addPropertySources_Collection", 1); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertySources(Arrays.asList(new PropertySource[]{testPropertySource, testPS2})); + ConfigurationContext ctx = b.build(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPropertySource.getName())); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPS2.getName())); + assertEquals(ctx.getPropertySources().get(0).getName(), "MockedPropertySource"); + assertEquals(ctx.getPropertySources().get(1).getName(), "addPropertySources_Collection"); + // Ensure no sorting happens during add, so switch ordinals! + testPS2 = new MockedPropertySource("addPropertySources_Collection", 1); + ctx = new DefaultConfigurationContextBuilder() + .addPropertySources(Arrays.asList(new PropertySource[]{testPS2, testPropertySource})).build(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPropertySource.getName())); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPS2.getName())); + assertEquals(ctx.getPropertySources().get(1).getName(), "MockedPropertySource"); + assertEquals(ctx.getPropertySources().get(0).getName(), "addPropertySources_Collection"); + } + + @Test + public void removePropertySources_Array() throws Exception { + PropertySource testPS2 = new MockedPropertySource("removePropertySources_Array", 1); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ConfigurationContext ctx = b.build(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPropertySource.getName())); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPS2.getName())); + b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ctx = b.removePropertySources(testPropertySource).build(); + assertFalse(ctx.getPropertySources().contains(testPropertySource)); + //Throws an exception + //assertNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPropertySource.getName())); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPS2.getName())); + assertEquals(1, ctx.getPropertySources().size()); + } + + @Test + public void removePropertySources_Collection() throws Exception { + PropertySource testPS2 = new MockedPropertySource("removePropertySources_Array", 1); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ConfigurationContext ctx = b.build(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertNotNull(((DefaultConfigurationContextBuilder)b).getPropertySource(testPropertySource.getName())); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertNotNull(ctx.getPropertySource(testPS2.getName())); + b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ctx = b.removePropertySources(testPropertySource).build(); + assertEquals(1, ctx.getPropertySources().size()); + assertFalse(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertNotNull(ctx.getPropertySource(testPS2.getName())); + } + + @Test(expected = IllegalArgumentException.class) + public void missingPropertySource() throws Exception { + PropertySource testPS2 = new MockedPropertySource("removePropertySources_Array", 1); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + assertNull(((DefaultConfigurationContextBuilder)b).getPropertySource("missing")); + } + + @Test + public void addPropertyFilters_Array() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + ConfigurationContext ctx = b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.addPropertyFilters(filter1, filter2); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertEquals(2, ctx.getPropertyFilters().size()); + } + + @Test + public void addPropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + ConfigurationContext ctx = b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertEquals(2, ctx.getPropertyFilters().size()); + } + + @Test + public void removePropertyFilters_Array() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertyFilters(filter1, filter2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = new DefaultConfigurationContextBuilder() + .addPropertyFilters(filter1, filter2); + ctx = b.removePropertyFilters(filter1).build(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + boolean caughtAlreadyBuilt = false; + try { + b.removePropertyFilters(filter1); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void removePropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = new DefaultConfigurationContextBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + ctx = b.removePropertyFilters(Arrays.asList(filter1)).build(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + boolean caughtAlreadyBuilt = false; + try { + b.removePropertyFilters(filter1); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void addPropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + ConfigurationContext ctx = b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters().size()); + b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(1, ctx.getPropertyConverters().size()); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void addPropertyConverters_Collection() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), + Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + ConfigurationContext ctx = b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.addPropertyConverters(TypeLiteral.of(String.class), + Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(ctx.getPropertyConverters().size(), 1); + b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), + Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(ctx.getPropertyConverters().size(), 1); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void removePropertyConverters_Type() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + ctx = b.removePropertyConverters(TypeLiteral.of(String.class)).build(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); + } + + @Test + @SuppressWarnings({"rawtypes", "unchecked"}) + public void removePropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + ctx = b.removePropertyConverters(TypeLiteral.of(String.class), converter).build(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + @Test + public void removePropertyConverters_Collection() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + ctx = b.removePropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})).build(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); + } + + @Test + public void setPropertyValueCombinationPolicy() throws Exception { + PropertyValueCombinationPolicy combPol = (currentValue, key, propertySource) -> currentValue; + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .setPropertyValueCombinationPolicy(combPol); + ConfigurationContext ctx = b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.setPropertyValueCombinationPolicy(combPol); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + assertEquals(ctx.getPropertyValueCombinationPolicy(), combPol); + } + + @Test + public void increasePriority() { + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + b.increasePriority(propertySources[propertySources.length - 1]); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.increasePriority(propertySources[propertySources.length - 2]).build(); + for (int i = 0; i < propertySources.length - 2; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[propertySources.length - 1], b.getPropertySources().get(propertySources.length - 2)); + assertEquals(propertySources[propertySources.length - 2], b.getPropertySources().get(propertySources.length - 1)); + boolean caughtAlreadyBuilt = false; + try { + b.increasePriority(propertySources[propertySources.length - 2]); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void decreasePriority() { + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + b.decreasePriority(propertySources[0]); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.decreasePriority(propertySources[1]).build(); + for (int i = 2; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[0], b.getPropertySources().get(1)); + assertEquals(propertySources[1], b.getPropertySources().get(0)); + boolean caughtAlreadyBuilt = false; + try { + b.decreasePriority(propertySources[1]); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void lowestPriority() { + // setup + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + // test + b.lowestPriority(propertySources[0]); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.lowestPriority(propertySources[1]); + for (int i = 2; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[0], b.getPropertySources().get(1)); + assertEquals(propertySources[1], b.getPropertySources().get(0)); + b.lowestPriority(propertySources[5]).build(); + assertEquals(propertySources[5], b.getPropertySources().get(0)); + boolean caughtAlreadyBuilt = false; + try { + b.lowestPriority(propertySources[5]); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void highestPriority() { + // setup + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + // test + b.highestPriority(propertySources[propertySources.length - 1]); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.highestPriority(propertySources[propertySources.length - 2]); + for (int i = 0; i < propertySources.length - 2; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[propertySources.length - 2], b.getPropertySources().get(propertySources.length - 1)); + assertEquals(propertySources[propertySources.length - 1], b.getPropertySources().get(propertySources.length - 2)); + b.highestPriority(propertySources[5]).build(); + assertEquals(propertySources[5], b.getPropertySources().get(propertySources.length - 1)); + boolean caughtAlreadyBuilt = false; + try { + b.highestPriority(propertySources[5]); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void sortPropertySources() { + // setup + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + MockedPropertySource[] propertySources = new MockedPropertySource[10]; + for (int i = 0; i < propertySources.length; i++) { + propertySources[i] = new MockedPropertySource("ps" + i, i); + } + b.addPropertySources(propertySources); + Comparator<PropertySource> psComp = (o1, o2) -> o1.toString().compareTo(o2.toString()); + // test + assertEquals(DefaultConfigurationContextBuilder.class, b.sortPropertySources(psComp).getClass()); + Arrays.sort(propertySources, psComp); + for (int i = 0; i < propertySources.length; i++) { + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + } + + @Test + public void sortPropertyFilter() { + // setup + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + PropertyFilter[] propertyFilters = new PropertyFilter[10]; + for (int i = 0; i < propertyFilters.length; i++) { + propertyFilters[i] = (value, context) -> value.toBuilder().setValue(toString() + " - ").build(); + } + + b.addPropertyFilters(propertyFilters); + Comparator<PropertyFilter> pfComp = (o1, o2) -> o1.toString().compareTo(o2.toString()); + //test + assertEquals(DefaultConfigurationContextBuilder.class, b.sortPropertyFilter(pfComp).getClass()); + Arrays.sort(propertyFilters, pfComp); + for (int i = 0; i < propertyFilters.length; i++) { + assertEquals(propertyFilters[i], b.getPropertyFilters().get(i)); + } + } + + @Test + public void build() throws Exception { + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + ConfigurationContext ctx = b.build(); + assertNotNull(ctx); + assertTrue(ctx.getPropertySources().isEmpty()); + assertTrue(ctx.getPropertyFilters().isEmpty()); + boolean caughtAlreadyBuilt = false; + try { + b.build(); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + } + + @Test + public void testRemoveAllFilters() throws Exception { + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters((value, context) -> value.toBuilder().setValue(toString() + " - ").build()); + assertFalse(b.getPropertyFilters().isEmpty()); + b.removePropertyFilters(b.getPropertyFilters()); + assertTrue(b.getPropertyFilters().isEmpty()); + } + + @Test + public void testRemoveAllSources() throws Exception { + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.addPropertySources(new MockedPropertySource()); + assertFalse(b.getPropertySources().isEmpty()); + b.removePropertySources(b.getPropertySources()); + assertTrue(b.getPropertyFilters().isEmpty()); + } + + @Test + public void testResetContext() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + DefaultConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + ConfigurationContext empty = b.build(); + + b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters((value, context) -> value.toBuilder().setValue(toString() + " - ").build()); + b.addPropertySources(new MockedPropertySource()); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + ConfigurationContext full = b.build(); + + boolean caughtAlreadyBuilt = false; + try { + b.resetWithConfigurationContext(empty); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + + b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters((value, context) -> value.toBuilder().setValue(toString() + " - ").build()); + b.addPropertySources(new MockedPropertySource()); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + b.resetWithConfigurationContext(empty); + assertTrue(b.getPropertyConverter().isEmpty()); + assertTrue(b.getPropertySources().isEmpty()); + assertTrue(b.getPropertyFilters().isEmpty()); + b.resetWithConfigurationContext(full).build(); + assertFalse(b.getPropertyConverter().isEmpty()); + assertFalse(b.getPropertySources().isEmpty()); + assertFalse(b.getPropertyFilters().isEmpty()); + + } + + @Test + public void testLoadDefaults() throws Exception { + DefaultConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.loadDefaults(); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + + ConfigurationContext ctx = new DefaultConfigurationContextBuilder().loadDefaults().build(); + assertFalse(ctx.getPropertyConverters().isEmpty()); + assertFalse(ctx.getPropertyFilters().isEmpty()); + assertFalse(ctx.getPropertySources().isEmpty()); + } + + + @Test + public void testAddDefaultPropertyConverters() throws Exception { + DefaultConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.addDefaultPropertyConverters(); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + + ConfigurationContext ctx = new DefaultConfigurationContextBuilder().addDefaultPropertyConverters().build(); + assertFalse(ctx.getPropertyConverters().isEmpty()); + assertNotNull(ctx.getPropertyConverters(TypeLiteral.of(Integer.class))); + } + + @Test + public void testAddDefaultPropertyFilters() throws Exception { + DefaultConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.addDefaultPropertyFilters(); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + + ConfigurationContext ctx = new DefaultConfigurationContextBuilder().addDefaultPropertyFilters().build(); + assertFalse(ctx.getPropertyFilters().isEmpty()); + } + + @Test + public void testAddDefaultPropertySources() throws Exception { + DefaultConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.build(); + boolean caughtAlreadyBuilt = false; + try { + b.addDefaultPropertySources(); + } catch (IllegalStateException e) { + caughtAlreadyBuilt = true; + } + assertTrue(caughtAlreadyBuilt); + + ConfigurationContext ctx = new DefaultConfigurationContextBuilder().addDefaultPropertySources().build(); + assertFalse(ctx.getPropertySources().isEmpty()); + assertNotNull(ctx.getPropertySource("environment-properties")); + } + + @Test + public void testAddCorePropertyReources() throws Exception { + DefaultConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + List<PropertySource> ps = new ArrayList<>(); + b.addCorePropertyResources(ps); + assertFalse(ps.isEmpty()); + + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java new file mode 100644 index 0000000..ee63a31 --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java @@ -0,0 +1,59 @@ +/* + * 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 java.util.List; +import java.util.Map; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValueCombinationPolicy; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author William.Lieurance 2018.02.18 + */ +public class DefaultConfigurationContextTest { + + @Test + public void testEqualsAndHashAndToStringValues() { + PropertySource sharedSource = new MockedPropertySource(); + DefaultConfigurationContext ctx1 = (DefaultConfigurationContext) new DefaultConfigurationContextBuilder().build(); + ctx1.addPropertySources(sharedSource); + DefaultConfigurationContext ctx2 = (DefaultConfigurationContext) new DefaultConfigurationContextBuilder().build(); + ctx2.addPropertySources(sharedSource); + DefaultConfigurationContext ctx3 = (DefaultConfigurationContext) new DefaultConfigurationContextBuilder().build(); + ctx3.addPropertySources(new MockedPropertySource()); + + assertEquals(ctx1, ctx1); + assertNotEquals(null, ctx1); + assertNotEquals("aString", ctx1); + assertEquals(ctx1, ctx2); + assertNotEquals(ctx1, ctx3); + assertEquals(ctx1.hashCode(), ctx2.hashCode()); + assertNotEquals(ctx1.hashCode(), ctx3.hashCode()); + String spaces = new String(new char[70 - sharedSource.getName().length()]).replace("\0", " "); + System.out.println(ctx1.toString()); + assertTrue(ctx1.toString().contains(sharedSource.getName() + spaces)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java index 4711dc5..c05a09e 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationTest.java @@ -22,12 +22,9 @@ import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.*; import org.junit.Test; -import java.util.Collections; -import java.util.List; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.*; public class DefaultConfigurationTest { @@ -36,7 +33,7 @@ public class DefaultConfigurationTest { */ @Test(expected = NullPointerException.class) public void getDoesNotAcceptNull() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.get(null); } @@ -44,10 +41,10 @@ public class DefaultConfigurationTest { /** * Tests for get(String, Class) */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Test(expected = NullPointerException.class) + @SuppressWarnings({"rawtypes", "unchecked"}) + @Test(expected = NullPointerException.class) public void getDoesNotAcceptNullForClassTargetType() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.get("a", (Class) null); } @@ -57,9 +54,17 @@ public class DefaultConfigurationTest { */ @Test(expected = NullPointerException.class) public void getDoesNotAcceptNullForTypeLiteralTargetType() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + + c.get("a", (TypeLiteral<?>) null); + } - c.get("a", (TypeLiteral<?>)null); + @Test + public void getReturnsNullOrNotAsAppropriate() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + assertNotNull(c.get("valueOfValid")); + assertNull(c.get("valueOfNull")); + assertNull(c.get("Filternull")); //get does apply filtering } /** @@ -67,24 +72,17 @@ public class DefaultConfigurationTest { */ @Test(expected = NullPointerException.class) public void getOrDefaultDoesNotAcceptNullAsKeyForThreeParameterVariant() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.getOrDefault(null, String.class, "ok"); } - @Test - public void getOrDefaultDoesAcceptNullAsDefaultValueForThreeParameterVariant() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - assertNull(c.getOrDefault("a", String.class, null)); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Test(expected = NullPointerException.class) + @SuppressWarnings({"unchecked", "rawtypes"}) + @Test(expected = NullPointerException.class) public void getOrDefaultDoesNotAcceptNullAsTargetTypeForThreeParameterVariant() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); - c.getOrDefault("a", (Class)null, "b"); + c.getOrDefault("a", (Class) null, "b"); } /** @@ -92,21 +90,22 @@ public class DefaultConfigurationTest { */ @Test(expected = NullPointerException.class) public void getOrDefaultDoesNotAcceptNullAsKeyForThreeParameterVariantSecondIsTypeLiteral() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.getOrDefault(null, TypeLiteral.of(String.class), "ok"); } @Test - public void getOrDefaultDoesAcceptNullAsDefaultValueForThreeParameterVariantSecondIsTypeLiteral() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + public void getOrDefaultDoesAcceptNullAsDefaultValueForThreeParameterVariant() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); - assertNull(c.getOrDefault("a", TypeLiteral.of(String.class), null)); + assertNotNull(c.getOrDefault("a", String.class, null)); + assertNotNull(c.getOrDefault("a", TypeLiteral.of(String.class), null)); } @Test(expected = NullPointerException.class) public void getOrDefaultDoesNotAcceptNullAsTargetTypeForThreeParameterVariantSecondIsTypeLiteral() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.getOrDefault("a", (TypeLiteral<String>) null, "b"); } @@ -116,87 +115,101 @@ public class DefaultConfigurationTest { */ @Test(expected = NullPointerException.class) public void getOrDefaultDoesNotAcceptNullAsKeyForTwoParameterVariantDefaultValueIsSecond() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.getOrDefault(null, "ok"); } @Test public void getOrDefaultDoesAcceptNullAsDefaultValueForTwoParameterVariantDefaultValueIsSecond() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - assertNull(c.getOrDefault("a", null)); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + assertNotNull(c.getOrDefault("a", null)); + } + + @Test + public void getOrDefaultReturnDefaultIfValueWouldHaveBeenNull() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + assertEquals("ok", c.getOrDefault("valueOfNull", "ok")); + assertEquals("ok", c.getOrDefault("valueOfNull", String.class, "ok")); + assertEquals("ok", c.getOrDefault("valueOfNull", TypeLiteral.of(String.class), "ok")); + } + + /** + * Tests for evaluateRawValue(String) + */ + @Test + public void evaluateRawValueReturnsNullOrNotAsAppropriate() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + assertNotNull(c.evaluteRawValue("valueOfValid")); + assertNull(c.evaluteRawValue("valueOfNull")); + assertNotNull(c.evaluteRawValue("Filternull")); //evaluateRawValue does not apply filtering + } + + /** + * Tests for getProperties() + */ + @Test + public void getPropertiesReturnsNullOrNotAsAppropriate() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + Map<String, String> result = c.getProperties(); + assertEquals("valueFromMockedPropertySource", result.get("someKey")); + assertNull(result.get("notInThePropertiesMock")); + assertNull(result.get("valueOfNull")); + assertNull(result.get("Filternull")); + } + + /** + * Tests for convertValue(String key, String value, TypeLiteral<T> type) + */ + @Test + public void testConvertValue() { + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); + assertTrue(100 == (Integer) c.convertValue("aHundred", "100", TypeLiteral.of(Integer.class))); } @Test(expected = NullPointerException.class) public void with_Null() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.with(null); } @Test(expected = NullPointerException.class) public void query_Null() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); c.query(null); } @Test public void with() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); assertEquals(c.with(config -> config), c); } @Test public void query() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); + DefaultConfiguration c = new DefaultConfiguration(new MockedConfigurationContext()); assertEquals(c.query(config -> "testQ"), "testQ"); } - - public static class DummyConfigurationContext implements ConfigurationContext { - @Override - public void addPropertySources(PropertySource... propertySources) { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public List<PropertySource> getPropertySources() { - return Collections.emptyList(); - } - - @Override - public PropertySource getPropertySource(String name) { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public <T> void addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter) { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { - return Collections.emptyMap(); - } - - @Override - public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> type) { - return Collections.emptyList(); - } - - @Override - public List<PropertyFilter> getPropertyFilters() { - return Collections.emptyList(); - } - - @Override - public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy() { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public ConfigurationContextBuilder toBuilder() { - throw new RuntimeException("Method should be never called in this test"); - } - } -} \ No newline at end of file + + @Test + public void testEqualsAndHashAndToStringValues() { + ConfigurationContext sharedContext = new MockedConfigurationContext(); + DefaultConfiguration config1 = new DefaultConfiguration(sharedContext); + DefaultConfiguration config2 = new DefaultConfiguration(sharedContext); + DefaultConfiguration config3 = new DefaultConfiguration(new MockedConfigurationContext()); + + assertEquals(config1, config1); + assertNotEquals(null, config1); + assertNotEquals(sharedContext, config1); + assertNotEquals(config1, sharedContext); + assertNotEquals("aString", config1); + assertEquals(config1, config2); + assertNotEquals(config1, config3); + assertEquals(config1.hashCode(), config2.hashCode()); + assertNotEquals(config1.hashCode(), config3.hashCode()); + assertTrue(config1.toString().contains("Configuration{")); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/EnumConverterTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/EnumConverterTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/EnumConverterTest.java index 8391c3a..989c129 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/EnumConverterTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/EnumConverterTest.java @@ -24,29 +24,61 @@ import org.junit.Test; import java.math.RoundingMode; import java.util.Arrays; +import java.util.Objects; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; /** * Test class testing the {@link EnumConverter} class. */ public class EnumConverterTest { - private final EnumConverter<RoundingMode> testConverter = new EnumConverter<>(RoundingMode.class); + private final EnumConverter<RoundingMode> testConverter = new EnumConverter<>(RoundingMode.class); - private final ConversionContext DUMMY_CONTEXT = new ConversionContext.Builder("someKey", TypeLiteral.of(Enum.class)) - .build(); + private final ConversionContext DUMMY_CONTEXT = new ConversionContext.Builder("someKey", TypeLiteral.of(Enum.class)) + .build(); - @Test - public void testConversionWithMixedCasing() { - for (String input : Arrays.asList(RoundingMode.CEILING.toString(), "ceiling", "CeiLinG")) { - assertEquals(RoundingMode.CEILING, testConverter.convert(input, DUMMY_CONTEXT)); - } - } + private enum TEST_ENUM { + A, B, C, D + }; - @Test - public void testConvert_OtherValue() { - assertNull(testConverter.convert("fooBars", DUMMY_CONTEXT)); - } -} \ No newline at end of file + @Test + public void testConversionWithMixedCasing() { + for (String input : Arrays.asList(RoundingMode.CEILING.toString(), "ceiling", "CeiLinG")) { + assertEquals(RoundingMode.CEILING, testConverter.convert(input, DUMMY_CONTEXT)); + } + } + + @Test + public void testConvert_OtherValue() { + assertNull(testConverter.convert("fooBars", DUMMY_CONTEXT)); + } + + @Test + public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { + ConversionContext context = new ConversionContext.Builder("someKey", TypeLiteral.of(Enum.class)).build(); + EnumConverter<RoundingMode> converter = new EnumConverter<>(RoundingMode.class); + converter.convert("fooBars", context); + + assertTrue(context.getSupportedFormats().contains("<enumValue> (EnumConverter)")); + } + + @Test + public void testEqualsAndHash() { + EnumConverter converter1 = new EnumConverter<>(RoundingMode.class); + EnumConverter converter2 = new EnumConverter<>(RoundingMode.class); + EnumConverter converter3 = new EnumConverter<>(TEST_ENUM.class); + + assertEquals(converter1, converter1); + assertNotEquals(null, converter1); + assertNotEquals(converter1, "aString"); + assertNotEquals("aString", converter1); + assertEquals(converter1, converter2); + assertNotEquals(converter1, converter3); + assertEquals(converter1.hashCode(), converter2.hashCode()); + assertNotEquals(converter1.hashCode(), converter3.hashCode()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/IntegerTestConverter.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/IntegerTestConverter.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/IntegerTestConverter.java new file mode 100644 index 0000000..e6e6a2a --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/IntegerTestConverter.java @@ -0,0 +1,41 @@ +/* + * 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 java.util.Objects; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +/** + * Created by William.Lieurance 2018-02-17 + */ +public class IntegerTestConverter implements PropertyConverter<Integer> { + + @Override + public Integer convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), "<int>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return Integer.decode(trimmed); + } catch (NumberFormatException e) { + return null; + } + + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedConfigurationContext.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedConfigurationContext.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedConfigurationContext.java new file mode 100644 index 0000000..29a9492 --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedConfigurationContext.java @@ -0,0 +1,96 @@ +/* + * 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 java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValueCombinationPolicy; + +/** + * + * @author William.Lieurance 2018.02.18 + */ +public class MockedConfigurationContext implements ConfigurationContext { + + PropertyConverterManager pcm = new PropertyConverterManager(false); + List<PropertySource> pss = new ArrayList<>(); + + public MockedConfigurationContext() { + pcm.register(TypeLiteral.of(Integer.class), new IntegerTestConverter()); + pss.add(new MockedPropertySource()); + } + + @Override + public void addPropertySources(PropertySource... propertySources) { + throw new RuntimeException("addPropertySources should be never called in this test"); + } + + @Override + public List<PropertySource> getPropertySources() { + return pss; + } + + @Override + public PropertySource getPropertySource(String name) { + for (PropertySource ps : pss) { + if (ps.getName().equals(name)) { + return ps; + } + } + return null; + } + + @Override + public <T> void addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter) { + pcm.register(type, propertyConverter); + } + + @Override + public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { + return pcm.getPropertyConverters(); + } + + @Override + public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> type) { + return pcm.getPropertyConverters(type); + } + + @Override + public List<PropertyFilter> getPropertyFilters() { + return Arrays.asList(new MockedPropertyFilter()); + } + + @Override + public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy() { + return PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_POLICY; + } + + @Override + public ConfigurationContextBuilder toBuilder() { + throw new RuntimeException("toBuilder should be never called in this test"); + } +}
