http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/ConfigurationBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/ConfigurationBuilderTest.java b/code/core/src/test/java/org/apache/tamaya/core/ConfigurationBuilderTest.java new file mode 100644 index 0000000..1f702bb --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/ConfigurationBuilderTest.java @@ -0,0 +1,440 @@ +/* + * 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.core; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.core.internal.CoreConfigurationBuilder; +import org.apache.tamaya.spi.*; +import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; +import org.junit.Test; +import sun.security.krb5.Config; + +import java.util.Arrays; +import java.util.Comparator; + +import static org.junit.Assert.*; + +/** + * Tests for {@link ConfigurationBuilder} by atsticks on 06.09.16. + */ +public class ConfigurationBuilderTest { + + private TestPropertySource testPropertySource = new TestPropertySource(){}; + + @Test + public void setContext() throws Exception { + Configuration cfg = ConfigurationProvider.getConfiguration(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .setConfiguration(cfg); + assertEquals(cfg, b.build()); + } + + @Test + public void addPropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Array", 1); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + Configuration cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + // Ensure no sorting happens during add, so switch ordinals! + testPS2 = new TestPropertySource("addPropertySources_Array", 1); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPS2, testPropertySource); + cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + assertEquals(cfg.getContext().getPropertySources().get(1).getName(), "TestPropertySource"); + assertEquals(cfg.getContext().getPropertySources().get(0).getName(), "addPropertySources_Array"); + } + + @Test + public void addPropertySources_Collection() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Collection", 1); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertySources(Arrays.asList(new PropertySource[]{testPropertySource, testPS2})); + Configuration cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + assertEquals(cfg.getContext().getPropertySources().get(0).getName(), "TestPropertySource"); + assertEquals(cfg.getContext().getPropertySources().get(1).getName(), "addPropertySources_Collection"); + // Ensure no sorting happens during add, so switch ordinals! + testPS2 = new TestPropertySource("addPropertySources_Collection", 1); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(Arrays.asList(new PropertySource[]{testPS2, testPropertySource})); + cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + assertEquals(cfg.getContext().getPropertySources().get(1).getName(), "TestPropertySource"); + assertEquals(cfg.getContext().getPropertySources().get(0).getName(), "addPropertySources_Collection"); + } + + @Test + public void removePropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("removePropertySources_Array", 1); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + Configuration cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + cfg = b.build(); + assertFalse(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + assertEquals(1, cfg.getContext().getPropertySources().size()); + } + + @Test + public void removePropertySources_Collection() throws Exception { + PropertySource testPS2 = new TestPropertySource("removePropertySources_Array", 1); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + Configuration cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + cfg = b.build(); + assertEquals(1, cfg.getContext().getPropertySources().size()); + assertFalse(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + } + + @Test + public void addPropertyFilters_Array() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertyFilters(filter1, filter2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationBuilder(); + 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; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationBuilder(); + 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; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyFilters(filter1, filter2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyFilters(filter1, filter2); + b.removePropertyFilters(filter1); + cfg = b.build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + @Test + public void removePropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + b.removePropertyFilters(filter1); + cfg = b.build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void addPropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters().size()); + b = ConfigurationProvider.getConfigurationBuilder() + .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(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), + Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(ctx.getPropertyConverters().size(), 1); + b = ConfigurationProvider.getConfigurationBuilder() + .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_Array() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + 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()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.removePropertyConverters(TypeLiteral.of(String.class), converter); + cfg = b.build(); + ctx = cfg.getContext(); + 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(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + 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()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + b.removePropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + cfg = b.build(); + ctx = cfg.getContext(); + 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; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .setPropertyValueCombinationPolicy(combPol); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertEquals(ctx.getPropertyValueCombinationPolicy(), combPol); + } + + @Test + public void increasePriority(){ + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("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]); + 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)); + } + + @Test + public void decreasePriority(){ + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("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]); + 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)); + } + + @Test + public void lowestPriority(){ + // setup + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("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]); + assertEquals(propertySources[5], b.getPropertySources().get(0)); + } + + @Test + public void highestPriority(){ + // setup + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("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]); + assertEquals(propertySources[5], b.getPropertySources().get(propertySources.length-1)); + } + + @Test + public void sortPropertySources(){ + // setup + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("ps"+i,i); + } + b.addPropertySources(propertySources); + Comparator<PropertySource> psComp = (o1, o2) -> o1.toString().compareTo(o2.toString()); + // test + b.sortPropertySources(psComp); + Arrays.sort(propertySources, psComp); + for(int i=0;i<propertySources.length;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + } + + @Test + public void sortPropertyFilter(){ + // setup + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + 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 + b.sortPropertyFilter(pfComp); + 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 { + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertNotNull(ctx); + assertTrue(ctx.getPropertySources().isEmpty()); + assertTrue(ctx.getPropertyFilters().isEmpty()); + } + + @Test + public void testRemoveAllFilters() throws Exception { + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + 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 { + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertySources(new TestPropertySource()); + assertFalse(b.getPropertySources().isEmpty()); + b.removePropertySources(b.getPropertySources()); + assertTrue(b.getPropertyFilters().isEmpty()); + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java b/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java index 5fb65ff..ac353eb 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java @@ -21,7 +21,7 @@ package org.apache.tamaya.core; import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.*; -import org.apache.tamaya.core.internal.CoreConfigurationContextBuilder; +import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; import org.junit.Test; import java.util.Arrays; @@ -30,7 +30,7 @@ import java.util.Comparator; import static org.junit.Assert.*; /** - * Tests for {@link CoreConfigurationContextBuilder} by atsticks on 06.09.16. + * Tests for {@link ConfigurationContextBuilder} by atsticks on 06.09.16. */ public class ConfigurationContextBuilderTest { @@ -47,7 +47,7 @@ public class ConfigurationContextBuilderTest { @Test public void addPropertySources_Array() throws Exception { PropertySource testPS2 = new TestPropertySource("addPropertySources_Array", 1); - ConfigurationContextBuilder b = new CoreConfigurationContextBuilder() + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertySources(testPropertySource, testPS2); ConfigurationContext ctx = b.build(); assertEquals(2, ctx.getPropertySources().size()); @@ -68,7 +68,7 @@ public class ConfigurationContextBuilderTest { @Test public void addPropertySources_Collection() throws Exception { PropertySource testPS2 = new TestPropertySource("addPropertySources_Collection", 1); - ConfigurationContextBuilder b = new CoreConfigurationContextBuilder() + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() .addPropertySources(Arrays.asList(new PropertySource[]{testPropertySource, testPS2})); ConfigurationContext ctx = b.build(); assertEquals(2, ctx.getPropertySources().size()); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationBuilderTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationBuilderTest.java new file mode 100644 index 0000000..37dc2cb --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationBuilderTest.java @@ -0,0 +1,219 @@ +/* + * 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.core.internal; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.*; +import org.junit.Test; + +import java.util.Collections; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for {@link CoreConfigurationBuilder} by atsticks on 06.09.16. + */ +public class CoreConfigurationBuilderTest { + + private TestPropertySource testPropertySource = new TestPropertySource(){}; + + @Test + public void setContext() throws Exception { + ConfigurationContext context = ConfigurationProvider.getConfiguration().getContext(); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .setContext(context); + assertEquals(context, b.build().getContext()); + } + + @Test + public void setConfiguration() throws Exception { + Configuration cfg = ConfigurationProvider.getConfiguration(); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .setConfiguration(cfg); + assertEquals(cfg, b.build()); + } + + @Test + public void addPropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertySources(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)); + } + + @Test + public void removePropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertySources(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 CoreConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + cfg = b.build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertySources().size()); + assertFalse(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + } + + @Test + public void addPropertyFilters_Array() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + CoreConfigurationBuilder b = new CoreConfigurationBuilder(); + b.addPropertyFilters(filter1, filter2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = new CoreConfigurationBuilder(); + 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; + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertyFilters(filter1, filter2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = new CoreConfigurationBuilder() + .addPropertyFilters(filter1, filter2); + b.removePropertyFilters(filter1); + cfg = b.build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void addPropertyConverter() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters().size()); + b = new CoreConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(1, ctx.getPropertyConverters().size()); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void removePropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + 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()); + b = new CoreConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.removePropertyConverters(TypeLiteral.of(String.class), converter); + cfg = b.build(); + ctx = cfg.getContext(); + 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; + ConfigurationBuilder b = new CoreConfigurationBuilder() + .setPropertyValueCombinationPolicy(combPol); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertEquals(ctx.getPropertyValueCombinationPolicy(), combPol); + } + + @Test + public void build() throws Exception { + assertNotNull(new CoreConfigurationBuilder().build()); + } + + @Test + public void bla() throws Exception { + ConfigurationBuilder builder = ConfigurationProvider.getConfigurationBuilder(); + builder.addDefaultPropertyConverters(); + } + + private static class TestPropertySource implements PropertySource{ + + private String id; + + public TestPropertySource(){ + this(null); + } + + public TestPropertySource(String id){ + this.id = id; + } + + @Override + public int getOrdinal() { + return 200; + } + + @Override + public String getName() { + return id!=null?id:"TestPropertySource"; + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.of(key, key + "Value", getName()); + } + + @Override + public Map<String, PropertyValue> getProperties() { + return Collections.emptyMap(); + } + + @Override + public boolean isScannable() { + return false; + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationContextBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationContextBuilderTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationContextBuilderTest.java deleted file mode 100644 index 8458366..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationContextBuilderTest.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal; - -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.*; -import org.junit.Test; - -import java.util.Collections; -import java.util.Map; - -import static org.junit.Assert.*; - -/** - * Tests for {@link CoreConfigurationContextBuilder} by atsticks on 06.09.16. - */ -public class CoreConfigurationContextBuilderTest { - - private TestPropertySource testPropertySource = new TestPropertySource(){}; - - @Test - public void setContext() throws Exception { - ConfigurationContext context = ConfigurationProvider.getConfiguration().getContext(); - ConfigurationContextBuilder b = new CoreConfigurationContextBuilder() - .setContext(context); - assertEquals(context, b.build()); - } - - @Test - public void addPropertySources_Array() throws Exception { - PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); - ConfigurationContextBuilder b = new CoreConfigurationContextBuilder() - .addPropertySources(testPropertySource, testPS2); - ConfigurationContext ctx = b.build(); - assertEquals(2, ctx.getPropertySources().size()); - assertTrue(ctx.getPropertySources().contains(testPropertySource)); - assertTrue(ctx.getPropertySources().contains(testPS2)); - } - - @Test - public void removePropertySources_Array() throws Exception { - PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); - ConfigurationContextBuilder b = new CoreConfigurationContextBuilder() - .addPropertySources(testPropertySource, testPS2); - ConfigurationContext ctx = b.build(); - assertEquals(2, ctx.getPropertySources().size()); - assertTrue(ctx.getPropertySources().contains(testPropertySource)); - assertTrue(ctx.getPropertySources().contains(testPS2)); - b = new CoreConfigurationContextBuilder() - .addPropertySources(testPropertySource, testPS2); - b.removePropertySources(testPropertySource); - ctx = b.build(); - assertEquals(1, ctx.getPropertySources().size()); - assertFalse(ctx.getPropertySources().contains(testPropertySource)); - assertTrue(ctx.getPropertySources().contains(testPS2)); - } - - @Test - public void addPropertyFilters_Array() throws Exception { - PropertyFilter filter1 = (value, context) -> value; - PropertyFilter filter2 = (value, context) -> value; - CoreConfigurationContextBuilder b = new CoreConfigurationContextBuilder(); - b.addPropertyFilters(filter1, filter2); - ConfigurationContext ctx = b.build(); - assertTrue(ctx.getPropertyFilters().contains(filter1)); - assertTrue(ctx.getPropertyFilters().contains(filter2)); - assertEquals(2, ctx.getPropertyFilters().size()); - b = new CoreConfigurationContextBuilder(); - 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 CoreConfigurationContextBuilder() - .addPropertyFilters(filter1, filter2); - ConfigurationContext ctx = b.build(); - assertTrue(ctx.getPropertyFilters().contains(filter1)); - assertTrue(ctx.getPropertyFilters().contains(filter2)); - assertEquals(2, ctx.getPropertyFilters().size()); - b = new CoreConfigurationContextBuilder() - .addPropertyFilters(filter1, filter2); - b.removePropertyFilters(filter1); - ctx = b.build(); - assertEquals(1, ctx.getPropertyFilters().size()); - assertFalse(ctx.getPropertyFilters().contains(filter1)); - assertTrue(ctx.getPropertyFilters().contains(filter2)); - } - - @Test - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void addPropertyConverter() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); - ConfigurationContextBuilder b = new CoreConfigurationContextBuilder() - .addPropertyConverters(TypeLiteral.of(String.class), converter); - ConfigurationContext ctx = b.build(); - assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); - assertEquals(1, ctx.getPropertyConverters().size()); - b = new CoreConfigurationContextBuilder() - .addPropertyConverters(TypeLiteral.of(String.class), converter); - b.addPropertyConverters(TypeLiteral.of(String.class), converter); - assertEquals(1, ctx.getPropertyConverters().size()); - } - - @Test - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void removePropertyConverters_Array() throws Exception { - PropertyConverter converter = (value, context) -> value.toLowerCase(); - ConfigurationContextBuilder b = new CoreConfigurationContextBuilder() - .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 CoreConfigurationContextBuilder() - .addPropertyConverters(TypeLiteral.of(String.class), converter); - b.removePropertyConverters(TypeLiteral.of(String.class), converter); - ctx = b.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 CoreConfigurationContextBuilder() - .setPropertyValueCombinationPolicy(combPol); - ConfigurationContext ctx = b.build(); - assertEquals(ctx.getPropertyValueCombinationPolicy(), combPol); - } - - @Test - public void build() throws Exception { - assertNotNull(new CoreConfigurationContextBuilder().build()); - } - - @Test - public void bla() throws Exception { - ConfigurationContextBuilder builder = ConfigurationProvider.getConfigurationContextBuilder(); - builder.addDefaultPropertyConverters(); - } - - private static class TestPropertySource implements PropertySource{ - - private String id; - - public TestPropertySource(){ - this(null); - } - - public TestPropertySource(String id){ - this.id = id; - } - - @Override - public int getOrdinal() { - return 200; - } - - @Override - public String getName() { - return id!=null?id:"TestPropertySource"; - } - - @Override - public PropertyValue get(String key) { - return PropertyValue.of(key, key + "Value", getName()); - } - - @Override - public Map<String, PropertyValue> getProperties() { - return Collections.emptyMap(); - } - - @Override - public boolean isScannable() { - return false; - } - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationContextTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationContextTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationContextTest.java deleted file mode 100644 index 1c10124..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationContextTest.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal; - -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.core.testdata.TestPropertyDefaultSource; -import org.apache.tamaya.spi.*; -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Simple tests for {@link CoreConfigurationContext} by atsticks on 16.08.16. - */ -public class CoreConfigurationContextTest { - - @Test - public void addPropertySources() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - TestPropertyDefaultSource def = new TestPropertyDefaultSource(); - assertFalse(ctx.getPropertySources().contains(def)); - ctx.addPropertySources(def); - assertTrue(ctx.getPropertySources().contains(def)); - } - - @Test - public void testToString() throws Exception { - String toString = ConfigurationProvider.getConfiguration().getContext().toString(); - } - - @Test - public void getPropertySources() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - assertNotNull(ctx.getPropertySources()); - assertEquals(ctx.getPropertySources().size(), 0); - ctx = new CoreConfigurationContextBuilder().addDefaultPropertySources().build(); - assertNotNull(ctx.getPropertySources()); - assertEquals(7, ctx.getPropertySources().size()); - } - - @Test - public void getPropertySource() throws Exception { - TestPropertyDefaultSource ps = new TestPropertyDefaultSource(); - ConfigurationContext ctx = new CoreConfigurationContextBuilder() - .addPropertySources(ps).build(); - assertNotNull(ctx.getPropertySources()); - assertEquals(ctx.getPropertySources().size(), 1); - assertNotNull(((CoreConfigurationContext)ctx).getPropertySource(ps.getName())); - assertEquals(ps.getName(), ((CoreConfigurationContext)ctx).getPropertySource(ps.getName()).getName()); - assertNull(((CoreConfigurationContext)ctx).getPropertySource("huhu")); - - } - - @Test - public void testHashCode() throws Exception { - TestPropertyDefaultSource ps = new TestPropertyDefaultSource(); - ConfigurationContext ctx1 = new CoreConfigurationContextBuilder() - .addPropertySources(ps).build(); - ConfigurationContext ctx2 = new CoreConfigurationContextBuilder() - .addPropertySources(ps).build(); - assertEquals(ctx1.hashCode(), ctx2.hashCode()); - ctx2 = new CoreConfigurationContextBuilder() - .build(); - assertNotEquals(ctx1.hashCode(), ctx2.hashCode()); - - } - - @Test - public void addPropertyConverter() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - PropertyConverter testConverter = new PropertyConverter() { - @Override - public Object convert(String value, ConversionContext context) { - return ""; - } - }; - assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(testConverter)); - ctx.addPropertyConverter(TypeLiteral.of(String.class), testConverter); - assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(testConverter)); - } - - @Test - public void getPropertyConverters() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - PropertyConverter testConverter = new PropertyConverter() { - @Override - public Object convert(String value, ConversionContext context) { - return ""; - } - }; - ctx.addPropertyConverter(TypeLiteral.of(String.class), testConverter); - assertNotNull(ctx.getPropertyConverters()); - assertTrue(ctx.getPropertyConverters().containsKey(TypeLiteral.of(String.class))); - assertTrue(ctx.getPropertyConverters().get(TypeLiteral.of(String.class)).contains(testConverter)); - testConverter = new PropertyConverter() { - @Override - public Object convert(String value, ConversionContext context) { - return Integer.valueOf(5); - } - }; - ctx.addPropertyConverter(TypeLiteral.of(Integer.class), testConverter); - assertTrue(ctx.getPropertyConverters().containsKey(TypeLiteral.of(Integer.class))); - assertTrue(ctx.getPropertyConverters().get(TypeLiteral.of(Integer.class)).contains(testConverter)); - } - - @Test - public void getPropertyConverters1() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - PropertyConverter testConverter = new PropertyConverter() { - @Override - public Object convert(String value, ConversionContext context) { - return ""; - } - }; - assertNotNull(ctx.getPropertyConverters(TypeLiteral.of(String.class))); - assertEquals(ctx.getPropertyConverters(TypeLiteral.of(String.class)).size(),0); - ctx.addPropertyConverter(TypeLiteral.of(String.class), testConverter); - assertNotNull(ctx.getPropertyConverters(TypeLiteral.of(String.class))); - assertEquals(ctx.getPropertyConverters(TypeLiteral.of(String.class)).size(),1); - assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(testConverter)); - - } - - @Test - public void getPropertyFilters() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - PropertyFilter testFilter = new PropertyFilter() { - - @Override - public PropertyValue filterProperty(PropertyValue value, FilterContext context) { - return value; - } - }; - assertNotNull(ctx.getPropertyFilters()); - assertFalse(ctx.getPropertyFilters().contains(testFilter)); - ctx = ctx.toBuilder().addPropertyFilters(testFilter).build(); - assertTrue(ctx.getPropertyFilters().contains(testFilter)); - } - - @Test - public void getPropertyValueCombinationPolicy() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - assertNotNull(ctx.getPropertyValueCombinationPolicy()); - assertEquals(ctx.getPropertyValueCombinationPolicy(), - PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_POLICY); - } - - @Test - public void toBuilder() throws Exception { - assertNotNull(new CoreConfigurationContextBuilder().build().toBuilder()); - } - - @Test - public void testRoundTrip() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - assertEquals(ctx.toBuilder().build(), ctx); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationProviderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationProviderTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationProviderTest.java new file mode 100644 index 0000000..e4dab56 --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationProviderTest.java @@ -0,0 +1,93 @@ +/* + * 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.core.internal; + +import org.apache.tamaya.Configuration; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by atsticks on 11.09.16. + */ +public class CoreConfigurationProviderTest { + + @Test + public void testInstantiation() throws Exception { + new CoreConfigurationProvider(); + } + + @Test + public void getConfiguration() throws Exception { + assertNotNull(new CoreConfigurationProvider().getConfiguration()); + } + + @Test + public void createConfiguration() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + assertNotNull(new CoreConfigurationProvider().createConfiguration(cfg.getContext())); + assertEquals(cfg, + new CoreConfigurationProvider().createConfiguration(cfg.getContext())); + } + + @Test + public void getConfigurationContext() throws Exception { + assertNotNull(new CoreConfigurationProvider().getConfigurationContext()); + assertEquals(new CoreConfigurationProvider().getConfigurationContext(), + new CoreConfigurationProvider().getConfiguration().getContext()); + } + + @Test + public void getConfigurationContextBuilder() throws Exception { + assertNotNull(new CoreConfigurationProvider().getConfigurationContextBuilder()); + } + + @Test + public void getConfigurationBuilder() throws Exception { + assertNotNull(new CoreConfigurationProvider().getConfigurationBuilder()); + } + + @SuppressWarnings("deprecation") + @Test + public void setConfigurationContext() throws Exception { + new CoreConfigurationProvider() + .setConfigurationContext(new CoreConfigurationProvider().getConfiguration().getContext()); + } + + @SuppressWarnings("deprecation") + @Test + public void setConfiguration() throws Exception { + new CoreConfigurationProvider() + .setConfiguration(new CoreConfigurationProvider().getConfiguration()); + } + + @SuppressWarnings("deprecation") + @Test + public void isConfigurationContextSettable() throws Exception { + assertTrue(new CoreConfigurationProvider().isConfigurationContextSettable()); + } + + @SuppressWarnings("deprecation") + @Test + public void isConfigurationSettable() throws Exception { + assertTrue(new CoreConfigurationProvider().isConfigurationSettable()); + } + + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationTest.java new file mode 100644 index 0000000..6b8d1dc --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationTest.java @@ -0,0 +1,177 @@ +/* + * 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.core.internal; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.core.testdata.TestPropertyDefaultSource; +import org.apache.tamaya.spi.*; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Simple tests for {@link CoreConfiguration} by atsticks on 16.08.16. + */ +public class CoreConfigurationTest { + + @Test + public void addPropertySources() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + TestPropertyDefaultSource def = new TestPropertyDefaultSource(); + assertFalse(cfg.getContext().getPropertySources().contains(def)); + cfg.getContext().addPropertySources(def); + assertTrue(cfg.getContext().getPropertySources().contains(def)); + } + + @Test + public void testToString() throws Exception { + String toString = ConfigurationProvider.getConfiguration().getContext().toString(); + } + + @Test + public void getPropertySources() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + assertNotNull(cfg.getContext().getPropertySources()); + assertEquals(cfg.getContext().getPropertySources().size(), 0); + cfg = new CoreConfigurationBuilder().addDefaultPropertySources().build(); + assertNotNull(cfg.getContext().getPropertySources()); + assertEquals(7, cfg.getContext().getPropertySources().size()); + } + + @Test + public void getPropertySource() throws Exception { + TestPropertyDefaultSource ps = new TestPropertyDefaultSource(); + Configuration cfg = new CoreConfigurationBuilder() + .addPropertySources(ps).build(); + assertNotNull(cfg.getContext().getPropertySources()); + assertEquals(cfg.getContext().getPropertySources().size(), 1); + assertNotNull((cfg.getContext()).getPropertySource(ps.getName())); + assertEquals(ps.getName(), cfg.getContext().getPropertySource(ps.getName()).getName()); + assertNull(cfg.getContext().getPropertySource("huhu")); + + } + + @Test + public void testHashCode() throws Exception { + TestPropertyDefaultSource ps = new TestPropertyDefaultSource(); + Configuration cfg1 = new CoreConfigurationBuilder() + .addPropertySources(ps).build(); + Configuration cfg2 = new CoreConfigurationBuilder() + .addPropertySources(ps).build(); + assertEquals(cfg1.hashCode(), cfg2.hashCode()); + cfg2 = new CoreConfigurationBuilder() + .build(); + assertNotEquals(cfg1.hashCode(), cfg2.hashCode()); + + } + + @Test + public void addPropertyConverter() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + PropertyConverter testConverter = new PropertyConverter() { + @Override + public Object convert(String value, ConversionContext context) { + return ""; + } + }; + assertFalse(cfg.getContext().getPropertyConverters(TypeLiteral.of(String.class)).contains(testConverter)); + cfg.getContext().addPropertyConverter(TypeLiteral.of(String.class), testConverter); + assertTrue(cfg.getContext().getPropertyConverters(TypeLiteral.of(String.class)).contains(testConverter)); + } + + @Test + public void getPropertyConverters() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + PropertyConverter testConverter = new PropertyConverter() { + @Override + public Object convert(String value, ConversionContext context) { + return ""; + } + }; + cfg.getContext().addPropertyConverter(TypeLiteral.of(String.class), testConverter); + assertNotNull(cfg.getContext().getPropertyConverters()); + assertTrue(cfg.getContext().getPropertyConverters().containsKey(TypeLiteral.of(String.class))); + assertTrue(cfg.getContext().getPropertyConverters().get(TypeLiteral.of(String.class)).contains(testConverter)); + testConverter = new PropertyConverter() { + @Override + public Object convert(String value, ConversionContext context) { + return Integer.valueOf(5); + } + }; + cfg.getContext().addPropertyConverter(TypeLiteral.of(Integer.class), testConverter); + assertTrue(cfg.getContext().getPropertyConverters().containsKey(TypeLiteral.of(Integer.class))); + assertTrue(cfg.getContext().getPropertyConverters().get(TypeLiteral.of(Integer.class)).contains(testConverter)); + } + + @Test + public void getPropertyConverters1() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + PropertyConverter testConverter = new PropertyConverter() { + @Override + public Object convert(String value, ConversionContext context) { + return ""; + } + }; + assertNotNull(cfg.getContext().getPropertyConverters(TypeLiteral.of(String.class))); + assertEquals(cfg.getContext().getPropertyConverters(TypeLiteral.of(String.class)).size(),0); + cfg.getContext().addPropertyConverter(TypeLiteral.of(String.class), testConverter); + assertNotNull(cfg.getContext().getPropertyConverters(TypeLiteral.of(String.class))); + assertEquals(cfg.getContext().getPropertyConverters(TypeLiteral.of(String.class)).size(),1); + assertTrue(cfg.getContext().getPropertyConverters(TypeLiteral.of(String.class)).contains(testConverter)); + + } + + @Test + public void getPropertyFilters() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + PropertyFilter testFilter = new PropertyFilter() { + + @Override + public PropertyValue filterProperty(PropertyValue value, FilterContext context) { + return value; + } + }; + assertNotNull(cfg.getContext().getPropertyFilters()); + assertFalse(cfg.getContext().getPropertyFilters().contains(testFilter)); + cfg = cfg.toBuilder().addPropertyFilters(testFilter).build(); + assertTrue(cfg.getContext().getPropertyFilters().contains(testFilter)); + } + + @Test + public void getPropertyValueCombinationPolicy() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + assertNotNull(cfg.getContext().getPropertyValueCombinationPolicy()); + assertEquals(cfg.getContext().getPropertyValueCombinationPolicy(), + PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_POLICY); + } + + @Test + public void toBuilder() throws Exception { + assertNotNull(new CoreConfigurationBuilder().build().toBuilder()); + } + + @Test + public void testRoundTrip() throws Exception { + Configuration cfg = new CoreConfigurationBuilder().build(); + assertEquals(cfg.toBuilder().build(), cfg); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationProviderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationProviderTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationProviderTest.java deleted file mode 100644 index 9ba3c80..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationProviderTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal; - -import org.apache.tamaya.spi.ConfigurationContext; -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Created by atsticks on 11.09.16. - */ -public class DefaultConfigurationProviderTest { - - @Test - public void testInstantiation() throws Exception { - new DefaultConfigurationProvider(); - } - - @Test - public void getConfiguration() throws Exception { - assertNotNull(new DefaultConfigurationProvider().getConfiguration()); - } - - @Test - public void createConfiguration() throws Exception { - ConfigurationContext ctx = new CoreConfigurationContextBuilder().build(); - assertNotNull(new DefaultConfigurationProvider().createConfiguration(ctx)); - assertEquals(ctx, - new DefaultConfigurationProvider().createConfiguration(ctx).getContext()); - } - - @Test - public void getConfigurationContext() throws Exception { - assertNotNull(new DefaultConfigurationProvider().getConfiguration().getContext()); - } - - @Test - public void getConfigurationContextBuilder() throws Exception { - assertNotNull(new DefaultConfigurationProvider().getConfigurationContextBuilder()); - } - - @SuppressWarnings("deprecation") - @Test - public void setConfigurationContext() throws Exception { - new DefaultConfigurationProvider() - .setConfigurationContext(new DefaultConfigurationProvider().getConfiguration().getContext()); - } - - @SuppressWarnings("deprecation") - @Test - public void isConfigurationContextSettable() throws Exception { - assertTrue(new DefaultConfigurationProvider().isConfigurationContextSettable()); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultServiceContextTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultServiceContextTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultServiceContextTest.java deleted file mode 100644 index 13b2eb6..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultServiceContextTest.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.ConfigurationProviderSpi; -import org.junit.Assert; -import org.junit.Test; - -import javax.annotation.Priority; -import java.util.Collection; -import java.util.List; - -public class DefaultServiceContextTest { - - /** - * context to test - */ - private final DefaultServiceContext context = new DefaultServiceContext(); - - - @Test - public void testGetService() { - ConfigurationProviderSpi providerSpi = context.getService(ConfigurationProviderSpi.class); - Assert.assertNotNull(providerSpi); - Assert.assertTrue(providerSpi instanceof DefaultConfigurationProvider); - } - - @Test(expected = ConfigException.class) - public void testGetService_multipleServicesWithoutPriority_shouldThrowConfigException() { - context.getService(InvalidPriorityInterface.class); - } - - @Test - public void testGetService_multipleService_shouldReturnServiceWithHighestPriority() { - MultiImplsInterface service = context.getService(MultiImplsInterface.class); - - Assert.assertNotNull(service); - Assert.assertTrue(service instanceof MultiImpl2); - } - - @Test - public void testGetService_noImpl_shouldReturnEmptyOpional() { - NoImplInterface service = context.getService(NoImplInterface.class); - Assert.assertNull(service); - } - - - @Test - public void testGetServices_shouldReturnServices() { - { - Collection<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class); - Assert.assertNotNull(services); - Assert.assertEquals(2, services.size()); - - for (InvalidPriorityInterface service : services) { - Assert.assertTrue(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2); - } - } - - { - List<MultiImplsInterface> services = context.getServices(MultiImplsInterface.class); - Assert.assertNotNull(services); - Assert.assertEquals(3, services.size()); - - Assert.assertTrue(services.get(0) instanceof MultiImpl2); - Assert.assertTrue(services.get(1) instanceof MultiImpl1); - Assert.assertTrue(services.get(2) instanceof MultiImpl3); - } - } - - @Test - public void testGetServices_redundantAccessToServices() { - for(int i=0;i<10;i++){ - Collection<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class); - Assert.assertNotNull(services); - Assert.assertEquals(2, services.size()); - for (InvalidPriorityInterface service : services) { - Assert.assertTrue(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2); - } - } - } - - @Test - public void testGetServices_noImpl_shouldReturnEmptyList() { - Collection<NoImplInterface> services = context.getServices(NoImplInterface.class); - Assert.assertNotNull(services); - Assert.assertTrue(services.isEmpty()); - } - - - // some test interfaces and classes - - public interface InvalidPriorityInterface { - } - - @Priority(value = 50) - public static class InvalidPriorityImpl1 implements InvalidPriorityInterface { - } - - @Priority(value = 50) - public static class InvalidPriorityImpl2 implements InvalidPriorityInterface { - } - - - public interface MultiImplsInterface { - } - - public static class MultiImpl1 implements MultiImplsInterface { - } - - @Priority(value = 500) - public static class MultiImpl2 implements MultiImplsInterface { - } - - @Priority(value = -10) - public static class MultiImpl3 implements MultiImplsInterface { - } - - private interface NoImplInterface { - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/resources/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$InvalidPriorityInterface ---------------------------------------------------------------------- diff --git a/code/core/src/test/resources/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$InvalidPriorityInterface b/code/core/src/test/resources/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$InvalidPriorityInterface deleted file mode 100644 index f203fa6..0000000 --- a/code/core/src/test/resources/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$InvalidPriorityInterface +++ /dev/null @@ -1,18 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -org.apache.tamaya.core.internal.DefaultServiceContextTest$InvalidPriorityImpl1 -org.apache.tamaya.core.internal.DefaultServiceContextTest$InvalidPriorityImpl2 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/resources/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$MultiImplsInterface ---------------------------------------------------------------------- diff --git a/code/core/src/test/resources/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$MultiImplsInterface b/code/core/src/test/resources/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$MultiImplsInterface deleted file mode 100644 index b144790..0000000 --- a/code/core/src/test/resources/META-INF/services/org.apache.tamaya.core.internal.DefaultServiceContextTest$MultiImplsInterface +++ /dev/null @@ -1,20 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -org.apache.tamaya.core.internal.DefaultServiceContextTest$MultiImpl1 -org.apache.tamaya.core.internal.DefaultServiceContextTest$MultiImpl2 -org.apache.tamaya.core.internal.DefaultServiceContextTest$MultiImpl3 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi new file mode 100644 index 0000000..968be8f --- /dev/null +++ b/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi @@ -0,0 +1,18 @@ +# 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. + +org.apache.tamaya.core.internal.CoreConfigurationProvider \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java index 4376a9a..fbcf35f 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java @@ -124,7 +124,6 @@ public class DefaultConfiguration implements Configuration { @Override public String getOrDefault(String key, String defaultValue) { Objects.requireNonNull(key, "Key must not be null."); - Objects.requireNonNull(defaultValue, "Default value must not be null"); String val = get(key); if(val==null){ @@ -258,6 +257,24 @@ public class DefaultConfiguration implements Configuration { } @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DefaultConfiguration that = (DefaultConfiguration) o; + + if (!configurationContext.equals(that.configurationContext)) return false; + return configEvaluator.getClass().equals(that.configEvaluator.getClass()); + } + + @Override + public int hashCode() { + int result = configurationContext.hashCode(); + result = 31 * result + configEvaluator.getClass().hashCode(); + return result; + } + + @Override public String toString() { return "Configuration{\n " + configurationContext + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java index f1c0f46..ec26ba6 100644 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java +++ b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java @@ -21,21 +21,29 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.*; +import org.apache.tamaya.spi.ConfigurationBuilder; import java.util.*; /** - * Default implementation of {@link ConfigurationContextBuilder}. + * Default implementation of {@link ConfigurationBuilder}. */ public class DefaultConfigurationBuilder implements ConfigurationBuilder { - private final ConfigurationContextBuilder contextBuilder; + protected final DefaultConfigurationContextBuilder contextBuilder; /** * Creates a new builder instance. */ - public DefaultConfigurationBuilder(ConfigurationContextBuilder contextBuilder) { - this.contextBuilder = Objects.requireNonNull(contextBuilder); + public DefaultConfigurationBuilder() { + this.contextBuilder = new DefaultConfigurationContextBuilder(); + } + + /** + * Creates a new builder instance. + */ + public DefaultConfigurationBuilder(ConfigurationContext context) { + this.contextBuilder = new DefaultConfigurationContextBuilder(context); } /** @@ -43,13 +51,13 @@ public class DefaultConfigurationBuilder implements ConfigurationBuilder { * @param configuration the configuration to be used, not null. */ public DefaultConfigurationBuilder(Configuration configuration) { - this.contextBuilder = configuration.getContext().toBuilder(); + this.contextBuilder = new DefaultConfigurationContextBuilder(configuration.getContext()); } /** * Allows to set configuration context during unit tests. */ - ConfigurationBuilder setConfiguration(Configuration configuration) { + public ConfigurationBuilder setConfiguration(Configuration configuration) { this.contextBuilder.setContext(configuration.getContext()); return this; } @@ -181,7 +189,6 @@ public class DefaultConfigurationBuilder implements ConfigurationBuilder { return this; } - @Override public <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> type, PropertyConverter<T>... propertyConverters){ this.contextBuilder.addPropertyConverters(type, propertyConverters);