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/21 22:09:50 UTC

[2/4] incubator-tamaya-extensions git commit: [TAMAYA-244] Wrote tests for FilteredPropertySource.

[TAMAYA-244] Wrote tests for FilteredPropertySource.


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/8551df16
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/8551df16
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/8551df16

Branch: refs/heads/master
Commit: 8551df163f42692d65273444c0b575d7d19b60b1
Parents: 6cc0d65
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Tue Mar 21 06:38:23 2017 +0100
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Tue Mar 21 11:24:47 2017 +0100

----------------------------------------------------------------------
 .../functions/FilteredPropertySource.java       |  14 +-
 .../functions/FilteredPropertySourceTest.java   | 213 +++++++++++++++++++
 .../functions/InMemoryPropertySource.java       |  83 ++++++++
 3 files changed, 305 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/8551df16/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredPropertySource.java b/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredPropertySource.java
index 10736aa..133862d 100644
--- a/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredPropertySource.java
+++ b/modules/functions/src/main/java/org/apache/tamaya/functions/FilteredPropertySource.java
@@ -46,7 +46,7 @@ class FilteredPropertySource implements PropertySource {
 
     @Override
     public int getOrdinal(){
-        return PropertySourceComparator.getOrdinal(baseSource);
+        return PropertySourceComparator.getOrdinal(getBaseSource());
     }
 
     @Override
@@ -56,7 +56,7 @@ class FilteredPropertySource implements PropertySource {
 
     @Override
     public PropertyValue get(String key) {
-        PropertyValue val = this.baseSource.get(key);
+        PropertyValue val = this.getBaseSource().get(key);
         if(val!=null && filter.test(val.getKey())) {
             return val;
         }
@@ -66,7 +66,7 @@ class FilteredPropertySource implements PropertySource {
     @Override
     public Map<String, PropertyValue> getProperties(){
         final Map<String,PropertyValue> result = new HashMap<>();
-        for(PropertyValue val: this.baseSource.getProperties().values()) {
+        for(PropertyValue val: this.getBaseSource().getProperties().values()) {
             if (filter.test(val.getKey())) {
                 result.put(val.getKey(), val);
             }
@@ -76,14 +76,18 @@ class FilteredPropertySource implements PropertySource {
 
     @Override
     public boolean isScannable() {
-        return baseSource.isScannable();
+        return getBaseSource().isScannable();
     }
 
     @Override
     public String toString() {
         return "FilteredPropertySource{" +
-                "baseSource=" + baseSource +
+                "baseSource=" + getBaseSource() +
                 ", filter=" + filter +
                 '}';
     }
+
+    protected PropertySource getBaseSource() {
+        return baseSource;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/8551df16/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
new file mode 100644
index 0000000..677711d
--- /dev/null
+++ b/modules/functions/src/test/java/org/apache/tamaya/functions/FilteredPropertySourceTest.java
@@ -0,0 +1,213 @@
+/*
+ * 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/8551df16/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
new file mode 100644
index 0000000..f927ca6
--- /dev/null
+++ b/modules/functions/src/test/java/org/apache/tamaya/functions/InMemoryPropertySource.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.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 void setName(String name) {
+        this.name = name;
+    }
+
+    @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()) {
+            result.put(entry.getKey(), PropertyValue.of(entry.getKey(), entry.getValue(), getName()));
+        }
+
+        return result;
+    }
+
+    @Override
+    public boolean isScannable() {
+        return isScannable;
+    }
+
+    public void setScannable(boolean scannable) {
+        isScannable = scannable;
+    }
+}