http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java new file mode 100644 index 0000000..cd034be --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java @@ -0,0 +1,422 @@ +/* + * 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.spi.TypeLiteral; +import org.apache.tamaya.spi.*; +import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; + +import static org.junit.Assert.*; + +/** + * Tests for {@link ConfigurationContextBuilder} 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(); + assertEquals(2, ctx.getPropertySources().size()); + 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(); + assertEquals(2, ctx.getPropertySources().size()); + 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(); + assertEquals(2, ctx.getPropertySources().size()); + 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(); + assertEquals(2, ctx.getPropertySources().size()); + 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(); + assertEquals(2, ctx.getPropertySources().size()); + 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)); + assertEquals(1, ctx.getPropertySources().size()); + } + + @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(); + assertEquals(2, ctx.getPropertySources().size()); + assertTrue(ctx.getPropertySources().contains(testPropertySource)); + assertTrue(ctx.getPropertySources().contains(testPS2)); + b = ConfigurationProvider.getConfigurationContextBuilder() + .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; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationContextBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertEquals(2, ctx.getPropertyFilters().size()); + } + + @Test + public void addPropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + b.addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationContextBuilder(); + 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 = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyFilters(filter1, filter2); + ConfigurationContext ctx = b.build(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationContextBuilder() + .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 + public void removePropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> 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)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{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 addPropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> 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(1, ctx.getPropertyConverters().size()); + b = ConfigurationProvider.getConfigurationContextBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(1, ctx.getPropertyConverters().size()); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void addPropertyConverters_Collection() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationContextBuilder b = 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(), 1); + 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(), 1); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void removePropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> 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(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + 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)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + public void removePropertyConverters_Collection() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationContextBuilder b = 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(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + 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)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); + } + + @Test + public void setPropertyValueCombinationPolicy() throws Exception { + PropertyValueCombinationPolicy combPol = (currentValue, key, propertySource) -> 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 = (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 + ConfigurationContextBuilder b = ConfigurationProvider.getConfigurationContextBuilder(); + 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 { + 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((value, context) -> value.toBuilder().setValue(toString() + " - ").build()); + assertFalse(b.getPropertyFilters().isEmpty()); + b.removePropertyFilters(b.getPropertyFilters()); + assertTrue(b.getPropertyFilters().isEmpty()); + } + + @Test + public void testRemoveAllSources() throws Exception { + ConfigurationContextBuilder b = 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/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java new file mode 100644 index 0000000..0fad63c --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java @@ -0,0 +1,57 @@ +/* + * 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.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +/** + * This tests checks if the combination of 2 prioritized PropertySource return valid results of the final configuration. + */ +public class ConfigurationTest { + + @Test + public void testAccess(){ + assertNotNull(current()); + } + + private Configuration current() { + return ConfigurationProvider.getConfiguration(); + } + + @Test + public void testContent(){ + assertNotNull(current().get("name")); + assertNotNull(current().get("name2")); // from default + assertNotNull(current().get("name3")); // overridden default, mapped by filter to name property + assertNotNull(current().get("name4")); // final only + + + assertEquals("Robin", current().get("name")); + assertEquals("Sabine", current().get("name2")); // from default + assertEquals("Mapped to name: Robin", current().get("name3")); // overridden default, mapped by filter to name property + assertEquals("Sereina(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)", current().get("name4")); // final only + assertNull(current().get("name5")); // final only, but removed from filter + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java b/code/old/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java new file mode 100644 index 0000000..7ec4458 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/TestPropertySource.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.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; + } + + 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()) + .addMetaEntry("ordinal", String.valueOf(getOrdinal())) + .addMetaEntry("createdAt", String.valueOf(new Date())) + .build(); + } + + @Override + public Map<String, PropertyValue> getProperties() { + return Collections.emptyMap(); + } + + @Override + public boolean isScannable() { + return false; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/A.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/A.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/A.java new file mode 100644 index 0000000..aaab601 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/A.java @@ -0,0 +1,29 @@ +/* + * 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; + +/** + * Test class for testing transitively evaluated property converters. + */ +class A implements AutoCloseable{ + @Override + public void close() throws Exception { + + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/B.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/B.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/B.java new file mode 100644 index 0000000..31bafb6 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/B.java @@ -0,0 +1,29 @@ +/* + * 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; + +/** + * Test class for testing transitively evaluated property converters. + */ +public class B extends A implements Runnable{ + @Override + public void run() { + + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java new file mode 100644 index 0000000..9fc4433 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java @@ -0,0 +1,90 @@ +/* + * 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 org.mockito.Mockito; + +import java.io.PrintStream; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.Permission; + +/* + * Note: + * The tests of this class will fail PIT, our coverage tool. + * Therefore we excluded this class in the parent POM + * from the test execution. + * Oliver B. Fischer, 2017-09-16 + */ +public class BannerManagerTest { + + @Test + public void valueConsoleSendsBannerToSystemOut() { + + SecurityManager sm = new SecurityManager(); + AccessControlContext con = AccessController.getContext(); + + Permission p = new RuntimePermission("setIO"); + + /* + * Here we check the precondition for this unit test + * and the correct setup of the test environment + * The JVM must have been started with + * -Djava.security.policy=<path_to_core_module</src/test/resources/java-security.policy + */ + sm.checkPermission(p, con); + + PrintStream standard = System.out; + PrintStream printStream = Mockito.mock(PrintStream.class); + + System.setOut(printStream); + standard.println("Changed stream for STDOUT successfully"); + + try { + BannerManager bm = new BannerManager("console"); + bm.outputBanner(); + + } finally { + System.setOut(standard); + } + + Mockito.verify(printStream, Mockito.atLeastOnce()).println(Mockito.anyString()); + } + + @Test + public void invalidValueAvoidsLoggingToConsonle() { + + PrintStream standard = System.out; + PrintStream printStream = Mockito.mock(PrintStream.class); + + System.setOut(printStream); + + try { + BannerManager bm = new BannerManager("snafu"); + bm.outputBanner(); + + } finally { + System.setOut(standard); + } + + Mockito.verify(printStream, Mockito.never()).println(Mockito.anyString()); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/C.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/C.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/C.java new file mode 100644 index 0000000..fdd3476 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/C.java @@ -0,0 +1,56 @@ +/* + * 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 java.io.IOException; +import java.nio.CharBuffer; + +/** + * Test class for testing transitively evaluated property converters. + */ +public class C extends B implements Readable{ + + private final String inValue; + + public C(String inValue){ + this.inValue = inValue; + } + + @Override + public int read(CharBuffer cb) throws IOException { + return 0; + } + + /** + * Returns the input value, set on creation. Used for test assertion. + * @return the in value. + */ + public String getInValue() { + return inValue; + } + + @Override + public String toString() { + return "C{" + + "inValue='" + inValue + '\'' + + '}'; + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/CTestConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/CTestConverter.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/CTestConverter.java new file mode 100644 index 0000000..7ee2a35 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/CTestConverter.java @@ -0,0 +1,32 @@ +/* + * 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.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +/** + * Created by Anatole on 13.06.2015. + */ +public class CTestConverter implements PropertyConverter<C>{ + @Override + public C convert(String value, ConversionContext context) { + return new C(value); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationBuilderTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationBuilderTest.java new file mode 100644 index 0000000..fbad630 --- /dev/null +++ b/code/old/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.spi.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/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationProviderTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationProviderTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationProviderTest.java new file mode 100644 index 0000000..e4dab56 --- /dev/null +++ b/code/old/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/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationTest.java new file mode 100644 index 0000000..5c5cc20 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/CoreConfigurationTest.java @@ -0,0 +1,178 @@ +/* + * 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.base.filter.FilterContext; +import org.apache.tamaya.spi.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/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/DefaultJavaConfigurationTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/DefaultJavaConfigurationTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/DefaultJavaConfigurationTest.java new file mode 100644 index 0000000..a1d638f --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/DefaultJavaConfigurationTest.java @@ -0,0 +1,50 @@ +/* + * 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.PropertySource; +import org.apache.tamaya.spisupport.propertysource.JavaConfigurationPropertySource; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +import static org.apache.tamaya.ConfigurationProvider.getConfiguration; + +public class DefaultJavaConfigurationTest { + + private static final String A_UMLAUT = "\u00E4"; + private static final String O_UMLAUT = "\u00F6"; + + @Test + public void loadsSimpleAndXMLPropertyFilesProper() { + for (int i = 1; i < 6; i++) { + String key = "confkey" + i; + String value = "javaconf-value" + i; + // check if we had our key in configuration.current + MatcherAssert.assertThat(getConfiguration().getProperties().containsKey(key), Matchers.is(true)); + MatcherAssert.assertThat(value, Matchers.equalTo(getConfiguration().get(key))); + } + + MatcherAssert.assertThat(getConfiguration().getProperties().containsKey("aaeehh"), Matchers.is(true)); + MatcherAssert.assertThat(getConfiguration().getProperties().get("aaeehh"), Matchers.equalTo(A_UMLAUT)); + + MatcherAssert.assertThat(getConfiguration().getProperties().containsKey(O_UMLAUT), Matchers.is(true)); + MatcherAssert.assertThat(getConfiguration().getProperties().get(O_UMLAUT), Matchers.equalTo("o")); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java new file mode 100644 index 0000000..91d3bb8 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java @@ -0,0 +1,133 @@ +/* + * 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.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.spi.ConversionContext; +import org.junit.Test; + +import java.math.BigDecimal; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +/** + * Tests the default converter for bytes. + */ +public class BigDecimalConverterTest { + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * + * @throws Exception + */ + @Test + public void testConvert_BigDecimal_Decimal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.decimal", BigDecimal.class); + assertThat(valueRead).isNotNull(); + assertEquals(new BigDecimal(101), valueRead); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * + * @throws Exception + */ + @Test + public void testConvert_BigDecimal_Hex() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.hex.lowerX", BigDecimal.class); + assertThat(valueRead).isNotNull(); + assertEquals(new BigDecimal("47"), valueRead); + valueRead = config.get("tests.converter.bd.hex.upperX", BigDecimal.class); + assertThat(valueRead).isNotNull(); + assertEquals(new BigDecimal("63"), valueRead); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * + * @throws Exception + */ + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.foo", BigDecimal.class); + assertNull(valueRead); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * + * @throws Exception + */ + @Test + public void testConvert_BigDecimal_BigValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.big", BigDecimal.class); + assertThat(valueRead).isNotNull(); + assertEquals(new BigDecimal("101666666666666662333337263723628763821638923628193612983618293628763"), + valueRead); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * + * @throws Exception + */ + @Test + public void testConvert_BigDecimal_BigFloatValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.bigFloat", BigDecimal.class); + assertThat(valueRead).isNotNull(); + assertEquals(new BigDecimal("1016666666666666623333372637236287638216389293628763.1016666666666666623333372" + + "63723628763821638923628193612983618293628763"), valueRead); + } + + @Test + public void converterHandlesNullValueCorrectly() throws Exception { + ConversionContext context = mock(ConversionContext.class); + + BigDecimalConverter converter = new BigDecimalConverter(); + BigDecimal value = converter.convert("", context); + + assertThat(value).isNull(); + } + + @Test + public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { + ConversionContext context = mock(ConversionContext.class); + + BigDecimalConverter converter = new BigDecimalConverter(); + BigDecimal value = converter.convert("", context); + + assertThat(value).isNull(); + verify(context).addSupportedFormats(BigDecimalConverter.class, "<bigDecimal> -> new BigDecimal(String)"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java new file mode 100644 index 0000000..46c1e58 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java @@ -0,0 +1,118 @@ +/* + * 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.converters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +/** + * Tests the default converter for bytes. + */ +public class BooleanConverterTest { + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_BooleanTrue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Boolean valueRead = config.get("tests.converter.boolean.y1", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.y2", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.yes1", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.yes2", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.yes3", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.true1", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.true2", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.true3", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.t1", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + valueRead = config.get("tests.converter.boolean.t2", Boolean.class); + assertNotNull(valueRead); + assertEquals(valueRead, Boolean.TRUE); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_BooleanFalse() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Boolean valueRead = config.get("tests.converter.boolean.y1", Boolean.class); + assertNotNull(valueRead); + valueRead = config.get("tests.converter.boolean.n1", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.n2", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.no1", Boolean.class); + assertFalse(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.no2", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.no3", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.false1", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.false2", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.false3", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.f1", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.f2", Boolean.class); + assertNotNull(valueRead); + assertFalse(valueRead); + valueRead = config.get("tests.converter.boolean.foo", Boolean.class); + assertNull(valueRead); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java new file mode 100644 index 0000000..3eb48e3 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java @@ -0,0 +1,81 @@ +/* + * 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.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.*; +/** + * Tests the default converter for bytes. + */ +public class ByteConverterTest { + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Byte valueRead = config.get("tests.converter.byte.decimal", Byte.class); + assertNotNull(valueRead); + assertEquals(valueRead.byteValue(), 101); + valueRead = config.get("tests.converter.byte.octal", Byte.class); + assertNotNull(valueRead); + assertEquals(valueRead.byteValue(), Byte.decode("02").byteValue()); + valueRead = config.get("tests.converter.byte.hex.lowerX", Byte.class); + assertNotNull(valueRead); + assertEquals(valueRead.byteValue(), Byte.decode("0x2F").byteValue()); + valueRead = config.get("tests.converter.byte.hex.upperX", Byte.class); + assertNotNull(valueRead); + assertEquals(valueRead.byteValue(), Byte.decode("0X3F").byteValue()); + valueRead = config.get("tests.converter.byte.foo", Byte.class); + assertNull(valueRead); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte_MinValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Byte valueRead = config.get("tests.converter.byte.min", Byte.class); + assertThat(valueRead).isNotNull(); + assertEquals(Byte.MIN_VALUE, valueRead.byteValue()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte_MaxValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Byte valueRead = config.get("tests.converter.byte.max", Byte.class); + assertThat(valueRead).isNotNull(); + assertEquals(Byte.MAX_VALUE, valueRead.byteValue()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java new file mode 100644 index 0000000..c420fcd --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java @@ -0,0 +1,86 @@ +/* + * 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.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.*; +/** + * Tests conversion of the {@link CharConverter}. + */ +public class CharConverterTest { + + @Test + public void testConvert_Character() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f", Character.class); + assertThat(valueRead).isNotNull(); + assertEquals(valueRead.charValue(), 'f'); + } + + @Test + public void testConvert_Character_Numeric() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f-numeric", Character.class); + assertThat(valueRead).isNotNull(); + assertEquals(valueRead.charValue(), (char)101); + } + + @Test + public void testConvert_Character_Quoted() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.d", Character.class); + assertThat(valueRead).isNotNull(); + assertEquals(valueRead.charValue(), 'd'); + } + + @Test + public void testConvert_Character_WithWhitespace_Before() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f-before", Character.class); + assertThat(valueRead).isNotNull(); + assertEquals(valueRead.charValue(), 'f'); + } + + @Test + public void testConvert_Character_WithWhitespace_After() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f-after", Character.class); + assertThat(valueRead).isNotNull(); + assertEquals(valueRead.charValue(), 'f'); + } + + @Test + public void testConvert_Character_WithWhitespace_Around() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f-around", Character.class); + assertThat(valueRead).isNotNull(); + assertEquals(valueRead.charValue(), 'f'); + } + + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.foo", Character.class); + assertNull(valueRead); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/ClassConverterTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/ClassConverterTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/ClassConverterTest.java new file mode 100644 index 0000000..a2a984b --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/internal/converters/ClassConverterTest.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.core.internal.converters; + +import org.apache.tamaya.spi.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.junit.Test; + +import java.math.BigDecimal; + +import static org.junit.Assert.*; + +/** + * Tests conversion of the {@link ClassConverter}. + */ +public class ClassConverterTest { + + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Class.class)) + .build(); + + @Test + public void testConvert_Class() throws Exception { + ClassConverter converter = new ClassConverter(); + assertEquals(BigDecimal.class, converter.convert("java.math.BigDecimal", context)); + } + + @Test + public void testConvert_Class_WithSpaces() throws Exception { + ClassConverter converter = new ClassConverter(); + assertEquals(BigDecimal.class, converter.convert(" java.math.BigDecimal\t", context)); + } + + @Test + public void testConvert_Class_WithSpacesBefore() throws Exception { + ClassConverter converter = new ClassConverter(); + assertEquals(BigDecimal.class, converter.convert(" java.math.BigDecimal", context)); + } + + @Test + public void testConvert_Class_WithSpacesAfter() throws Exception { + ClassConverter converter = new ClassConverter(); + assertEquals(BigDecimal.class, converter.convert("java.math.BigDecimal ", context)); + } + + @Test + public void testConvert_NotPresent() throws Exception { + ClassConverter converter = new ClassConverter(); + assertNull(converter.convert("", context)); + assertNull(converter.convert(null, context)); + } +}
