tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [05/15] incubator-tamaya git commit: TAMAYA-79: added non local tests for events. Added additional testing for module and related fixes.
Date Thu, 21 May 2015 07:40:23 GMT
TAMAYA-79: added non local tests for events.
Added additional testing for module and related fixes.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/3013dbd5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/3013dbd5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/3013dbd5

Branch: refs/heads/master
Commit: 3013dbd5b5c3a15bbd3a4b1c48abb92f71e49c72
Parents: 4048625
Author: anatole <anatole@apache.org>
Authored: Wed May 20 17:49:03 2015 +0200
Committer: anatole <anatole@apache.org>
Committed: Wed May 20 17:49:03 2015 +0200

----------------------------------------------------------------------
 .../examples/fileobserver/TestConfigView.java   |  90 ---------
 modules/events/pom.xml                          |   6 +
 .../tamaya/events/FrozenPropertySource.java     |   3 +-
 .../events/delta/ConfigurationChange.java       |   3 +-
 .../delta/PropertySourceChangeBuilder.java      |   4 +-
 .../tamaya/events/ConfigEventSupportTest.java   |  63 ++++++
 .../tamaya/events/FrozenPropertySourceTest.java |  94 +++++++++
 .../tamaya/events/ObservedConfigTest.java       | 138 ++++++++++----
 .../apache/tamaya/events/TestConfigView.java    |  90 +++++++++
 .../tamaya/events/TestObservingProvider.java    |   4 +-
 .../delta/ConfigurationContextChangeTest.java   | 133 +++++++++++++
 .../events/delta/PropertySourceChangeTest.java  | 191 +++++++++++++++++++
 .../internal/DefaultEventSupportSpiTest.java    |  65 +++++++
 .../events/src/test/resources/test.properties   |  21 ++
 14 files changed, 770 insertions(+), 135 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/examples/6-fileobserver-example/src/main/java/org/apache/tamaya/examples/fileobserver/TestConfigView.java
----------------------------------------------------------------------
diff --git a/examples/6-fileobserver-example/src/main/java/org/apache/tamaya/examples/fileobserver/TestConfigView.java
b/examples/6-fileobserver-example/src/main/java/org/apache/tamaya/examples/fileobserver/TestConfigView.java
deleted file mode 100644
index 386ca8d..0000000
--- a/examples/6-fileobserver-example/src/main/java/org/apache/tamaya/examples/fileobserver/TestConfigView.java
+++ /dev/null
@@ -1,90 +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.examples.fileobserver;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.ConfigOperator;
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.ConfigurationProvider;
-import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.spi.PropertyConverter;
-
-import java.util.List;
-import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import java.util.stream.Collectors;
-
-/**
- * Created by Anatole on 24.03.2015.
- */
-public class TestConfigView implements ConfigOperator{
-
-    private static final TestConfigView INSTANCE = new TestConfigView();
-
-    private TestConfigView(){}
-
-    public static ConfigOperator of(){
-        return INSTANCE;
-    }
-
-    @Override
-    public Configuration operate(Configuration config) {
-        return new Configuration() {
-            @Override
-            public Map<String, String> getProperties() {
-                return config.getProperties().entrySet().stream().filter(e -> e.getKey().startsWith("test")).collect(
-                        Collectors.toMap(en -> en.getKey(), en -> en.getValue()));
-            }
-            /**
-             * Accesses the current String value for the given key and tries to convert it
-             * using the {@link org.apache.tamaya.spi.PropertyConverter} instances provided
by the current
-             * {@link org.apache.tamaya.spi.ConfigurationContext}.
-             *
-             * @param key  the property's absolute, or relative path, e.g. @code
-             *             a/b/c/d.myProperty}.
-             * @param type The target type required, not null.
-             * @param <T>  the value type
-             * @return the converted value, never null.
-             */
-            @Override
-            public <T> T get(String key, TypeLiteral<T> type) {
-                String value = get(key);
-                if (value != null) {
-                    List<PropertyConverter<T>> converters = ConfigurationProvider.getConfigurationContext()
-                            .getPropertyConverters(type);
-                    for (PropertyConverter<T> converter : converters) {
-                        try {
-                            T t = converter.convert(value);
-                            if (t != null) {
-                                return t;
-                            }
-                        } catch (Exception e) {
-                            Logger.getLogger(getClass().getName())
-                                    .log(Level.FINEST, "PropertyConverter: " + converter
+ " failed to convert value: "
-                                            + value, e);
-                        }
-                    }
-                    throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName()
+ ": " + key);
-                }
-                return null;
-            }
-        };
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/pom.xml
----------------------------------------------------------------------
diff --git a/modules/events/pom.xml b/modules/events/pom.xml
index 8c157fa..e23dd1d 100644
--- a/modules/events/pom.xml
+++ b/modules/events/pom.xml
@@ -52,6 +52,12 @@ under the License.
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.4</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/main/java/org/apache/tamaya/events/FrozenPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/FrozenPropertySource.java
b/modules/events/src/main/java/org/apache/tamaya/events/FrozenPropertySource.java
index 0b9dc69..2c6eddb 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/FrozenPropertySource.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/FrozenPropertySource.java
@@ -112,8 +112,9 @@ public final class FrozenPropertySource implements PropertySource, Serializable
     @Override
     public String toString() {
         return "FrozenPropertySource{" +
-                "properties=" + properties +
+                "name=" + name +
                 ", ordinal=" + ordinal +
+                ", properties=" + properties +
                 '}';
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java
b/modules/events/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java
index fcc10d5..dffb835 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/delta/ConfigurationChange.java
@@ -118,7 +118,8 @@ public final class ConfigurationChange implements Serializable{
      * @return the number current added entries.
      */
     public int getAddedSize() {
-        return (int) this.changes.values().stream().filter((e) -> e.getOldValue() == null).count();
+        return (int) this.changes.values().stream().filter((e) -> e.getOldValue() == null
&&
+                e.getNewValue() != null).count();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/main/java/org/apache/tamaya/events/delta/PropertySourceChangeBuilder.java
----------------------------------------------------------------------
diff --git a/modules/events/src/main/java/org/apache/tamaya/events/delta/PropertySourceChangeBuilder.java
b/modules/events/src/main/java/org/apache/tamaya/events/delta/PropertySourceChangeBuilder.java
index 3aa0c18..b03e570 100644
--- a/modules/events/src/main/java/org/apache/tamaya/events/delta/PropertySourceChangeBuilder.java
+++ b/modules/events/src/main/java/org/apache/tamaya/events/delta/PropertySourceChangeBuilder.java
@@ -103,9 +103,9 @@ public final class PropertySourceChangeBuilder {
         for (Map.Entry<String, String> en : map2.getProperties().entrySet()) {
             String val = map1.get(en.getKey());
             if (val == null) {
-                changes.add(new PropertyChangeEvent(map1, en.getKey(), null, en.getValue()));
+                changes.add(new PropertyChangeEvent(map1, en.getKey(), en.getValue(), null));
             } else if (!val.equals(en.getValue())) {
-                changes.add(new PropertyChangeEvent(map1, en.getKey(), val, en.getValue()));
+                changes.add(new PropertyChangeEvent(map1, en.getKey(), en.getValue(), val));
             }
         }
         return changes;

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/java/org/apache/tamaya/events/ConfigEventSupportTest.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/ConfigEventSupportTest.java
b/modules/events/src/test/java/org/apache/tamaya/events/ConfigEventSupportTest.java
new file mode 100644
index 0000000..f56659e
--- /dev/null
+++ b/modules/events/src/test/java/org/apache/tamaya/events/ConfigEventSupportTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.events;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for {@link org.apache.tamaya.events.ConfigEventSupport}.
+ */
+public class ConfigEventSupportTest {
+
+    private String testAddListenerValue;
+
+    @Test
+    public void testAddRemoveListener() throws Exception {
+        Listener<String> testListener = new Listener<String>() {
+            @Override
+            public void onEvent(String event) {
+                testAddListenerValue = event;
+            }
+        };
+        ConfigEventSupport.addListener(testListener);
+        ConfigEventSupport.fireEvent("Event1", String.class);
+        assertEquals(testAddListenerValue, "Event1");
+        ConfigEventSupport.removeListener(testListener);
+        ConfigEventSupport.fireEvent("Event2", String.class);
+        assertEquals(testAddListenerValue, "Event1");
+    }
+
+    @Test
+    public void testFireEvent() throws Exception {
+        Listener<String> testListener = new Listener<String>() {
+            @Override
+            public void onEvent(String event) {
+                testAddListenerValue = event;
+            }
+        };
+        ConfigEventSupport.addListener(testListener);
+        ConfigEventSupport.fireEvent("Event1");
+        assertEquals(testAddListenerValue, "Event1");
+        ConfigEventSupport.removeListener(testListener);
+        ConfigEventSupport.fireEvent("Event2");
+        assertEquals(testAddListenerValue, "Event1");
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/java/org/apache/tamaya/events/FrozenPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/FrozenPropertySourceTest.java
b/modules/events/src/test/java/org/apache/tamaya/events/FrozenPropertySourceTest.java
new file mode 100644
index 0000000..40d3335
--- /dev/null
+++ b/modules/events/src/test/java/org/apache/tamaya/events/FrozenPropertySourceTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.events;
+
+import org.apache.tamaya.core.propertysource.SystemPropertySource;
+import org.apache.tamaya.spi.PropertySource;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for {@link org.apache.tamaya.events.FrozenPropertySource}.
+ */
+public class FrozenPropertySourceTest {
+
+    private static final PropertySource myPS = new SystemPropertySource();
+
+    @Test
+    public void testOf() throws Exception {
+        PropertySource ps = FrozenPropertySource.of(myPS);
+        assertNotNull(ps);
+    }
+
+    @Test
+    public void testGetName() throws Exception {
+        PropertySource ps = FrozenPropertySource.of(myPS);
+        String name = ps.getName();
+        assertNotNull(name);
+        assertEquals(name, ps.getName());
+    }
+
+    @Test
+    public void testGetOrdinal() throws Exception {
+        PropertySource ps = FrozenPropertySource.of(myPS);
+        assertEquals(myPS.getOrdinal(), ps.getOrdinal());
+    }
+
+    @Test
+    public void testGet() throws Exception {
+        PropertySource ps = FrozenPropertySource.of(myPS);
+        assertNotNull(ps);
+        myPS.getProperties().entrySet().forEach(
+                e -> assertEquals(ps.get(e.getKey()), e.getValue())
+        );
+    }
+
+    @Test
+    public void testGetProperties() throws Exception {
+        PropertySource ps = FrozenPropertySource.of(myPS);
+        assertNotNull(ps);
+        assertNotNull(ps.getProperties());
+        assertFalse(ps.getProperties().isEmpty());
+    }
+
+    @Test
+    public void testEquals() throws Exception {
+        PropertySource ps1 = FrozenPropertySource.of(myPS);
+        PropertySource ps2 = FrozenPropertySource.of(myPS);
+        assertEquals(ps1.getName(), ps2.getName());
+        assertEquals(ps1.getProperties(), ps2.getProperties());
+    }
+
+    @Test
+    public void testHashCode() throws Exception {
+        PropertySource ps1 = FrozenPropertySource.of(myPS);
+        PropertySource ps2 = FrozenPropertySource.of(myPS);
+        assertEquals(ps1.hashCode(), ps2.hashCode());
+    }
+
+    @Test
+    public void testToString() throws Exception {
+        PropertySource ps = FrozenPropertySource.of(myPS);
+        String toString = ps.toString();
+        assertNotNull(toString);
+        assertTrue(toString.contains("FrozenPropertySource"));
+        assertTrue(toString.contains(myPS.getName()));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java
b/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java
index 8e9296a..bc3d087 100644
--- a/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java
+++ b/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java
@@ -18,27 +18,124 @@
  */
 package org.apache.tamaya.events;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.tamaya.Configuration;
 import org.apache.tamaya.ConfigurationProvider;
 import org.junit.AfterClass;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.io.File;
+import java.io.IOException;
 import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.Map;
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertEquals;
 
 /**
  * Test (currently manual) to test configuration changes.
  */
 public class ObservedConfigTest {
 
+    private static Path getSourceFile(String name) throws Exception {
+        URL url = ObservedConfigTest.class.getResource("/data");
+        File testFile = new File(new File(url.toURI()), name);
+        return Paths.get(testFile.toURI());
+    }
+
+    private static Path getTargetFile(String name) {
+        File testFile = new File(TestObservingProvider.getTestDirectory(), name);
+        return Paths.get(testFile.toURI());
+    }
+
+    /**
+     * Test method that periodically prints out what is happening.
+     */
+    public static void main() {
+        while (true) {
+            System.out.println("1: " + ConfigurationProvider.getConfiguration().get("1"));
+            System.out.println("2: " + ConfigurationProvider.getConfiguration().get("2"));
+            System.out.println("3: " + ConfigurationProvider.getConfiguration().get("3"));
+            System.out.println("4: " + ConfigurationProvider.getConfiguration().get("4"));
+            System.out.println("5: " + ConfigurationProvider.getConfiguration().get("5"));
+            System.out.println("6: " + ConfigurationProvider.getConfiguration().get("6"));
+            System.out.println("=======================================================================");
+            try {
+                Thread.sleep(2000L);
+            } catch (Exception e) {
+                // stop here...
+            }
+        }
+    }
+
+    @AfterClass
+    public static void cleanup() throws Exception {
+        // cleanup directory
+        Files.deleteIfExists(getTargetFile("test1.properties"));
+        Files.deleteIfExists(getTargetFile("test2.properties"));
+        Files.deleteIfExists(getTargetFile("test3.properties"));
+    }
+
+    @Before
+    public void setup() throws IOException {
+        // create some temporary config
+        Path tempDir = Files.createTempDirectory("observedFolder");
+
+        TestObservingProvider.propertyLocation = tempDir;
+
+        FileUtils.copyInputStreamToFile(
+                getClass().getResourceAsStream("/test.properties"),
+                new File(tempDir.toFile(), "test.properties"));
+    }
+
+    public void testInitialConfig() throws IOException {
+        Configuration config = ConfigurationProvider.getConfiguration().with(TestConfigView.of());
+
+        Map<String, String> props = config.getProperties();
+
+        assertEquals(props.get("test"), "test2");
+        assertEquals(props.get("testValue1"), "value");
+        assertNull(props.get("a"));
+
+    }
+
+    @Test
+    public void testChangingConfig() throws IOException {
+        Configuration config = ConfigurationProvider.getConfiguration().with(TestConfigView.of());
+
+        Map<String, String> props = config.getProperties();
+        assertEquals(props.get("test"), "test2");
+        assertEquals(props.get("testValue1"), "value");
+        assertNull(props.get("testValue2"));
+
+        //insert a new properties file into the tempdirectory
+        FileUtils.writeStringToFile(
+                new File(TestObservingProvider.propertyLocation.toFile(), "test2.properties"),
+                "testValue2=anotherValue");
+
+        try {
+            Thread.sleep(10000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+
+        config = ConfigurationProvider.getConfiguration().with(TestConfigView.of());
+
+        props = config.getProperties();
+
+        assertEquals(props.get("test"), "test2");
+        assertEquals(props.get("testValue1"), "value");
+        assertEquals(props.get("testValue2"), "anotherValue");
+    }
+
     @Test
     //Y TODO Check tests later
-    public void testConfigChanges()throws Exception{
+    public void testConfigChanges() throws Exception {
 //        // test empty directory
 //        testEmpty();
 //        // add a file, test for changes
@@ -85,17 +182,6 @@ public class ObservedConfigTest {
 //        testEmpty();
     }
 
-    private static Path getSourceFile(String name) throws Exception{
-        URL url = ObservedConfigTest.class.getResource("/data");
-        File testFile = new File(new File(url.toURI()), name);
-        return Paths.get(testFile.toURI());
-    }
-
-    private static Path getTargetFile(String name) {
-        File testFile = new File(TestObservingProvider.getTestDirectory(), name);
-        return Paths.get(testFile.toURI());
-    }
-
     private void testEmpty() {
         assertNull(ConfigurationProvider.getConfiguration().get("1"));
         assertNull(ConfigurationProvider.getConfiguration().get("2"));
@@ -142,32 +228,4 @@ public class ObservedConfigTest {
         assertNull(ConfigurationProvider.getConfiguration().get("6"));
     }
 
-    /**
-     * Test method that periodically prints out what is happening.
-     */
-    public static void main() {
-        while(true){
-            System.out.println("1: " + ConfigurationProvider.getConfiguration().get("1"));
-            System.out.println("2: " + ConfigurationProvider.getConfiguration().get("2"));
-            System.out.println("3: " + ConfigurationProvider.getConfiguration().get("3"));
-            System.out.println("4: " + ConfigurationProvider.getConfiguration().get("4"));
-            System.out.println("5: " + ConfigurationProvider.getConfiguration().get("5"));
-            System.out.println("6: " + ConfigurationProvider.getConfiguration().get("6"));
-            System.out.println("=======================================================================");
-            try {
-                Thread.sleep(2000L);
-            } catch (Exception e) {
-                // stop here...
-            }
-        }
-    }
-
-    @AfterClass
-    public static void cleanup()throws Exception{
-        // cleanup directory
-        Files.deleteIfExists(getTargetFile("test1.properties"));
-        Files.deleteIfExists(getTargetFile("test2.properties"));
-        Files.deleteIfExists(getTargetFile("test3.properties"));
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/java/org/apache/tamaya/events/TestConfigView.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/TestConfigView.java b/modules/events/src/test/java/org/apache/tamaya/events/TestConfigView.java
new file mode 100644
index 0000000..ab6135d
--- /dev/null
+++ b/modules/events/src/test/java/org/apache/tamaya/events/TestConfigView.java
@@ -0,0 +1,90 @@
+/*
+ * 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.events;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.ConfigOperator;
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+/**
+ * Created by Anatole on 24.03.2015.
+ */
+public class TestConfigView implements ConfigOperator{
+
+    private static final TestConfigView INSTANCE = new TestConfigView();
+
+    private TestConfigView(){}
+
+    public static ConfigOperator of(){
+        return INSTANCE;
+    }
+
+    @Override
+    public Configuration operate(Configuration config) {
+        return new Configuration() {
+            @Override
+            public Map<String, String> getProperties() {
+                return config.getProperties().entrySet().stream().filter(e -> e.getKey().startsWith("test")).collect(
+                        Collectors.toMap(en -> en.getKey(), en -> en.getValue()));
+            }
+            /**
+             * Accesses the current String value for the given key and tries to convert it
+             * using the {@link org.apache.tamaya.spi.PropertyConverter} instances provided
by the current
+             * {@link org.apache.tamaya.spi.ConfigurationContext}.
+             *
+             * @param key  the property's absolute, or relative path, e.g. @code
+             *             a/b/c/d.myProperty}.
+             * @param type The target type required, not null.
+             * @param <T>  the value type
+             * @return the converted value, never null.
+             */
+            @Override
+            public <T> T get(String key, TypeLiteral<T> type) {
+                String value = get(key);
+                if (value != null) {
+                    List<PropertyConverter<T>> converters = ConfigurationProvider.getConfigurationContext()
+                            .getPropertyConverters(type);
+                    for (PropertyConverter<T> converter : converters) {
+                        try {
+                            T t = converter.convert(value);
+                            if (t != null) {
+                                return t;
+                            }
+                        } catch (Exception e) {
+                            Logger.getLogger(getClass().getName())
+                                    .log(Level.FINEST, "PropertyConverter: " + converter
+ " failed to convert value: "
+                                            + value, e);
+                        }
+                    }
+                    throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName()
+ ": " + key);
+                }
+                return null;
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java
b/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java
index 5b5a3aa..775de58 100644
--- a/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java
+++ b/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java
@@ -40,8 +40,10 @@ import java.util.logging.Logger;
  */
 public class TestObservingProvider extends ObservingPropertySourceProvider{
 
+    public static Path propertyLocation;
+
     public TestObservingProvider(){
-        super(Paths.get(getTestPath()),
+        super(propertyLocation,
                 new PropertiesFormat());
         Logger.getLogger(getClass().getName()).info("Using test directory: " + getTestPath());
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/java/org/apache/tamaya/events/delta/ConfigurationContextChangeTest.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/delta/ConfigurationContextChangeTest.java
b/modules/events/src/test/java/org/apache/tamaya/events/delta/ConfigurationContextChangeTest.java
new file mode 100644
index 0000000..5640b81
--- /dev/null
+++ b/modules/events/src/test/java/org/apache/tamaya/events/delta/ConfigurationContextChangeTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.events.delta;
+
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.core.propertysource.SystemPropertySource;
+import org.apache.tamaya.spi.PropertySource;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Test class for {@link org.apache.tamaya.events.delta.ConfigurationContextChange}.
+ */
+public class ConfigurationContextChangeTest {
+
+    @Test
+    public void testEmptyChangeSet() throws Exception {
+        ConfigurationContextChange change = ConfigurationContextChange.emptyChangeSet();
+        assertNotNull(change);
+        assertTrue(change.isEmpty());
+    }
+
+    @Test
+    public void testGetVersion() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().build();
+        assertNotNull(change.getVersion());
+        change = ConfigurationContextChangeBuilder.of().setVersion("version2").build();
+        assertNotNull(change.getVersion());
+        assertEquals("version2", change.getVersion());
+    }
+
+    @Test
+    public void testGetTimestamp() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue((System.currentTimeMillis() - change.getTimestamp()) <= 10L);
+        change = ConfigurationContextChangeBuilder.of().setTimestamp(10L).build();
+        assertEquals(10L, change.getTimestamp());
+    }
+
+    @Test
+    public void testGetPropertySourceChanges() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getPropertySourceChanges(). isEmpty());
+        change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getPropertySourceChanges(). isEmpty());
+    }
+
+    @Test
+    public void testGetPropertySourceUpdates() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getPropertySourceChanges(). isEmpty());
+        change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getPropertySourceUpdates(). isEmpty());
+    }
+
+    @Test
+    public void testGetRemovedPropertySources() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getPropertySourceChanges(). isEmpty());
+        change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getRemovedPropertySources(). isEmpty());
+    }
+
+    @Test
+    public void testGetAddedPropertySources() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getPropertySourceChanges(). isEmpty());
+        change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getAddedPropertySources().isEmpty());
+    }
+
+    @Test
+    public void testGetUpdatedPropertySources() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getPropertySourceChanges(). isEmpty());
+        change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.getUpdatedPropertySources().isEmpty());
+    }
+
+    @Test
+    public void testIsAffected() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        PropertySource ps = new SystemPropertySource();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().changedPropertySource(
+                PropertySourceChangeBuilder.of(ps, ChangeType.UPDATED).build()
+        ).build();
+        String toString = change.toString();
+        assertTrue(change.isAffected(ps));
+    }
+
+    @Test
+    public void testIsEmpty() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().build();
+        assertTrue(change.isEmpty());
+        change = ConfigurationContextChangeBuilder.of().newPropertySource(new SystemPropertySource()).build();
+        assertFalse(change.isEmpty());
+    }
+
+    @Test
+    public void testToString() throws Exception {
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ConfigurationContextChange change = ConfigurationContextChangeBuilder.of().newPropertySource(new
SystemPropertySource()).build();
+        String toString = change.toString();
+        assertNotNull(toString);
+        assertTrue(toString.contains(new SystemPropertySource().getName()));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java
b/modules/events/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java
new file mode 100644
index 0000000..b3b6145
--- /dev/null
+++ b/modules/events/src/test/java/org/apache/tamaya/events/delta/PropertySourceChangeTest.java
@@ -0,0 +1,191 @@
+/*
+ * 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.events.delta;
+
+import org.apache.tamaya.core.propertysource.EnvironmentPropertySource;
+import org.apache.tamaya.core.propertysource.SystemPropertySource;
+import org.apache.tamaya.resource.PropertySourceBuilder;
+import org.apache.tamaya.spi.PropertySource;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for {@link org.apache.tamaya.events.delta.PropertySourceChange} and its builder.
+ */
+public class PropertySourceChangeTest {
+
+    private static final PropertySource myPS = new SystemPropertySource();
+
+    @Test
+    public void testGetChangeType() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.DELETED).build();
+        assertEquals(change.getChangeType(), ChangeType.DELETED);
+        change = PropertySourceChangeBuilder.of(myPS, ChangeType.UPDATED).build();
+        assertEquals(change.getChangeType(), ChangeType.UPDATED);
+    }
+
+    @Test
+    public void testGetPropertySource() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.DELETED).build();
+        assertEquals(change.getPropertySource().getName(), myPS.getName());
+    }
+
+    @Test
+    public void testGetVersion() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.DELETED)
+                .setVersion("myVersion1").build();
+        assertEquals(change.getVersion(), "myVersion1");
+    }
+
+    @Test
+    public void testGetTimestamp() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.DELETED)
+                .setTimestamp(111L).build();
+        assertEquals(change.getTimestamp(), 111L);
+    }
+
+    @Test
+    public void testGetEvents() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.DELETED)
+                .addChanges(
+                        new EnvironmentPropertySource()
+                ).build();
+        assertTrue(change.getEvents().size()>0);
+    }
+
+    @Test
+    public void testGetRemovedSize() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.UPDATED)
+                .addChanges(
+                        new EnvironmentPropertySource()
+                ).build();
+        assertTrue(change.getRemovedSize()>0);
+    }
+
+    @Test
+    public void testGetAddedSize() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.DELETED)
+                .addChanges(
+                        new EnvironmentPropertySource()
+                ).build();
+        assertTrue(change.getAddedSize()>0);
+    }
+
+    @Test
+    public void testGetUpdatedSize() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(myPS, ChangeType.DELETED)
+                .addChanges(
+                        new EnvironmentPropertySource()
+                ).build();
+        assertTrue(change.getUpdatedSize()==0);
+    }
+
+    @Test
+    public void testIsRemoved() throws Exception {
+        PropertySource ps1 = PropertySourceBuilder.of("test").add("key1", "value1")
+                .add("key2", "value2").build();
+        PropertySource ps2 = PropertySourceBuilder.of("test").add("key1", "value2")
+                .add("key3", "value3").build();
+        PropertySourceChange change = PropertySourceChangeBuilder.of(ps1, ChangeType.UPDATED)
+                .addChanges(
+                        ps2
+                ).build();
+        assertFalse(change.isRemoved("key1"));
+        assertTrue(change.isRemoved("key2"));
+        assertFalse(change.isRemoved("key3"));
+    }
+
+    @Test
+    public void testIsAdded() throws Exception {
+        PropertySource ps1 = PropertySourceBuilder.of("test").add("key1", "value1")
+                .add("key2", "value2").build();
+        PropertySource ps2 = PropertySourceBuilder.of("test").add("key1", "value2")
+                .add("key3", "value3").build();
+        PropertySourceChange change = PropertySourceChangeBuilder.of(ps1, ChangeType.UPDATED)
+                .addChanges(
+                        ps2
+                ).build();
+        assertTrue(change.isAdded("key3"));
+        assertFalse(change.isAdded("key2"));
+        assertFalse(change.isAdded("key1"));
+    }
+
+    @Test
+    public void testIsUpdated() throws Exception {
+        PropertySource ps1 = PropertySourceBuilder.of("test").add("key1", "value1")
+                .add("key2", "value2").build();
+        PropertySource ps2 = PropertySourceBuilder.of("test").add("key1", "value2")
+                .add("key3", "value3").build();
+        PropertySourceChange change = PropertySourceChangeBuilder.of(ps1, ChangeType.UPDATED)
+                .addChanges(
+                        ps2
+                ).build();
+        assertTrue(change.isUpdated("key1"));
+        assertFalse(change.isUpdated("key2"));
+        assertFalse(change.isUpdated("key3"));
+    }
+
+    @Test
+    public void testContainsKey() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(new EnvironmentPropertySource(),
ChangeType.DELETED)
+                .addChanges(
+                        myPS
+                ).build();
+        assertTrue(change.containsKey("java.version"));
+    }
+
+    @Test
+    public void testIsEmpty() throws Exception {
+        PropertySourceChange change = PropertySourceChangeBuilder.of(new EnvironmentPropertySource(),
ChangeType.DELETED)
+                .build();
+        assertTrue(change.isEmpty());
+        change = PropertySourceChangeBuilder.of(new EnvironmentPropertySource(), ChangeType.DELETED)
+                .addChanges(
+                        myPS
+                ).build();
+        assertFalse(change.isEmpty());
+    }
+
+    @Test
+    public void testOfAdded() throws Exception {
+        PropertySourceChange change = PropertySourceChange.ofAdded(myPS);
+        assertNotNull(change);
+        assertEquals(change.getChangeType(), ChangeType.NEW);
+    }
+
+    @Test
+    public void testOfDeleted() throws Exception {
+        PropertySourceChange change = PropertySourceChange.ofDeleted(myPS);
+        assertNotNull(change);
+        assertEquals(change.getChangeType(), ChangeType.DELETED);
+    }
+
+    @Test
+    public void testToString() throws Exception {
+        PropertySourceChange change = PropertySourceChange.ofAdded(myPS);
+        String toString = change.toString();
+        assertNotNull(toString);
+        assertTrue(toString.contains(myPS.getName()));
+        change = PropertySourceChange.ofDeleted(myPS);
+        toString = change.toString();
+        assertNotNull(toString);
+        assertTrue(toString.contains(myPS.getName()));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/java/org/apache/tamaya/events/internal/DefaultEventSupportSpiTest.java
----------------------------------------------------------------------
diff --git a/modules/events/src/test/java/org/apache/tamaya/events/internal/DefaultEventSupportSpiTest.java
b/modules/events/src/test/java/org/apache/tamaya/events/internal/DefaultEventSupportSpiTest.java
new file mode 100644
index 0000000..f650b9d
--- /dev/null
+++ b/modules/events/src/test/java/org/apache/tamaya/events/internal/DefaultEventSupportSpiTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.events.internal;
+
+import org.apache.tamaya.events.Listener;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests for {@link org.apache.tamaya.events.internal.DefaultEventSupportSpi}.
+ */
+public class DefaultEventSupportSpiTest {
+
+    private DefaultEventSupportSpi spi = new DefaultEventSupportSpi();
+    private String testAddListenerValue;
+
+    @Test
+    public void testAddListener() throws Exception {
+        Listener<String> testListener = new Listener<String>() {
+
+            @Override
+            public void onEvent(String event) {
+                testAddListenerValue = event;
+            }
+        };
+        spi.addListener(testListener);
+        spi.fireEvent("Event1", String.class);
+        assertEquals(testAddListenerValue, "Event1");
+        spi.removeListener(testListener);
+        spi.fireEvent("Event2", String.class);
+        assertEquals(testAddListenerValue, "Event1");
+
+    }
+
+    @Test
+    public void testRemoveListener() throws Exception {
+        Listener<String> testListener = new Listener<String>() {
+
+            @Override
+            public void onEvent(String event) {
+                testAddListenerValue = event;
+            }
+        };
+        spi.removeListener(testListener);
+        spi.removeListener(testListener);
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3013dbd5/modules/events/src/test/resources/test.properties
----------------------------------------------------------------------
diff --git a/modules/events/src/test/resources/test.properties b/modules/events/src/test/resources/test.properties
new file mode 100644
index 0000000..af06631
--- /dev/null
+++ b/modules/events/src/test/resources/test.properties
@@ -0,0 +1,21 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy current the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+testValue1=value
+test=test2
+a=b
\ No newline at end of file



Mime
View raw message