Repository: incubator-tamaya Updated Branches: refs/heads/master 513620391 -> 125eed202
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedPropertyFilter.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedPropertyFilter.java new file mode 100644 index 0000000..9a56476 --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedPropertyFilter.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.spisupport; + +import org.apache.tamaya.spi.FilterContext; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertyValue; + +/** + * + * @author William.Lieurance 2018.02.18 + */ +public class MockedPropertyFilter implements PropertyFilter { + + @Override + public PropertyValue filterProperty(PropertyValue value, FilterContext context) { + if (value.getKey().contains("Filternull")) { + return null; + } else { + return value; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedPropertySource.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedPropertySource.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedPropertySource.java new file mode 100644 index 0000000..59ae3cd --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/MockedPropertySource.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.spisupport; + +import java.util.HashMap; +import java.util.Map; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +/** + * + * @author William.Lieurance 2018.02.17 + */ +public class MockedPropertySource implements PropertySource { + + private String name = "MockedPropertySource"; + private int ordinal = 10; + + public MockedPropertySource() { + this("MockedPropertySource", 10); + } + + public MockedPropertySource(String id, int ordinal) { + this.name = id; + this.ordinal = ordinal; + } + + @Override + public int getOrdinal() { + return ordinal; + } + + @Override + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public PropertyValue get(String key) { + if (key.contains("Null")) { + return PropertyValue.of(key, null, "MockedPropertySource"); + } else if (key.contains("missing")){ + return null; + } + return PropertyValue.of(key, "valueFromMockedPropertySource", "MockedPropertySource"); + } + + @Override + public Map<String, PropertyValue> getProperties() { + Map<String, PropertyValue> returnable = new HashMap<>(); + returnable.put("shouldBeNull", get("shouldBeNull")); + returnable.put("Filterednull", get("shouldBeFiltered")); + returnable.put("someKey", get("someKey")); + + return returnable; + } + + @Override + public boolean isScannable() { + return true; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertyConverterManagerTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertyConverterManagerTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertyConverterManagerTest.java index 668409a..116937a 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertyConverterManagerTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertyConverterManagerTest.java @@ -26,6 +26,7 @@ import org.junit.Test; import java.util.List; +import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -66,6 +67,7 @@ public class PropertyConverterManagerTest { @Test public void testDirectConverterMapping() { PropertyConverterManager manager = new PropertyConverterManager(true); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(C.class))); List<PropertyConverter<C>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(C.class))); assertThat(converters, hasSize(1)); @@ -80,6 +82,7 @@ public class PropertyConverterManagerTest { @Test public void testDirectSuperclassConverterMapping() { PropertyConverterManager manager = new PropertyConverterManager(true); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(B.class))); List<PropertyConverter<B>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class))); assertThat(converters, hasSize(1)); converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class))); @@ -96,6 +99,7 @@ public class PropertyConverterManagerTest { @Test public void testMultipleConverterLoad() { PropertyConverterManager manager = new PropertyConverterManager(true); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(B.class))); List<PropertyConverter<B>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class))); assertThat(converters, hasSize(1)); manager = new PropertyConverterManager(true); @@ -106,6 +110,7 @@ public class PropertyConverterManagerTest { @Test public void testTransitiveSuperclassConverterMapping() { PropertyConverterManager manager = new PropertyConverterManager(true); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(A.class))); List<PropertyConverter<A>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(A.class))); assertThat(converters, hasSize(1)); @@ -120,6 +125,7 @@ public class PropertyConverterManagerTest { @Test public void testDirectInterfaceMapping() { PropertyConverterManager manager = new PropertyConverterManager(true); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(Readable.class))); List<PropertyConverter<Readable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(Readable.class))); assertThat(converters, hasSize(1)); @@ -134,6 +140,7 @@ public class PropertyConverterManagerTest { @Test public void testTransitiveInterfaceMapping1() { PropertyConverterManager manager = new PropertyConverterManager(true); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(Runnable.class))); List<PropertyConverter<Runnable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(Runnable.class))); assertThat(converters, hasSize(1)); @@ -148,6 +155,7 @@ public class PropertyConverterManagerTest { @Test public void testTransitiveInterfaceMapping2() { PropertyConverterManager manager = new PropertyConverterManager(true); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(AutoCloseable.class))); List<PropertyConverter<AutoCloseable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(AutoCloseable.class))); assertThat(converters, hasSize(1)); @@ -160,6 +168,50 @@ public class PropertyConverterManagerTest { } @Test + public void testBoxedConverterMapping() { + PropertyConverterManager manager = new PropertyConverterManager(true); + assertFalse(manager.isTargetTypeSupported(TypeLiteral.of(int.class))); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(Integer.class))); + List<PropertyConverter<Integer>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(int.class))); + assertThat(converters, hasSize(1)); + + PropertyConverter<Integer> converter = converters.get(0); + Integer result = converter.convert("101", DUMMY_CONTEXT); + + assertThat(result, notNullValue()); + assertThat(result, instanceOf(Integer.class)); + assertThat(result, equalTo(101)); + } + + + @Test + public void testCreateEnumPropertyConverter() { + PropertyConverterManager manager = new PropertyConverterManager(false); + PropertyConverter pc = manager.createDefaultPropertyConverter(TypeLiteral.of(MyEnum.class)); + assertTrue(pc instanceof EnumConverter); + assertTrue(manager.isTargetTypeSupported(TypeLiteral.of(MyEnum.class))); + } + + @Test + public void testGetFactoryMethod() throws Exception { + PropertyConverterManager manager = new PropertyConverterManager(false); + Method getFactoryMethod = PropertyConverterManager.class.getDeclaredMethod("getFactoryMethod", new Class[]{Class.class, String[].class}); + getFactoryMethod.setAccessible(true); + + Method foundMethod = (Method) getFactoryMethod.invoke(manager, MyType.class, new String[]{"instanceOf"}); + assertEquals("instanceOf", foundMethod.getName()); + + Method staticOf = (Method) getFactoryMethod.invoke(manager, MyType.class, new String[]{"of"}); + assertEquals("of", staticOf.getName()); + + Method notFoundMethod = (Method) getFactoryMethod.invoke(manager, MyType.class, new String[]{"missingMethod"}); + assertNull(notFoundMethod); + + Method wrongSignature = (Method) getFactoryMethod.invoke(manager, MyType.class, new String[]{"getValue"}); + assertNull(wrongSignature); + } + + @Test public void testMapBoxedType() throws Exception { PropertyConverterManager manager = new PropertyConverterManager(false); @@ -199,6 +251,14 @@ public class PropertyConverterManagerTest { return typeValue; } + public String instanceOf(String input) { + return input; + } + + } + + private enum MyEnum { + A, B, C } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/ReflectionUtilTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/ReflectionUtilTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/ReflectionUtilTest.java new file mode 100644 index 0000000..6f65d1a --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/ReflectionUtilTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.spisupport; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author William.Lieurance 2018-02-11 + */ +public class ReflectionUtilTest<E> { + + private final List<E> reflectable = new ArrayList<>(); + private final Multi<Integer> multi = new Multi<>(); + + /** + * Test of getParametrizedType method, of class ReflectionUtil. + * @throws java.lang.Exception + */ + @Test + public void testGetParametrizedType() throws Exception { + Field stringListField = this.getClass().getDeclaredField("reflectable"); + ParameterizedType genericListType = (ParameterizedType) stringListField.getGenericType(); + + assertEquals(genericListType.toString(), ReflectionUtil.getParametrizedType(reflectable.getClass()).toString()); + assertEquals(First.class.getName(), ReflectionUtil.getParametrizedType(multi.getClass()).getRawType().getTypeName()); + assertNull(ReflectionUtil.getParametrizedType(Object.class)); + } + + private interface First<T> {} + private interface Second<T> {} + private class Multi<T> implements First<T>, Second<T> {}; + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java index 2822cb6..dbc257e 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java @@ -58,6 +58,21 @@ public class RegexPropertyFilterTest { assertNotNull(filter.filterProperty(prop1, new FilterContext(prop1, map, configContext))); assertNull(filter.filterProperty(prop2, new FilterContext(prop2, map, configContext))); assertNotNull(filter.filterProperty(prop3, new FilterContext(prop3, map, configContext))); + filter = new RegexPropertyFilter(); + filter.setExcludes("test1.*"); + assertNull(filter.filterProperty(prop1, new FilterContext(prop1, map, configContext))); + assertEquals(filter.filterProperty(prop2, new FilterContext(prop2, map, configContext)), + prop2); + assertNull(filter.filterProperty(prop3, new FilterContext(prop3, map, configContext))); + + filter = new RegexPropertyFilter(); + filter.setIncludes("test1.*"); //Includes overrides Excludes + filter.setExcludes("test1.*"); + assertNotNull(filter.filterProperty(prop1, new FilterContext(prop1, map, configContext))); + assertNull(filter.filterProperty(prop2, new FilterContext(prop2, map, configContext))); + assertNotNull(filter.filterProperty(prop3, new FilterContext(prop3, map, configContext))); + + } @org.junit.Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/BasePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/BasePropertySourceTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/BasePropertySourceTest.java index eb549a4..971dd1c 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/BasePropertySourceTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/BasePropertySourceTest.java @@ -27,29 +27,20 @@ import org.junit.Test; import java.util.*; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.*; public class BasePropertySourceTest { @Test public void isAlwaysScanable() { - BasePropertySource bs = new BasePropertySource() { - @Override - public Map<String, PropertyValue> getProperties() { - return Collections.emptyMap(); - } - }; + BasePropertySource bs = new EmptyPropertySource(); assertThat(bs.isScannable()).isTrue(); } @Test public void givenOrdinalOverwritesGivenDefaulOrdinal() { - BasePropertySource bs = new BasePropertySource() { - @Override - public Map<String, PropertyValue> getProperties() { - return Collections.emptyMap(); - } - }; + BasePropertySource bs = new EmptyPropertySource(); bs.setDefaultOrdinal(10); @@ -94,6 +85,33 @@ public class BasePropertySourceTest { Assert.assertEquals(1000, new OverriddenOrdinalPropertySource().getOrdinal()); } + @Test + public void testEqualsAndHashAndToStringValues() { + BasePropertySource bs1 = new EmptyPropertySource(); + bs1.setName("testEqualsName"); + BasePropertySource bs2 = new EmptyPropertySource(); + bs2.setName("testEqualsName"); + BasePropertySource bs3 = new EmptyPropertySource(); + bs3.setName("testNotEqualsName"); + + assertEquals(bs1, bs1); + assertNotEquals(null, bs1); + assertNotEquals("aString", bs1); + assertEquals(bs1, bs2); + assertNotEquals(bs1, bs3); + assertEquals(bs1.hashCode(), bs2.hashCode()); + assertNotEquals(bs1.hashCode(), bs3.hashCode()); + assertTrue(bs1.toStringValues().contains("name='testEqualsName'")); + } + + private class EmptyPropertySource extends BasePropertySource { + + @Override + public Map<String, PropertyValue> getProperties() { + return Collections.emptyMap(); + } + } + private static class OverriddenOrdinalPropertySource extends BasePropertySource { private OverriddenOrdinalPropertySource() { @@ -107,7 +125,7 @@ public class BasePropertySourceTest { @Override public Map<String, PropertyValue> getProperties() { - Map<String,PropertyValue> result = new HashMap<>(1); + Map<String, PropertyValue> result = new HashMap<>(1); result.put(PropertySource.TAMAYA_ORDINAL, PropertyValue.of(PropertySource.TAMAYA_ORDINAL, "1000", getName())); return result; } @@ -126,11 +144,10 @@ public class BasePropertySourceTest { @Override public Map<String, PropertyValue> getProperties() { - Map<String,PropertyValue> result = new HashMap<>(1); + Map<String, PropertyValue> result = new HashMap<>(1); result.put(PropertySource.TAMAYA_ORDINAL, PropertyValue.of(PropertySource.TAMAYA_ORDINAL, "invalid", getName())); return result; } } - } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/CLIPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/CLIPropertySourceTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/CLIPropertySourceTest.java index 4507772..7f0b1d2 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/CLIPropertySourceTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/CLIPropertySourceTest.java @@ -18,7 +18,8 @@ */ package org.apache.tamaya.spisupport.propertysource; -import org.apache.tamaya.spisupport.propertysource.CLIPropertySource; +import java.io.StringReader; +import java.io.StringWriter; import org.junit.Test; import static org.junit.Assert.*; @@ -30,30 +31,59 @@ public class CLIPropertySourceTest { @Test public void setCLIProps() throws Exception { - System.clearProperty("main.args"); - CLIPropertySource ps = new CLIPropertySource(); - assertTrue(ps.getProperties().isEmpty()); - CLIPropertySource.initMainArgs("-a", "b"); - assertFalse(ps.getProperties().isEmpty()); - assertEquals(ps.getProperties().get("a").getValue(), "b"); - CLIPropertySource.initMainArgs("--c"); - assertFalse(ps.getProperties().isEmpty()); - assertEquals(ps.getProperties().get("c").getValue(), "c"); - CLIPropertySource.initMainArgs("sss"); - assertFalse(ps.getProperties().isEmpty()); - assertEquals(ps.getProperties().get("sss").getValue(), "sss"); - CLIPropertySource.initMainArgs("-a", "b", "--c", "sss", "--val=vvv"); - assertFalse(ps.getProperties().isEmpty()); - assertEquals(ps.getProperties().get("a").getValue(), "b"); - assertEquals(ps.getProperties().get("c").getValue(), "c"); - assertEquals(ps.getProperties().get("sss").getValue(), "sss"); - // getProperties() throws Exception { - System.setProperty("main.args", "-a b\t--c sss "); - ps = new CLIPropertySource(); - assertFalse(ps.getProperties().isEmpty()); - System.clearProperty("main.args"); - assertEquals(ps.getProperties().get("a").getValue(), "b"); - assertEquals(ps.getProperties().get("c").getValue(), "c"); - assertEquals(ps.getProperties().get("sss").getValue(), "sss"); + StringWriter stringBufferWriter = new StringWriter(); + System.getProperties().store(stringBufferWriter, null); + String before = stringBufferWriter.toString(); + + try { + System.clearProperty("main.args"); + + CLIPropertySource ps = new CLIPropertySource(); + assertTrue(ps.getProperties().isEmpty()); + + ps = new CLIPropertySource(26); + assertTrue(ps.getProperties().isEmpty()); + assertEquals(26, ps.getOrdinal()); + + ps = new CLIPropertySource("-a", "b"); + assertFalse(ps.getProperties().isEmpty()); + assertEquals(ps.getProperties().get("a").getValue(), "b"); + assertTrue(ps.toStringValues().contains("args=[-a, b]")); + + ps = new CLIPropertySource(16, "-c", "d"); + assertFalse(ps.getProperties().isEmpty()); + assertEquals(ps.getProperties().get("c").getValue(), "d"); + assertEquals(16, ps.getOrdinal()); + + CLIPropertySource.initMainArgs("-e", "f"); + assertFalse(ps.getProperties().isEmpty()); + assertEquals(ps.getProperties().get("e").getValue(), "f"); + + CLIPropertySource.initMainArgs("--g"); + assertFalse(ps.getProperties().isEmpty()); + assertEquals(ps.getProperties().get("g").getValue(), "g"); + + CLIPropertySource.initMainArgs("sss"); + assertFalse(ps.getProperties().isEmpty()); + assertEquals(ps.getProperties().get("sss").getValue(), "sss"); + + CLIPropertySource.initMainArgs("-a", "b", "--c", "sss", "--val=vvv"); + assertFalse(ps.getProperties().isEmpty()); + assertEquals(ps.getProperties().get("a").getValue(), "b"); + assertEquals(ps.getProperties().get("c").getValue(), "c"); + assertEquals(ps.getProperties().get("sss").getValue(), "sss"); + + System.setProperty("main.args", "-a b\t--c sss "); + ps = new CLIPropertySource(); + assertFalse(ps.getProperties().isEmpty()); + System.clearProperty("main.args"); + assertEquals(ps.getProperties().get("a").getValue(), "b"); + assertEquals(ps.getProperties().get("c").getValue(), "c"); + assertEquals(ps.getProperties().get("sss").getValue(), "sss"); + + } finally { + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + } } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySourceTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySourceTest.java index 1e6c958..3e57d16 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySourceTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/EnvironmentPropertySourceTest.java @@ -18,14 +18,16 @@ */ package org.apache.tamaya.spisupport.propertysource; -import org.apache.tamaya.spisupport.propertysource.EnvironmentPropertySource; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; import org.apache.tamaya.spi.PropertyValue; import org.junit.Test; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; +import org.junit.Ignore; /** * Tests for {@link EnvironmentPropertySource}. @@ -35,8 +37,58 @@ public class EnvironmentPropertySourceTest { private final EnvironmentPropertySource envPropertySource = new EnvironmentPropertySource(); @Test + public void testConstructionPropertiesAndDisabledBehavior() throws IOException { + EnvironmentPropertySource localEnvironmentPropertySource; + StringWriter stringBufferWriter = new StringWriter(); + System.getProperties().store(stringBufferWriter, null); + String before = stringBufferWriter.toString(); + + try { + assertFalse(envPropertySource.isDisabled()); + + System.setProperty("tamaya.envprops.prefix", "fakeprefix"); + System.setProperty("tamaya.envprops.disable", "true"); + localEnvironmentPropertySource = new EnvironmentPropertySource(); + //assertEquals("fakeprefix", environmentSource.getPrefix()); + assertTrue(localEnvironmentPropertySource.isDisabled()); + assertNull(localEnvironmentPropertySource.get(System.getenv().entrySet().iterator().next().getKey())); + assertTrue(localEnvironmentPropertySource.getName().contains("(disabled)")); + assertTrue(localEnvironmentPropertySource.getProperties().isEmpty()); + assertTrue(localEnvironmentPropertySource.toString().contains("disabled=true")); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.defaults.disable", "true"); + localEnvironmentPropertySource = new EnvironmentPropertySource(); + assertTrue(localEnvironmentPropertySource.isDisabled()); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.envprops.disable", ""); + localEnvironmentPropertySource = new EnvironmentPropertySource(); + assertFalse(localEnvironmentPropertySource.isDisabled()); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.defaults.disable", ""); + localEnvironmentPropertySource = new EnvironmentPropertySource(); + assertFalse(localEnvironmentPropertySource.isDisabled()); + + } finally { + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + } + } + + @Test public void testGetOrdinal() throws Exception { assertEquals(EnvironmentPropertySource.DEFAULT_ORDINAL, envPropertySource.getOrdinal()); + EnvironmentPropertySource constructorSetOrdinal22 = new EnvironmentPropertySource(22); + assertEquals(22, constructorSetOrdinal22.getOrdinal()); + + EnvironmentPropertySource constructorSetOrdinal16 = new EnvironmentPropertySource("sixteenprefix", 16); + assertEquals(16, constructorSetOrdinal16.getOrdinal()); + } @Test @@ -50,19 +102,48 @@ public class EnvironmentPropertySourceTest { assertEquals(envPropertySource.get(envEntry.getKey()).getValue(), envEntry.getValue()); } } + + @Ignore + @Test + public void testPrefixedGet() throws Exception { + EnvironmentPropertySource localEnvironmentPropertySource = new EnvironmentPropertySource("fancyprefix"); + localEnvironmentPropertySource.setPropertiesProvider(new MockedSystemPropertiesProvider()); + assertEquals("fancyprefix.somekey.value", localEnvironmentPropertySource.get("somekey").getValue()); + } @Test public void testGetProperties() throws Exception { Map<String, PropertyValue> props = envPropertySource.getProperties(); - for(Map.Entry<String,PropertyValue> en: props.entrySet()){ - if(!en.getKey().startsWith("_")){ + for (Map.Entry<String, PropertyValue> en : props.entrySet()) { + if (!en.getKey().startsWith("_")) { assertEquals(System.getenv(en.getKey()), en.getValue().getValue()); } } } @Test + public void testPrefixedGetProperties() throws Exception { + EnvironmentPropertySource localEnvironmentPropertySource = new EnvironmentPropertySource("someprefix"); + Map<String, PropertyValue> props = localEnvironmentPropertySource.getProperties(); + for (Map.Entry<String, PropertyValue> en : props.entrySet()) { + assertTrue(en.getKey().startsWith("someprefix")); + String thisKey = en.getKey().replaceFirst("someprefix", ""); + if (!thisKey.startsWith("_")) { + assertEquals(System.getenv(thisKey), en.getValue().getValue()); + } + } + } + + @Test public void testIsScannable() throws Exception { assertTrue(envPropertySource.isScannable()); } -} \ No newline at end of file + + private class MockedSystemPropertiesProvider extends EnvironmentPropertySource.SystemPropertiesProvider { + @Override + String getenv(String key) { + System.out.println("Called with key " + key); + return key + ".value"; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationProviderTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationProviderTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationProviderTest.java index 0e363b9..18c9df5 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationProviderTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/JavaConfigurationProviderTest.java @@ -18,12 +18,17 @@ */ package org.apache.tamaya.spisupport.propertysource; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; import org.apache.tamaya.spi.PropertySource; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; -import static org.apache.tamaya.ConfigurationProvider.getConfiguration; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class JavaConfigurationProviderTest { @@ -43,4 +48,44 @@ public class JavaConfigurationProviderTest { } } + + @Test + public void testConstructionPropertiesAndDisabledBehavior() throws IOException { + JavaConfigurationPropertySource localJavaConfigurationPropertySource = new JavaConfigurationPropertySource(); + StringWriter stringBufferWriter = new StringWriter(); + System.getProperties().store(stringBufferWriter, null); + String before = stringBufferWriter.toString(); + + try { + assertTrue(localJavaConfigurationPropertySource.isEnabled()); + + System.setProperty("tamaya.defaultprops.disable", "true"); + localJavaConfigurationPropertySource = new JavaConfigurationPropertySource(); + assertFalse(localJavaConfigurationPropertySource.isEnabled()); + assertTrue(localJavaConfigurationPropertySource.getProperties().isEmpty()); + assertTrue(localJavaConfigurationPropertySource.toString().contains("enabled=false")); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.defaults.disable", "true"); + localJavaConfigurationPropertySource = new JavaConfigurationPropertySource(); + assertFalse(localJavaConfigurationPropertySource.isEnabled()); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.defaultprops.disable", ""); + localJavaConfigurationPropertySource = new JavaConfigurationPropertySource(); + assertTrue(localJavaConfigurationPropertySource.isEnabled()); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.defaults.disable", ""); + localJavaConfigurationPropertySource = new JavaConfigurationPropertySource(); + assertTrue(localJavaConfigurationPropertySource.isEnabled()); + + } finally { + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + } + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySourceTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySourceTest.java new file mode 100644 index 0000000..5775768 --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/PropertiesResourcePropertySourceTest.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.spisupport.propertysource; + +import java.net.URL; +import org.junit.Test; +import static org.junit.Assert.*; + + +/** + * + * @author William.Lieurance 2018.02.17 + */ +public class PropertiesResourcePropertySourceTest { + + private final String testFileName = "testfile.properties"; + private final URL resource = getClass().getResource("/" + testFileName); + + @Test + public void testBasicConstructor() { + PropertiesResourcePropertySource source = new PropertiesResourcePropertySource(resource); + assertNotNull(source); + assertTrue(5 == source.getProperties().size()); // double the size for .source values. + assertTrue(source.getProperties().containsKey("key1")); + } + + @Test + public void testPrefixedConstructor() { + PropertiesResourcePropertySource source = new PropertiesResourcePropertySource(resource, "somePrefix"); + assertNotNull(source); + assertTrue(5 == source.getProperties().size()); + assertTrue(source.getProperties().containsKey("somePrefixkey1")); + } + + @Test + public void testPrefixedPathConstructor() { + //File path must be relative to classloader, not the class + System.out.println(resource.getPath()); + PropertiesResourcePropertySource source = new PropertiesResourcePropertySource(testFileName, "somePrefix"); + assertNotNull(source); + assertTrue(5 == source.getProperties().size()); + assertTrue(source.getProperties().containsKey("somePrefixkey1")); + } + + @Test + public void testPrefixedPathBadClassloaderConstructor() { + ClassLoader badLoader = new ClassLoader() { + @Override + public URL getResource(String name){ + return null; + } + }; + PropertiesResourcePropertySource source = new PropertiesResourcePropertySource(testFileName, "somePrefix", badLoader); + assertNotNull(source); + assertTrue(source.getProperties().isEmpty()); + } + + @Test + public void testPrefixedPathClassloaderConstructor() { + PropertiesResourcePropertySource source = new PropertiesResourcePropertySource(testFileName, "somePrefix", null); + assertNotNull(source); + assertTrue(5 == source.getProperties().size()); + assertTrue(source.getProperties().containsKey("somePrefixkey1")); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySourceTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySourceTest.java index 288b8cd..5ceb332 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySourceTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SimplePropertySourceTest.java @@ -18,11 +18,15 @@ */ package org.apache.tamaya.spisupport.propertysource; +import java.io.File; import org.apache.tamaya.ConfigException; import org.apache.tamaya.spi.PropertyValue; import org.junit.Test; import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.endsWith; @@ -31,8 +35,13 @@ import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.aMapWithSize; import static org.hamcrest.Matchers.hasEntry; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; public class SimplePropertySourceTest { + @Test public void successfulCreationWithPropertiesFromXMLPropertiesFile() { URL resource = getClass().getResource("/valid-properties.xml"); @@ -58,7 +67,7 @@ public class SimplePropertySourceTest { } assertThat(catchedException.getMessage(), allOf(startsWith("Error loading properties from"), - endsWith("non-xml-properties.xml"))); + endsWith("non-xml-properties.xml"))); } @Test @@ -73,10 +82,9 @@ public class SimplePropertySourceTest { } assertThat(catchedException.getMessage(), allOf(startsWith("Error loading properties from"), - endsWith("invalid-properties.xml"))); + endsWith("invalid-properties.xml"))); } - @Test public void successfulCreationWithPropertiesFromSimplePropertiesFile() { URL resource = getClass().getResource("/testfile.properties"); @@ -86,4 +94,99 @@ public class SimplePropertySourceTest { assertThat(source, notNullValue()); assertThat(source.getProperties(), aMapWithSize(5)); // double the size for .source values. } + + @Test + public void testWithMap() { + Map<String, String> propertyFirst = new HashMap<>(); + propertyFirst.put("firstKey", "firstValue"); + + SimplePropertySource source = new SimplePropertySource("testWithMap", propertyFirst, 166); + assertEquals("testWithMap", source.getName()); + assertEquals(166, source.getDefaultOrdinal()); + assertTrue(source.getProperties().containsKey("firstKey")); + + } + + @Test + public void builder() throws Exception { + assertNotNull(SimplePropertySource.newBuilder()); + assertNotEquals(SimplePropertySource.newBuilder(), SimplePropertySource.newBuilder()); + } + + @Test + public void getOrdinal() throws Exception { + SimplePropertySource ps1 = SimplePropertySource.newBuilder() + .withUuidName() + .withOrdinal(55) + .withDefaultOrdinal(166) + .build(); + + assertEquals(55, ps1.getOrdinal()); + assertEquals(166, ps1.getDefaultOrdinal()); + } + + @Test + public void getName() throws Exception { + //SimplePropertySource ps1 = SimplePropertySource.newBuilder() + // .withName("test1") + // .build(); + //assertEquals("test1", ps1.getName()); + SimplePropertySource ps1 = SimplePropertySource.newBuilder() + .withUuidName().build(); + assertNotNull(UUID.fromString(ps1.getName())); + } + + @Test + public void get() throws Exception { + SimplePropertySource ps1 = SimplePropertySource.newBuilder() + .withUuidName() + .withProperty("a", "b").build(); + assertEquals("b", ps1.get("a").getValue()); + } + + @Test + public void getProperties() throws Exception { + SimplePropertySource ps1 = SimplePropertySource.newBuilder() + .withUuidName() + .withProperty("a", "b") + .build(); + assertNotNull(ps1.getProperties()); + assertEquals(1, ps1.getProperties().size()); + assertEquals("b", ps1.getProperties().get("a").getValue()); + } + + @Test + public void testScannable() { + SimplePropertySource sps = SimplePropertySource.newBuilder().withUuidName().build(); + assertTrue(sps.isScannable()); + } + + @Test + public void testBuilderWithMaps() { + URL resource = getClass().getResource("/valid-properties.xml"); + File resourceAsFile = new File(resource.getPath()); + + Map<String, String> propertyFirst = new HashMap<>(); + propertyFirst.put("firstKey", "firstValue"); + + SimplePropertySource sps = SimplePropertySource.newBuilder() + .withUuidName() + .withProperties(propertyFirst) + .withProperties(resource) + .build(); + + assertEquals("firstValue", sps.get("firstKey").getValue()); + assertThat(sps.getProperties(), hasEntry("a", PropertyValue.of("a", "b", resource.toString()))); + assertThat(sps.getProperties(), hasEntry("b", PropertyValue.of("b", "1", resource.toString()))); + + sps = SimplePropertySource.newBuilder() + .withUuidName() + .withProperties(propertyFirst) + .withProperties(resourceAsFile) + .build(); + + assertEquals("firstValue", sps.get("firstKey").getValue()); + assertThat(sps.getProperties(), hasEntry("a", PropertyValue.of("a", "b", resource.toString()))); + assertThat(sps.getProperties(), hasEntry("b", PropertyValue.of("b", "1", resource.toString()))); + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySourceTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySourceTest.java index e67630c..acac8ea 100644 --- a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySourceTest.java +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/SystemPropertySourceTest.java @@ -18,6 +18,9 @@ */ package org.apache.tamaya.spisupport.propertysource; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; import org.junit.Assert; @@ -25,11 +28,58 @@ import org.junit.Test; import java.util.Map; import java.util.Properties; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class SystemPropertySourceTest { private final SystemPropertySource testPropertySource = new SystemPropertySource(); + @Test + public void testConstructionPropertiesAndDisabledBehavior() throws IOException { + SystemPropertySource localSystemPropertySource; + StringWriter stringBufferWriter = new StringWriter(); + System.getProperties().store(stringBufferWriter, null); + String before = stringBufferWriter.toString(); + + try { + assertFalse(testPropertySource.toStringValues().contains("disabled=true")); + + System.setProperty("tamaya.sysprops.prefix", "fakeprefix"); + System.setProperty("tamaya.sysprops.disable", "true"); + localSystemPropertySource = new SystemPropertySource(); + //assertEquals("fakeprefix", localSystemPropertySource.getPrefix()); + assertTrue(localSystemPropertySource.toStringValues().contains("disabled=true")); + assertNull(localSystemPropertySource.get(System.getenv().entrySet().iterator().next().getKey())); + assertTrue(localSystemPropertySource.getName().contains("(disabled)")); + assertTrue(localSystemPropertySource.getProperties().isEmpty()); + assertTrue(localSystemPropertySource.toString().contains("disabled=true")); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.defaults.disable", "true"); + localSystemPropertySource = new SystemPropertySource(); + assertTrue(localSystemPropertySource.toStringValues().contains("disabled=true")); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.sysprops.disable", ""); + localSystemPropertySource = new SystemPropertySource(); + assertFalse(localSystemPropertySource.toStringValues().contains("disabled=true")); + + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + System.setProperty("tamaya.defaults.disable", ""); + localSystemPropertySource = new SystemPropertySource(); + assertFalse(localSystemPropertySource.toStringValues().contains("disabled=true")); + + } finally { + System.getProperties().clear(); + System.getProperties().load(new StringReader(before)); + } + } @Test public void testGetOrdinal() throws Exception { @@ -44,6 +94,17 @@ public class SystemPropertySourceTest { // reset it to not destroy other tests!! System.clearProperty(PropertySource.TAMAYA_ORDINAL); + + SystemPropertySource constructorSetOrdinal22 = new SystemPropertySource(22); + assertEquals(22, constructorSetOrdinal22.getOrdinal()); + + SystemPropertySource constructorSetOrdinal16 = new SystemPropertySource("sixteenprefix", 16); + assertEquals(16, constructorSetOrdinal16.getOrdinal()); + } + + @Test + public void testIsScannable() throws Exception { + assertTrue(testPropertySource.isScannable()); } @Test @@ -56,8 +117,8 @@ public class SystemPropertySourceTest { String propertyKeyToCheck = System.getProperties().stringPropertyNames().iterator().next(); PropertyValue property = testPropertySource.get(propertyKeyToCheck); - Assert.assertNotNull("Property '" + propertyKeyToCheck + "' is not present in " + - SystemPropertySource.class.getSimpleName(), property); + Assert.assertNotNull("Property '" + propertyKeyToCheck + "' is not present in " + + SystemPropertySource.class.getSimpleName(), property); Assert.assertEquals(System.getProperty(propertyKeyToCheck), property.getValue()); } @@ -74,18 +135,18 @@ public class SystemPropertySourceTest { System.clearProperty("test"); } - private void checkWithSystemProperties(Map<String,PropertyValue> toCheck) { + private void checkWithSystemProperties(Map<String, PropertyValue> toCheck) { Properties systemEntries = System.getProperties(); int num = 0; for (PropertyValue propertySourceEntry : toCheck.values()) { - if(propertySourceEntry.getKey().startsWith("_")){ + if (propertySourceEntry.getKey().startsWith("_")) { continue; // meta entry } - num ++; + num++; Assert.assertEquals("Entry values for key '" + propertySourceEntry.getKey() + "' do not match", - systemEntries.getProperty(propertySourceEntry.getKey()), propertySourceEntry.getValue()); + systemEntries.getProperty(propertySourceEntry.getKey()), propertySourceEntry.getValue()); } Assert.assertEquals("size of System.getProperties().entrySet() must be the same as SystemPropertySrouce.getProperties().entrySet()", systemEntries.size(), num); } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/WrappedPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/WrappedPropertySourceTest.java b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/WrappedPropertySourceTest.java new file mode 100644 index 0000000..5b46ee7 --- /dev/null +++ b/code/spi-support/src/test/java/org/apache/tamaya/spisupport/propertysource/WrappedPropertySourceTest.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.spisupport.propertysource; + +import java.util.HashMap; +import java.util.Map; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author William.Lieurance 2018.02.17 + */ +public class WrappedPropertySourceTest { + + /** + * Test of of method, of class WrappedPropertySource. + */ + @Test + public void testOf_PropertySource() { + WrappedPropertySource instance = WrappedPropertySource.of(new MockedWrappablePropertySource()); + assertEquals("MockedWrappablePropertySource", instance.getName()); + WrappedPropertySource instance2 = WrappedPropertySource.of(instance); + assertEquals("MockedWrappablePropertySource", instance2.getName()); + } + + /** + * Test of getOrdinal method, of class WrappedPropertySource. + */ + @Test + public void testOrdinalsAndOrdinalConstructors() { + WrappedPropertySource instance = WrappedPropertySource.of(new MockedWrappablePropertySource(), null); + assertEquals(10, instance.getOrdinal()); + instance.setOrdinal(20); + assertEquals(20, instance.getOrdinal()); + + WrappedPropertySource instance2 = WrappedPropertySource.of(instance, null); + assertEquals(10, instance2.getOrdinal()); + } + + /** + * Test of setDelegate method, of class WrappedPropertySource. + */ + @Test + public void testGetSetDelegate() { + MockedWrappablePropertySource first = new MockedWrappablePropertySource(); + first.setName("first"); + MockedWrappablePropertySource second = new MockedWrappablePropertySource(); + first.setName("second"); + + WrappedPropertySource instance = WrappedPropertySource.of(first); + assertEquals(first, instance.getDelegate()); + instance.setDelegate(second); + assertEquals(second, instance.getDelegate()); + + } + + + /** + * Test of get method, of class WrappedPropertySource. + */ + @Test + public void testGet() { + WrappedPropertySource instance = WrappedPropertySource.of(new MockedWrappablePropertySource()); + PropertyValue result = instance.get("thisKey"); + assertEquals("valueFromMockedWrappablePropertySource", result.getValue()); + } + + /** + * Test of getProperties method, of class WrappedPropertySource. + */ + @Test + public void testGetProperties() { + WrappedPropertySource instance = WrappedPropertySource.of(new MockedWrappablePropertySource()); + Map<String, PropertyValue> result = instance.getProperties(); + assertTrue(result.containsKey("someKey")); + assertEquals(1, result.size()); + } + + /** + * Test of isScannable method, of class WrappedPropertySource. + */ + @Test + public void testIsScannable() { + WrappedPropertySource instance = WrappedPropertySource.of(new MockedWrappablePropertySource()); + assertTrue(instance.isScannable()); + } + + @Test + public void testEqualsAndHashAndToStringValues() { + MockedWrappablePropertySource source1 = new MockedWrappablePropertySource(); + source1.setName("testEqualsName"); + MockedWrappablePropertySource source2 = new MockedWrappablePropertySource(); + source2.setName("testEqualsName"); + MockedWrappablePropertySource source3 = new MockedWrappablePropertySource(); + source3.setName("testNotEqualsName"); + + WrappedPropertySource wps1 = WrappedPropertySource.of(source1); + WrappedPropertySource wps2 = WrappedPropertySource.of(source2); + WrappedPropertySource wps3 = WrappedPropertySource.of(source3); + + assertEquals(wps1, wps1); + assertNotEquals(null, wps1); + assertNotEquals(source1, wps1); + assertNotEquals(wps1, source1); + assertNotEquals("aString", wps1); + assertEquals(wps1, wps2); + assertNotEquals(wps1, wps3); + assertEquals(wps1.hashCode(), wps2.hashCode()); + assertNotEquals(wps1.hashCode(), wps3.hashCode()); + assertTrue(wps1.toString().contains("name=testEqualsName")); + } + + private class MockedWrappablePropertySource implements PropertySource{ + + private String name = "MockedWrappablePropertySource"; + + @Override + public int getOrdinal() { + return 10; + } + + @Override + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.of(key, "valueFromMockedWrappablePropertySource", "MockedWrappablePropertySource"); + } + + @Override + public Map<String, PropertyValue> getProperties() { + Map<String, PropertyValue> returnable = new HashMap<>(); + returnable.put("someKey", this.get("someKey")); + return returnable; + } + + @Override + public boolean isScannable(){ + return true; + } + + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter index 7c62bb2..65a3dfe 100644 --- a/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter +++ b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter @@ -16,4 +16,5 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.spisupport.CTestConverter \ No newline at end of file +org.apache.tamaya.spisupport.CTestConverter +org.apache.tamaya.spisupport.IntegerTestConverter http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter new file mode 100644 index 0000000..2e5456d --- /dev/null +++ b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter @@ -0,0 +1,19 @@ +# +# 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 current the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +org.apache.tamaya.spisupport.MockedPropertyFilter http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/125eed20/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource new file mode 100644 index 0000000..06799dd --- /dev/null +++ b/code/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -0,0 +1,19 @@ +# +# 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 current the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +org.apache.tamaya.spisupport.MockedPropertySource
