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 2015/03/14 01:00:52 UTC
[2/2] incubator-tamaya git commit: TAMAYA-39 Removed the modules
package for the package hierarchy.
TAMAYA-39 Removed the modules package for the package hierarchy.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/11acca95
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/11acca95
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/11acca95
Branch: refs/heads/master
Commit: 11acca954c368bf8d7fca62379c896539af77f71
Parents: 812b11e
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Sat Mar 14 01:00:12 2015 +0100
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Sat Mar 14 01:00:12 2015 +0100
----------------------------------------------------------------------
.../tamaya/json/JSONConfigurationFormat.java | 42 ++++
.../java/org/apache/tamaya/json/JSONFormat.java | 71 +++++++
.../apache/tamaya/json/JSONPropertySource.java | 199 +++++++++++++++++++
.../tamaya/json/JSONPropertySourceProvider.java | 64 ++++++
.../org/apache/tamaya/json/JSONVisitor.java | 107 ++++++++++
.../modules/json/JSONConfigurationFormat.java | 42 ----
.../apache/tamaya/modules/json/JSONFormat.java | 71 -------
.../tamaya/modules/json/JSONPropertySource.java | 199 -------------------
.../json/JSONPropertySourceProvider.java | 64 ------
.../apache/tamaya/modules/json/JSONVisitor.java | 107 ----------
...org.apache.tamaya.format.ConfigurationFormat | 2 +-
.../json/CommonJSONTestCaseCollection.java | 173 ++++++++++++++++
.../tamaya/json/ConfigurationDataUCD.java | 44 ++++
.../org/apache/tamaya/json/JSONFormatIT.java | 47 +++++
.../org/apache/tamaya/json/JSONFormatTest.java | 75 +++++++
...pertySourceProviderExistingConfigFileIT.java | 87 ++++++++
...opertySourceProviderMissingConfigFileIT.java | 69 +++++++
.../tamaya/json/JSONPropertySourceTest.java | 49 +++++
.../tamaya/json/JSONPropertySourceUCD.java | 39 ++++
.../apache/tamaya/json/UnifiedConfigData.java | 32 +++
.../json/CommonJSONTestCaseCollection.java | 173 ----------------
.../modules/json/ConfigurationDataUCD.java | 44 ----
.../tamaya/modules/json/JSONFormatIT.java | 47 -----
.../tamaya/modules/json/JSONFormatTest.java | 75 -------
...pertySourceProviderExistingConfigFileIT.java | 87 --------
...opertySourceProviderMissingConfigFileIT.java | 69 -------
.../modules/json/JSONPropertySourceTest.java | 49 -----
.../modules/json/JSONPropertySourceUCD.java | 39 ----
.../tamaya/modules/json/UnifiedConfigData.java | 32 ---
29 files changed, 1099 insertions(+), 1099 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/main/java/org/apache/tamaya/json/JSONConfigurationFormat.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/json/JSONConfigurationFormat.java b/modules/json/src/main/java/org/apache/tamaya/json/JSONConfigurationFormat.java
new file mode 100644
index 0000000..47950a7
--- /dev/null
+++ b/modules/json/src/main/java/org/apache/tamaya/json/JSONConfigurationFormat.java
@@ -0,0 +1,42 @@
+/*
+ * 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.json;
+
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationFormat;
+
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * Configuration format able to read JSON files and to turn them in
+ * {@link org.apache.tamaya.format.ConfigurationData} instances.
+ */
+public class JSONConfigurationFormat implements ConfigurationFormat {
+
+ @Override
+ public boolean accepts(URL url) {
+ throw new java.lang.RuntimeException("Not implemented yet!");
+ }
+
+ @Override
+ public ConfigurationData readConfiguration(String resource, InputStream inputStream) {
+ throw new java.lang.RuntimeException("Not implemented yet!");
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/main/java/org/apache/tamaya/json/JSONFormat.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/json/JSONFormat.java b/modules/json/src/main/java/org/apache/tamaya/json/JSONFormat.java
new file mode 100644
index 0000000..70613df
--- /dev/null
+++ b/modules/json/src/main/java/org/apache/tamaya/json/JSONFormat.java
@@ -0,0 +1,71 @@
+/*
+ * 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.json;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationDataBuilder;
+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.HashMap;
+import java.util.Objects;
+
+/**
+ * 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 {
+
+ @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);
+
+ HashMap<String, String> values = new HashMap<>();
+ JSONVisitor visitor = new JSONVisitor((ObjectNode) root, values);
+ visitor.run();
+
+ return ConfigurationDataBuilder.of(resource, this).addProperties(values)
+ .build();
+ } catch (IOException e) {
+ throw new ConfigException("Failed to read data from " + resource);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java b/modules/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java
new file mode 100644
index 0000000..2712f3f
--- /dev/null
+++ b/modules/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java
@@ -0,0 +1,199 @@
+/*
+ * 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.json;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.core.propertysource.DefaultOrdinal;
+import org.apache.tamaya.resource.ResourceResolver;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.ServiceContext;
+
+import java.io.File;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.StampedLock;
+
+import static java.lang.String.format;
+
+/**
+ * Property source based on a JSON file.
+ */
+public class JSONPropertySource
+ implements PropertySource {
+
+ private URL urlResource;
+ private int priority = DefaultOrdinal.FILE_PROPERTIES;
+
+ private HashMap<String, String> values;
+
+ /**
+ * Lock for internal synchronization.
+ */
+ private StampedLock propertySourceLock = new StampedLock();
+
+ public JSONPropertySource(URL resource) {
+ init(resource);
+ }
+
+ public JSONPropertySource(String resourcePath) {
+ this(resourcePath, DefaultOrdinal.FILE_PROPERTIES);
+ }
+
+ public JSONPropertySource(String resourcePath, int prio) {
+ priority = prio;
+
+ Optional<ResourceResolver> resolver = ServiceContext.getInstance().getService(ResourceResolver.class);
+
+ if (!resolver.isPresent()) {
+ throw new ConfigException("Unable to load " + ResourceResolver.class.getCanonicalName());
+ }
+
+ Collection<URL> resources = resolver.get().getResources(resourcePath);
+
+ if (resources.size() == 0) {
+ throw new ConfigException("Unable to find " + resourcePath);
+ } else if (resources.size() > 1) {
+ throw new ConfigException("Unable to resolve " + resourcePath + " to a single resource.");
+ }
+
+ URL url = resources.iterator().next();
+
+ init(url, prio);
+ }
+
+ public JSONPropertySource(File file) {
+ init(file);
+ }
+
+ private void init(File resource) {
+ init(resource, DefaultOrdinal.FILE_PROPERTIES);
+ }
+
+ private void init(File resource, int prio) {
+
+ try {
+ init(resource.toURI().toURL(), prio);
+ } catch (MalformedURLException e) {
+ throw new ConfigException(format("%s seems not to be a valid file.", resource), e);
+ }
+ }
+
+ private void init(URL resource) {
+ init(resource, DefaultOrdinal.FILE_PROPERTIES);
+ }
+
+ private void init(URL resource, int prio) {
+ priority = prio;
+ urlResource = resource;
+ }
+
+ public JSONPropertySource(File file, int priority) {
+ init(file, priority);
+ }
+
+ @Override
+ public int getOrdinal() {
+ Lock writeLock = propertySourceLock.asWriteLock();
+
+ try {
+ writeLock.lock();
+
+ if (values == null) {
+ readSource();
+ }
+ } finally {
+ writeLock.unlock();
+ }
+
+ return priority;
+ }
+
+ @Override
+ public String getName() {
+ return "json-properties";
+ }
+
+ @Override
+ public String get(String key) {
+ Objects.requireNonNull(key, "Key must not be null");
+
+ return getProperties().get(key);
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ Lock writeLock = propertySourceLock.asWriteLock();
+
+ try {
+ writeLock.lock();
+
+ if (values == null) {
+ readSource();
+ }
+
+ return Collections.unmodifiableMap(values);
+ } finally {
+ writeLock.unlock();
+ }
+ }
+
+ protected void readSource() {
+ try (InputStream is = urlResource.openStream()) {
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode root = mapper.readTree(is);
+
+ // @todo Add test for this. Oliver B. Fischer, 5. Jan. 2015
+ if (!(root instanceof ObjectNode)) {
+ throw new ConfigException("Currently only JSON objects are supported");
+ }
+
+ HashMap<String, String> values = new HashMap<>();
+ JSONVisitor visitor = new JSONVisitor((ObjectNode) root, values);
+ visitor.run();
+
+ this.values = values;
+
+ if (this.values.containsKey(TAMAYA_ORDINAL)) {
+ int newPriority = Integer.parseInt(this.values.get(TAMAYA_ORDINAL));
+ priority = newPriority;
+ this.values.remove(TAMAYA_ORDINAL);
+ }
+ }
+ catch (Throwable t) {
+ throw new ConfigException(format("Failed to read properties from %s", urlResource.toExternalForm()), t);
+ }
+
+ }
+
+ @Override
+ public boolean isScannable() {
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/main/java/org/apache/tamaya/json/JSONPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/json/JSONPropertySourceProvider.java b/modules/json/src/main/java/org/apache/tamaya/json/JSONPropertySourceProvider.java
new file mode 100644
index 0000000..23818ac
--- /dev/null
+++ b/modules/json/src/main/java/org/apache/tamaya/json/JSONPropertySourceProvider.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.json;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+
+import java.net.URL;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+
+import static java.util.stream.Collectors.toList;
+
+/**
+ * Provides all <a href="http://json.org">JSON</a>
+ * property sources found in the classpath
+ * in {@code META-INF/javaconfiguration.json}.
+ *
+ * @see PropertySourceProvider
+ */
+public class JSONPropertySourceProvider implements PropertySourceProvider {
+ public final static String DEFAULT_RESOURCE_NAME = "javaconfiguration.json";
+
+ @Override
+ public Collection<PropertySource> getPropertySources() {
+ List<PropertySource> sources;
+
+ try {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ Enumeration<URL> urls = classLoader.getResources("META-INF/" + DEFAULT_RESOURCE_NAME);
+
+ sources = Collections.list(urls)
+ .stream()
+ .map(JSONPropertySource::new)
+ .collect(toList());
+
+ } catch (Exception e) {
+ String msg = "Failure while loading JSON property sources.";
+
+ throw new ConfigException(msg, e);
+ }
+
+ return sources;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java b/modules/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java
new file mode 100644
index 0000000..d233005
--- /dev/null
+++ b/modules/json/src/main/java/org/apache/tamaya/json/JSONVisitor.java
@@ -0,0 +1,107 @@
+/*
+ * 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.json;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.ValueNode;
+import org.apache.tamaya.ConfigException;
+
+import java.util.*;
+
+/**
+ * Visitor implementation to read a JSON formatted input source.
+ */
+public class JSONVisitor {
+ private final ObjectNode rootNode;
+ private final HashMap<String, String> targetStore;
+
+ public JSONVisitor(ObjectNode startNode, HashMap<String, String> target) {
+ rootNode = startNode;
+ targetStore = target;
+ }
+
+ public void run() {
+ Deque<VisitingContext> stack = new ArrayDeque<>();
+
+ stack.add(new VisitingContext(rootNode));
+ 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 if (current.getValue() instanceof ArrayNode) {
+ throw new ConfigException("Arrays are not supported at the moment.");
+ } 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();
+ }
+ } while (goOn);
+ }
+ }
+
+ /**
+ * Context for a sub context visited.
+ */
+ private static class VisitingContext {
+ private String namespace;
+ private final ObjectNode node;
+ private final Iterator<Map.Entry<String, JsonNode>> elements;
+
+ public VisitingContext(ObjectNode node) {
+ this(node, "");
+ }
+
+ public VisitingContext(ObjectNode rootNode, String currentNamespace) {
+ namespace = currentNamespace;
+ node = rootNode;
+ elements = node.fields();
+ }
+
+ public Map.Entry<String, JsonNode> nextElement() {
+ return elements.next();
+ }
+
+
+ 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/11acca95/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONConfigurationFormat.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONConfigurationFormat.java b/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONConfigurationFormat.java
deleted file mode 100644
index fdc1907..0000000
--- a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONConfigurationFormat.java
+++ /dev/null
@@ -1,42 +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.modules.json;
-
-import org.apache.tamaya.format.ConfigurationData;
-import org.apache.tamaya.format.ConfigurationFormat;
-
-import java.io.InputStream;
-import java.net.URL;
-
-/**
- * Configuration format able to read JSON files and to turn them in
- * {@link org.apache.tamaya.format.ConfigurationData} instances.
- */
-public class JSONConfigurationFormat implements ConfigurationFormat {
-
- @Override
- public boolean accepts(URL url) {
- throw new java.lang.RuntimeException("Not implemented yet!");
- }
-
- @Override
- public ConfigurationData readConfiguration(String resource, InputStream inputStream) {
- throw new java.lang.RuntimeException("Not implemented yet!");
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/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
deleted file mode 100644
index 21def33..0000000
--- a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONFormat.java
+++ /dev/null
@@ -1,71 +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.modules.json;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.format.ConfigurationData;
-import org.apache.tamaya.format.ConfigurationDataBuilder;
-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.HashMap;
-import java.util.Objects;
-
-/**
- * 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 {
-
- @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);
-
- HashMap<String, String> values = new HashMap<>();
- JSONVisitor visitor = new JSONVisitor((ObjectNode) root, values);
- visitor.run();
-
- return ConfigurationDataBuilder.of(resource, this).addProperties(values)
- .build();
- } catch (IOException e) {
- throw new ConfigException("Failed to read data from " + resource);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java b/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java
deleted file mode 100644
index c4471ad..0000000
--- a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java
+++ /dev/null
@@ -1,199 +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.modules.json;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.core.propertysource.DefaultOrdinal;
-import org.apache.tamaya.resource.ResourceResolver;
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.ServiceContext;
-
-import java.io.File;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Optional;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.StampedLock;
-
-import static java.lang.String.format;
-
-/**
- * Property source based on a JSON file.
- */
-public class JSONPropertySource
- implements PropertySource {
-
- private URL urlResource;
- private int priority = DefaultOrdinal.FILE_PROPERTIES;
-
- private HashMap<String, String> values;
-
- /**
- * Lock for internal synchronization.
- */
- private StampedLock propertySourceLock = new StampedLock();
-
- public JSONPropertySource(URL resource) {
- init(resource);
- }
-
- public JSONPropertySource(String resourcePath) {
- this(resourcePath, DefaultOrdinal.FILE_PROPERTIES);
- }
-
- public JSONPropertySource(String resourcePath, int prio) {
- priority = prio;
-
- Optional<ResourceResolver> resolver = ServiceContext.getInstance().getService(ResourceResolver.class);
-
- if (!resolver.isPresent()) {
- throw new ConfigException("Unable to load " + ResourceResolver.class.getCanonicalName());
- }
-
- Collection<URL> resources = resolver.get().getResources(resourcePath);
-
- if (resources.size() == 0) {
- throw new ConfigException("Unable to find " + resourcePath);
- } else if (resources.size() > 1) {
- throw new ConfigException("Unable to resolve " + resourcePath + " to a single resource.");
- }
-
- URL url = resources.iterator().next();
-
- init(url, prio);
- }
-
- public JSONPropertySource(File file) {
- init(file);
- }
-
- private void init(File resource) {
- init(resource, DefaultOrdinal.FILE_PROPERTIES);
- }
-
- private void init(File resource, int prio) {
-
- try {
- init(resource.toURI().toURL(), prio);
- } catch (MalformedURLException e) {
- throw new ConfigException(format("%s seems not to be a valid file.", resource), e);
- }
- }
-
- private void init(URL resource) {
- init(resource, DefaultOrdinal.FILE_PROPERTIES);
- }
-
- private void init(URL resource, int prio) {
- priority = prio;
- urlResource = resource;
- }
-
- public JSONPropertySource(File file, int priority) {
- init(file, priority);
- }
-
- @Override
- public int getOrdinal() {
- Lock writeLock = propertySourceLock.asWriteLock();
-
- try {
- writeLock.lock();
-
- if (values == null) {
- readSource();
- }
- } finally {
- writeLock.unlock();
- }
-
- return priority;
- }
-
- @Override
- public String getName() {
- return "json-properties";
- }
-
- @Override
- public String get(String key) {
- Objects.requireNonNull(key, "Key must not be null");
-
- return getProperties().get(key);
- }
-
- @Override
- public Map<String, String> getProperties() {
- Lock writeLock = propertySourceLock.asWriteLock();
-
- try {
- writeLock.lock();
-
- if (values == null) {
- readSource();
- }
-
- return Collections.unmodifiableMap(values);
- } finally {
- writeLock.unlock();
- }
- }
-
- protected void readSource() {
- try (InputStream is = urlResource.openStream()) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode root = mapper.readTree(is);
-
- // @todo Add test for this. Oliver B. Fischer, 5. Jan. 2015
- if (!(root instanceof ObjectNode)) {
- throw new ConfigException("Currently only JSON objects are supported");
- }
-
- HashMap<String, String> values = new HashMap<>();
- JSONVisitor visitor = new JSONVisitor((ObjectNode) root, values);
- visitor.run();
-
- this.values = values;
-
- if (this.values.containsKey(TAMAYA_ORDINAL)) {
- int newPriority = Integer.parseInt(this.values.get(TAMAYA_ORDINAL));
- priority = newPriority;
- this.values.remove(TAMAYA_ORDINAL);
- }
- }
- catch (Throwable t) {
- throw new ConfigException(format("Failed to read properties from %s", urlResource.toExternalForm()), t);
- }
-
- }
-
- @Override
- public boolean isScannable() {
- return true;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySourceProvider.java b/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySourceProvider.java
deleted file mode 100644
index 426e0ed..0000000
--- a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySourceProvider.java
+++ /dev/null
@@ -1,64 +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.modules.json;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertySourceProvider;
-
-import java.net.URL;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.List;
-
-import static java.util.stream.Collectors.toList;
-
-/**
- * Provides all <a href="http://json.org">JSON</a>
- * property sources found in the classpath
- * in {@code META-INF/javaconfiguration.json}.
- *
- * @see PropertySourceProvider
- */
-public class JSONPropertySourceProvider implements PropertySourceProvider {
- public final static String DEFAULT_RESOURCE_NAME = "javaconfiguration.json";
-
- @Override
- public Collection<PropertySource> getPropertySources() {
- List<PropertySource> sources;
-
- try {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- Enumeration<URL> urls = classLoader.getResources("META-INF/" + DEFAULT_RESOURCE_NAME);
-
- sources = Collections.list(urls)
- .stream()
- .map(JSONPropertySource::new)
- .collect(toList());
-
- } catch (Exception e) {
- String msg = "Failure while loading JSON property sources.";
-
- throw new ConfigException(msg, e);
- }
-
- return sources;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONVisitor.java
----------------------------------------------------------------------
diff --git a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONVisitor.java b/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONVisitor.java
deleted file mode 100644
index 840387e..0000000
--- a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONVisitor.java
+++ /dev/null
@@ -1,107 +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.modules.json;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.fasterxml.jackson.databind.node.ValueNode;
-import org.apache.tamaya.ConfigException;
-
-import java.util.*;
-
-/**
- * Visitor implementation to read a JSON formatted input source.
- */
-public class JSONVisitor {
- private final ObjectNode rootNode;
- private final HashMap<String, String> targetStore;
-
- public JSONVisitor(ObjectNode startNode, HashMap<String, String> target) {
- rootNode = startNode;
- targetStore = target;
- }
-
- public void run() {
- Deque<VisitingContext> stack = new ArrayDeque<>();
-
- stack.add(new VisitingContext(rootNode));
- 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 if (current.getValue() instanceof ArrayNode) {
- throw new ConfigException("Arrays are not supported at the moment.");
- } 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();
- }
- } while (goOn);
- }
- }
-
- /**
- * Context for a sub context visited.
- */
- private static class VisitingContext {
- private String namespace;
- private final ObjectNode node;
- private final Iterator<Map.Entry<String, JsonNode>> elements;
-
- public VisitingContext(ObjectNode node) {
- this(node, "");
- }
-
- public VisitingContext(ObjectNode rootNode, String currentNamespace) {
- namespace = currentNamespace;
- node = rootNode;
- elements = node.fields();
- }
-
- public Map.Entry<String, JsonNode> nextElement() {
- return elements.next();
- }
-
-
- 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/11acca95/modules/json/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
----------------------------------------------------------------------
diff --git a/modules/json/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat b/modules/json/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
index a482df3..a843cbe 100644
--- a/modules/json/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
+++ b/modules/json/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
@@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#
-org.apache.tamaya.modules.json.JSONFormat
\ No newline at end of file
+org.apache.tamaya.json.JSONFormat
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/CommonJSONTestCaseCollection.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/CommonJSONTestCaseCollection.java b/modules/json/src/test/java/org/apache/tamaya/json/CommonJSONTestCaseCollection.java
new file mode 100644
index 0000000..54e8994
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/CommonJSONTestCaseCollection.java
@@ -0,0 +1,173 @@
+/*
+ * 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.json;
+
+import org.apache.tamaya.ConfigException;
+import org.hamcrest.CoreMatchers;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+import java.net.URL;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+
+/**
+ * Class with a collection of common test cases each JSON processing
+ * class must be able to pass.
+ */
+public abstract class CommonJSONTestCaseCollection {
+
+ abstract UnifiedConfigData getPropertiesFrom(URL source) throws Exception;
+
+ @Test
+ public void canReadNestedStringOnlyJSONConfigFile() throws Exception {
+ URL configURL = JSONPropertySourceTest.class
+ .getResource("/configs/valid/simple-nested-string-only-config-1.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ UnifiedConfigData properties = getPropertiesFrom(configURL);
+
+ assertThat(properties.getProperties().keySet(), hasSize(5));
+
+ String keyB = properties.get("b");
+ String keyDO = properties.get("d.o");
+ String keyDP = properties.get("d.p");
+
+ assertThat(keyB, notNullValue());
+ assertThat(keyB, equalTo("B"));
+ assertThat(keyDO, notNullValue());
+ assertThat(keyDO, equalTo("O"));
+ assertThat(keyDP, Matchers.notNullValue());
+ assertThat(keyDP, 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());
+
+ UnifiedConfigData properties = getPropertiesFrom(configURL);
+
+ assertThat(properties.getProperties().keySet(), hasSize(4));
+
+ String keyA = properties.get("a");
+ String keyDO = properties.get("b.o");
+ String keyDP = properties.get("b.p");
+ String keyC = properties.get("c");
+
+ assertThat(keyA, notNullValue());
+ assertThat(keyA, is("A"));
+ assertThat(keyC, notNullValue());
+ assertThat(keyC, equalTo("C"));
+ assertThat(keyDO, notNullValue());
+ assertThat(keyDO, equalTo("O"));
+ assertThat(keyDP, notNullValue());
+ assertThat(keyDP, is("P"));
+ }
+
+ @Test(expected = ConfigException.class)
+ public void canHandleIllegalJSONFileWhichContainsAnArray() throws Exception {
+ URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/with-array.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ getPropertiesFrom(configURL).getProperties();
+ }
+
+ @Test(expected = ConfigException.class)
+ public void canHandleIllegalJSONFileConsistingOfOneOpeningBracket() throws Exception {
+ URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/only-opening-bracket.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ getPropertiesFrom(configURL).getProperties();
+ }
+
+ @Test(expected = ConfigException.class)
+ public void canHandleIllegalJSONFileWhichIsEmpty() throws Exception {
+ URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/empty-file.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ getPropertiesFrom(configURL).getProperties();
+ }
+
+ @Test
+ public void priorityInConfigFileOverwriteExplicitlyGivenPriority() throws Exception {
+ URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/with-explicit-priority.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ UnifiedConfigData properties = getPropertiesFrom(configURL);
+
+ assertThat(properties.getOrdinal(), is(16784));
+ }
+
+ @Test
+ public void canReadFlatStringOnlyJSONConfigFile() throws Exception {
+ URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/simple-flat-string-only-config.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ UnifiedConfigData properties = getPropertiesFrom(configURL);
+
+ assertThat(properties.getProperties().keySet(), hasSize(3));
+
+ String keyA = properties.get("a");
+ String keyB = properties.get("b");
+ String keyC = properties.get("c");
+
+ assertThat(keyA, notNullValue());
+ assertThat(keyA, equalTo("A"));
+ assertThat(keyB, notNullValue());
+ assertThat(keyB, is("B"));
+ assertThat(keyC, notNullValue());
+ assertThat(keyC, is("C"));
+ }
+
+ @Test(expected = ConfigException.class)
+ public void emptyJSONFileResultsInConfigException() throws Exception {
+ URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/empty-file.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ UnifiedConfigData properties = getPropertiesFrom(configURL);
+
+ properties.getProperties();
+ }
+
+ @Test
+ public void canHandleEmptyJSONObject() throws Exception {
+ URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/empty-object-config.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ UnifiedConfigData properties = getPropertiesFrom(configURL);
+
+ assertThat(properties.getProperties().keySet(), hasSize(0));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/ConfigurationDataUCD.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/ConfigurationDataUCD.java b/modules/json/src/test/java/org/apache/tamaya/json/ConfigurationDataUCD.java
new file mode 100644
index 0000000..ab3e4ce
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/ConfigurationDataUCD.java
@@ -0,0 +1,44 @@
+/*
+ * 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.json;
+
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.spi.PropertySource;
+
+import java.util.Map;
+
+public class ConfigurationDataUCD implements UnifiedConfigData {
+ private ConfigurationData data;
+
+ public ConfigurationDataUCD(ConfigurationData configurationData) {
+ data = configurationData;
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return data.getDefaultSection();
+ }
+
+ @Override
+ public int getOrdinal() {
+ String value = data.getDefaultSection().get(PropertySource.TAMAYA_ORDINAL);
+
+ return Integer.parseInt(value);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/JSONFormatIT.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/JSONFormatIT.java b/modules/json/src/test/java/org/apache/tamaya/json/JSONFormatIT.java
new file mode 100644
index 0000000..e5bb91e
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/JSONFormatIT.java
@@ -0,0 +1,47 @@
+/*
+ * 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.json;
+
+import org.apache.tamaya.format.ConfigurationFormat;
+import org.apache.tamaya.spi.ServiceContext;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Integration tests for {@link JSONFormat}.
+ */
+public class JSONFormatIT {
+ @Test
+ public void jsonFormatCanBeFoundViaServiceLoader() throws Exception {
+ List<ConfigurationFormat> formats = ServiceContext.getInstance()
+ .getServices(ConfigurationFormat.class);
+
+ ConfigurationFormat format = formats.stream()
+ .filter(s -> s instanceof JSONFormat)
+ .findFirst().get();
+
+ assertThat(format, notNullValue());
+ assertThat(format, instanceOf(JSONFormat.class));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/JSONFormatTest.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/JSONFormatTest.java b/modules/json/src/test/java/org/apache/tamaya/json/JSONFormatTest.java
new file mode 100644
index 0000000..84c57f8
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/JSONFormatTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.json;
+
+
+import org.apache.tamaya.format.ConfigurationData;
+import org.junit.Test;
+
+import java.io.InputStream;
+import java.net.URL;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+public class JSONFormatTest extends CommonJSONTestCaseCollection {
+ 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));
+ }
+
+ @Override
+ UnifiedConfigData getPropertiesFrom(URL source) throws Exception {
+ try (InputStream is = source.openStream()) {
+ ConfigurationData data = format.readConfiguration(source.toString(), is);
+
+ return new ConfigurationDataUCD(data);
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceProviderExistingConfigFileIT.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceProviderExistingConfigFileIT.java b/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceProviderExistingConfigFileIT.java
new file mode 100644
index 0000000..f9ecb89
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceProviderExistingConfigFileIT.java
@@ -0,0 +1,87 @@
+/*
+ * 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.json;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.PropertyName;
+import org.apache.tamaya.core.internal.DefaultServiceContext;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+import org.apache.tamaya.spi.ServiceContext;
+import org.hamcrest.Matchers;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
+
+@RunWith(Arquillian.class)
+public class JSONPropertySourceProviderExistingConfigFileIT {
+
+ @Deployment
+ public static JavaArchive createDeployment() {
+ JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
+
+ archive.addPackage(ObjectMapper.class.getPackage())
+ .addPackages(true, JsonFactory.class.getPackage())
+ .addPackages(true, PropertyName.class.getPackage())
+ .addPackages(true, JsonAutoDetect.class.getPackage());
+
+ archive.addPackage(org.apache.tamaya.Configuration.class.getPackage())
+ .addPackage(org.apache.tamaya.spi.PropertySource.class.getPackage());
+
+ archive.addPackage(DefaultServiceContext.class.getPackage())
+ .addAsServiceProvider(ServiceContext.class, DefaultServiceContext.class);
+
+ archive.addPackage(JSONPropertySource.class.getPackage())
+ .addAsServiceProvider(PropertySourceProvider.class, JSONPropertySourceProvider.class);
+
+ archive.addAsManifestResource("configs/valid/simple-flat-string-only-config.json",
+ JSONPropertySourceProvider.DEFAULT_RESOURCE_NAME);
+
+ return archive;
+ }
+
+ @Test
+ public void providerReturnsListOfProvidersIfThereIsOneDefaultJSONConfig() {
+ List<PropertySourceProvider> services = ServiceContext.getInstance()
+ .getServices(PropertySourceProvider.class);
+
+ PropertySourceProvider provider = services.stream()
+ .filter(s -> s instanceof JSONPropertySourceProvider)
+ .findFirst().get();
+
+ assertThat(provider.getPropertySources(), notNullValue());
+ assertThat(provider.getPropertySources(), hasSize(1));
+
+ PropertySource source = provider.getPropertySources().iterator().next();
+
+ assertThat(source.getProperties().keySet(), Matchers.containsInAnyOrder("a", "b", "c"));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceProviderMissingConfigFileIT.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceProviderMissingConfigFileIT.java b/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceProviderMissingConfigFileIT.java
new file mode 100644
index 0000000..a87b49d
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceProviderMissingConfigFileIT.java
@@ -0,0 +1,69 @@
+/*
+ * 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.json;
+
+import org.apache.tamaya.core.internal.DefaultServiceContext;
+import org.apache.tamaya.spi.PropertySourceProvider;
+import org.apache.tamaya.spi.ServiceContext;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
+
+@RunWith(Arquillian.class)
+public class JSONPropertySourceProviderMissingConfigFileIT {
+
+ @Deployment
+ public static JavaArchive createDeployment() {
+ JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
+
+ archive.addPackage(org.apache.tamaya.Configuration.class.getPackage())
+ .addPackage(org.apache.tamaya.spi.PropertySource.class.getPackage());
+
+ archive.addPackage(DefaultServiceContext.class.getPackage())
+ .addAsServiceProvider(ServiceContext.class, DefaultServiceContext.class);
+
+
+ archive.addPackage(JSONPropertySource.class.getPackage())
+ .addAsServiceProvider(PropertySourceProvider.class, JSONPropertySourceProvider.class);
+
+ return archive;
+ }
+
+ @Test
+ public void providerReturnsEmptyListOfProvidersIfThereIsNoOneDefaultJSONConfig() {
+ List<PropertySourceProvider> services = ServiceContext.getInstance().getServices(PropertySourceProvider.class);
+
+ PropertySourceProvider provider = services.stream()
+ .filter(s -> s instanceof JSONPropertySourceProvider)
+ .findFirst().get();
+
+ assertThat(provider.getPropertySources(), notNullValue());
+ assertThat(provider.getPropertySources(), hasSize(0));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceTest.java b/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceTest.java
new file mode 100644
index 0000000..0f27863
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.json;
+
+import org.apache.tamaya.spi.PropertySource;
+import org.hamcrest.CoreMatchers;
+import org.junit.Test;
+
+import java.net.URL;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class JSONPropertySourceTest extends CommonJSONTestCaseCollection {
+
+ @Test
+ public void tamayaOrdinalKeywordIsNotPropagatedAsNormalProperty() throws Exception {
+ URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/with-explicit-priority.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ JSONPropertySource source = new JSONPropertySource(configURL.toString(), 10);
+
+ assertThat(source.get(PropertySource.TAMAYA_ORDINAL), nullValue());
+ }
+
+ @Override
+ UnifiedConfigData getPropertiesFrom(URL source) throws Exception {
+ JSONPropertySource propertySource = new JSONPropertySource(source);
+
+ return new JSONPropertySourceUCD(propertySource);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceUCD.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceUCD.java b/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceUCD.java
new file mode 100644
index 0000000..4feb2ed
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceUCD.java
@@ -0,0 +1,39 @@
+/*
+ * 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.json;
+
+import java.util.Map;
+
+public class JSONPropertySourceUCD implements UnifiedConfigData {
+ private JSONPropertySource propertySource;
+
+ public JSONPropertySourceUCD(JSONPropertySource source) {
+ propertySource = source;
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return propertySource.getProperties();
+ }
+
+ @Override
+ public int getOrdinal() {
+ return propertySource.getOrdinal();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/json/UnifiedConfigData.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/json/UnifiedConfigData.java b/modules/json/src/test/java/org/apache/tamaya/json/UnifiedConfigData.java
new file mode 100644
index 0000000..645d5b5
--- /dev/null
+++ b/modules/json/src/test/java/org/apache/tamaya/json/UnifiedConfigData.java
@@ -0,0 +1,32 @@
+/*
+ * 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.json;
+
+import java.util.Map;
+
+public interface UnifiedConfigData {
+ Map<String, String> getProperties();
+
+ int getOrdinal();
+
+ default String get(String key) {
+ return getProperties().get(key);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/modules/json/CommonJSONTestCaseCollection.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/modules/json/CommonJSONTestCaseCollection.java b/modules/json/src/test/java/org/apache/tamaya/modules/json/CommonJSONTestCaseCollection.java
deleted file mode 100644
index 2929bfa..0000000
--- a/modules/json/src/test/java/org/apache/tamaya/modules/json/CommonJSONTestCaseCollection.java
+++ /dev/null
@@ -1,173 +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.modules.json;
-
-import org.apache.tamaya.ConfigException;
-import org.hamcrest.CoreMatchers;
-import org.hamcrest.Matchers;
-import org.junit.Test;
-
-import java.net.URL;
-
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.hasSize;
-
-/**
- * Class with a collection of common test cases each JSON processing
- * class must be able to pass.
- */
-public abstract class CommonJSONTestCaseCollection {
-
- abstract UnifiedConfigData getPropertiesFrom(URL source) throws Exception;
-
- @Test
- public void canReadNestedStringOnlyJSONConfigFile() throws Exception {
- URL configURL = JSONPropertySourceTest.class
- .getResource("/configs/valid/simple-nested-string-only-config-1.json");
-
- assertThat(configURL, CoreMatchers.notNullValue());
-
- UnifiedConfigData properties = getPropertiesFrom(configURL);
-
- assertThat(properties.getProperties().keySet(), hasSize(5));
-
- String keyB = properties.get("b");
- String keyDO = properties.get("d.o");
- String keyDP = properties.get("d.p");
-
- assertThat(keyB, notNullValue());
- assertThat(keyB, equalTo("B"));
- assertThat(keyDO, notNullValue());
- assertThat(keyDO, equalTo("O"));
- assertThat(keyDP, Matchers.notNullValue());
- assertThat(keyDP, 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());
-
- UnifiedConfigData properties = getPropertiesFrom(configURL);
-
- assertThat(properties.getProperties().keySet(), hasSize(4));
-
- String keyA = properties.get("a");
- String keyDO = properties.get("b.o");
- String keyDP = properties.get("b.p");
- String keyC = properties.get("c");
-
- assertThat(keyA, notNullValue());
- assertThat(keyA, is("A"));
- assertThat(keyC, notNullValue());
- assertThat(keyC, equalTo("C"));
- assertThat(keyDO, notNullValue());
- assertThat(keyDO, equalTo("O"));
- assertThat(keyDP, notNullValue());
- assertThat(keyDP, is("P"));
- }
-
- @Test(expected = ConfigException.class)
- public void canHandleIllegalJSONFileWhichContainsAnArray() throws Exception {
- URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/with-array.json");
-
- assertThat(configURL, CoreMatchers.notNullValue());
-
- getPropertiesFrom(configURL).getProperties();
- }
-
- @Test(expected = ConfigException.class)
- public void canHandleIllegalJSONFileConsistingOfOneOpeningBracket() throws Exception {
- URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/only-opening-bracket.json");
-
- assertThat(configURL, CoreMatchers.notNullValue());
-
- getPropertiesFrom(configURL).getProperties();
- }
-
- @Test(expected = ConfigException.class)
- public void canHandleIllegalJSONFileWhichIsEmpty() throws Exception {
- URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/empty-file.json");
-
- assertThat(configURL, CoreMatchers.notNullValue());
-
- getPropertiesFrom(configURL).getProperties();
- }
-
- @Test
- public void priorityInConfigFileOverwriteExplicitlyGivenPriority() throws Exception {
- URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/with-explicit-priority.json");
-
- assertThat(configURL, CoreMatchers.notNullValue());
-
- UnifiedConfigData properties = getPropertiesFrom(configURL);
-
- assertThat(properties.getOrdinal(), is(16784));
- }
-
- @Test
- public void canReadFlatStringOnlyJSONConfigFile() throws Exception {
- URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/simple-flat-string-only-config.json");
-
- assertThat(configURL, CoreMatchers.notNullValue());
-
- UnifiedConfigData properties = getPropertiesFrom(configURL);
-
- assertThat(properties.getProperties().keySet(), hasSize(3));
-
- String keyA = properties.get("a");
- String keyB = properties.get("b");
- String keyC = properties.get("c");
-
- assertThat(keyA, notNullValue());
- assertThat(keyA, equalTo("A"));
- assertThat(keyB, notNullValue());
- assertThat(keyB, is("B"));
- assertThat(keyC, notNullValue());
- assertThat(keyC, is("C"));
- }
-
- @Test(expected = ConfigException.class)
- public void emptyJSONFileResultsInConfigException() throws Exception {
- URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/empty-file.json");
-
- assertThat(configURL, CoreMatchers.notNullValue());
-
- UnifiedConfigData properties = getPropertiesFrom(configURL);
-
- properties.getProperties();
- }
-
- @Test
- public void canHandleEmptyJSONObject() throws Exception {
- URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/empty-object-config.json");
-
- assertThat(configURL, CoreMatchers.notNullValue());
-
- UnifiedConfigData properties = getPropertiesFrom(configURL);
-
- assertThat(properties.getProperties().keySet(), hasSize(0));
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/modules/json/ConfigurationDataUCD.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/modules/json/ConfigurationDataUCD.java b/modules/json/src/test/java/org/apache/tamaya/modules/json/ConfigurationDataUCD.java
deleted file mode 100644
index 9de90e0..0000000
--- a/modules/json/src/test/java/org/apache/tamaya/modules/json/ConfigurationDataUCD.java
+++ /dev/null
@@ -1,44 +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.modules.json;
-
-import org.apache.tamaya.format.ConfigurationData;
-import org.apache.tamaya.spi.PropertySource;
-
-import java.util.Map;
-
-public class ConfigurationDataUCD implements UnifiedConfigData {
- private ConfigurationData data;
-
- public ConfigurationDataUCD(ConfigurationData configurationData) {
- data = configurationData;
- }
-
- @Override
- public Map<String, String> getProperties() {
- return data.getDefaultSection();
- }
-
- @Override
- public int getOrdinal() {
- String value = data.getDefaultSection().get(PropertySource.TAMAYA_ORDINAL);
-
- return Integer.parseInt(value);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatIT.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatIT.java b/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatIT.java
deleted file mode 100644
index 3c3fee1..0000000
--- a/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatIT.java
+++ /dev/null
@@ -1,47 +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.modules.json;
-
-import org.apache.tamaya.format.ConfigurationFormat;
-import org.apache.tamaya.spi.ServiceContext;
-import org.junit.Test;
-
-import java.util.List;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.instanceOf;
-import static org.hamcrest.Matchers.notNullValue;
-
-/**
- * Integration tests for {@link JSONFormat}.
- */
-public class JSONFormatIT {
- @Test
- public void jsonFormatCanBeFoundViaServiceLoader() throws Exception {
- List<ConfigurationFormat> formats = ServiceContext.getInstance()
- .getServices(ConfigurationFormat.class);
-
- ConfigurationFormat format = formats.stream()
- .filter(s -> s instanceof JSONFormat)
- .findFirst().get();
-
- assertThat(format, notNullValue());
- assertThat(format, instanceOf(JSONFormat.class));
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/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
deleted file mode 100644
index b6473d8..0000000
--- a/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONFormatTest.java
+++ /dev/null
@@ -1,75 +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.modules.json;
-
-
-import org.apache.tamaya.format.ConfigurationData;
-import org.junit.Test;
-
-import java.io.InputStream;
-import java.net.URL;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.core.Is.is;
-
-public class JSONFormatTest extends CommonJSONTestCaseCollection {
- 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));
- }
-
- @Override
- UnifiedConfigData getPropertiesFrom(URL source) throws Exception {
- try (InputStream is = source.openStream()) {
- ConfigurationData data = format.readConfiguration(source.toString(), is);
-
- return new ConfigurationDataUCD(data);
- }
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/11acca95/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceProviderExistingConfigFileIT.java
----------------------------------------------------------------------
diff --git a/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceProviderExistingConfigFileIT.java b/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceProviderExistingConfigFileIT.java
deleted file mode 100644
index c5ab552..0000000
--- a/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceProviderExistingConfigFileIT.java
+++ /dev/null
@@ -1,87 +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.modules.json;
-
-import com.fasterxml.jackson.annotation.JsonAutoDetect;
-import com.fasterxml.jackson.core.JsonFactory;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.PropertyName;
-import org.apache.tamaya.core.internal.DefaultServiceContext;
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertySourceProvider;
-import org.apache.tamaya.spi.ServiceContext;
-import org.hamcrest.Matchers;
-import org.jboss.arquillian.container.test.api.Deployment;
-import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.util.List;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.notNullValue;
-import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
-
-@RunWith(Arquillian.class)
-public class JSONPropertySourceProviderExistingConfigFileIT {
-
- @Deployment
- public static JavaArchive createDeployment() {
- JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
-
- archive.addPackage(ObjectMapper.class.getPackage())
- .addPackages(true, JsonFactory.class.getPackage())
- .addPackages(true, PropertyName.class.getPackage())
- .addPackages(true, JsonAutoDetect.class.getPackage());
-
- archive.addPackage(org.apache.tamaya.Configuration.class.getPackage())
- .addPackage(org.apache.tamaya.spi.PropertySource.class.getPackage());
-
- archive.addPackage(DefaultServiceContext.class.getPackage())
- .addAsServiceProvider(ServiceContext.class, DefaultServiceContext.class);
-
- archive.addPackage(JSONPropertySource.class.getPackage())
- .addAsServiceProvider(PropertySourceProvider.class, JSONPropertySourceProvider.class);
-
- archive.addAsManifestResource("configs/valid/simple-flat-string-only-config.json",
- JSONPropertySourceProvider.DEFAULT_RESOURCE_NAME);
-
- return archive;
- }
-
- @Test
- public void providerReturnsListOfProvidersIfThereIsOneDefaultJSONConfig() {
- List<PropertySourceProvider> services = ServiceContext.getInstance()
- .getServices(PropertySourceProvider.class);
-
- PropertySourceProvider provider = services.stream()
- .filter(s -> s instanceof JSONPropertySourceProvider)
- .findFirst().get();
-
- assertThat(provider.getPropertySources(), notNullValue());
- assertThat(provider.getPropertySources(), hasSize(1));
-
- PropertySource source = provider.getPropertySources().iterator().next();
-
- assertThat(source.getProperties().keySet(), Matchers.containsInAnyOrder("a", "b", "c"));
- }
-
-}