tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ple...@apache.org
Subject incubator-tamaya git commit: TAMAYA-39 Enhenced the functionality of the property source for JSON.
Date Tue, 06 Jan 2015 21:24:08 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 70c9ae457 -> 817dca2c8


TAMAYA-39 Enhenced the functionality of the property source for JSON.


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

Branch: refs/heads/master
Commit: 817dca2c8517ca3340d146869e190e97b5a0c995
Parents: 70c9ae4
Author: Oliver B. Fischer <plexus@apache.org>
Authored: Tue Jan 6 22:22:58 2015 +0100
Committer: Oliver B. Fischer <plexus@apache.org>
Committed: Tue Jan 6 22:22:58 2015 +0100

----------------------------------------------------------------------
 .../tamaya/extras/json/JSONPropertySource.java  |   2 +-
 .../apache/tamaya/extras/json/JSONVisitor.java  |  58 +++++---
 .../extras/json/JSONPropertySourceTest.java     | 137 +++++++++++++++++++
 .../resources/configs/valid/empty-file.json     |   0
 .../configs/valid/empty-object-config.json      |   2 +
 .../resources/configs/valid/simple-config.json  |   5 -
 .../valid/simple-flat-string-only-config.json   |   5 +
 .../simple-nested-string-only-config-1.json     |   9 ++
 .../simple-nested-string-only-config-2.json     |   8 ++
 9 files changed, 202 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java
----------------------------------------------------------------------
diff --git a/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java
b/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java
index 6950f2c..7e6487c 100644
--- a/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java
+++ b/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONPropertySource.java
@@ -91,7 +91,7 @@ public class JSONPropertySource
             this.values = values;
         }
         catch (Throwable t) {
-            throw new ConfigException(format("Failed to read properties from %s", source.getDescription()));
+            throw new ConfigException(format("Failed to read properties from %s", source.getDescription()),
t);
         }
 
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java
----------------------------------------------------------------------
diff --git a/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java b/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java
index fc929e1..15bf16b 100644
--- a/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java
+++ b/extras/json/src/main/java/org/apache/tamaya/extras/json/JSONVisitor.java
@@ -35,31 +35,48 @@ public class JSONVisitor {
     }
 
     public void run() {
-        Queue<VisitingContext> stack = new ArrayDeque<>();
+        Deque<VisitingContext> stack = new ArrayDeque<>();
 
         stack.add(new VisitingContext(rootNode));
-
-        while(!stack.peek().completed()) {
-            Map.Entry<String, JsonNode> current = stack.peek().nextElement();
-
-            if (current.getValue() instanceof ValueNode) {
-                System.out.println("I:" + current.getValue().asText());
-            } else {
-                // @todo
-                throw new ConfigException("");
-            }
-
-            System.out.println(current);
+        boolean goOn = stack.peek().hasNext();
+
+        if (goOn) {
+            do {
+                Map.Entry<String, JsonNode> current = stack.peek().nextElement();
+
+                if (current.getValue() instanceof ValueNode) {
+                    String key = stack.peek().getNSPrefix() + current.getKey();
+                    String value = current.getValue().asText();
+                    targetStore.put(key, value);
+                } else if (current.getValue() instanceof ObjectNode) {
+                    String key = stack.peek().getNSPrefix() + current.getKey();
+                    ObjectNode node = (ObjectNode) current.getValue();
+                    stack.push(new VisitingContext(node, key));
+                } else {
+                    throw new ConfigException("Internal failure while processing JSON document.");
+                }
+
+                goOn = stack.peek().hasNext();
+
+                while (!goOn && stack.size() > 0) {
+                    stack.remove();
+                    goOn = stack.size() > 0 ? stack.peek().hasNext() : false;
+                }
+            } while (goOn);
         }
-
-
     }
 
     private class VisitingContext {
+        private String namespace;
         private final ObjectNode node;
         private final Iterator<Map.Entry<String, JsonNode>> elements;
 
-        public VisitingContext(ObjectNode rootNode) {
+        public VisitingContext(ObjectNode node) {
+            this(node, "");
+        }
+
+        public VisitingContext(ObjectNode rootNode, String currentNamespace) {
+            namespace = currentNamespace;
             node = rootNode;
             elements = node.fields();
         }
@@ -69,8 +86,13 @@ public class JSONVisitor {
         }
 
 
-        public boolean completed() {
-            return elements.hasNext() == false;
+        public boolean hasNext() {
+            boolean hasNext = elements.hasNext();
+            return hasNext;
+        }
+
+        public String getNSPrefix() {
+            return namespace.isEmpty() ? namespace : namespace + ".";
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/test/java/org/apache/tamaya/extras/json/JSONPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/extras/json/src/test/java/org/apache/tamaya/extras/json/JSONPropertySourceTest.java
b/extras/json/src/test/java/org/apache/tamaya/extras/json/JSONPropertySourceTest.java
new file mode 100644
index 0000000..65c709f
--- /dev/null
+++ b/extras/json/src/test/java/org/apache/tamaya/extras/json/JSONPropertySourceTest.java
@@ -0,0 +1,137 @@
+/*
+ * 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.extras.json;
+
+import org.apache.tamaya.ConfigException;
+import org.hamcrest.CoreMatchers;
+import org.junit.Test;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+
+public class JSONPropertySourceTest {
+
+    @Test(expected = ConfigException.class)
+    public void emptyJSONFileResultsInConfigException() throws Exception {
+        URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/empty-file.json");
+
+        assertThat(configURL, CoreMatchers.notNullValue());
+
+        File configFile = new File(configURL.toURI());
+
+        JSONPropertySource source = new JSONPropertySource(configFile, 10);
+
+        source.getProperties();
+    }
+
+    @Test
+    public void canHandleEmptyJSONObject() throws Exception {
+        URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/empty-object-config.json");
+
+        assertThat(configURL, CoreMatchers.notNullValue());
+
+        File configFile = new File(configURL.toURI());
+
+        JSONPropertySource source = new JSONPropertySource(configFile, 10);
+
+        assertThat(source.getProperties().keySet(), hasSize(0));
+    }
+
+    @Test
+    public void canReadFlatStringOnlyJSONConfigFile() throws Exception {
+        URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/simple-flat-string-only-config.json");
+
+        assertThat(configURL, CoreMatchers.notNullValue());
+
+        File configFile = new File(configURL.toURI());
+
+        JSONPropertySource source = new JSONPropertySource(configFile, 10);
+
+        assertThat(source.getProperties().keySet(), hasSize(3));
+
+        Optional<String> keyA = source.get("a");
+        Optional<String> keyB = source.get("b");
+        Optional<String> keyC = source.get("c");
+
+        assertThat(keyA.isPresent(), is(true));
+        assertThat(keyA.get(), equalTo("A"));
+        assertThat(keyB.isPresent(), is(true));
+        assertThat(keyB.get(), is("B"));
+        assertThat(keyC.isPresent(), is(true));
+        assertThat(keyC.get(), is("C"));
+    }
+
+    @Test
+    public void canReadNestedStringOnlyJSONConfigFile() throws Exception {
+        URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/simple-nested-string-only-config-1.json");
+
+        assertThat(configURL, CoreMatchers.notNullValue());
+
+        File configFile = new File(configURL.toURI());
+
+        JSONPropertySource source = new JSONPropertySource(configFile, 10);
+
+        assertThat(source.getProperties().keySet(), hasSize(5));
+
+        Optional<String> keyb = source.get("b");
+        Optional<String> keyDO = source.get("d.o");
+        Optional<String> keyDP = source.get("d.p");
+
+        assertThat(keyb.isPresent(), is(true));
+        assertThat(keyb.get(), equalTo("B"));
+        assertThat(keyDO.isPresent(), is(true));
+        assertThat(keyDO.get(), equalTo("O"));
+        assertThat(keyDP.isPresent(), is(true));
+        assertThat(keyDP.get(), is("P"));
+    }
+
+    @Test
+    public void canReadNestedStringOnlyJSONConfigFileWithObjectInTheMiddle()
+            throws Exception {
+        URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/simple-nested-string-only-config-2.json");
+
+        assertThat(configURL, CoreMatchers.notNullValue());
+
+        File configFile = new File(configURL.toURI());
+
+        JSONPropertySource source = new JSONPropertySource(configFile, 10);
+
+        assertThat(source.getProperties().keySet(), hasSize(4));
+
+        Optional<String> keyA = source.get("a");
+        Optional<String> keyDO = source.get("b.o");
+        Optional<String> keyDP = source.get("b.p");
+        Optional<String> keyC = source.get("c");
+
+        assertThat(keyA.isPresent(), is(true));
+        assertThat(keyA.get(), is("A"));
+        assertThat(keyC.isPresent(), is(true));
+        assertThat(keyC.get(), equalTo("C"));
+        assertThat(keyDO.isPresent(), is(true));
+        assertThat(keyDO.get(), equalTo("O"));
+        assertThat(keyDP.isPresent(), is(true));
+        assertThat(keyDP.get(), is("P"));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/test/resources/configs/valid/empty-file.json
----------------------------------------------------------------------
diff --git a/extras/json/src/test/resources/configs/valid/empty-file.json b/extras/json/src/test/resources/configs/valid/empty-file.json
new file mode 100644
index 0000000..e69de29

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/test/resources/configs/valid/empty-object-config.json
----------------------------------------------------------------------
diff --git a/extras/json/src/test/resources/configs/valid/empty-object-config.json b/extras/json/src/test/resources/configs/valid/empty-object-config.json
new file mode 100644
index 0000000..7a73a41
--- /dev/null
+++ b/extras/json/src/test/resources/configs/valid/empty-object-config.json
@@ -0,0 +1,2 @@
+{
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/test/resources/configs/valid/simple-config.json
----------------------------------------------------------------------
diff --git a/extras/json/src/test/resources/configs/valid/simple-config.json b/extras/json/src/test/resources/configs/valid/simple-config.json
deleted file mode 100644
index c8ea179..0000000
--- a/extras/json/src/test/resources/configs/valid/simple-config.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
-  "a" : "A",
-  "b" : "B",
-  "c" : "C"
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/test/resources/configs/valid/simple-flat-string-only-config.json
----------------------------------------------------------------------
diff --git a/extras/json/src/test/resources/configs/valid/simple-flat-string-only-config.json
b/extras/json/src/test/resources/configs/valid/simple-flat-string-only-config.json
new file mode 100644
index 0000000..c8ea179
--- /dev/null
+++ b/extras/json/src/test/resources/configs/valid/simple-flat-string-only-config.json
@@ -0,0 +1,5 @@
+{
+  "a" : "A",
+  "b" : "B",
+  "c" : "C"
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/test/resources/configs/valid/simple-nested-string-only-config-1.json
----------------------------------------------------------------------
diff --git a/extras/json/src/test/resources/configs/valid/simple-nested-string-only-config-1.json
b/extras/json/src/test/resources/configs/valid/simple-nested-string-only-config-1.json
new file mode 100644
index 0000000..89df957
--- /dev/null
+++ b/extras/json/src/test/resources/configs/valid/simple-nested-string-only-config-1.json
@@ -0,0 +1,9 @@
+{
+  "a" : "A",
+  "b" : "B",
+  "c" : "C",
+  "d" : {
+    "o" : "O",
+    "p" : "P"
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/817dca2c/extras/json/src/test/resources/configs/valid/simple-nested-string-only-config-2.json
----------------------------------------------------------------------
diff --git a/extras/json/src/test/resources/configs/valid/simple-nested-string-only-config-2.json
b/extras/json/src/test/resources/configs/valid/simple-nested-string-only-config-2.json
new file mode 100644
index 0000000..c275153
--- /dev/null
+++ b/extras/json/src/test/resources/configs/valid/simple-nested-string-only-config-2.json
@@ -0,0 +1,8 @@
+{
+  "a" : "A",
+  "b" : {
+    "o" : "O",
+    "p" : "P"
+  },
+  "c" : "C"
+}


Mime
View raw message