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 Started to work on the JSON format implementation.
Date Sun, 01 Mar 2015 07:25:45 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 6e9a46b5b -> 967c546d1


TAMAYA-39 Started to work on the JSON format implementation.


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

Branch: refs/heads/master
Commit: 967c546d1d49506fdc5e0923bbaa0414a56cb998
Parents: 6e9a46b
Author: Oliver B. Fischer <plexus@apache.org>
Authored: Sun Mar 1 08:25:26 2015 +0100
Committer: Oliver B. Fischer <plexus@apache.org>
Committed: Sun Mar 1 08:25:26 2015 +0100

----------------------------------------------------------------------
 .../apache/tamaya/modules/json/JSONFormat.java  | 73 ++++++++++++++++++++
 .../tamaya/modules/json/JSONFormatTest.java     | 64 +++++++++++++++++
 2 files changed, 137 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/967c546d/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONFormat.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONFormat.java b/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONFormat.java
new file mode 100644
index 0000000..c7b680f
--- /dev/null
+++ b/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONFormat.java
@@ -0,0 +1,73 @@
+/*
+ * 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.modules.json;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationFormat;
+import org.apache.tamaya.format.InputStreamCloser;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Objects;
+import java.util.concurrent.locks.StampedLock;
+
+/**
+ * Implementation of the {@link org.apache.tamaya.format.ConfigurationFormat}
+ * able to read configuration properties represented in JSON
+ *
+ * @see <a href="http://www.json.org">JSON format specification</a>
+ */
+public class JSONFormat implements ConfigurationFormat {
+    /**
+     * Lock for internal synchronization.
+     */
+    private StampedLock lock = new StampedLock();
+
+    @Override
+    public boolean accepts(URL url) {
+        Objects.requireNonNull(url);
+
+        boolean isAFile = url.getProtocol().equals("file");
+        boolean isJSON = url.getPath().endsWith(".json");
+
+
+        return isAFile && isJSON;
+    }
+
+    @Override
+    public ConfigurationData readConfiguration(String resource, InputStream inputStream)
{
+
+        try (InputStream is = new InputStreamCloser(inputStream)){
+            ObjectMapper mapper = new ObjectMapper();
+            JsonNode root = mapper.readTree(is);
+
+
+        } catch (IOException e) {
+            throw new ConfigException("Failed to ");
+        }
+
+
+
+        throw new RuntimeException("Not implemented yet!");
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/967c546d/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatTest.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatTest.java
b/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatTest.java
new file mode 100644
index 0000000..f5f2d96
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.modules.json;
+
+
+import org.junit.Test;
+
+import java.net.URL;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+public class JSONFormatTest {
+    private JSONFormat format = new JSONFormat();
+
+    @Test(expected = NullPointerException.class)
+    public void acceptsNeedsNonNullParameter() throws Exception {
+        format.accepts(null);
+    }
+
+    @Test
+    public void aNonJSONFileBasedURLIsNotAccepted() throws Exception {
+        URL url = new URL("file:///etc/service/conf.conf");
+
+        assertThat(format.accepts(url), is(false));
+    }
+
+    @Test
+    public void aJSONFileBasedURLIsAccepted() throws Exception {
+        URL url = new URL("file:///etc/service/conf.json");
+
+        assertThat(format.accepts(url), is(true));
+    }
+
+    @Test
+    public void aHTTPBasedURLIsNotAccepted() throws Exception {
+        URL url = new URL("http://nowhere.somewhere/conf.json");
+
+        assertThat(format.accepts(url), is(false));
+    }
+
+    @Test
+    public void aFTPBasedURLIsNotAccepted() throws Exception {
+        URL url = new URL("ftp://nowhere.somewhere/a/b/c/d/conf.json");
+
+        assertThat(format.accepts(url), is(false));
+    }
+}
\ No newline at end of file


Mime
View raw message