You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by pl...@apache.org on 2017/03/27 20:28:14 UTC
[1/2] incubator-tamaya-extensions git commit: [TAMAYA-258] Disabled
timestamped reports for PIT.
Repository: incubator-tamaya-extensions
Updated Branches:
refs/heads/master d1598fe2a -> cdd7f9c0b
[TAMAYA-258] Disabled timestamped reports for PIT.
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/7c0faa90
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/7c0faa90
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/7c0faa90
Branch: refs/heads/master
Commit: 7c0faa90664d93710a40e8d81b8eec98f9bf2774
Parents: d1598fe
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Sun Mar 26 15:05:52 2017 +0200
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Sun Mar 26 15:05:52 2017 +0200
----------------------------------------------------------------------
pom.xml | 1 +
1 file changed, 1 insertion(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7c0faa90/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 897b5e1..a883f32 100644
--- a/pom.xml
+++ b/pom.xml
@@ -523,6 +523,7 @@ under the License.
<version>${pitest-plugin.version}</version>
<configuration>
<coverageThreshold>80</coverageThreshold>
+ <timestampedReports>false</timestampedReports>
<targetClasses>
<param>org.apache.tamaya.*</param>
</targetClasses>
[2/2] incubator-tamaya-extensions git commit: [TAMAYA-244] Wrote more
tests and fixed some issues.
Posted by pl...@apache.org.
[TAMAYA-244] Wrote more tests and fixed some issues.
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/cdd7f9c0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/cdd7f9c0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/cdd7f9c0
Branch: refs/heads/master
Commit: cdd7f9c0b007bc4837d4f5ae015c110a8ca392e2
Parents: 7c0faa9
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Mon Mar 27 22:27:54 2017 +0200
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Mon Mar 27 22:27:54 2017 +0200
----------------------------------------------------------------------
.../functions/PropertySourceFunctions.java | 109 +++++--
.../functions/ValueMappedPropertySource.java | 6 +-
.../functions/InMemoryPropertySource.java | 3 +-
.../functions/PropertySourceFunctionsTest.java | 307 +++++++++++++++++--
.../ValueMappedPropertySourceTest.java | 183 +++++++++++
5 files changed, 558 insertions(+), 50 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cdd7f9c0/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java
----------------------------------------------------------------------
diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java b/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java
index 4646df5..6f20d6f 100644
--- a/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java
+++ b/modules/functions/src/main/java/org/apache/tamaya/functions/PropertySourceFunctions.java
@@ -22,15 +22,9 @@ import org.apache.tamaya.ConfigurationProvider;
import org.apache.tamaya.spi.PropertySource;
import org.apache.tamaya.spi.PropertyValue;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
+import java.util.*;
+
+import static java.lang.System.arraycopy;
/**
* Accessor that provides useful functions along with configuration.
@@ -83,28 +77,89 @@ public final class PropertySourceFunctions {
*
* @param key the fully qualified entry key, not null
* @param sectionKey the section key, not null
+ *
* @return true, if the entry is exact in this section
*/
public static boolean isKeyInSection(String key, String sectionKey) {
+ Objects.requireNonNull(key, "Key must be given.");
+ Objects.requireNonNull(sectionKey, "Section key must be given.");
+
+ sectionKey = normalizeSectionKey(sectionKey);
+
int lastIndex = key.lastIndexOf('.');
String curAreaKey = lastIndex > 0 ? key.substring(0, lastIndex) : "";
return curAreaKey.equals(sectionKey);
}
+ private static String normalizeKey(String key) {
+ return key.startsWith(".") ? key.substring(1)
+ : key;
+ }
+
+ static String normalizeSectionKey(String sectionKey) {
+ // Ignore unneeded and trailing dot at the end of the section key
+
+ String normalizedKey = sectionKey.endsWith(".")
+ ? sectionKey.substring(0, sectionKey.length() - 1)
+ : sectionKey;
+
+ normalizedKey = sectionKey.startsWith(".") ? sectionKey.length() == 1 ? ""
+ : normalizedKey.substring(1)
+ : normalizedKey;
+
+ return normalizedKey;
+ }
+
/**
* Calculates the current section key and compares it to the given section keys.
*
- * @param key the fully qualified entry key, not null
- * @param sectionKeys the section keys, not null
- * @return true, if the entry is exact in this section
+ * @param key the fully qualified entry key, not {@code null}
+ * @param sectionKey the section keys, not {@code null}
+ * @param moreSectionKeys the more section keys, not {@code null}
+ *
+ * @return true, if the entry is in one of the given sections
*/
- public static boolean isKeyInSections(String key, String... sectionKeys) {
+ public static boolean isKeyInSections(String key, String sectionKey, String... moreSectionKeys) {
+ Objects.requireNonNull(key, "Key must be given.");
+ Objects.requireNonNull(sectionKey, "At least one section key must be given.");
+ Objects.requireNonNull(moreSectionKeys, "Additional section keys must not be null.");
+
+ String[] sectionKeys = new String[moreSectionKeys.length + 1];
+ sectionKeys[0] = sectionKey;
+
+ if (moreSectionKeys.length > 0) {
+ arraycopy(moreSectionKeys, 0, sectionKeys, 1, moreSectionKeys.length);
+ }
+
+ return isKeyInSections(key, sectionKeys);
+ }
+
+ /**
+ * Calculates the current section key and compares it to the given section keys.
+ *
+ * @param key the fully qualified entry key, not {@code null}
+ * @param sectionKeys the section keys, not {@code null}
+ *
+ * @return true, if the entry is in one of the given sections
+ */
+ public static boolean isKeyInSections(String key, String[] sectionKeys) {
+ Objects.requireNonNull(key, "Key must be given.");
+ Objects.requireNonNull(sectionKeys, "Section keys must be given.");
+
+ boolean result = false;
+
for (String areaKey : sectionKeys) {
+ if (areaKey == null) {
+ continue;
+ }
+
if (isKeyInSection(key, areaKey)) {
- return true;
+ result = true;
+ break;
}
}
- return false;
+
+ return result;
}
/**
@@ -117,10 +172,12 @@ public final class PropertySourceFunctions {
*/
public static Set<String> sections(Map<String, String> properties) {
final Set<String> areas = new HashSet<>();
- for (String s : properties.keySet()) {
- int index = s.lastIndexOf('.');
+ for (String key : properties.keySet()) {
+ String normalizedKey = normalizeKey(key);
+
+ int index = normalizedKey.lastIndexOf('.');
if (index > 0) {
- areas.add(s.substring(0, index));
+ areas.add(normalizedKey.substring(0, index));
} else {
areas.add("<root>");
}
@@ -139,15 +196,19 @@ public final class PropertySourceFunctions {
*/
public static Set<String> transitiveSections(Map<String, String> properties) {
final Set<String> transitiveAreas = new HashSet<>();
- for (String s : sections(properties)) {
- int index = s.lastIndexOf('.');
- if (index < 0) {
+ for (String section : sections(properties)) {
+ section = normalizeSectionKey(section);
+
+ int index = section.lastIndexOf('.');
+ if (index < 0 && section.isEmpty()) {
transitiveAreas.add("<root>");
+ } if (index < 0) {
+ transitiveAreas.add(section);
} else {
while (index > 0) {
- s = s.substring(0, index);
- transitiveAreas.add(s);
- index = s.lastIndexOf('.');
+ section = section.substring(0, index);
+ transitiveAreas.add(section);
+ index = section.lastIndexOf('.');
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cdd7f9c0/modules/functions/src/main/java/org/apache/tamaya/functions/ValueMappedPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/ValueMappedPropertySource.java b/modules/functions/src/main/java/org/apache/tamaya/functions/ValueMappedPropertySource.java
index 6264a43..dfb128f 100644
--- a/modules/functions/src/main/java/org/apache/tamaya/functions/ValueMappedPropertySource.java
+++ b/modules/functions/src/main/java/org/apache/tamaya/functions/ValueMappedPropertySource.java
@@ -62,8 +62,10 @@ class ValueMappedPropertySource implements PropertySource{
@Override
public Map<String, PropertyValue> getProperties() {
Map<String,PropertyValue> result = new HashMap<>();
- for(PropertyValue val:source.getProperties().values()) {
- result.put(val.getKey(), val.toBuilder().setValue(valueFilter.mapProperty(val.getKey(), val.getValue())).build());
+ for(PropertyValue val : source.getProperties().values()) {
+ String mappedValue = valueFilter.mapProperty(val.getKey(), val.getValue());
+ PropertyValue value = val.toBuilder().setValue(mappedValue).build();
+ result.put(val.getKey(), value);
}
return result;
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cdd7f9c0/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
index 5f9b236..77ad378 100644
--- a/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryPropertySource.java
+++ b/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryPropertySource.java
@@ -68,7 +68,8 @@ public class InMemoryPropertySource implements PropertySource {
Map<String, PropertyValue> result = new HashMap<>();
for (Map.Entry<String, String> entry : properties.entrySet()) {
- result.put(entry.getKey(), PropertyValue.of(entry.getKey(), entry.getValue(), getName()));
+ PropertyValue value = PropertyValue.of(entry.getKey(), entry.getValue(), getName());
+ result.put(entry.getKey(), value);
}
return result;
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/cdd7f9c0/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
index 2e90fbc..aa51135 100644
--- a/modules/functions/src/test/java/org/apache/tamaya/functions/PropertySourceFunctionsTest.java
+++ b/modules/functions/src/test/java/org/apache/tamaya/functions/PropertySourceFunctionsTest.java
@@ -20,87 +20,348 @@ 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 static org.junit.Assert.*;
+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;
-/**
- * Created by Anatole on 01.10.2015.
- */
public class PropertySourceFunctionsTest {
+ @Ignore
@Test
public void testAddMetaData() throws Exception {
-// TODO implement test
+ 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 testIsKeyInSection() throws Exception {
-// TODO implement 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 {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testSections() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testTransitiveSections() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testSections1() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testTransitiveSections1() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testSectionsRecursive() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testSectionRecursive() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testStripSectionKeys() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testAddItems() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testAddItems1() throws Exception {
-// TODO implement test
+ throw new RuntimeException("Not implement or look at me!");
}
+ @Ignore
@Test
public void testReplaceItems() throws Exception {
-// TODO implement test
+ 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());
+// 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/cdd7f9c0/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
new file mode 100644
index 0000000..1ce5025
--- /dev/null
+++ b/modules/functions/src/test/java/org/apache/tamaya/functions/ValueMappedPropertySourceTest.java
@@ -0,0 +1,183 @@
+/*
+ * 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