TAMAYA-182: Streamlined/unified builder API and impl: bugfixes.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/77dbcc4d Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/77dbcc4d Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/77dbcc4d Branch: refs/heads/master Commit: 77dbcc4d3a9fa38f0e9bbc4901ea5cae7efcf24c Parents: d1ae885 Author: anatole <[email protected]> Authored: Sat Oct 29 14:38:24 2016 +0200 Committer: anatole <[email protected]> Committed: Sat Oct 29 14:38:24 2016 +0200 ---------------------------------------------------------------------- .../tamaya/TestConfigurationProvider.java | 7 +- .../tamaya/spi/ConversionContextTest.java | 194 +++++++ .../apache/tamaya/spi/FilterContextTest.java | 72 +++ .../tamaya/spi/ServiceContextManagerTest.java | 67 +++ .../core/internal/DefaultConfiguration.java | 8 +- .../internal/DefaultConfigurationContext.java | 43 +- .../internal/DefaultConfigurationProvider.java | 1 + .../core/ConfigurationContextBuilderTest.java | 513 +++++++++++++++++++ .../apache/tamaya/core/TestPropertySource.java | 73 +++ .../DefaultConfigurationContextBuilderTest.java | 232 +++++++++ .../DefaultConfigurationContextTest.java | 177 +++++++ .../DefaultConfigurationProviderTest.java | 61 +++ .../internal/PriorityServiceComparatorTest.java | 47 ++ 13 files changed, 1485 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java b/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java index 4b6ca31..e326ee8 100644 --- a/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java +++ b/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java @@ -23,11 +23,12 @@ import org.apache.tamaya.spi.ConfigurationContextBuilder; import org.apache.tamaya.spi.ConfigurationProviderSpi; import javax.annotation.Priority; +import java.util.Objects; /** * Test Configuration class, that is used to testdata the default methods provided by the API. */ -@Priority(2) +@Priority(-1) public class TestConfigurationProvider implements ConfigurationProviderSpi { private static final Configuration config = new TestConfiguration(); @@ -39,12 +40,12 @@ public class TestConfigurationProvider implements ConfigurationProviderSpi { @Override public Configuration createConfiguration(ConfigurationContext context) { - return null; + return config; } @Override public ConfigurationContext getConfigurationContext() { - return null; + return config.getContext(); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/code/api/src/test/java/org/apache/tamaya/spi/ConversionContextTest.java ---------------------------------------------------------------------- diff --git a/code/api/src/test/java/org/apache/tamaya/spi/ConversionContextTest.java b/code/api/src/test/java/org/apache/tamaya/spi/ConversionContextTest.java new file mode 100644 index 0000000..30f5aba --- /dev/null +++ b/code/api/src/test/java/org/apache/tamaya/spi/ConversionContextTest.java @@ -0,0 +1,194 @@ +/* + * 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.spi; + +import org.apache.tamaya.ConfigOperator; +import org.apache.tamaya.ConfigQuery; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; +import org.junit.Test; + +import java.net.InetAddress; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for {@link ConversionContext}, created by atsticks on 20.08.16. + */ +public class ConversionContextTest { + @Test + public void getKey() throws Exception { + ConversionContext ctx = new ConversionContext.Builder("getKey", TypeLiteral.of(String.class)).build(); + assertEquals("getKey", ctx.getKey()); + } + + @Test + public void getTargetType() throws Exception { + ConversionContext ctx = new ConversionContext.Builder("getTargetType", TypeLiteral.of(String.class)).build(); + assertEquals(TypeLiteral.of(String.class), ctx.getTargetType()); + } + + @Test + public void getAnnotatedElement() throws Exception { + ConversionContext ctx = new ConversionContext.Builder("getAnnotatedElement", TypeLiteral.of(List.class)).build(); + assertNull(ctx.getAnnotatedElement()); + } + + @Test + public void testConfiguration() throws Exception { + Configuration config = new MyConfiguration(); + ConversionContext ctx = new ConversionContext.Builder("testConfiguration", TypeLiteral.of(List.class)) + .setConfiguration(config).build(); + assertEquals(config, ctx.getConfiguration()); + } + + @Test + public void testSupportedFormats() throws Exception { + ConversionContext ctx = new ConversionContext.Builder("getAnnotatedElement", TypeLiteral.of(List.class)) + .addSupportedFormats(MyConverter.class, "0.0.0.0/nnn").build(); + assertTrue(ctx.getSupportedFormats().contains("0.0.0.0/nnn (MyConverter)")); + } + + @Test + public void testToString() throws Exception { + ConversionContext ctx = new ConversionContext.Builder("getAnnotatedElement", TypeLiteral.of(List.class)) + .addSupportedFormats(MyConverter.class, "0.0.0.0/nnn").build(); + assertEquals("ConversionContext{configuration=null, key='getAnnotatedElement', targetType=TypeLiteral{type=interface java.util.List}, annotatedElement=null, supportedFormats=[0.0.0.0/nnn (MyConverter)]}", ctx.toString()); + } + + @Test + public void getConfigurationContext() throws Exception { + ConfigurationContext context = new MyConfigurationContext(); + ConversionContext ctx = new ConversionContext.Builder("getAnnotatedElement", TypeLiteral.of(List.class)) + .setConfigurationContext(context).build(); + assertEquals(context, ctx.getConfigurationContext()); + } + + + private static final class MyConverter implements PropertyConverter<InetAddress>{ + @Override + public InetAddress convert(String value, ConversionContext context) { + return null; + } + } + + private static final class MyConfigurationContext implements ConfigurationContext{ + + @Override + public void addPropertySources(PropertySource... propertySources) { + + } + + @Override + public List<PropertySource> getPropertySources() { + return null; + } + + @Override + public PropertySource getPropertySource(String name) { + return null; + } + + @Override + public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) { + + } + + @Override + public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { + return null; + } + + @Override + public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> type) { + return null; + } + + @Override + public List<PropertyFilter> getPropertyFilters() { + return null; + } + + @Override + public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy() { + return null; + } + + @Override + public ConfigurationContextBuilder toBuilder() { + return null; + } + } + + private static final class MyConfiguration implements Configuration{ + + @Override + public String get(String key) { + return null; + } + + @Override + public String getOrDefault(String key, String defaultValue) { + return null; + } + + @Override + public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { + return null; + } + + @Override + public <T> T get(String key, Class<T> type) { + return null; + } + + @Override + public <T> T get(String key, TypeLiteral<T> type) { + return null; + } + + @Override + public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue) { + return null; + } + + @Override + public Map<String, String> getProperties() { + return null; + } + + @Override + public Configuration with(ConfigOperator operator) { + return null; + } + + @Override + public <T> T query(ConfigQuery<T> query) { + return null; + } + + @Override + public ConfigurationContext getContext() { + return null; + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java ---------------------------------------------------------------------- diff --git a/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java b/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java new file mode 100644 index 0000000..590d450 --- /dev/null +++ b/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java @@ -0,0 +1,72 @@ +/* + * 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.spi; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for {@link FilterContext}. + */ +public class FilterContextTest { + @Test + public void getKey() throws Exception { + FilterContext ctx = new FilterContext("getKey", + new HashMap<String,String>(), true); + assertEquals("getKey", ctx.getKey()); + } + + @Test + public void isSinglePropertyScoped() throws Exception { + FilterContext ctx = new FilterContext("isSinglePropertyScoped", + new HashMap<String,String>(), true); + assertEquals(true, ctx.isSinglePropertyScoped()); + ctx = new FilterContext("isSinglePropertyScoped", + new HashMap<String,String>(), false); + assertEquals(false, ctx.isSinglePropertyScoped()); + } + + @Test + public void getConfigEntries() throws Exception { + Map<String,String> config = new HashMap<>(); + for(int i=0;i<10;i++) { + config.put("key-"+i, "value-"+i); + } + FilterContext ctx = new FilterContext("getConfigEntries", + config, true); + assertEquals(config, ctx.getConfigEntries()); + assertTrue(config != ctx.getConfigEntries()); + } + + @Test + public void testToString() throws Exception { + Map<String,String> config = new HashMap<>(); + for(int i=0;i<2;i++) { + config.put("key-"+i, "value-"+i); + } + FilterContext ctx = new FilterContext("testToString", + config, true); + assertEquals("FilterContext{key='testToString', configEntries={key-0=value-0, key-1=value-1}}", ctx.toString()); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/code/api/src/test/java/org/apache/tamaya/spi/ServiceContextManagerTest.java ---------------------------------------------------------------------- diff --git a/code/api/src/test/java/org/apache/tamaya/spi/ServiceContextManagerTest.java b/code/api/src/test/java/org/apache/tamaya/spi/ServiceContextManagerTest.java new file mode 100644 index 0000000..7e9a010 --- /dev/null +++ b/code/api/src/test/java/org/apache/tamaya/spi/ServiceContextManagerTest.java @@ -0,0 +1,67 @@ +/* + * 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.spi; + +import org.junit.Test; + +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Additional tests for {@link ServiceContextManager}, created by atsticks on 20.08.16. + */ +public class ServiceContextManagerTest { + + @Test + public void setGetServiceContext() throws Exception { + ServiceContext prev = ServiceContextManager.getServiceContext(); + try { + MyServiceContext mine = new MyServiceContext(); + ServiceContextManager.set(mine); + assertTrue(ServiceContextManager.getServiceContext() == mine); + ServiceContextManager.set(mine); + assertTrue(ServiceContextManager.getServiceContext() == mine); + }finally{ + ServiceContextManager.set(prev); + assertTrue(ServiceContextManager.getServiceContext() == prev); + } + + } + + private static final class MyServiceContext implements ServiceContext{ + + @Override + public int ordinal() { + return 0; + } + + @Override + public <T> T getService(Class<T> serviceType) { + return null; + } + + @Override + public <T> List<T> getServices(Class<T> serviceType) { + return Collections.emptyList(); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java index 1c76d8c..1b37233 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java @@ -168,7 +168,8 @@ public class DefaultConfiguration implements Configuration { protected <T> T convertValue(String key, String value, TypeLiteral<T> type) { if (value != null) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); - ConversionContext context = new ConversionContext.Builder(this, this.configurationContext, key, type).build(); + ConversionContext context = new ConversionContext.Builder(this, this.configurationContext, key, type) + .build(); for (PropertyConverter<T> converter : converters) { try { T t = converter.convert(value, context); @@ -179,6 +180,11 @@ public class DefaultConfiguration implements Configuration { LOG.log(Level.INFO, "PropertyConverter: " + converter + " failed to convert value: " + value, e); } } + // if the target type is a String, we can return the value, no conversion required. + if(type.equals(TypeLiteral.of(String.class))){ + return (T)value; + } + // unsupported type, throw an exception throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + ", supported formats: " + context.getSupportedFormats()); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java index 29be92f..7a65fa5 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java @@ -78,14 +78,33 @@ public class DefaultConfigurationContext implements ConfigurationContext { propertySources.addAll(builder.propertySources); // now sort them according to their ordinal values immutablePropertySources = Collections.unmodifiableList(propertySources); - LOG.info("Registered " + immutablePropertySources.size() + " property sources: " + - immutablePropertySources); + StringBuilder b= new StringBuilder(); + b.append("Registered " + immutablePropertySources.size() + " property sources: \n"); + b.append(" NAME [TYPE]\n"); + b.append(" ---------------------------------------------------\n"); + for(PropertySource ps: immutablePropertySources){ + b.append(" ").append(ps.getName()).append(" [").append(ps.getClass().getSimpleName()).append("],\n"); + } + if(b.length()>0) { + b.setLength(b.length() - 2); + } + LOG.info(b.toString()); + b.setLength(0); // as next step we pick up the PropertyFilters pretty much the same way List<PropertyFilter> propertyFilters = new ArrayList<>(builder.getPropertyFilters()); immutablePropertyFilters = Collections.unmodifiableList(propertyFilters); - LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " + - immutablePropertyFilters); + b.append("Registered " + immutablePropertyFilters.size() + " property filters: \n"); + b.append(" TYPE\n"); + b.append(" ----------------------------------------------------------\n"); + for(PropertyFilter pf: immutablePropertyFilters){ + b.append(" ").append(pf.getClass().getName()).append(",\n"); + } + if(b.length()>0) { + b.setLength(b.length() - 2); + } + LOG.info(b.toString()); + b.setLength(0); // Finally add the converters for(Map.Entry<TypeLiteral<?>, Collection<PropertyConverter<?>>> en:builder.getPropertyConverter().entrySet()) { @@ -93,8 +112,20 @@ public class DefaultConfigurationContext implements ConfigurationContext { this.propertyConverterManager.register(en.getKey(), converter); } } - LOG.info("Registered " + propertyConverterManager.getPropertyConverters().size() + " property converters: " + - propertyConverterManager.getPropertyConverters()); + b.append("Registered " + propertyConverterManager.getPropertyConverters().size() + " property converters:\n"); + b.append(" TYPE\n"); + b.append(" ----------------------------------------------------------\n"); + for(Map.Entry<TypeLiteral<?>,List<PropertyConverter<?>>> list: propertyConverterManager.getPropertyConverters().entrySet()){ + b.append(" ").append(list.getKey().getRawType().getName()).append(" -> \n"); + for(PropertyConverter<?> conv:list.getValue()) { + b.append(" ").append(conv.toString()).append(",\n"); + } + } + if(b.length()>0) { + b.setLength(b.length() - 2); + } + LOG.info(b.toString()); + b.setLength(0); propertyValueCombinationPolicy = builder.combinationPolicy; if(propertyValueCombinationPolicy==null){ http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java index e03d9ce..be04a6e 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java @@ -24,6 +24,7 @@ import org.apache.tamaya.spi.ConfigurationContextBuilder; import org.apache.tamaya.spi.ConfigurationProviderSpi; import org.apache.tamaya.spi.ServiceContextManager; +import javax.annotation.Priority; import java.util.Objects; /** http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/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 new file mode 100644 index 0000000..f6f7316 --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java @@ -0,0 +1,513 @@ +/* + * 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.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.core.internal.DefaultConfigurationContextBuilder; +import org.apache.tamaya.spi.*; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for {@link DefaultConfigurationContextBuilder} by atsticks on 06.09.16. + */ +public class ConfigurationContextBuilderTest { + + private TestPropertySource testPropertySource = new TestPropertySource(){}; + + @Test + public void setContext() throws Exception { + ConfigurationContext context = ConfigurationProvider.getConfiguration().getContext(); + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .setContext(context); + assertEquals(context, b.build()); + } + + @Test + public void addPropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Array", 1); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==2); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + // Ensure no sorting happens during add, so switch ordinals! + testPS2 = new TestPropertySource("addPropertySources_Array", 1); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertySources(testPS2, testPropertySource); + ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==2); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertEquals(ctx.getPropertySources().get(1).getName(), "TestPropertySource"); + assertEquals(ctx.getPropertySources().get(0).getName(), "addPropertySources_Array"); + } + + @Test + public void addPropertySources_Collection() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Collection", 1); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertySources(Arrays.asList(new PropertySource[]{testPropertySource, testPS2})); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==2); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertEquals(ctx.getPropertySources().get(0).getName(), "TestPropertySource"); + assertEquals(ctx.getPropertySources().get(1).getName(), "addPropertySources_Collection"); + // Ensure no sorting happens during add, so switch ordinals! + testPS2 = new TestPropertySource("addPropertySources_Collection", 1); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertySources(Arrays.asList(new PropertySource[]{testPS2, testPropertySource})); + ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==2); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertEquals(ctx.getPropertySources().get(1).getName(), "TestPropertySource"); + assertEquals(ctx.getPropertySources().get(0).getName(), "addPropertySources_Collection"); + } + + @Test + public void removePropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("removePropertySources_Array", 1); + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==2); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + ctx = b.build(); + assertFalse(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + assertTrue(ctx.getPropertySources().size()==1); + } + + @Test + public void removePropertySources_Collection() throws Exception { + PropertySource testPS2 = new TestPropertySource("removePropertySources_Array", 1); + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==2); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==1); + assertFalse(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + } + + @Test + public void addPropertyFilters_Array() throws Exception { + PropertyFilter filter1 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + PropertyFilter filter2 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertTrue(ctx.getPropertyFilters().size()==2); + b = ConfigurationProvider.getConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertTrue(ctx.getPropertyFilters().size()==2); + } + + @Test + public void addPropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + PropertyFilter filter2 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + b.addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertTrue(ctx.getPropertyFilters().size()==2); + b = ConfigurationProvider.getConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertTrue(ctx.getPropertyFilters().size()==2); + } + + @Test + public void removePropertyFilters_Array() throws Exception { + PropertyFilter filter1 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + PropertyFilter filter2 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyFilters(filter1, filter2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertTrue(ctx.getPropertyFilters().size()==2); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyFilters(filter1, filter2); + b.removePropertyFilters(filter1); + ctx = b.build(); + assertTrue(ctx.getPropertyFilters().size()==1); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + @Test + public void removePropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + PropertyFilter filter2 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertTrue(ctx.getPropertyFilters().size()==2); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + b.removePropertyFilters(filter1); + ctx = b.build(); + assertTrue(ctx.getPropertyFilters().size()==1); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + + @Test + public void addPropertyConverters_Array() throws Exception { + PropertyConverter converter = new PropertyConverter(){ + + @Override + public Object convert(String value, ConversionContext context) { + return value.toLowerCase(); + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(ctx.getPropertyConverters().size(), 19); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(ctx.getPropertyConverters().size(), 19); + } + + @Test + public void addPropertyConverters_Collection() throws Exception { + PropertyConverter converter = new PropertyConverter(){ + + @Override + public Object convert(String value, ConversionContext context) { + return value.toLowerCase(); + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .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(ctx.getPropertyConverters().size(), 19); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), + Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(ctx.getPropertyConverters().size(), 19); + } + + @Test + public void removePropertyConverters_Array() throws Exception { + PropertyConverter converter = new PropertyConverter(){ + + @Override + public Object convert(String value, ConversionContext context) { + return value.toLowerCase(); + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(ctx.getPropertyConverters(TypeLiteral.of(String.class)).size(), 1); + b = ConfigurationProvider.getConfigurationContextBuilder() + .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)); + assertEquals(ctx.getPropertyConverters(TypeLiteral.of(String.class)).size(), 0); + } + + @Test + public void removePropertyConverters_Collection() throws Exception { + PropertyConverter converter = new PropertyConverter(){ + + @Override + public Object convert(String value, ConversionContext context) { + return value.toLowerCase(); + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .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(ctx.getPropertyConverters(TypeLiteral.of(String.class)).size(), 1); + b = ConfigurationProvider.getConfigurationContextBuilder() + .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})); + ctx = b.build(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(ctx.getPropertyConverters(TypeLiteral.of(String.class)).size(), 0); + } + + @Test + public void setPropertyValueCombinationPolicy() throws Exception { + PropertyValueCombinationPolicy combPol = new PropertyValueCombinationPolicy(){ + + @Override + public Map<String, String> collect(Map<String, String> currentValue, String key, PropertySource propertySource) { + return currentValue; + } + }; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder() + .setPropertyValueCombinationPolicy(combPol); + ConfigurationContext ctx = b.build(); + assertEquals(ctx.getPropertyValueCombinationPolicy(), combPol); + } + + @Test + public void increasePriority(){ + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + 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(){ + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + 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 + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + 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 + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + 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 + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + 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 = new Comparator<PropertySource>() { + @Override + public int compare(PropertySource o1, PropertySource o2) { + return 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 + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + PropertyFilter[] propertyFilters = new PropertyFilter[10]; + for(int i=0;i<propertyFilters.length;i++){ + propertyFilters[i] = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return toString() + " - "; + } + }; + } + b.addPropertyFilters(propertyFilters); + Comparator<PropertyFilter> pfComp = new Comparator<PropertyFilter>() { + @Override + public int compare(PropertyFilter o1, PropertyFilter o2) { + return 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 { + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + ConfigurationContext ctx = b.build(); + assertNotNull(ctx); + assertTrue(ctx.getPropertySources().isEmpty()); + assertTrue(ctx.getPropertyFilters().isEmpty()); + } + + @Test + public void testRemoveAllFilters() throws Exception { + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + b.addPropertyFilters(new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return toString() + " - "; + } + }); + assertFalse(b.getPropertyFilters().isEmpty()); + b.removePropertyFilters(b.getPropertyFilters()); + assertTrue(b.getPropertyFilters().isEmpty()); + } + + @Test + public void testRemoveAllSources() throws Exception { + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + 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/77dbcc4d/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java b/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java new file mode 100644 index 0000000..1125f7a --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java @@ -0,0 +1,73 @@ +/* + * 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.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import java.util.Collections; +import java.util.Date; +import java.util.Map; + +/** + * Created by atsticks on 18.10.16. + */ +public class TestPropertySource implements PropertySource { + + private String id; + private int ordinal; + + public TestPropertySource() { + this("TestPropertySource", 0); + } + + public TestPropertySource(String id, int ordinal) { + this.id = id; + this.ordinal = ordinal; + } + + @Override + public int getOrdinal() { + return ordinal; + } + + @Override + public String getName() { + return id != null ? id : "TestPropertySource"; + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.builder(key, key + "Value", getName()) + .addContextData("ordinal", String.valueOf(getOrdinal())) + .addContextData("createdAt", String.valueOf(new Date())) + .build(); + } + + @Override + public Map<String, String> getProperties() { + return Collections.emptyMap(); + } + + @Override + public boolean isScannable() { + return false; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java new file mode 100644 index 0000000..1075fcc --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java @@ -0,0 +1,232 @@ +/* + * 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 DefaultConfigurationContextBuilder} by atsticks on 06.09.16. + */ +public class DefaultConfigurationContextBuilderTest { + + private TestPropertySource testPropertySource = new TestPropertySource(){}; + + @Test + public void setContext() throws Exception { + ConfigurationContext context = ConfigurationProvider.getConfiguration().getContext(); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .setContext(context); + assertEquals(context, b.build()); + } + + @Test + public void addPropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Array_2"); + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==2); + 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 DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==2); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + b = new DefaultConfigurationContextBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + ctx = b.build(); + assertTrue(ctx.getPropertySources().size()==1); + assertFalse(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + } + + @Test + public void addPropertyFilters_Array() throws Exception { + PropertyFilter filter1 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + PropertyFilter filter2 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + DefaultConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertTrue(ctx.getPropertyFilters().size()==2); + b = new DefaultConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertTrue(ctx.getPropertyFilters().size()==2); + } + + @Test + public void removePropertyFilters_Array() throws Exception { + PropertyFilter filter1 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + PropertyFilter filter2 = new PropertyFilter(){ + @Override + public String filterProperty(String value, FilterContext context) { + return value; + } + }; + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .addPropertyFilters(filter1, filter2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertTrue(ctx.getPropertyFilters().size()==2); + b = new DefaultConfigurationContextBuilder() + .addPropertyFilters(filter1, filter2); + b.removePropertyFilters(filter1); + ctx = b.build(); + assertTrue(ctx.getPropertyFilters().size()==1); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + @Test + public void addPropertyConverter() throws Exception { + PropertyConverter converter = new PropertyConverter(){ + + @Override + public Object convert(String value, ConversionContext context) { + return 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(ctx.getPropertyConverters().size(), 19); + b = new DefaultConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(ctx.getPropertyConverters().size(), 19); + } + + @Test + public void removePropertyConverters_Array() throws Exception { + PropertyConverter converter = new PropertyConverter(){ + + @Override + public Object convert(String value, ConversionContext context) { + return 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(ctx.getPropertyConverters(TypeLiteral.of(String.class)).size(), 1); + b = new DefaultConfigurationContextBuilder() + .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)); + assertEquals(ctx.getPropertyConverters(TypeLiteral.of(String.class)).size(), 0); + } + + @Test + public void setPropertyValueCombinationPolicy() throws Exception { + PropertyValueCombinationPolicy combPol = new PropertyValueCombinationPolicy(){ + + @Override + public Map<String, String> collect(Map<String, String> currentValue, String key, PropertySource propertySource) { + return currentValue; + } + }; + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder() + .setPropertyValueCombinationPolicy(combPol); + ConfigurationContext ctx = b.build(); + assertEquals(ctx.getPropertyValueCombinationPolicy(), combPol); + } + + @Test + public void build() throws Exception { + ConfigurationContextBuilder b = new DefaultConfigurationContextBuilder(); + assertNotNull(b.build()); + } + + + 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, String> 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/77dbcc4d/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java new file mode 100644 index 0000000..ac18b32 --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.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.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 DefaultConfigurationContext} by atsticks on 16.08.16. + */ +public class DefaultConfigurationContextTest { + + @Test + public void addPropertySources() throws Exception { + ConfigurationContext ctx = new DefaultConfigurationContextBuilder().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(); + System.out.println(toString); + } + + @Test + public void getPropertySources() throws Exception { + ConfigurationContext ctx = new DefaultConfigurationContextBuilder().build(); + assertNotNull(ctx.getPropertySources()); + assertEquals(ctx.getPropertySources().size(), 0); + ctx = new DefaultConfigurationContextBuilder().loadDefaultPropertySources().build(); + assertNotNull(ctx.getPropertySources()); + assertEquals(8, ctx.getPropertySources().size()); + } + + @Test + public void getPropertySource() throws Exception { + TestPropertyDefaultSource ps = new TestPropertyDefaultSource(); + ConfigurationContext ctx = new DefaultConfigurationContextBuilder() + .addPropertySources(ps).build(); + assertNotNull(ctx.getPropertySources()); + assertEquals(ctx.getPropertySources().size(), 1); + assertNotNull(((DefaultConfigurationContext)ctx).getPropertySource(ps.getName())); + assertEquals(ps.getName(), ((DefaultConfigurationContext)ctx).getPropertySource(ps.getName()).getName()); + assertNull(((DefaultConfigurationContext)ctx).getPropertySource("huhu")); + + } + + @Test + public void testHashCode() throws Exception { + TestPropertyDefaultSource ps = new TestPropertyDefaultSource(); + ConfigurationContext ctx1 = new DefaultConfigurationContextBuilder() + .addPropertySources(ps).build(); + ConfigurationContext ctx2 = new DefaultConfigurationContextBuilder() + .addPropertySources(ps).build(); + assertEquals(ctx1.hashCode(), ctx2.hashCode()); + ctx2 = new DefaultConfigurationContextBuilder() + .build(); + assertNotEquals(ctx1.hashCode(), ctx2.hashCode()); + + } + + @Test + public void addPropertyConverter() throws Exception { + ConfigurationContext ctx = new DefaultConfigurationContextBuilder().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 DefaultConfigurationContextBuilder().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 DefaultConfigurationContextBuilder().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 DefaultConfigurationContextBuilder().build(); + PropertyFilter testFilter = new PropertyFilter() { + + @Override + public String filterProperty(String 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 DefaultConfigurationContextBuilder().build(); + assertNotNull(ctx.getPropertyValueCombinationPolicy()); + assertEquals(ctx.getPropertyValueCombinationPolicy(), + PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR); + } + + @Test + public void toBuilder() throws Exception { + assertNotNull(new DefaultConfigurationContextBuilder().build().toBuilder()); + } + + @Test + public void testRoundTrip() throws Exception { + ConfigurationContext ctx = new DefaultConfigurationContextBuilder().build(); + assertEquals(ctx.toBuilder().build(), ctx); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/77dbcc4d/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 new file mode 100644 index 0000000..b5fb80c --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationProviderTest.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.core.internal; + +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 getConfigurationContext() throws Exception { + assertNotNull(new DefaultConfigurationProvider().getConfigurationContext()); + } + + @Test + public void getConfigurationContextBuilder() throws Exception { + assertNotNull(new DefaultConfigurationProvider().getConfigurationContextBuilder()); + } + + @Test + public void setConfigurationContext() throws Exception { + new DefaultConfigurationProvider() + .setConfigurationContext(new DefaultConfigurationProvider().getConfigurationContext()); + } + + @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/77dbcc4d/code/core/src/test/java/org/apache/tamaya/core/internal/PriorityServiceComparatorTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/PriorityServiceComparatorTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/PriorityServiceComparatorTest.java new file mode 100644 index 0000000..e08c8b0 --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/PriorityServiceComparatorTest.java @@ -0,0 +1,47 @@ +/* + * 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.core.propertysource.EnvironmentPropertySource; +import org.apache.tamaya.core.propertysource.SystemPropertySource; +import org.junit.Test; + +import javax.annotation.Priority; + +import static org.junit.Assert.*; + +/** + * Created by atsticks on 12.09.16. + */ +public class PriorityServiceComparatorTest { + + private PriorityServiceComparator comp = new PriorityServiceComparator(); + + @Test + public void compare() throws Exception { + assertTrue(comp.compare("a", "b")==0); + assertTrue(comp.compare(getClass(), getClass())==0); + assertTrue(comp.compare(new A(), new SystemPropertySource())==-1); + assertTrue(comp.compare(new SystemPropertySource(), new A())==1); + } + + @Priority(100) + private static final class A{} + +} \ No newline at end of file
