Repository: incubator-tamaya-extensions Updated Branches: refs/heads/master 0f772ca02 -> d1598fe2a
[TAMAYA-244] Wrote more tests and fixed a bug in MappedPropertySource#get(String). Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/d1598fe2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/d1598fe2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/d1598fe2 Branch: refs/heads/master Commit: d1598fe2aae49d358d2ad842699e37156e37129e Parents: 0f772ca Author: Oliver B. Fischer <[email protected]> Authored: Sat Mar 25 23:51:43 2017 +0100 Committer: Oliver B. Fischer <[email protected]> Committed: Sat Mar 25 23:52:02 2017 +0100 ---------------------------------------------------------------------- .../tamaya/functions/EnrichedConfiguration.java | 13 + .../tamaya/functions/MappedPropertySource.java | 38 +- .../functions/EnrichedConfigurationTest.java | 709 +++++++++++++++++++ .../functions/MappedPropertySourceTest.java | 176 +++++ 4 files changed, 930 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d1598fe2/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java b/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java index 7ace497..a223a45 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/EnrichedConfiguration.java @@ -54,6 +54,8 @@ class EnrichedConfiguration implements Configuration { @Override public String get(String key) { + Objects.requireNonNull(key, "Key must be given."); + if (overriding) { Object val = addedProperties.get(key); if (val != null) { @@ -74,6 +76,9 @@ class EnrichedConfiguration implements Configuration { @Override public String getOrDefault(String key, String defaultValue) { + Objects.requireNonNull(key, "Key must be given."); + Objects.requireNonNull(defaultValue, "Default value must be given."); + String val = get(key); if (val == null) { return defaultValue; @@ -83,6 +88,10 @@ class EnrichedConfiguration implements Configuration { @Override public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { + Objects.requireNonNull(key, "Key not given."); + Objects.requireNonNull(type, "Class not given."); + Objects.requireNonNull(defaultValue, "Default value not given."); + T val = get(key, type); if (val == null) { return defaultValue; @@ -117,6 +126,10 @@ class EnrichedConfiguration implements Configuration { @Override public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue) { + Objects.requireNonNull(key, "Key not given."); + Objects.requireNonNull(type, "Type not given."); + Objects.requireNonNull(defaultValue, "Default value not given."); + T val = get(key, type); if (val == null) { return defaultValue; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d1598fe2/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java b/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java index dfef0f9..7e48f22 100644 --- a/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/MappedPropertySource.java @@ -35,6 +35,7 @@ class MappedPropertySource implements PropertySource { * The mapping operator. */ private final KeyMapper keyMapper; + /** * The base configuration. */ @@ -78,16 +79,41 @@ class MappedPropertySource implements PropertySource { return propertySource.isScannable(); } + + /** + * <p>Access a property by its key.</p> + * + * <p> + * The key of the property to be returned must be equal to the key + * returned by the mapping operator (key mapper) and not equal + * to the key of the base configuration. + * </p> + * + * @param key the property's key, not {@code null}. + * @return the property value map, where {@code map.get(key) == value}, + * including also any metadata. In case a value is {@code null}, + * simply return {@code null}. + */ @Override public PropertyValue get(String key) { - PropertyValue result = this.propertySource.get(key); - if(result!=null){ - String targetKey = keyMapper.mapKey(key); - if (targetKey != null) { - return result.toBuilder().mapKey(targetKey).build(); + Objects.requireNonNull(key, "Key must be given."); + + String mappedKey = keyMapper.mapKey(key); + PropertyValue result = null; + + if (mappedKey != null) { + for (PropertyValue property : propertySource.getProperties().values()) { + String newKey = keyMapper.mapKey(property.getKey()); + + if (mappedKey.equals(newKey)) { + String mappedName = getName(); + return property.toBuilder().mapKey(newKey) + .setSource(mappedName).build(); + } } } - return null; + + return result; } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d1598fe2/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 new file mode 100644 index 0000000..6bf97ac --- /dev/null +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/EnrichedConfigurationTest.java @@ -0,0 +1,709 @@ +/* + * 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.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 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.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +public class EnrichedConfigurationTest { + + /* + * Tests for get(String) + */ + + @Test + public void getKeyIsNull() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + final EnrichedConfiguration sut = new EnrichedConfiguration(base, EMPTY_MAP, true); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.get(null); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Key must be given."); + } + + @Test + public void getKeyIsNotKownAndHasNotAnOverriderWithOverridingOn() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("b")); + doReturn(null).when(base).get(eq("y")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("z0", "0"); + additions.put("b", "1"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.get("y"); + + assertThat(result).isNull(); + + } + + @Test + public void getKeyIsNotKownAndHasNotAnOverriderWithOverridingOff() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("b")); + doReturn(null).when(base).get(eq("y")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("z0", "0"); + additions.put("b", "1"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.get("y"); + + assertThat(result).isNull(); + } + + @Test + public void getKeyIsNotKownAndHasOverriderAndConfigurationIsOverridingIsOn() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("y")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("z0", "0"); + additions.put("b", "1"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.get("b"); + + assertThat(result).isNotNull().isEqualTo("1"); + } + + @Test + public void getKeyIsNotKownAndHasOverriderAndConfigurationIsOverridingIsOff() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("z0", "0"); + additions.put("b", "1"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.get("b"); + + assertThat(result).isNotNull().isEqualTo("1"); + } + + @Test + public void getKeyIsKownAndHasOverriderAndConfigurationIsNotOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("b")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("z0", "0"); + additions.put("b", "1"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.get("b"); + + assertThat(result).isNotNull().isEqualTo("9"); + } + + @Test + public void getKeyIsKownAndHasOverriderAndConfigurationIsOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("b")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("z0", "0"); + additions.put("b", "1"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.get("b"); + + assertThat(result).isNotNull().isEqualTo("1"); + } + + @Test + public void getKeyIsKnownAndHasNoOverrider() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("b")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("z0", "0"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.get("b"); + + assertThat(result).isNotNull().isEqualTo("9"); + } + + /* + * Tests for getOrDefault(String, String) + */ + + @Test + public void getOrDefaultStringStringWithKeyIsNull() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + + final EnrichedConfiguration sut = new EnrichedConfiguration(base, EMPTY_MAP, true); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.getOrDefault(null, "v"); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Key must be given."); + } + + @Test + public void getOrDefaultStringStringWithDefaultValueIsNull() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + + final EnrichedConfiguration sut = new EnrichedConfiguration(base, EMPTY_MAP, true); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.getOrDefault("v", null); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Default value must be given."); + } + + @Test + public void getOrDefaultStringStringWithKeyIsOverriddenAndOverridingOn() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("b")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "0"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.getOrDefault("b", "100"); + + assertThat(result).isNotNull().isEqualTo("0"); + } + + @Test + public void getOrDefaultStringStringWithKeyIsOverriddenAndOverridingOff() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("b")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "0"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.get("b"); + + assertThat(result).isNotNull().isEqualTo("9"); + } + + @Test + public void getOrDefaultStringStringWithKeyIsKnownAndIsNotOverridden() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("9").when(base).get(eq("b")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("z0", "0"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.getOrDefault("b", "100"); + + assertThat(result).isNotNull().isEqualTo("9"); + } + + @Test + public void getOrDefaultStringStringWithKeyIsNotKnownButIsOverridden() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b")); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "0"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.get("b"); + + assertThat(result).isNotNull().isEqualTo("0"); + } + + @Test + public void getOrDefaultStringStringWithKeyIsUnKnownAndIsNotOverridden() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b")); + EnrichedConfiguration sut = new EnrichedConfiguration(base, EMPTY_MAP, true); + + String result = sut.getOrDefault("b", "1000"); + + assertThat(result).isNotNull().isEqualTo("1000"); + } + + /* + * Tests for getOrDefault(String, Class<T>, T) + */ + + @Test + public void getOrDefaultStringClassTThrowsNPEIfKeyIsNull() throws Exception { + final EnrichedConfiguration sut = mock(EnrichedConfiguration.class, NOT_MOCKED_ANSWER); + doCallRealMethod().when(sut).getOrDefault(anyString(), any(Class.class), anyString()); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.getOrDefault("b", String.class, null); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Default value not given."); + } + + @Test + public void getOrDefaultStringClassTThrowsNPEIfClassIsNull() throws Exception { + final EnrichedConfiguration sut = mock(EnrichedConfiguration.class, NOT_MOCKED_ANSWER); + doCallRealMethod().when(sut).getOrDefault(anyString(), any(Class.class), anyString()); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.getOrDefault("b", (Class<String>)null, "20"); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Class not given."); + } + + @Test + public void getOrDefaultStringClassTThrowsNPEIfDefaultValueIsNull() throws Exception { + final EnrichedConfiguration sut = mock(EnrichedConfiguration.class, NOT_MOCKED_ANSWER); + doCallRealMethod().when(sut).getOrDefault(anyString(), any(Class.class), anyString()); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.getOrDefault("b", String.class, null); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Default value not given."); + } + + @Test + public void getOrDefaultStringClassTKeyInBaseAndInAdditionsNotOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("1").when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "2"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.getOrDefault("b", String.class, "3"); + + assertThat(result).isEqualTo("1"); + } + + + @Test + public void getOrDefaultStringClassTKeyInBaseAndInAddtionsOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("1").when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "2"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.getOrDefault("b", String.class, "3"); + + assertThat(result).isEqualTo("2"); + } + + @Test + public void getOrDefaultStringClassTKeyNotInBaseInAdditionsNotOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "20"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.getOrDefault("b", String.class, "B"); + + assertThat(result).isEqualTo("20"); + } + + + + @Test + public void getOrDefaultStringClassTKeyNotInBaseInAddtionsOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "20"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.getOrDefault("b", String.class, "B"); + + assertThat(result).isEqualTo("20"); + } + + + @Test + public void getOrDefaultStringClassTKeyNotInBaseNotInAdditionsAndNotOverrding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.getOrDefault("b", String.class, "B"); + + assertThat(result).isEqualTo("B"); + } + + @Test + public void getOrDefaultStringClassTKeyNotInBaseNotInAdditionsAndOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.getOrDefault("b", String.class, "3"); + + assertThat(result).isEqualTo("3"); + } + + + + + /* + * Tests for get(String, Class<T>) + */ + + /* + * Tests for get(String, TypeLiteral) + */ + + /* + * Tests for getOrDefault(String, TypeLiteral<T>, T) + */ + + @Test + public void getOrDefaultStringTypeLiteralTThrowsNPEIfKeyIsNull() throws Exception { + final EnrichedConfiguration sut = mock(EnrichedConfiguration.class, NOT_MOCKED_ANSWER); + doCallRealMethod().when(sut).getOrDefault(anyString(), any(TypeLiteral.class), anyString()); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.getOrDefault("b", TypeLiteral.<String>of(String.class), null); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Default value not given."); + + } + + @Test + public void getOrDefaultStringLiteralTThrowsNPEIfClassIsNull() throws Exception { + final EnrichedConfiguration sut = mock(EnrichedConfiguration.class, NOT_MOCKED_ANSWER); + doCallRealMethod().when(sut).getOrDefault(anyString(), any(TypeLiteral.class), anyString()); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.getOrDefault("b", (TypeLiteral<String>)null, "20"); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Type not given."); + + } + + @Test + public void getOrDefaultStringTypeLiteralThrowsNPEIfDefaultValueIsNull() throws Exception { + final EnrichedConfiguration sut = mock(EnrichedConfiguration.class, NOT_MOCKED_ANSWER); + doCallRealMethod().when(sut).getOrDefault(anyString(), any(TypeLiteral.class), anyString()); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() throws Throwable { + sut.getOrDefault("b", TypeLiteral.<String>of(String.class), null); + } + }).isInstanceOf(NullPointerException.class) + .hasMessage("Default value not given."); + } + + @Test + public void getOrDefaultStringTypeLiteralTKeyInBaseAndInAdditionsNotOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("1").when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "2"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.getOrDefault("b", TypeLiteral.<String>of(String.class), "3"); + + assertThat(result).isEqualTo("1"); + } + + + @Test + public void getOrDefaultStringTypeLiteralTKeyInBaseAndInAddtionsOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn("1").when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "2"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.getOrDefault("b", TypeLiteral.<String>of(String.class), "3"); + + assertThat(result).isEqualTo("2"); + } + + @Test + public void getOrDefaultStringTypeLiteralTKeyNotInBaseInAdditionsNotOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "20"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.getOrDefault("b", TypeLiteral.<String>of(String.class), "B"); + + assertThat(result).isEqualTo("20"); + } + + + + @Test + public void getOrDefaultStringTypeLiteralTKeyNotInBaseInAddtionsOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + additions.put("b", "20"); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.getOrDefault("b", TypeLiteral.<String>of(String.class), "B"); + + assertThat(result).isEqualTo("20"); + } + + + @Test + public void getOrDefaultStringTypeLiteralTKeyNotInBaseNotInAdditionsAndNotOverrding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, false); + + String result = sut.getOrDefault("b", TypeLiteral.<String>of(String.class), "B"); + + assertThat(result).isEqualTo("B"); + } + + @Test + public void getOrDefaultStringTypeLiteralTKeyNotInBaseNotInAdditionsAndOverriding() throws Exception { + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(null).when(base).get(eq("b"), any(TypeLiteral.class)); + + Map<String, Object> additions = new HashMap<>(); + + EnrichedConfiguration sut = new EnrichedConfiguration(base, additions, true); + + String result = sut.getOrDefault("b", TypeLiteral.<String>of(String.class), "3"); + + assertThat(result).isEqualTo("3"); + } + + /* + * Tests for getProperties() + */ + + // all in base, not additions + @Test + public void getPropertiesAllInBaseAndNoneInAdditions() { + Map<String, Object> baseProps = new HashMap<>(); + baseProps.put("a", "A"); + baseProps.put("b", "B"); + baseProps.put("c", "C"); + + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(baseProps).when(base).getProperties(); + + EnrichedConfiguration enrichedConfiguration = new EnrichedConfiguration(base, EMPTY_MAP, true); + + Map<String, String> result = enrichedConfiguration.getProperties(); + + assertThat(result).isNotEmpty() + .hasSize(3) + .containsEntry("a", "A") + .containsEntry("b", "B") + .containsEntry("c", "C"); + + } + + @Test + public void getPropertiesAllInBaseAndSomeOverriddenByAdditions() { + Map<String, Object> baseProps = new HashMap<>(); + baseProps.put("a", "A"); + baseProps.put("b", "B"); + baseProps.put("c", "C"); + + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(baseProps).when(base).getProperties(); + + Map<String, Object> additionalProps = new HashMap<>(); + additionalProps.put("b", "b"); + + EnrichedConfiguration enrichedConfiguration = new EnrichedConfiguration(base, additionalProps, true); + + Map<String, String> result = enrichedConfiguration.getProperties(); + + assertThat(result).isNotEmpty() + .hasSize(3) + .containsEntry("a", "A") + .containsEntry("b", "b") + .containsEntry("c", "C"); + } + + @Test + public void getPropertiesWithAdditionalPropertiesWhichAreNotInBaseOverriding() { + Map<String, Object> baseProps = new HashMap<>(); + baseProps.put("a", "A"); + baseProps.put("b", "B"); + baseProps.put("c", "C"); + + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(baseProps).when(base).getProperties(); + + Map<String, Object> additionalProps = new HashMap<>(); + additionalProps.put("e", "E"); + + EnrichedConfiguration enrichedConfiguration = new EnrichedConfiguration(base, additionalProps, true); + + Map<String, String> result = enrichedConfiguration.getProperties(); + + assertThat(result).isNotEmpty() + .hasSize(4) + .containsEntry("a", "A") + .containsEntry("b", "B") + .containsEntry("c", "C") + .containsEntry("e", "E"); + + } + + @Test + public void getPropertiesWithAdditionalPropertiesWhichAreNotInBaseNotOverriding() { + Map<String, Object> baseProps = new HashMap<>(); + baseProps.put("a", "A"); + baseProps.put("b", "B"); + baseProps.put("c", "C"); + + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(baseProps).when(base).getProperties(); + + Map<String, Object> additionalProps = new HashMap<>(); + additionalProps.put("e", "E"); + + EnrichedConfiguration enrichedConfiguration = new EnrichedConfiguration(base, additionalProps, false); + + Map<String, String> result = enrichedConfiguration.getProperties(); + + assertThat(result).isNotEmpty() + .hasSize(4) + .containsEntry("a", "A") + .containsEntry("b", "B") + .containsEntry("c", "C") + .containsEntry("e", "E"); + + } + + @Test + public void getPropertiesSomeAlsoInAdditionsNotOverriding() { + Map<String, Object> baseProps = new HashMap<>(); + baseProps.put("a", "A"); + baseProps.put("b", "B"); + baseProps.put("c", "C"); + + Configuration base = mock(Configuration.class, NOT_MOCKED_ANSWER); + doReturn(baseProps).when(base).getProperties(); + + Map<String, Object> additionalProps = new HashMap<>(); + additionalProps.put("b", "b"); + + EnrichedConfiguration enrichedConfiguration = new EnrichedConfiguration(base, additionalProps, false); + + Map<String, String> result = enrichedConfiguration.getProperties(); + + assertThat(result).isNotEmpty() + .hasSize(3) + .containsEntry("a", "A") + .containsEntry("b", "B") + .containsEntry("c", "C"); + } + + + /* + * Tests for with(ConfigOperator) + */ + + /* + * Tests for query(ConfigQuery) + */ + + /* + * Tests for getContext() + */ +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/d1598fe2/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 new file mode 100644 index 0000000..56c8921 --- /dev/null +++ b/modules/functions/src/test/java/org/apache/tamaya/functions/MappedPropertySourceTest.java @@ -0,0 +1,176 @@ +/* + * 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
