http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigSourceTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigSourceTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigSourceTest.java new file mode 100644 index 0000000..beb2390 --- /dev/null +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigSourceTest.java @@ -0,0 +1,216 @@ +/* + * 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.functions; + +import org.junit.Test; + +import javax.config.spi.ConfigSource; +import java.util.HashMap; +import java.util.Map; + +import static java.util.Collections.EMPTY_MAP; +import static org.apache.tamaya.functions.MethodNotMockedAnswer.NOT_MOCKED_ANSWER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +public class EnrichedConfigSourceTest { + + /* + * Tests for getName() + */ + + @Test + public void getNameReturnsTheNameOfTheBaseConfiguration() { + ConfigSource propertySource = mock(ConfigSource.class, NOT_MOCKED_ANSWER); + doReturn("abc").when(propertySource).getName(); + + EnrichedConfigSource sut = new EnrichedConfigSource(propertySource, EMPTY_MAP, false); + + String name = sut.getName(); + + assertThat(name).isEqualTo("abc"); + } + + + /* + * Tests for getOrdinal() + */ + + @Test + public void getOrdinalReturnsTheValueOfTheBaseConfiguration() { + ConfigSource propertySource = mock(ConfigSource.class, NOT_MOCKED_ANSWER); + doReturn(13).when(propertySource).getOrdinal(); + + EnrichedConfigSource sut = new EnrichedConfigSource(propertySource, EMPTY_MAP, false); + + int ordinal = sut.getOrdinal(); + + assertThat(ordinal).isEqualTo(13); + } + + /* + * Tests for EnrichedConfigSource(ConfigSource, Map<String, String>, boolean) + */ + + /* + * Tests for get(String) + */ + + @Test + public void getReturnsAdditional() { + InMemoryConfigSource base = new InMemoryConfigSource(); + + base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); + + Map<String, String> additions = new HashMap<>(); + additions.put("e", "9"); + additions.put("f", "11"); + + EnrichedConfigSource sut = new EnrichedConfigSource(base, additions, false); + + assertThat(sut.getValue("e")).isNotNull().isNotNull().isEqualTo("9"); + } + + @Test + public void getReturnsOverriddenValue() { + InMemoryConfigSource base = new InMemoryConfigSource(); + + base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); + + Map<String, String> additions = new HashMap<>(); + additions.put("b", "9"); + additions.put("d", "11"); + + EnrichedConfigSource sut = new EnrichedConfigSource(base, additions, true); + + String result = sut.getValue("b"); + + assertThat(result).isNotNull().isEqualTo("9"); + } + + @Test + public void getReturnsGivenValueWhichIsNotOverridden() { + InMemoryConfigSource base = new InMemoryConfigSource(); + + base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); + + Map<String, String> additions = new HashMap<>(); + additions.put("b", "9"); + additions.put("d", "11"); + + EnrichedConfigSource sut = new EnrichedConfigSource(base, additions, true); + + String result = sut.getValue("a"); + + assertThat(result).isNotNull().isEqualTo("1"); + } + + @Test + public void getPropertiesReturnsNotOverriddenValue() { + InMemoryConfigSource base = new InMemoryConfigSource(); + + base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); + + Map<String, String> additions = new HashMap<>(); + additions.put("b", "9"); + additions.put("d", "11"); + + EnrichedConfigSource sut = new EnrichedConfigSource(base, additions, false); + + String result = sut.getValue("b"); + + assertThat(result).isNotNull().isEqualTo("2"); + } + + + /* + * Tests for getProperties() + */ + + @Test + public void getPropertiesReturnsAllAdditionalToo() { + InMemoryConfigSource base = new InMemoryConfigSource(); + + base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); + + Map<String, String> additions = new HashMap<>(); + additions.put("e", "9"); + additions.put("f", "11"); + + EnrichedConfigSource sut = new EnrichedConfigSource(base, additions, false); + + Map<String, String> properties = sut.getProperties(); + + assertThat(properties).isNotNull().isNotEmpty() + .containsEntry("a", "1") + .containsEntry("b", "2") + .containsEntry("c", "3") + .containsEntry("d", "4") + .containsEntry("e", "9") + .containsEntry("f", "11") + .hasSize(6); + } + + @Test + public void getPropertiesReturnsAllWithOverriddenValues() { + InMemoryConfigSource base = new InMemoryConfigSource(); + + base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); + + Map<String, String> additions = new HashMap<>(); + additions.put("b", "9"); + additions.put("d", "11"); + + EnrichedConfigSource sut = new EnrichedConfigSource(base, additions, true); + + Map<String, String> properties = sut.getProperties(); + + assertThat(properties).isNotNull().isNotEmpty() + .containsEntry("a", "1") + .containsEntry("b", "9") + .containsEntry("c", "3") + .containsEntry("d", "11") + .hasSize(4); + + } + + @Test + public void getPropertiesReturnsAllNoOverriddenValues() { + InMemoryConfigSource base = new InMemoryConfigSource(); + + base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); + + Map<String, String> additions = new HashMap<>(); + additions.put("b", "9"); + additions.put("d", "11"); + + EnrichedConfigSource sut = new EnrichedConfigSource(base, additions, false); + + Map<String, String> properties = sut.getProperties(); + + assertThat(properties).isNotNull().isNotEmpty() + .containsEntry("a", "1") + .containsEntry("b", "2") + .containsEntry("c", "3") + .containsEntry("d", "4") + .hasSize(4); + } + +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java index 6134144..5bb3eb4 100644 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java @@ -18,11 +18,7 @@ */ package org.apache.tamaya.functions; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.TypeLiteral; -import org.assertj.core.api.Assertions; import org.assertj.core.api.ThrowableAssert; -import org.junit.Ignore; import org.junit.Test; import javax.config.Config; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedPropertySourceTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedPropertySourceTest.java deleted file mode 100644 index 563a986..0000000 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedPropertySourceTest.java +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.functions; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.junit.Test; - -import javax.management.RuntimeMBeanException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import static java.util.Collections.EMPTY_MAP; -import static org.apache.tamaya.functions.MethodNotMockedAnswer.NOT_MOCKED_ANSWER; -import static org.apache.tamaya.spi.PropertyValue.of; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; - -public class EnrichedPropertySourceTest { - - /* - * Tests for getName() - */ - - @Test - public void getNameReturnsTheNameOfTheBaseConfiguration() { - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn("abc").when(propertySource).getName(); - - EnrichedPropertySource sut = new EnrichedPropertySource(propertySource, EMPTY_MAP, false); - - String name = sut.getName(); - - assertThat(name).isEqualTo("abc"); - } - - - /* - * Tests for getOrdinal() - */ - - @Test - public void getOrdinalReturnsTheValueOfTheBaseConfiguration() { - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn(13).when(propertySource).getOrdinal(); - - EnrichedPropertySource sut = new EnrichedPropertySource(propertySource, EMPTY_MAP, false); - - int ordinal = sut.getOrdinal(); - - assertThat(ordinal).isEqualTo(13); - } - - /* - * Tests for EnrichedPropertySource(PropertySource, Map<String, String>, boolean) - */ - - /* - * Tests for get(String) - */ - - @Test - public void getReturnsAdditional() { - InMemoryPropertySource base = new InMemoryPropertySource(); - - base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); - - Map<String, String> additions = new HashMap<>(); - additions.put("e", "9"); - additions.put("f", "11"); - - EnrichedPropertySource sut = new EnrichedPropertySource(base, additions, false); - - PropertyValue result = sut.get("e"); - - assertThat(result).isNotNull().isNotNull().isEqualTo(of("e", "9", "name")); - } - - @Test - public void getReturnsOverriddenValue() { - InMemoryPropertySource base = new InMemoryPropertySource(); - - base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); - - Map<String, String> additions = new HashMap<>(); - additions.put("b", "9"); - additions.put("d", "11"); - - EnrichedPropertySource sut = new EnrichedPropertySource(base, additions, true); - - PropertyValue result = sut.get("b"); - - assertThat(result).isNotNull().isEqualTo(of("b", "9", "name")); - } - - @Test - public void getReturnsGivenValueWhichIsNotOverridden() { - InMemoryPropertySource base = new InMemoryPropertySource(); - - base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); - - Map<String, String> additions = new HashMap<>(); - additions.put("b", "9"); - additions.put("d", "11"); - - EnrichedPropertySource sut = new EnrichedPropertySource(base, additions, true); - - PropertyValue result = sut.get("a"); - - assertThat(result).isNotNull().isEqualTo(of("a", "1", "name")); - } - - @Test - public void getPropertiesReturnsNotOverriddenValue() { - InMemoryPropertySource base = new InMemoryPropertySource(); - - base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); - - Map<String, String> additions = new HashMap<>(); - additions.put("b", "9"); - additions.put("d", "11"); - - EnrichedPropertySource sut = new EnrichedPropertySource(base, additions, false); - - PropertyValue result = sut.get("b"); - - assertThat(result).isNotNull().isEqualTo(of("b", "2", "name")); - } - - - /* - * Tests for getProperties() - */ - - @Test - public void getPropertiesReturnsAllAdditionalToo() { - InMemoryPropertySource base = new InMemoryPropertySource(); - - base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); - - Map<String, String> additions = new HashMap<>(); - additions.put("e", "9"); - additions.put("f", "11"); - - EnrichedPropertySource sut = new EnrichedPropertySource(base, additions, false); - - Map<String, PropertyValue> properties = sut.getProperties(); - - assertThat(properties).isNotNull().isNotEmpty() - .containsEntry("a", of("a", "1", "name")) - .containsEntry("b", of("b", "2", "name")) - .containsEntry("c", of("c", "3", "name")) - .containsEntry("d", of("d", "4", "name")) - .containsEntry("e", of("e", "9", "name")) - .containsEntry("f", of("f", "11", "name")) - .hasSize(6); - } - - @Test - public void getPropertiesReturnsAllWithOverriddenValues() { - InMemoryPropertySource base = new InMemoryPropertySource(); - - base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); - - Map<String, String> additions = new HashMap<>(); - additions.put("b", "9"); - additions.put("d", "11"); - - EnrichedPropertySource sut = new EnrichedPropertySource(base, additions, true); - - Map<String, PropertyValue> properties = sut.getProperties(); - - assertThat(properties).isNotNull().isNotEmpty() - .containsEntry("a", of("a", "1", "name")) - .containsEntry("b", of("b", "9", "name")) - .containsEntry("c", of("c", "3", "name")) - .containsEntry("d", of("d", "11", "name")) - .hasSize(4); - - } - - @Test - public void getPropertiesReturnsAllNoOverriddenValues() { - InMemoryPropertySource base = new InMemoryPropertySource(); - - base.setName("name").add("a", "1").add("b", "2").add("c", "3").add("d", "4"); - - Map<String, String> additions = new HashMap<>(); - additions.put("b", "9"); - additions.put("d", "11"); - - EnrichedPropertySource sut = new EnrichedPropertySource(base, additions, false); - - Map<String, PropertyValue> properties = sut.getProperties(); - - assertThat(properties).isNotNull().isNotEmpty() - .containsEntry("a", of("a", "1", "name")) - .containsEntry("b", of("b", "2", "name")) - .containsEntry("c", of("c", "3", "name")) - .containsEntry("d", of("d", "4", "name")) - .hasSize(4); - } - - /* - * Tests for isScannable() - */ - - @Test - public void isScannableReturnsTheValueOfTheBaseConfigurationWhichIsTrue() { - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn(true).when(propertySource).isScannable(); - - EnrichedPropertySource sut = new EnrichedPropertySource(propertySource, EMPTY_MAP, false); - - boolean isScannable = sut.isScannable(); - - assertThat(isScannable).isEqualTo(true); - } - - @Test - public void isScannableReturnsTheValueOfTheBaseConfigurationWhichIsFalse() { - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn(false).when(propertySource).isScannable(); - - EnrichedPropertySource sut = new EnrichedPropertySource(propertySource, EMPTY_MAP, false); - - boolean isScannable = sut.isScannable(); - - assertThat(isScannable).isEqualTo(false); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredConfigSourceTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredConfigSourceTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredConfigSourceTest.java new file mode 100644 index 0000000..65e08f5 --- /dev/null +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredConfigSourceTest.java @@ -0,0 +1,188 @@ +/* + * 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.functions; + +import org.junit.Test; + +import javax.config.spi.ConfigSource; + +import static org.apache.tamaya.functions.MethodNotMockedAnswer.NOT_MOCKED_ANSWER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +public class FilteredConfigSourceTest { + + /* + * Tests for getName() + */ + + @Test + public void getNameReturnsTheNameOfTheBaseConfiguration() { + ConfigSource propertySource = mock(ConfigSource.class, NOT_MOCKED_ANSWER); + doReturn("abc").when(propertySource).getName(); + + Predicate<String> filter = new Predicate<String>() { + @Override + public boolean test(String s) { + return false; + } + }; + + FilteredConfigSource sut = new FilteredConfigSource(propertySource, filter); + + String name = sut.getName(); + + assertThat(name).isEqualTo("abc"); + } + + /* + * Tests for getOrdinal() + */ + + @Test + public void getOrdinalReturnsTheValueOfTheBaseConfiguration() { + ConfigSource propertySource = mock(ConfigSource.class, NOT_MOCKED_ANSWER); + doReturn(13).when(propertySource).getOrdinal(); + + Predicate<String> filter = new Predicate<String>() { + @Override + public boolean test(String s) { + return false; + } + }; + + FilteredConfigSource sut = new FilteredConfigSource(propertySource, filter); + + int ordinal = sut.getOrdinal(); + + assertThat(ordinal).isEqualTo(13); + } + + /* + * Tests for get(String) + */ + + @Test + public void getReturnsNullInsteadOfValueBecausOfFilter() { + ConfigSource propertySource = mock(ConfigSource.class, NOT_MOCKED_ANSWER); + doReturn("000").when(propertySource).getValue(eq("abc")); + + Predicate<String> filter = new Predicate<String>() { + @Override + public boolean test(String s) { + return !"abc".equals(s); + } + }; + + FilteredConfigSource sut = new FilteredConfigSource(propertySource, filter); + + String result = sut.getValue("abc"); + + assertThat(result).isNull(); + } + + @Test + public void getReturnsValueBecauseItIsNotFiltered() { + ConfigSource propertySource = mock(ConfigSource.class, NOT_MOCKED_ANSWER); + doReturn("000").when(propertySource).getValue(eq("abc")); + + Predicate<String> filter = new Predicate<String>() { + @Override + public boolean test(String s) { + return true; + } + }; + + FilteredConfigSource sut = new FilteredConfigSource(propertySource, filter); + + String result = sut.getValue("abc"); + + assertThat(result).isNotNull(); + } + + /* + * Tests for getProperties() + */ + + @Test + public void getPropertiesAndFilterRemovesAllProperties() { + InMemoryConfigSource imps = new InMemoryConfigSource(); + imps.add("a", "1").add("b", "2").add("c", "3"); + imps.setName("s"); + + Predicate<String> filter = new Predicate<String>() { + @Override + public boolean test(String s) { + return false; + } + }; + + FilteredConfigSource fps = new FilteredConfigSource(imps, filter); + + assertThat(fps.getProperties()).isEmpty();; + } + + @Test + public void getPropertiesAndFilterRemovesNoProperties() { + InMemoryConfigSource imps = new InMemoryConfigSource(); + imps.add("a", "1").add("b", "2").add("c", "3"); + imps.setName("s"); + + Predicate<String> filter = new Predicate<String>() { + @Override + public boolean test(String s) { + return true; + } + }; + + FilteredConfigSource fps = new FilteredConfigSource(imps, filter); + + assertThat(fps.getProperties()).isNotEmpty() + .containsEntry("a", "1") + .containsEntry("b", "2") + .containsEntry("c","3") + .hasSize(3); + } + + @Test + public void getPropertiesAndFilterRemovesSomeProperties() { + InMemoryConfigSource imps = new InMemoryConfigSource(); + imps.add("a", "1").add("b", "2").add("c", "3"); + imps.setName("s"); + + Predicate<String> filter = new Predicate<String>() { + @Override + public boolean test(String s) { + return !s.startsWith("a"); + } + }; + + FilteredConfigSource fps = new FilteredConfigSource(imps, filter); + + assertThat(fps.getProperties()).isNotEmpty() + .containsEntry("b", "2") + .containsEntry("c", "3") + .hasSize(2); + + } + + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredPropertySourceTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredPropertySourceTest.java deleted file mode 100644 index e86cbc5..0000000 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredPropertySourceTest.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.functions; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.junit.Test; - -import static org.apache.tamaya.functions.MethodNotMockedAnswer.NOT_MOCKED_ANSWER; -import static org.apache.tamaya.spi.PropertyValue.of; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; - -public class FilteredPropertySourceTest { - - /* - * Tests for getName() - */ - - @Test - public void getNameReturnsTheNameOfTheBaseConfiguration() { - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn("abc").when(propertySource).getName(); - - Predicate<String> filter = new Predicate<String>() { - @Override - public boolean test(String s) { - return false; - } - }; - - FilteredPropertySource sut = new FilteredPropertySource(propertySource, filter); - - String name = sut.getName(); - - assertThat(name).isEqualTo("abc"); - } - - /* - * Tests for isScannable() - */ - - @Test - public void isScannableReturnsTheValueOfTheBaseConfiguration() { - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn(true).when(propertySource).isScannable(); - - Predicate<String> filter = new Predicate<String>() { - @Override - public boolean test(String s) { - return false; - } - }; - - FilteredPropertySource sut = new FilteredPropertySource(propertySource, filter); - - boolean isScannable = sut.isScannable(); - - assertThat(isScannable).isEqualTo(true); - } - - /* - * Tests for getOrdinal() - */ - - @Test - public void getOrdinalReturnsTheValueOfTheBaseConfiguration() { - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn(13).when(propertySource).getOrdinal(); - - Predicate<String> filter = new Predicate<String>() { - @Override - public boolean test(String s) { - return false; - } - }; - - FilteredPropertySource sut = new FilteredPropertySource(propertySource, filter); - - int ordinal = sut.getOrdinal(); - - assertThat(ordinal).isEqualTo(13); - } - - /* - * Tests for get(String) - */ - - @Test - public void getReturnsNullInsteadOfValueBecausOfFilter() { - PropertyValue pv = of("abc", "000", "UT"); - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn(pv).when(propertySource).get(eq("abc")); - - Predicate<String> filter = new Predicate<String>() { - @Override - public boolean test(String s) { - return !"abc".equals(s); - } - }; - - FilteredPropertySource sut = new FilteredPropertySource(propertySource, filter); - - PropertyValue result = sut.get("abc"); - - assertThat(result).isNull(); - } - - @Test - public void getReturnsValueBecauseItIsNotFiltered() { - PropertyValue pv = of("abc", "000", "UT"); - PropertySource propertySource = mock(PropertySource.class, NOT_MOCKED_ANSWER); - doReturn(pv).when(propertySource).get(eq("abc")); - - Predicate<String> filter = new Predicate<String>() { - @Override - public boolean test(String s) { - return true; - } - }; - - FilteredPropertySource sut = new FilteredPropertySource(propertySource, filter); - - PropertyValue result = sut.get("abc"); - - assertThat(result).isNotNull(); - } - - /* - * Tests for getProperties() - */ - - @Test - public void getPropertiesAndFilterRemovesAllProperties() { - InMemoryPropertySource imps = new InMemoryPropertySource(); - imps.add("a", "1").add("b", "2").add("c", "3"); - imps.setName("s"); - - Predicate<String> filter = new Predicate<String>() { - @Override - public boolean test(String s) { - return false; - } - }; - - FilteredPropertySource fps = new FilteredPropertySource(imps, filter); - - assertThat(fps.getProperties()).isEmpty();; - } - - @Test - public void getPropertiesAndFilterRemovesNoProperties() { - InMemoryPropertySource imps = new InMemoryPropertySource(); - imps.add("a", "1").add("b", "2").add("c", "3"); - imps.setName("s"); - - Predicate<String> filter = new Predicate<String>() { - @Override - public boolean test(String s) { - return true; - } - }; - - FilteredPropertySource fps = new FilteredPropertySource(imps, filter); - - assertThat(fps.getProperties()).isNotEmpty() - .containsEntry("a", of("a", "1", "s")) - .containsEntry("b", of("b", "2", "s")) - .containsEntry("c", of("c", "3", "s")) - .hasSize(3); - } - - @Test - public void getPropertiesAndFilterRemovesSomeProperties() { - InMemoryPropertySource imps = new InMemoryPropertySource(); - imps.add("a", "1").add("b", "2").add("c", "3"); - imps.setName("s"); - - Predicate<String> filter = new Predicate<String>() { - @Override - public boolean test(String s) { - return !s.startsWith("a"); - } - }; - - FilteredPropertySource fps = new FilteredPropertySource(imps, filter); - - assertThat(fps.getProperties()).isNotEmpty() - .containsEntry("b", of("b", "2", "s")) - .containsEntry("c", of("c", "3", "s")) - .hasSize(2); - - } - - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfigSource.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfigSource.java b/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfigSource.java new file mode 100644 index 0000000..c22c108 --- /dev/null +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfigSource.java @@ -0,0 +1,68 @@ +/* + * 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.functions; + +import javax.config.spi.ConfigSource; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class InMemoryConfigSource implements ConfigSource { + private int ordinal; + private String name; + private Map<String, String> properties = new HashMap<>(); + private boolean isScannable; + + @Override + public int getOrdinal() { + return ordinal; + } + + public void setOrdinal(int ordinal) { + this.ordinal = ordinal; + } + + @Override + public String getName() { + return name; + } + + public InMemoryConfigSource setName(String name) { + this.name = name; + + return this; + } + + @Override + public String getValue(String key) { + return properties.get(key); + } + + public InMemoryConfigSource add(String key, String value) { + properties.put(key, value); + + return this; + } + + @Override + public Map<String, String> getProperties() { + return Collections.unmodifiableMap(properties); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java b/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java deleted file mode 100644 index deb544c..0000000 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryConfiguration.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.functions; - -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spisupport.DefaultConfiguration; - -class InMemoryConfiguration extends DefaultConfiguration { - public InMemoryConfiguration(ConfigurationContext configurationContext) { - super(configurationContext); - } - // private Map<String, String> entries = new TreeMap<>(); - -// public InMemoryConfiguration addEntry(String key, String value) { -// entries.put(key, value); -// -// return this; -// } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryPropertySource.java b/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryPropertySource.java deleted file mode 100644 index 77ad378..0000000 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryPropertySource.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.functions; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import java.util.HashMap; -import java.util.Map; - -public class InMemoryPropertySource implements PropertySource { - private int ordinal; - private String name; - private Map<String, String> properties = new HashMap<>(); - private boolean isScannable; - - @Override - public int getOrdinal() { - return ordinal; - } - - public void setOrdinal(int ordinal) { - this.ordinal = ordinal; - } - - @Override - public String getName() { - return name; - } - - public InMemoryPropertySource setName(String name) { - this.name = name; - - return this; - } - - @Override - public PropertyValue get(String key) { - String value = properties.get(key); - - return PropertyValue.of(key, value, getName()); - } - - public InMemoryPropertySource add(String key, String value) { - properties.put(key, value); - - return this; - } - - @Override - public Map<String, PropertyValue> getProperties() { - Map<String, PropertyValue> result = new HashMap<>(); - - for (Map.Entry<String, String> entry : properties.entrySet()) { - PropertyValue value = PropertyValue.of(entry.getKey(), entry.getValue(), getName()); - result.put(entry.getKey(), value); - } - - return result; - } - - @Override - public boolean isScannable() { - return isScannable; - } - - public void setScannable(boolean scannable) { - isScannable = scannable; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/MappedConfigSourceTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/MappedConfigSourceTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/MappedConfigSourceTest.java new file mode 100644 index 0000000..f0dd3ed --- /dev/null +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/MappedConfigSourceTest.java @@ -0,0 +1,159 @@ +/* + * 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.functions; + +import org.junit.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MappedConfigSourceTest { + private static final KeyMapper KEY_MAPPER = new KeyMapper() { + @Override + public String mapKey(String key) { + String result = key; + + if ("M".compareTo(key.toUpperCase()) <= 0) { + result = key.toUpperCase(); + } + + return result; + } + }; + + /* + * Tests for getProperties() + */ + + @Test + public void getPropertiesWithMappedKeys() { + InMemoryConfigSource propertySource = new InMemoryConfigSource(); + propertySource.setName("PS"); + propertySource.add("a", "1"); + propertySource.add("b", "2"); + propertySource.add("m", "3"); + + MappedConfigSource mappedConfigSource = new MappedConfigSource(propertySource, KEY_MAPPER); + + Map<String, String> result = mappedConfigSource.getProperties(); + + assertThat(result).isNotNull() + .containsEntry("a", "1") + .containsEntry("b", "2") + .containsEntry("M", "3") + .hasSize(3); + } + + @Test + public void getPropertiesWithoutMappedKeys() { + InMemoryConfigSource propertySource = new InMemoryConfigSource(); + propertySource.setName("PS"); + propertySource.add("a", "1"); + propertySource.add("b", "2"); + propertySource.add("c", "3"); + + MappedConfigSource mappedConfigSource = new MappedConfigSource(propertySource, KEY_MAPPER); + + Map<String, String> result = mappedConfigSource.getProperties(); + + assertThat(result).isNotNull() + .containsEntry("a", "1") + .containsEntry("b", "2") + .containsEntry("c", "3") + .hasSize(3); + } + + @Test + public void getPropertiesMapperDiscardsOneKey() { + InMemoryConfigSource propertySource = new InMemoryConfigSource(); + propertySource.setName("PS"); + propertySource.add("a", "1"); + propertySource.add("b", "2"); + propertySource.add("c", "3"); + + MappedConfigSource mappedConfigSource = new MappedConfigSource(propertySource, new KeyMapper() { + @Override + public String mapKey(String key) { + return "c".equals(key) ? null : key; + } + }); + + Map<String, String> result = mappedConfigSource.getProperties(); + + assertThat(result).isNotNull() + .containsEntry("a", "1") + .containsEntry("b", "2") + .hasSize(2); + } + + @Test + public void getPropertiesAndNoKeys() { + InMemoryConfigSource propertySource = new InMemoryConfigSource(); + propertySource.setName("PS"); + + MappedConfigSource mappedConfigSource = new MappedConfigSource(propertySource, KEY_MAPPER); + + Map<String, String> result = mappedConfigSource.getProperties(); + + assertThat(result).isNotNull() + .isEmpty(); + } + + /* + * Test for getOrdinal() + */ + + @Test + public void getOrdinalReturnsCorrectOrdinal() { + InMemoryConfigSource propertySource = new InMemoryConfigSource(); + MappedConfigSource mappedConfigSource = new MappedConfigSource(propertySource, KEY_MAPPER); + + propertySource.setOrdinal(999); + + assertThat(mappedConfigSource.getOrdinal()).isEqualTo(999); + } + + /* + * Tests for get(String) + */ + + @Test + public void getReturnsNullIfKeyIsNotInUnderlayingConfiguration() { + InMemoryConfigSource propertySource = new InMemoryConfigSource(); + MappedConfigSource mappedConfigSource = new MappedConfigSource(propertySource, KEY_MAPPER); + + assertThat(mappedConfigSource.getValue("nonexisting")).isNull(); + } + + @Test + public void getReturnsCorrectValueIfKeyIsMapped() { + InMemoryConfigSource propertySource = new InMemoryConfigSource(); + propertySource.add("m", "_a_"); + propertySource.setName("PS"); + + MappedConfigSource mappedConfigSource = new MappedConfigSource(propertySource, KEY_MAPPER); + + assertThat(mappedConfigSource.getValue("M")).isNotNull().isEqualTo("_a_"); + } + + + + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/MappedPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/MappedPropertySourceTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/MappedPropertySourceTest.java deleted file mode 100644 index 56c8921..0000000 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/MappedPropertySourceTest.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.functions; - -import org.apache.tamaya.spi.PropertyValue; -import org.junit.Test; - -import javax.management.ImmutableDescriptor; -import java.util.Map; - -import static org.apache.tamaya.spi.PropertyValue.of; -import static org.assertj.core.api.Assertions.assertThat; - -public class MappedPropertySourceTest { - private static final KeyMapper KEY_MAPPER = new KeyMapper() { - @Override - public String mapKey(String key) { - String result = key; - - if ("M".compareTo(key.toUpperCase()) <= 0) { - result = key.toUpperCase(); - } - - return result; - } - }; - - /* - * Tests for getProperties() - */ - - @Test - public void getPropertiesWithMappedKeys() { - InMemoryPropertySource propertySource = new InMemoryPropertySource(); - propertySource.setName("PS"); - propertySource.add("a", "1"); - propertySource.add("b", "2"); - propertySource.add("m", "3"); - - MappedPropertySource mappedPropertySource = new MappedPropertySource(propertySource, KEY_MAPPER); - - Map<String, PropertyValue> result = mappedPropertySource.getProperties(); - - assertThat(result).isNotNull() - .containsEntry("a", of("a", "1", "PS[mapped]")) - .containsEntry("b", of("b", "2", "PS[mapped]")) - .containsEntry("M", of("M", "3", "PS[mapped]")) - .hasSize(3); - } - - @Test - public void getPropertiesWithoutMappedKeys() { - InMemoryPropertySource propertySource = new InMemoryPropertySource(); - propertySource.setName("PS"); - propertySource.add("a", "1"); - propertySource.add("b", "2"); - propertySource.add("c", "3"); - - MappedPropertySource mappedPropertySource = new MappedPropertySource(propertySource, KEY_MAPPER); - - Map<String, PropertyValue> result = mappedPropertySource.getProperties(); - - assertThat(result).isNotNull() - .containsEntry("a", of("a", "1", "PS[mapped]")) - .containsEntry("b", of("b", "2", "PS[mapped]")) - .containsEntry("c", of("c", "3", "PS[mapped]")) - .hasSize(3); - } - - @Test - public void getPropertiesMapperDiscardsOneKey() { - InMemoryPropertySource propertySource = new InMemoryPropertySource(); - propertySource.setName("PS"); - propertySource.add("a", "1"); - propertySource.add("b", "2"); - propertySource.add("c", "3"); - - MappedPropertySource mappedPropertySource = new MappedPropertySource(propertySource, new KeyMapper() { - @Override - public String mapKey(String key) { - return "c".equals(key) ? null : key; - } - }); - - Map<String, PropertyValue> result = mappedPropertySource.getProperties(); - - assertThat(result).isNotNull() - .containsEntry("a", of("a", "1", "PS[mapped]")) - .containsEntry("b", of("b", "2", "PS[mapped]")) - .hasSize(2); - } - - @Test - public void getPropertiesAndNoKeys() { - InMemoryPropertySource propertySource = new InMemoryPropertySource(); - propertySource.setName("PS"); - - MappedPropertySource mappedPropertySource = new MappedPropertySource(propertySource, KEY_MAPPER); - - Map<String, PropertyValue> result = mappedPropertySource.getProperties(); - - assertThat(result).isNotNull() - .isEmpty(); - } - - /* - * Test for getOrdinal() - */ - - @Test - public void getOrdinalReturnsCorrectOrdinal() { - InMemoryPropertySource propertySource = new InMemoryPropertySource(); - MappedPropertySource mappedPropertySource = new MappedPropertySource(propertySource, KEY_MAPPER); - - propertySource.setOrdinal(999); - - assertThat(mappedPropertySource.getOrdinal()).isEqualTo(999); - } - - /* - * Tests for isScannable() - */ - - @Test - public void isScannableReturnsTrueIfIsTrue() { - InMemoryPropertySource propertySource = new InMemoryPropertySource(); - MappedPropertySource mappedPropertySource = new MappedPropertySource(propertySource, KEY_MAPPER); - - propertySource.setScannable(false); - - assertThat(mappedPropertySource.isScannable()).isFalse(); - } - - /* - * Tests for get(String) - */ - - @Test - public void getReturnsNullIfKeyIsNotInUnderlayingConfiguration() { - InMemoryPropertySource propertySource = new InMemoryPropertySource(); - MappedPropertySource mappedPropertySource = new MappedPropertySource(propertySource, KEY_MAPPER); - - assertThat(mappedPropertySource.get("nonexisting")).isNull(); - } - - @Test - public void getReturnsCorrectValueIfKeyIsMapped() { - InMemoryPropertySource propertySource = new InMemoryPropertySource(); - propertySource.add("m", "_a_"); - propertySource.setName("PS"); - - MappedPropertySource mappedPropertySource = new MappedPropertySource(propertySource, KEY_MAPPER); - - assertThat(mappedPropertySource.get("M")).isNotNull().isEqualTo(of("M", "_a_", "PS[mapped]")); - } - - - - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/PropertySourceFunctionsTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/PropertySourceFunctionsTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/PropertySourceFunctionsTest.java deleted file mode 100644 index aa51135..0000000 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/PropertySourceFunctionsTest.java +++ /dev/null @@ -1,367 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.functions; - -import org.apache.tamaya.functions.PropertySourceFunctions; -import org.apache.tamaya.spi.PropertySource; -import org.assertj.core.api.ThrowableAssert; -import org.assertj.core.description.TextDescription; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Set; - -import static org.apache.tamaya.functions.PropertySourceFunctions.*; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -public class PropertySourceFunctionsTest { - - @Ignore - @Test - public void testAddMetaData() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - /* - * Tests for isKeyInSection(String, String) - */ - - @Test - public void isKeyInSectionThrowsNPEIfKeyIsNull() { - assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { - @Override - public void call() throws Throwable { - isKeyInSection("a.b.c", null); - } - }).isInstanceOf(NullPointerException.class) - .hasMessage("Section key must be given."); - } - - @Test - public void isKeyInSectionThrowsNPEIfSectionKeyIsNull() { - assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { - @Override - public void call() throws Throwable { - isKeyInSection(null, "a.b.c"); - } - }).isInstanceOf(NullPointerException.class) - .hasMessage("Key must be given."); - } - - @Test - public void isKeyInSectionForKeyInRootSection() { - String key = "key"; - String sectionKey = ""; - - boolean result = isKeyInSection(key, sectionKey); - - assertThat(result).describedAs("Key '%s' is in root section '%s'") - .isTrue(); - } - - @Test - public void isKeyInSectionForKeyInExplicitRootSection() { - String key = "key"; - String sectionKey = "."; - - boolean result = isKeyInSection(key, sectionKey); - - assertThat(result).describedAs("Key '%s' is in root section '%s'") - .isTrue(); - } - - @Test - public void isKeyInSectionForKeyInSection() throws Exception { - String key = "abc.def.g.h.key"; - String section = "abc.def.g.h"; - - boolean result = isKeyInSection(key, section); - - assertThat(result).describedAs("Key %s is in section %s", key, section) - .isTrue(); - } - - @Test - public void isKeyInSectionForKeyNotInSection() throws Exception { - String key = "abc.def.g.h.i.key"; - String section = "abc.def.g.h"; - - boolean result = isKeyInSection(key, section); - - assertThat(result).describedAs("Key %s is not in section %s", key, section) - .isFalse(); - } - - @Test - public void isKeyInSectionIgnoresTrailingDotAtTheEndOfTheSection() throws Exception { - String key = "abc.def.g.h.key"; - String section = "abc.def.g.h."; - - boolean result = isKeyInSection(key, section); - - assertThat(result).describedAs("Key %s is in section %s", key, section) - .isTrue(); - } - - - /* - * Tests for isKeyInSections(String, String, String...) - */ - - @Test - public void isKeyInSectionsStringStringStringVarargThrowsNPEIfKeyIsNull() { - assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { - @Override - public void call() throws Throwable { - isKeyInSections(null, "a.b.", "a.b", "b.c"); - } - }).isInstanceOf(NullPointerException.class) - .hasMessage("Key must be given."); - } - - @Test - public void isKeyInSectionsStringStringStringVarargsThrowsNPEIfFirstSectionIsNotGiven() { - assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { - @Override - public void call() throws Throwable { - isKeyInSections("key", (String)null, "a.b"); - } - }).isInstanceOf(NullPointerException.class) - .hasMessage("At least one section key must be given."); - } - - @Test - public void isKeyInSectionsStringStringStringVarargshrowsNPEIfMoreSectionKeysIsNull() { - // null should not cause any problems - boolean result = isKeyInSections("key", "l.b", (String) null); - - assertThat(result).isFalse(); - } - - @Test - public void isKeyInSectionsStringStringStringVaragrsSectioOfKeyIsAtEndOfVarargs() { - String section = "abc.def."; - String key = section + "key"; - - // null should not cause any problems - boolean result = isKeyInSections(key, "l.b", null, "abc", section); - - assertThat(result).describedAs("Key '%s' is in section '%s'.", key, section).isTrue(); - } - - /* - * Tests for isKeyInSections(String, String[]) - */ - - @Test - public void isKeyInSectionsStringStringStringArrayCopesWithEmptyArrayForMoreSectionKeys() { - String key = "a.b.key"; - String first = "a.b"; - - boolean result = isKeyInSections(key, first, new String[]{}); - - assertThat(result).describedAs("Key '%s' is in section '%s'.", key, first) - .isTrue(); - } - - - /* - * Tests for sections(Map<String, String>) - */ - - // null as parameter - - // empty as parameter - - // all keys in root section - - // some keys in packages - - @Test - public void sectionsMapReturnsAllSectionsForGivenKeysInMap() { - HashMap<String, String> kv = new HashMap<>(); - - kv.put("abc.key", "v"); - kv.put("abc.def.key", "v"); - kv.put("a.key", "v"); - kv.put("b.key", "v"); - kv.put("key", "v"); - - Set<String> result = sections(kv); - - assertThat(result).isNotNull() - .isNotEmpty() - .contains("abc", "abc.def", "a", "b", "<root>"); - } - - @Test - public void sectionsMapTreatsLeadingDotAsOptional() { - HashMap<String, String> kv = new HashMap<>(); - - kv.put(".abc.key", "v"); - kv.put(".abc.def.key", "v"); - kv.put(".a.key", "v"); - kv.put(".b.key", "v"); - kv.put(".key", "v"); - - Set<String> result = sections(kv); - - assertThat(result).isNotNull() - .isNotEmpty() - .contains("abc", "abc.def", "a", "b", "<root>"); - } - - /* - * Tests for sections(Map<String, String> , Predicate<String>) - */ - - @Test - public void sectionsMapPredicateFiltersAccordingToFilter() { - HashMap<String, String> kv = new HashMap<>(); - - kv.put(".abc.key", "v"); - kv.put(".abc.def.key", "v"); - kv.put(".a.key", "v"); - kv.put(".b.key", "v"); - kv.put(".key", "v"); - - Set<String> result = sections(kv, new Predicate<String>() { - @Override - public boolean test(String s) { - return !s.startsWith("a"); - } - }); - - assertThat(result).isNotNull() - .isNotEmpty() - .contains("b", "<root>"); - } - - /* - * Tests for transitiveSections(Map<String, String>) - */ - - @Test - public void bla() { - HashMap<String, String> kv = new HashMap<>(); - - kv.put(".abc.key", "v"); - kv.put(".abc.def.key", "v"); - kv.put(".abc.def.ghi.key", "v"); - kv.put(".a.key", "v"); - kv.put(".b.key", "v"); - kv.put(".key", "v"); - - Set<String> result = transitiveSections(kv); - - for (String s : result) { - System.out.println(s); - } - - - assertThat(result).isNotNull() - .isNotEmpty() - .contains("abc", "abc.def", "a", "b", "<root>"); - - - } - - - //---- - @Ignore - @Test - public void testIsKeyInSections() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testSections() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testTransitiveSections() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testSections1() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testTransitiveSections1() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testSectionsRecursive() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testSectionRecursive() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testStripSectionKeys() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testAddItems() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testAddItems1() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testReplaceItems() throws Exception { - throw new RuntimeException("Not implement or look at me!"); - } - - @Ignore - @Test - public void testEmptyPropertySource() throws Exception { - PropertySource ps = PropertySourceFunctions.emptyPropertySource(); -// assertNotNull(ps); -// assertNotNull(ps.getProperties()); -// assertTrue(ps.getProperties().isEmpty()); -// assertEquals(ps.getName(), "<empty>" ); -// assertTrue(ps.isScannable()); - - throw new RuntimeException("Not implement or look at me!"); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedConfigSourceTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedConfigSourceTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedConfigSourceTest.java new file mode 100644 index 0000000..f8126aa --- /dev/null +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedConfigSourceTest.java @@ -0,0 +1,148 @@ +/* + * 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.functions; + +import org.junit.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ValueMappedConfigSourceTest { + + private PropertyMapper mapper = new PropertyMapper() { + @Override + public String mapProperty(String key, String value) { + String startOfOtherKey = key.toLowerCase().substring(0, 1); + if ("m".compareTo(startOfOtherKey) <= 0) { + return value + "m"; + } + + return value; + } + }; + + /* + * Tests for getOrdinal() + */ + + @Test + public void getOrdinalReturnsGivenOrdinal() throws Exception { + InMemoryConfigSource source = new InMemoryConfigSource(); + source.add("a", "1").add("b", "2").add("c", "3").setName("S"); + source.setOrdinal(99); + + ValueMappedConfigSource mappingSource = new ValueMappedConfigSource("vmps", mapper, source); + + assertThat(mappingSource.getOrdinal()).isEqualTo(99); + } + + /* + * Tests for getName() + */ + + @Test + public void getNameReturnsGivenName() throws Exception { + InMemoryConfigSource source = new InMemoryConfigSource(); + source.add("a", "1").add("b", "2").add("c", "3").setName("S"); + + ValueMappedConfigSource mappingSource = new ValueMappedConfigSource("vmps", mapper, source); + + assertThat(mappingSource.getName()).isEqualTo("vmps"); + } + + /* + * Tests for get(String) + */ + + @Test + public void getReturnNullIfKeyIsNotInBasePropertySource() throws Exception { + InMemoryConfigSource source = new InMemoryConfigSource(); + source.add("a", "1").add("b", "2").add("m", "3").setName("S"); + + ValueMappedConfigSource mappingSource = new ValueMappedConfigSource("vmps", mapper, source); + + assertThat(mappingSource.getValue("z")).isNull(); + } + + @Test + public void getReturnsUnmappedValue() throws Exception { + InMemoryConfigSource source = new InMemoryConfigSource(); + source.add("a", "1").add("b", "2").add("m", "3").setName("S"); + + ValueMappedConfigSource mappingSource = new ValueMappedConfigSource("vmps", mapper, source); + + assertThat(mappingSource.getValue("a")).isNotNull() + .isEqualTo("1"); + } + + @Test + public void getReturnsMappedValue() throws Exception { + InMemoryConfigSource source = new InMemoryConfigSource(); + source.add("a", "1").add("b", "2").add("m", "3").setName("S"); + + ValueMappedConfigSource mappingSource = new ValueMappedConfigSource("vmps", mapper, source); + + assertThat(mappingSource.getValue("m")).isNotNull() + .isEqualTo("3"); + } + + /* + * Tests for getProperties() + */ + + @Test + public void getPropertiesMapperMapsNoValue() { + InMemoryConfigSource source = new InMemoryConfigSource(); + source.add("a", "1").add("b", "2").add("c", "3").setName("S"); + + ValueMappedConfigSource mappingSource = new ValueMappedConfigSource("vmps", mapper, source); + + Map<String, String> result = mappingSource.getProperties(); + + assertThat(result).isNotNull() + .isNotEmpty() + .containsEntry("a", "1") + .containsEntry("b", "2") + .containsEntry("c", "3") + .hasSize(3); + + } + + @Test + public void getPropertiesMapperMapsSomeValues() throws Exception { + InMemoryConfigSource source = new InMemoryConfigSource(); + source.add("a", "1").add("b", "2").add("m", "3").setName("S"); + + ValueMappedConfigSource mappingSource = new ValueMappedConfigSource("vmps", mapper, source); + + Map<String, String> result = mappingSource.getProperties(); + + assertThat(result).isNotNull() + .isNotEmpty() + .containsEntry("a", "1") + .containsEntry("b", "2") + .containsEntry("m", "3m") + .hasSize(3); + } + + + + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/36b44661/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedPropertySourceTest.java b/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedPropertySourceTest.java deleted file mode 100644 index 1ce5025..0000000 --- a/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedPropertySourceTest.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.functions; - -import org.apache.tamaya.spi.PropertyValue; -import org.assertj.core.api.Condition; -import org.junit.Test; - -import java.util.Map; - -import static org.apache.tamaya.spi.PropertyValue.of; -import static org.assertj.core.api.Assertions.assertThat; - -public class ValueMappedPropertySourceTest { - - private PropertyMapper mapper = new PropertyMapper() { - @Override - public String mapProperty(String key, String value) { - String startOfOtherKey = key.toLowerCase().substring(0, 1); - if ("m".compareTo(startOfOtherKey) <= 0) { - return value + "m"; - } - - return value; - } - }; - - /* - * Tests for getOrdinal() - */ - - @Test - public void getOrdinalReturnsGivenOrdinal() throws Exception { - InMemoryPropertySource source = new InMemoryPropertySource(); - source.add("a", "1").add("b", "2").add("c", "3").setName("S"); - source.setOrdinal(99); - - ValueMappedPropertySource mappingSource = new ValueMappedPropertySource("vmps", mapper, source); - - assertThat(mappingSource.getOrdinal()).isEqualTo(99); - } - - /* - * Tests for isScannable() - */ - - @Test - public void isScannableReturnsTrueIfSetToTrue() throws Exception { - InMemoryPropertySource source = new InMemoryPropertySource(); - source.add("a", "1").add("b", "2").add("c", "3").setName("S"); - source.setScannable(true); - - ValueMappedPropertySource mappingSource = new ValueMappedPropertySource("vmps", mapper, source); - - assertThat(mappingSource.isScannable()).isTrue(); - } - - - /* - * Tests for getName() - */ - - @Test - public void getNameReturnsGivenName() throws Exception { - InMemoryPropertySource source = new InMemoryPropertySource(); - source.add("a", "1").add("b", "2").add("c", "3").setName("S"); - - ValueMappedPropertySource mappingSource = new ValueMappedPropertySource("vmps", mapper, source); - - assertThat(mappingSource.getName()).isEqualTo("vmps"); - } - - /* - * Tests for get(String) - */ - - @Test - public void getReturnNullIfKeyIsNotInBasePropertySource() throws Exception { - InMemoryPropertySource source = new InMemoryPropertySource(); - source.add("a", "1").add("b", "2").add("m", "3").setName("S"); - - ValueMappedPropertySource mappingSource = new ValueMappedPropertySource("vmps", mapper, source); - - PropertyValue result = mappingSource.get("z"); - - assertThat(result).isNull(); - } - - @Test - public void getReturnsUnmappedValue() throws Exception { - InMemoryPropertySource source = new InMemoryPropertySource(); - source.add("a", "1").add("b", "2").add("m", "3").setName("S"); - - ValueMappedPropertySource mappingSource = new ValueMappedPropertySource("vmps", mapper, source); - - PropertyValue result = mappingSource.get("a"); - - assertThat(result).isNotNull() - .has(new Condition<PropertyValue>() { - @Override - public boolean matches(PropertyValue propertyValue) { - return "1".equals(propertyValue.getValue()); - } - }); - } - - @Test - public void getReturnsMappedValue() throws Exception { - InMemoryPropertySource source = new InMemoryPropertySource(); - source.add("a", "1").add("b", "2").add("m", "3").setName("S"); - - ValueMappedPropertySource mappingSource = new ValueMappedPropertySource("vmps", mapper, source); - - PropertyValue result = mappingSource.get("m"); - - assertThat(result).isNotNull() - .has(new Condition<PropertyValue>() { - @Override - public boolean matches(PropertyValue propertyValue) { - return "3m".equals(propertyValue.getValue()); - } - }); - } - - /* - * Tests for getProperties() - */ - - @Test - public void getPropertiesMapperMapsNoValue() { - InMemoryPropertySource source = new InMemoryPropertySource(); - source.add("a", "1").add("b", "2").add("c", "3").setName("S"); - - ValueMappedPropertySource mappingSource = new ValueMappedPropertySource("vmps", mapper, source); - - Map<String, PropertyValue> result = mappingSource.getProperties(); - - assertThat(result).isNotNull() - .isNotEmpty() - .containsEntry("a", of("a", "1", "S")) - .containsEntry("b", of("b", "2", "S")) - .containsEntry("c", of("c", "3", "S")) - .hasSize(3); - - } - - @Test - public void getPropertiesMapperMapsSomeValues() throws Exception { - InMemoryPropertySource source = new InMemoryPropertySource(); - source.add("a", "1").add("b", "2").add("m", "3").setName("S"); - - ValueMappedPropertySource mappingSource = new ValueMappedPropertySource("vmps", mapper, source); - - Map<String, PropertyValue> result = mappingSource.getProperties(); - - assertThat(result).isNotNull() - .isNotEmpty() - .containsEntry("a", of("a", "1", "S")) - .containsEntry("b", of("b", "2", "S")) - .containsEntry("m", of("m", "3m", "S")) - .hasSize(3); - } - - - - -} \ No newline at end of file