You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2016/12/15 13:44:10 UTC
svn commit: r1774444 - in /felix/trunk/configurator: ./
src/main/java/org/apache/felix/configurator/impl/
src/main/java/org/apache/felix/configurator/impl/json/
src/main/java/org/apache/felix/configurator/impl/yaml/
src/test/java/org/apache/felix/confi...
Author: cziegeler
Date: Thu Dec 15 13:44:10 2016
New Revision: 1774444
URL: http://svn.apache.org/viewvc?rev=1774444&view=rev
Log:
Switch from YAML to JSON for configuration format
Added:
felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/json/
- copied from r1774443, felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/yaml/
felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java
- copied, changed from r1774129, felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/yaml/YAMLUtil.java
felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/json/
- copied from r1774443, felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/yaml/
felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java
- copied, changed from r1774129, felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/yaml/YAMLUtilTest.java
felix/trunk/configurator/src/test/resources/bundles/1.json
- copied, changed from r1774443, felix/trunk/configurator/src/test/resources/bundles/1.yaml
felix/trunk/configurator/src/test/resources/bundles/2.json
- copied, changed from r1774443, felix/trunk/configurator/src/test/resources/bundles/2.yaml
felix/trunk/configurator/src/test/resources/json/
- copied from r1774443, felix/trunk/configurator/src/test/resources/yaml/
felix/trunk/configurator/src/test/resources/json/simple-types.json
- copied, changed from r1774129, felix/trunk/configurator/src/test/resources/yaml/simple-types.yaml
felix/trunk/configurator/src/test/resources/json/valid.json
- copied, changed from r1774129, felix/trunk/configurator/src/test/resources/yaml/valid.yaml
Removed:
felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/json/YAMLUtil.java
felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/yaml/
felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/json/YAMLUtilTest.java
felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/yaml/
felix/trunk/configurator/src/test/resources/bundles/1.yaml
felix/trunk/configurator/src/test/resources/bundles/2.yaml
felix/trunk/configurator/src/test/resources/json/simple-types.yaml
felix/trunk/configurator/src/test/resources/json/valid.yaml
felix/trunk/configurator/src/test/resources/yaml/
Modified:
felix/trunk/configurator/pom.xml
felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java
felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java
felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java
Modified: felix/trunk/configurator/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/pom.xml?rev=1774444&r1=1774443&r2=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/pom.xml (original)
+++ felix/trunk/configurator/pom.xml Thu Dec 15 13:44:10 2016
@@ -77,8 +77,8 @@
<artifactId>apache-rat-plugin</artifactId>
<configuration>
<excludes combine.children="append">
- <!-- Exclude all YAML files -->
- <exclude>**/*.yaml</exclude>
+ <!-- Exclude all JSON files -->
+ <exclude>**/*.json</exclude>
</excludes>
</configuration>
</plugin>
@@ -106,7 +106,7 @@
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.configadmin</artifactId>
- <version>1.8.11-SNAPSHOT</version>
+ <version>1.9.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -122,9 +122,15 @@
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>org.yaml</groupId>
- <artifactId>snakeyaml</artifactId>
- <version>1.17</version>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-json_1.0_spec</artifactId>
+ <version>1.0-alpha-1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
+ <version>0.9.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
Modified: felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java?rev=1774444&r1=1774443&r2=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java (original)
+++ felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java Thu Dec 15 13:44:10 2016
@@ -199,7 +199,7 @@ public class Configurator {
}
final List<ConfigurationFile> allFiles = new ArrayList<>();
for(final Map.Entry<String, String> entry : files.entrySet()) {
- final ConfigurationFile file = org.apache.felix.configurator.impl.yaml.YAMLUtil.readYAML(null, entry.getKey(), null, -1, entry.getValue());
+ final ConfigurationFile file = org.apache.felix.configurator.impl.json.JSONUtil.readJSON(null, entry.getKey(), null, -1, entry.getValue());
if ( file != null ) {
allFiles.add(file);
}
@@ -246,7 +246,7 @@ public class Configurator {
}
final Set<String> paths = Util.isConfigurerBundle(bundle);
if ( paths != null ) {
- final BundleState config = org.apache.felix.configurator.impl.yaml.YAMLUtil.readConfigurationsFromBundle(bundle, paths);
+ final BundleState config = org.apache.felix.configurator.impl.json.JSONUtil.readConfigurationsFromBundle(bundle, paths);
for(final String pid : config.getPids()) {
state.addAll(pid, config.getConfigurations(pid));
}
Modified: felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java?rev=1774444&r1=1774443&r2=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java (original)
+++ felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java Thu Dec 15 13:44:10 2016
@@ -24,9 +24,9 @@ import java.util.ArrayList;
import java.util.List;
import org.osgi.framework.Bundle;
-import org.osgi.converter.Converter;
-import org.osgi.converter.StandardConverter;
-import org.osgi.converter.TypeReference;
+import org.osgi.util.converter.Converter;
+import org.osgi.util.converter.StandardConverter;
+import org.osgi.util.converter.TypeReference;
public class TypeConverter {
Copied: felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java (from r1774129, felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/yaml/YAMLUtil.java)
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java?p2=felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java&p1=felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/yaml/YAMLUtil.java&r1=1774129&r2=1774444&rev=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/yaml/YAMLUtil.java (original)
+++ felix/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java Thu Dec 15 13:44:10 2016
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.felix.configurator.impl.yaml;
+package org.apache.felix.configurator.impl.json;
import java.io.IOException;
import java.io.StringReader;
@@ -27,9 +27,18 @@ import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
-import java.util.Map;
import java.util.Set;
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.json.JsonString;
+import javax.json.JsonStructure;
+import javax.json.JsonValue;
+import javax.json.JsonValue.ValueType;
+
import org.apache.felix.configurator.impl.TypeConverter;
import org.apache.felix.configurator.impl.Util;
import org.apache.felix.configurator.impl.logger.SystemLogger;
@@ -38,12 +47,9 @@ import org.apache.felix.configurator.imp
import org.apache.felix.configurator.impl.model.ConfigPolicy;
import org.apache.felix.configurator.impl.model.ConfigurationFile;
import org.osgi.framework.Bundle;
-import org.osgi.converter.TypeReference;
-import org.yaml.snakeyaml.DumperOptions;
-import org.yaml.snakeyaml.Yaml;
-import org.yaml.snakeyaml.error.YAMLException;
+import org.osgi.util.converter.TypeReference;
-public class YAMLUtil {
+public class JSONUtil {
private static final String INTERNAL_PREFIX = ":configurator:";
@@ -55,12 +61,14 @@ public class YAMLUtil {
private static final String PROP_POLICY = "policy";
+ private static final String PROP_PID = "service.pid";
+
public static BundleState readConfigurationsFromBundle(final Bundle bundle, final Set<String> paths) {
final BundleState config = new BundleState();
final List<ConfigurationFile> allFiles = new ArrayList<>();
for(final String path : paths) {
- final List<ConfigurationFile> files = org.apache.felix.configurator.impl.yaml.YAMLUtil.readYAML(bundle, path);
+ final List<ConfigurationFile> files = readJSON(bundle, path);
allFiles.addAll(files);
}
Collections.sort(allFiles);
@@ -71,14 +79,14 @@ public class YAMLUtil {
}
/**
- * Read all yaml files from a given path in the bundle
+ * Read all json files from a given path in the bundle
* @param bundle The bundle
* @param path The path
* @return A list of configuration files - sorted by url, might be empty.
*/
- public static List<ConfigurationFile> readYAML(final Bundle bundle, final String path) {
+ public static List<ConfigurationFile> readJSON(final Bundle bundle, final String path) {
final List<ConfigurationFile> result = new ArrayList<>();
- final Enumeration<URL> urls = bundle.findEntries(path, "*.yaml", false);
+ final Enumeration<URL> urls = bundle.findEntries(path, "*.json", false);
if ( urls != null ) {
while ( urls.hasMoreElements() ) {
final URL url = urls.nextElement();
@@ -92,7 +100,7 @@ public class YAMLUtil {
boolean done = false;
final TypeConverter converter = new TypeConverter(bundle);
try {
- final ConfigurationFile file = readYAML(converter, name, url, bundle.getBundleId(), contents);
+ final ConfigurationFile file = readJSON(converter, name, url, bundle.getBundleId(), contents);
if ( file != null ) {
result.add(file);
done = true;
@@ -112,7 +120,7 @@ public class YAMLUtil {
}
/**
- * Read a single YAML file
+ * Read a single JSON file
* @param converter type converter
* @param name The name of the file
* @param url The url to that file or {@code null}
@@ -120,100 +128,96 @@ public class YAMLUtil {
* @param contents The contents of the file
* @return The configuration file or {@code null}.
*/
- public static ConfigurationFile readYAML(
+ public static ConfigurationFile readJSON(
final TypeConverter converter,
final String name,
final URL url,
final long bundleId,
final String contents) {
final String identifier = (url == null ? name : url.toString());
- final Map<String, Object> yaml = parseYAML(name, contents);
- final List<Object> configs = verifyYAML(name, yaml);
+ final JsonObject json = parseJSON(name, contents);
+ final List<?> configs = verifyJSON(name, json);
if ( configs != null ) {
final List<Config> configurations = new ArrayList<>();
for(final Object obj : configs) {
- if ( ! (obj instanceof Map) ) {
+ if ( ! (obj instanceof JsonObject) ) {
SystemLogger.error("Ignoring configuration in '" + identifier + "' (not a configuration) : " + obj);
} else {
- @SuppressWarnings("unchecked")
- final Map<String, Object> mainMap = (Map<String, Object>)obj;
- if ( mainMap.size() != 1 ) {
- SystemLogger.error("Ignoring configuration in '" + identifier + "' (more than one PID) : " + obj);
+ final JsonObject mainMap = (JsonObject)obj;
+ final Object pid = getValue(mainMap, PROP_PID);
+ if ( ! (pid instanceof String) ) {
+ SystemLogger.error("Ignoring configuration in '" + identifier + "' (no service.pid) : " + obj);
} else {
- final String pid = mainMap.keySet().iterator().next();
- final Object configObj = mainMap.values().iterator().next();
- if ( ! (configObj instanceof Map) ) {
- SystemLogger.error("Ignoring configuration in '" + identifier + "' (no configuration map) : " + obj);
- } else {
- Set<String> environments = null;
- int ranking = 0;
- ConfigPolicy policy = ConfigPolicy.DEFAULT;
-
- @SuppressWarnings("unchecked")
- final Map<String, Object> config = (Map<String, Object>)configObj;
- final Dictionary<String, Object> properties = new Hashtable<>();
- boolean valid = true;
- for(final Map.Entry<String, Object> entry : config.entrySet()) {
- String key = entry.getKey();
- final boolean internalKey = key.startsWith(INTERNAL_PREFIX);
- if ( internalKey ) {
- key = key.substring(INTERNAL_PREFIX.length());
- }
- final int pos = key.indexOf(':');
- String typeInfo = null;
- if ( pos != -1 ) {
- typeInfo = key.substring(pos + 1);
- key = key.substring(0, pos);
- }
+ Set<String> environments = null;
+ int ranking = 0;
+ ConfigPolicy policy = ConfigPolicy.DEFAULT;
+
+ final Dictionary<String, Object> properties = new Hashtable<>();
+ boolean valid = true;
+ for(final String mapKey : mainMap.keySet()) {
+ final Object value = getValue(mainMap, mapKey);
- final Object value = entry.getValue();
+ if ( mapKey.equals(PROP_PID) ) {
+ continue;
+ }
- if ( internalKey ) {
- // no need to do type conversion based on typeInfo for internal props, type conversion is done directly below
- if ( key.equals(PROP_ENVIRONMENTS) ) {
- environments = TypeConverter.getConverter().convert(value).defaultValue(null).to(new TypeReference<Set<String>>() {});
- if ( environments == null ) {
- SystemLogger.warning("Invalid environments for configuration in '" + identifier + "' : " + pid + " - " + value);
- }
- } else if ( key.equals(PROP_RANKING) ) {
- final Integer intObj = TypeConverter.getConverter().convert(value).defaultValue(null).to(Integer.class);
- if ( intObj == null ) {
- SystemLogger.warning("Invalid ranking for configuration in '" + identifier + "' : " + pid + " - " + value);
+ final boolean internalKey = mapKey.startsWith(INTERNAL_PREFIX);
+ String key = mapKey;
+ if ( internalKey ) {
+ key = key.substring(INTERNAL_PREFIX.length());
+ }
+ final int pos = key.indexOf(':');
+ String typeInfo = null;
+ if ( pos != -1 ) {
+ typeInfo = key.substring(pos + 1);
+ key = key.substring(0, pos);
+ }
+
+ if ( internalKey ) {
+ // no need to do type conversion based on typeInfo for internal props, type conversion is done directly below
+ if ( key.equals(PROP_ENVIRONMENTS) ) {
+ environments = TypeConverter.getConverter().convert(value).defaultValue(null).to(new TypeReference<Set<String>>() {});
+ if ( environments == null ) {
+ SystemLogger.warning("Invalid environments for configuration in '" + identifier + "' : " + pid + " - " + value);
+ }
+ } else if ( key.equals(PROP_RANKING) ) {
+ final Integer intObj = TypeConverter.getConverter().convert(value).defaultValue(null).to(Integer.class);
+ if ( intObj == null ) {
+ SystemLogger.warning("Invalid ranking for configuration in '" + identifier + "' : " + pid + " - " + value);
+ } else {
+ ranking = intObj.intValue();
+ }
+ } else if ( key.equals(PROP_POLICY) ) {
+ final String stringVal = TypeConverter.getConverter().convert(value).defaultValue(null).to(String.class);
+ if ( stringVal == null ) {
+ SystemLogger.error("Invalid policy for configuration in '" + identifier + "' : " + pid + " - " + value);
+ } else {
+ if ( value.equals("default") || value.equals("force") ) {
+ policy = ConfigPolicy.valueOf(stringVal.toUpperCase());
} else {
- ranking = intObj.intValue();
- }
- } else if ( key.equals(PROP_POLICY) ) {
- final String stringVal = TypeConverter.getConverter().convert(value).defaultValue(null).to(String.class);
- if ( stringVal == null ) {
SystemLogger.error("Invalid policy for configuration in '" + identifier + "' : " + pid + " - " + value);
- } else {
- if ( value.equals("default") || value.equals("force") ) {
- policy = ConfigPolicy.valueOf(stringVal.toUpperCase());
- } else {
- SystemLogger.error("Invalid policy for configuration in '" + identifier + "' : " + pid + " - " + value);
- }
}
}
- } else {
- try {
- Object convertedVal = converter.convert(value, typeInfo);
- if ( convertedVal == null ) {
- convertedVal = createYAML(value);
- }
- properties.put(key, convertedVal);
- } catch ( final IOException io ) {
- SystemLogger.error("Invalid value/type for configuration in '" + identifier + "' : " + pid + " - " + entry.getKey());
- valid = false;
- break;
+ }
+ } else {
+ try {
+ Object convertedVal = converter.convert(value, typeInfo);
+ if ( convertedVal == null ) {
+ convertedVal = value.toString();
}
+ properties.put(mapKey, convertedVal);
+ } catch ( final IOException io ) {
+ SystemLogger.error("Invalid value/type for configuration in '" + identifier + "' : " + pid + " - " + mapKey);
+ valid = false;
+ break;
}
}
+ }
- if ( valid ) {
- final Config c = new Config(pid, environments, properties, bundleId, ranking, policy);
- c.setFiles(converter.flushFiles());
- configurations.add(c);
- }
+ if ( valid ) {
+ final Config c = new Config((String)pid, environments, properties, bundleId, ranking, policy);
+ c.setFiles(converter.flushFiles());
+ configurations.add(c);
}
}
}
@@ -226,52 +230,74 @@ public class YAMLUtil {
}
/**
- * Parse a YAML content
+ * Parse a JSON content
* @param name The name of the file
* @param contents The contents
- * @return The parsed YAML map or {@code null} on failure,
+ * @return The parsed JSON object or {@code null} on failure,
*/
- @SuppressWarnings("unchecked")
- public static Map<String, Object> parseYAML(final String name, final String contents) {
- try {
- final Yaml yaml = new Yaml();
- try (final StringReader reader = new StringReader(contents) ) {
- final Object obj = yaml.load(reader);
- if ( obj instanceof Map ) {
- return (Map<String, Object>)obj;
- }
- SystemLogger.error("Invalid YAML from " + name);
+ public static JsonObject parseJSON(final String name, final String contents) {
+ try (final JsonReader reader = Json.createReader(new StringReader(contents)) ) {
+ final JsonStructure obj = reader.read();
+ if ( obj != null && obj.getValueType() == ValueType.OBJECT ) {
+ return (JsonObject)obj;
}
- } catch ( final YAMLException ignore ) {
- SystemLogger.error("Unable to read YAML from " + name, ignore);
+ SystemLogger.error("Invalid JSON from " + name);
}
return null;
}
- private static String createYAML(final Object data) throws IOException {
- final DumperOptions options = new DumperOptions();
- options.setIndent(2);
- options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
- try {
- final Yaml yaml = new Yaml(options);
- return yaml.dump(data);
- } catch ( final YAMLException ye) {
- throw new IOException(ye);
+ /**
+ * Get the value of a JSON property
+ * @param root The JSON Object
+ * @param key The key in the JSON Obejct
+ * @return The value or {@code null}
+ */
+ public static Object getValue(final JsonObject root, final String key) {
+ if ( !root.containsKey(key) ) {
+ return null;
+ }
+ final JsonValue value = root.get(key);
+ return getValue(value);
+ }
+
+ public static Object getValue(final JsonValue value) {
+ switch ( value.getValueType() ) {
+ // type NULL -> return null
+ case NULL : return null;
+ // type TRUE or FALSE -> return boolean
+ case FALSE : return false;
+ case TRUE : return true;
+ // type String -> return String
+ case STRING : return ((JsonString)value).getString();
+ // type Number -> return long or double
+ case NUMBER : final JsonNumber num = (JsonNumber)value;
+ if (num.isIntegral()) {
+ return num.longValue();
+ }
+ return num.doubleValue();
+ // type ARRAY -> return list and call this method for each value
+ case ARRAY : final List<Object> array = new ArrayList<>();
+ for(final JsonValue x : ((JsonArray)value)) {
+ array.add(getValue(x));
+ }
+ return array;
+ // type OBJECT -> return object
+ case OBJECT : return value;
}
+ return null;
}
/**
- * Verify the YAML according to the rules
- * @param name The YAML name
- * @param root The YAML root object.
- * @return List of objects if valid
+ * Verify the JSON according to the rules
+ * @param name The JSON name
+ * @param root The JSON root object.
+ * @return JSON array with configurations or {@code null}
*/
- @SuppressWarnings("unchecked")
- public static List<Object> verifyYAML(final String name, final Map<String, Object> root) {
+ public static List<?> verifyJSON(final String name, final JsonObject root) {
if ( root == null ) {
return null;
}
- final Object version = root.get(PROP_VERSION);
+ final Object version = getValue(root, PROP_VERSION);
if ( version != null ) {
final int v = TypeConverter.getConverter().convert(version).defaultValue(-1).to(Integer.class);
@@ -285,15 +311,15 @@ public class YAMLUtil {
return null;
}
}
- final Object configs = root.get("configurations");
+ final Object configs = getValue(root, "configurations");
if ( configs == null ) {
// short cut, we just return false as we don't have to process this file
return null;
}
if ( !(configs instanceof List) ) {
- SystemLogger.error("Configurations must be a list of configurations in " + name);
+ SystemLogger.error("Configurations must be an array of configurations in " + name);
return null;
}
- return (List<Object>) configs;
+ return (List<?>) configs;
}
}
Modified: felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java?rev=1774444&r1=1774443&r2=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java (original)
+++ felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java Thu Dec 15 13:44:10 2016
@@ -69,8 +69,8 @@ public class ConfiguratorTest {
final Requirement req = mock(Requirement.class);
when(rev.getRequirements(Util.NS_OSGI_IMPL)).thenReturn(Collections.singletonList(req));
final Vector<URL> urls = new Vector<>();
- urls.add(this.getClass().getResource("/bundles/" + id + ".yaml"));
- when(b.findEntries("OSGI-INF/configurator", "*.yaml", false)).thenReturn(urls.elements());
+ urls.add(this.getClass().getResource("/bundles/" + id + ".json"));
+ when(b.findEntries("OSGI-INF/configurator", "*.json", false)).thenReturn(urls.elements());
return b;
}
Modified: felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java?rev=1774444&r1=1774443&r2=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java (original)
+++ felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java Thu Dec 15 13:44:10 2016
@@ -26,10 +26,11 @@ import static org.junit.Assert.assertTru
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Collection;
-import java.util.Map;
-import org.apache.felix.configurator.impl.yaml.YAMLUtil;
-import org.apache.felix.configurator.impl.yaml.YAMLUtilTest;
+import javax.json.JsonObject;
+
+import org.apache.felix.configurator.impl.json.JSONUtil;
+import org.apache.felix.configurator.impl.json.JSONUtilTest;
import org.junit.Test;
public class TypeConverterTest {
@@ -104,150 +105,143 @@ public class TypeConverterTest {
@Test public void testSimpleTypeConversions() throws Exception {
final TypeConverter converter = new TypeConverter(null);
- final Object obj = YAMLUtil.parseYAML("a", YAMLUtilTest.readYAML("yaml/simple-types.yaml"));
- @SuppressWarnings("unchecked")
- final Map<String, Object> config = (Map<String, Object>)obj;
- @SuppressWarnings("unchecked")
- final Map<String, Object> properties = (Map<String, Object>)config.get("config");
-
- assertTrue(converter.convert(properties.get("string"), null) instanceof String);
- assertTrue(converter.convert(properties.get("boolean"), null) instanceof Boolean);
- assertTrue(converter.convert(properties.get("number"), null) instanceof Long);
- assertTrue(converter.convert(properties.get("float"), null) instanceof Double);
+ final JsonObject config = JSONUtil.parseJSON("a", JSONUtilTest.readJSON("json/simple-types.json"));
+ final JsonObject properties = (JsonObject)config.get("config");
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string"), null) instanceof String);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "boolean"), null) instanceof Boolean);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), null) instanceof Long);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float"), null) instanceof Double);
// arrays
- assertTrue(converter.convert(properties.get("string.array"), null).getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("string.array"), null), 0) instanceof String);
- assertTrue(Array.get(converter.convert(properties.get("string.array"), null), 1) instanceof String);
-
- assertTrue(converter.convert(properties.get("boolean.array"), null).getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("boolean.array"), null), 0) instanceof Boolean);
- assertTrue(Array.get(converter.convert(properties.get("boolean.array"), null), 1) instanceof Boolean);
-
- assertTrue(converter.convert(properties.get("number.array"), null).getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("number.array"), null), 0) instanceof Long);
- assertTrue(Array.get(converter.convert(properties.get("number.array"), null), 1) instanceof Long);
-
- assertTrue(converter.convert(properties.get("float.array"), null).getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("float.array"), null), 0) instanceof Double);
- assertTrue(Array.get(converter.convert(properties.get("float.array"), null), 1) instanceof Double);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string.array"), null).getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "string.array"), null), 0) instanceof String);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "string.array"), null), 1) instanceof String);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "boolean.array"), null).getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "boolean.array"), null), 0) instanceof Boolean);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "boolean.array"), null), 1) instanceof Boolean);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), null).getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), null), 0) instanceof Long);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), null), 1) instanceof Long);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float.array"), null).getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "float.array"), null), 0) instanceof Double);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "float.array"), null), 1) instanceof Double);
}
@Test public void testSimpleTypeConversionsWithTypeHint() throws Exception {
final TypeConverter converter = new TypeConverter(null);
- final Object obj = YAMLUtil.parseYAML("a", YAMLUtilTest.readYAML("yaml/simple-types.yaml"));
- @SuppressWarnings("unchecked")
- final Map<String, Object> config = (Map<String, Object>)obj;
- @SuppressWarnings("unchecked")
- final Map<String, Object> properties = (Map<String, Object>)config.get("config");
-
- assertTrue(converter.convert(properties.get("string"), "String") instanceof String);
- assertTrue(converter.convert(properties.get("boolean"), "Boolean") instanceof Boolean);
- assertTrue(converter.convert(properties.get("boolean"), "boolean") instanceof Boolean);
- assertTrue(converter.convert(properties.get("number"), "Integer") instanceof Integer);
- assertTrue(converter.convert(properties.get("number"), "int") instanceof Integer);
- assertTrue(converter.convert(properties.get("number"), "Long") instanceof Long);
- assertTrue(converter.convert(properties.get("number"), "long") instanceof Long);
- assertTrue(converter.convert(properties.get("float"), "Double") instanceof Double);
- assertTrue(converter.convert(properties.get("float"), "double") instanceof Double);
- assertTrue(converter.convert(properties.get("float"), "Float") instanceof Float);
- assertTrue(converter.convert(properties.get("float"), "float") instanceof Float);
- assertTrue(converter.convert(properties.get("number"), "Byte") instanceof Byte);
- assertTrue(converter.convert(properties.get("number"), "byte") instanceof Byte);
- assertTrue(converter.convert(properties.get("number"), "Short") instanceof Short);
- assertTrue(converter.convert(properties.get("number"), "short") instanceof Short);
- assertTrue(converter.convert(properties.get("string"), "Character") instanceof Character);
- assertTrue(converter.convert(properties.get("string"), "char") instanceof Character);
+ final JsonObject config = JSONUtil.parseJSON("a", JSONUtilTest.readJSON("json/simple-types.json"));
+ final JsonObject properties = (JsonObject)config.get("config");
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string"), "String") instanceof String);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "boolean"), "Boolean") instanceof Boolean);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "boolean"), "boolean") instanceof Boolean);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), "Integer") instanceof Integer);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), "int") instanceof Integer);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), "Long") instanceof Long);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), "long") instanceof Long);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float"), "Double") instanceof Double);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float"), "double") instanceof Double);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float"), "Float") instanceof Float);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float"), "float") instanceof Float);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), "Byte") instanceof Byte);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), "byte") instanceof Byte);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), "Short") instanceof Short);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number"), "short") instanceof Short);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string"), "Character") instanceof Character);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string"), "char") instanceof Character);
// arrays
- assertTrue(converter.convert(properties.get("string.array"), "String[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("string.array"), "String[]"), 0) instanceof String);
- assertTrue(Array.get(converter.convert(properties.get("string.array"), "String[]"), 1) instanceof String);
-
- assertTrue(converter.convert(properties.get("boolean.array"), "Boolean[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("boolean.array"), "Boolean[]"), 0) instanceof Boolean);
- assertTrue(Array.get(converter.convert(properties.get("boolean.array"), "Boolean[]"), 1) instanceof Boolean);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string.array"), "String[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "string.array"), "String[]"), 0) instanceof String);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "string.array"), "String[]"), 1) instanceof String);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "boolean.array"), "Boolean[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "boolean.array"), "Boolean[]"), 0) instanceof Boolean);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "boolean.array"), "Boolean[]"), 1) instanceof Boolean);
// the following would throw class cast exceptions
- boolean[] a0 = (boolean[])converter.convert(properties.get("boolean.array"), "boolean[]");
+ boolean[] a0 = (boolean[])converter.convert(JSONUtil.getValue(properties, "boolean.array"), "boolean[]");
assertNotNull(a0);
- int[] a1 = (int[])converter.convert(properties.get("number.array"), "int[]");
+ int[] a1 = (int[])converter.convert(JSONUtil.getValue(properties, "number.array"), "int[]");
assertNotNull(a1);
- long[] a2 = (long[])converter.convert(properties.get("number.array"), "long[]");
+ long[] a2 = (long[])converter.convert(JSONUtil.getValue(properties, "number.array"), "long[]");
assertNotNull(a2);
- double[] a3 = (double[])converter.convert(properties.get("float.array"), "double[]");
+ double[] a3 = (double[])converter.convert(JSONUtil.getValue(properties, "float.array"), "double[]");
assertNotNull(a3);
- float[] a4 = (float[])converter.convert(properties.get("float.array"), "float[]");
+ float[] a4 = (float[])converter.convert(JSONUtil.getValue(properties, "float.array"), "float[]");
assertNotNull(a4);
- byte[] a5 = (byte[])converter.convert(properties.get("number.array"), "byte[]");
+ byte[] a5 = (byte[])converter.convert(JSONUtil.getValue(properties, "number.array"), "byte[]");
assertNotNull(a5);
- short[] a6 = (short[])converter.convert(properties.get("number.array"), "short[]");
+ short[] a6 = (short[])converter.convert(JSONUtil.getValue(properties, "number.array"), "short[]");
assertNotNull(a6);
- char[] a7 = (char[])converter.convert(properties.get("string.array"), "char[]");
+ char[] a7 = (char[])converter.convert(JSONUtil.getValue(properties, "string.array"), "char[]");
assertNotNull(a7);
- assertTrue(converter.convert(properties.get("number.array"), "Integer[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("number.array"), "Integer[]"), 0) instanceof Integer);
- assertTrue(Array.get(converter.convert(properties.get("number.array"), "Integer[]"), 1) instanceof Integer);
-
- assertTrue(converter.convert(properties.get("number.array"), "Long[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("number.array"), "Long[]"), 0) instanceof Long);
- assertTrue(Array.get(converter.convert(properties.get("number.array"), "Long[]"), 1) instanceof Long);
-
- assertTrue(converter.convert(properties.get("number.array"), "Byte[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("number.array"), "Byte[]"), 0) instanceof Byte);
- assertTrue(Array.get(converter.convert(properties.get("number.array"), "Byte[]"), 1) instanceof Byte);
-
- assertTrue(converter.convert(properties.get("number.array"), "Short[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("number.array"), "Short[]"), 0) instanceof Short);
- assertTrue(Array.get(converter.convert(properties.get("number.array"), "Short[]"), 1) instanceof Short);
-
- assertTrue(converter.convert(properties.get("float.array"), "Float[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("float.array"), "Float[]"), 0) instanceof Float);
- assertTrue(Array.get(converter.convert(properties.get("float.array"), "Float[]"), 1) instanceof Float);
-
- assertTrue(converter.convert(properties.get("float.array"), "Double[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("float.array"), "Double[]"), 0) instanceof Double);
- assertTrue(Array.get(converter.convert(properties.get("float.array"), "Double[]"), 1) instanceof Double);
-
- assertTrue(converter.convert(properties.get("string.array"), "Character[]").getClass().isArray());
- assertTrue(Array.get(converter.convert(properties.get("string.array"), "Character[]"), 0) instanceof Character);
- assertTrue(Array.get(converter.convert(properties.get("string.array"), "Character[]"), 1) instanceof Character);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), "Integer[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), "Integer[]"), 0) instanceof Integer);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), "Integer[]"), 1) instanceof Integer);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), "Long[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), "Long[]"), 0) instanceof Long);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), "Long[]"), 1) instanceof Long);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), "Byte[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), "Byte[]"), 0) instanceof Byte);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), "Byte[]"), 1) instanceof Byte);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), "Short[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), "Short[]"), 0) instanceof Short);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "number.array"), "Short[]"), 1) instanceof Short);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float.array"), "Float[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "float.array"), "Float[]"), 0) instanceof Float);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "float.array"), "Float[]"), 1) instanceof Float);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float.array"), "Double[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "float.array"), "Double[]"), 0) instanceof Double);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "float.array"), "Double[]"), 1) instanceof Double);
+
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string.array"), "Character[]").getClass().isArray());
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "string.array"), "Character[]"), 0) instanceof Character);
+ assertTrue(Array.get(converter.convert(JSONUtil.getValue(properties, "string.array"), "Character[]"), 1) instanceof Character);
}
@SuppressWarnings("unchecked")
@Test public void testCollectionTypeConversion() throws Exception {
final TypeConverter converter = new TypeConverter(null);
- final Object obj = YAMLUtil.parseYAML("a", YAMLUtilTest.readYAML("yaml/simple-types.yaml"));
- final Map<String, Object> config = (Map<String, Object>)obj;
- final Map<String, Object> properties = (Map<String, Object>)config.get("config");
+ final JsonObject config = JSONUtil.parseJSON("a", JSONUtilTest.readJSON("json/simple-types.json"));
+ final JsonObject properties = (JsonObject)config.get("config");
- assertTrue(converter.convert(properties.get("string.array"), "Collection<String>") instanceof Collection<?>);
- assertTrue(((Collection<String>)converter.convert(properties.get("string.array"), "Collection<String>")).iterator().next() instanceof String);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string.array"), "Collection<String>") instanceof Collection<?>);
+ assertTrue(((Collection<String>)converter.convert(JSONUtil.getValue(properties, "string.array"), "Collection<String>")).iterator().next() instanceof String);
- assertTrue(converter.convert(properties.get("number.array"), "Collection<Integer>") instanceof Collection<?>);
- assertTrue(((Collection<Integer>)converter.convert(properties.get("number.array"), "Collection<Integer>")).iterator().next() instanceof Integer);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), "Collection<Integer>") instanceof Collection<?>);
+ assertTrue(((Collection<Integer>)converter.convert(JSONUtil.getValue(properties, "number.array"), "Collection<Integer>")).iterator().next() instanceof Integer);
- assertTrue(converter.convert(properties.get("number.array"), "Collection<Long>") instanceof Collection<?>);
- assertTrue(((Collection<Long>)converter.convert(properties.get("number.array"), "Collection<Long>")).iterator().next() instanceof Long);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), "Collection<Long>") instanceof Collection<?>);
+ assertTrue(((Collection<Long>)converter.convert(JSONUtil.getValue(properties, "number.array"), "Collection<Long>")).iterator().next() instanceof Long);
- assertTrue(converter.convert(properties.get("float.array"), "Collection<Float>") instanceof Collection<?>);
- assertTrue(((Collection<Float>)converter.convert(properties.get("float.array"), "Collection<Float>")).iterator().next() instanceof Float);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float.array"), "Collection<Float>") instanceof Collection<?>);
+ assertTrue(((Collection<Float>)converter.convert(JSONUtil.getValue(properties, "float.array"), "Collection<Float>")).iterator().next() instanceof Float);
- assertTrue(converter.convert(properties.get("float.array"), "Collection<Double>") instanceof Collection<?>);
- assertTrue(((Collection<Double>)converter.convert(properties.get("float.array"), "Collection<Double>")).iterator().next() instanceof Double);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "float.array"), "Collection<Double>") instanceof Collection<?>);
+ assertTrue(((Collection<Double>)converter.convert(JSONUtil.getValue(properties, "float.array"), "Collection<Double>")).iterator().next() instanceof Double);
- assertTrue(converter.convert(properties.get("number.array"), "Collection<Short>") instanceof Collection<?>);
- assertTrue(((Collection<Short>)converter.convert(properties.get("number.array"), "Collection<Short>")).iterator().next() instanceof Short);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), "Collection<Short>") instanceof Collection<?>);
+ assertTrue(((Collection<Short>)converter.convert(JSONUtil.getValue(properties, "number.array"), "Collection<Short>")).iterator().next() instanceof Short);
- assertTrue(converter.convert(properties.get("number.array"), "Collection<Byte>") instanceof Collection<?>);
- assertTrue(((Collection<Byte>)converter.convert(properties.get("number.array"), "Collection<Byte>")).iterator().next() instanceof Byte);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "number.array"), "Collection<Byte>") instanceof Collection<?>);
+ assertTrue(((Collection<Byte>)converter.convert(JSONUtil.getValue(properties, "number.array"), "Collection<Byte>")).iterator().next() instanceof Byte);
- assertTrue(converter.convert(properties.get("string.array"), "Collection<Character>") instanceof Collection<?>);
- assertTrue(((Collection<Character>)converter.convert(properties.get("string.array"), "Collection<Character>")).iterator().next() instanceof Character);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "string.array"), "Collection<Character>") instanceof Collection<?>);
+ assertTrue(((Collection<Character>)converter.convert(JSONUtil.getValue(properties, "string.array"), "Collection<Character>")).iterator().next() instanceof Character);
- assertTrue(converter.convert(properties.get("boolean.array"), "Collection<Boolean>") instanceof Collection<?>);
- assertTrue(((Collection<Boolean>)converter.convert(properties.get("boolean.array"), "Collection<Boolean>")).iterator().next() instanceof Boolean);
+ assertTrue(converter.convert(JSONUtil.getValue(properties, "boolean.array"), "Collection<Boolean>") instanceof Collection<?>);
+ assertTrue(((Collection<Boolean>)converter.convert(JSONUtil.getValue(properties, "boolean.array"), "Collection<Boolean>")).iterator().next() instanceof Boolean);
}
}
Copied: felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java (from r1774129, felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/yaml/YAMLUtilTest.java)
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java?p2=felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java&p1=felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/yaml/YAMLUtilTest.java&r1=1774129&r2=1774444&rev=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/yaml/YAMLUtilTest.java (original)
+++ felix/trunk/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java Thu Dec 15 13:44:10 2016
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.felix.configurator.impl.yaml;
+package org.apache.felix.configurator.impl.json;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -28,18 +28,19 @@ import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.List;
-import java.util.Map;
+
+import javax.json.JsonObject;
import org.apache.felix.configurator.impl.TypeConverter;
import org.apache.felix.configurator.impl.model.ConfigurationFile;
import org.junit.Test;
-public class YAMLUtilTest {
+public class JSONUtilTest {
- /** Read the model from that name */
- public static String readYAML(final String name) throws Exception {
+ /** Read the data from that name */
+ public static String readJSON(final String name) throws Exception {
- try ( final Reader reader = new InputStreamReader(YAMLUtilTest.class.getResourceAsStream("/" + name), "UTF-8");
+ try ( final Reader reader = new InputStreamReader(JSONUtilTest.class.getResourceAsStream("/" + name), "UTF-8");
final Writer writer = new StringWriter()) {
final char[] buf = new char[2048];
@@ -52,39 +53,38 @@ public class YAMLUtilTest {
}
}
- @Test public void testReadYAML() throws Exception {
- final ConfigurationFile cg = YAMLUtil.readYAML(new TypeConverter(null),
- "a", new URL("http://a"), 1, readYAML("yaml/valid.yaml"));
+ @Test public void testReadJSON() throws Exception {
+ final ConfigurationFile cg = JSONUtil.readJSON(new TypeConverter(null),
+ "a", new URL("http://a"), 1, readJSON("json/valid.json"));
assertNotNull(cg);
assertEquals(2, cg.getConfigurations().size());
}
@SuppressWarnings("unchecked")
@Test public void testTypes() throws Exception {
- final Object obj = YAMLUtil.parseYAML("a", YAMLUtilTest.readYAML("yaml/simple-types.yaml"));
- final Map<String, Object> config = (Map<String, Object>)obj;
- final Map<String, Object> properties = (Map<String, Object>)config.get("config");
-
- assertTrue(properties.get("string") instanceof String);
- assertTrue(properties.get("boolean") instanceof Boolean);
- assertTrue(properties.get("number") instanceof Integer);
- assertTrue(properties.get("float") instanceof Double);
+ final JsonObject config = JSONUtil.parseJSON("a", JSONUtilTest.readJSON("json/simple-types.json"));
+ final JsonObject properties = (JsonObject)config.get("config");
+
+ assertTrue(JSONUtil.getValue(properties, "string") instanceof String);
+ assertTrue(JSONUtil.getValue(properties, "boolean") instanceof Boolean);
+ assertTrue(JSONUtil.getValue(properties, "number") instanceof Long);
+ assertTrue(JSONUtil.getValue(properties, "float") instanceof Double);
// arrays
- assertTrue(properties.get("string.array") instanceof List<?>);
- assertTrue(((List<Object>)properties.get("string.array")).get(0) instanceof String);
- assertTrue(((List<Object>)properties.get("string.array")).get(1) instanceof String);
-
- assertTrue((List<Object>)properties.get("boolean.array") instanceof List<?>);
- assertTrue(((List<Object>)properties.get("boolean.array")).get(0) instanceof Boolean);
- assertTrue(((List<Object>)properties.get("boolean.array")).get(1) instanceof Boolean);
-
- assertTrue((List<Object>)properties.get("number.array") instanceof List<?>);
- assertTrue(((List<Object>)properties.get("number.array")).get(0) instanceof Integer);
- assertTrue(((List<Object>)properties.get("number.array")).get(1) instanceof Integer);
-
- assertTrue((List<Object>)properties.get("float.array") instanceof List<?>);
- assertTrue(((List<Object>)properties.get("float.array")).get(0) instanceof Double);
- assertTrue(((List<Object>)properties.get("float.array")).get(1) instanceof Double);
+ assertTrue(JSONUtil.getValue(properties, "string.array") instanceof List<?>);
+ assertTrue(((List<Object>)JSONUtil.getValue(properties, "string.array")).get(0) instanceof String);
+ assertTrue(((List<Object>)JSONUtil.getValue(properties, "string.array")).get(1) instanceof String);
+
+ assertTrue((List<Object>)JSONUtil.getValue(properties, "boolean.array") instanceof List<?>);
+ assertTrue(((List<Object>)JSONUtil.getValue(properties, "boolean.array")).get(0) instanceof Boolean);
+ assertTrue(((List<Object>)JSONUtil.getValue(properties, "boolean.array")).get(1) instanceof Boolean);
+
+ assertTrue((List<Object>)JSONUtil.getValue(properties, "number.array") instanceof List<?>);
+ assertTrue(((List<Object>)JSONUtil.getValue(properties, "number.array")).get(0) instanceof Long);
+ assertTrue(((List<Object>)JSONUtil.getValue(properties, "number.array")).get(1) instanceof Long);
+
+ assertTrue((List<Object>)JSONUtil.getValue(properties, "float.array") instanceof List<?>);
+ assertTrue(((List<Object>)JSONUtil.getValue(properties, "float.array")).get(0) instanceof Double);
+ assertTrue(((List<Object>)JSONUtil.getValue(properties, "float.array")).get(1) instanceof Double);
}
}
\ No newline at end of file
Copied: felix/trunk/configurator/src/test/resources/bundles/1.json (from r1774443, felix/trunk/configurator/src/test/resources/bundles/1.yaml)
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/test/resources/bundles/1.json?p2=felix/trunk/configurator/src/test/resources/bundles/1.json&p1=felix/trunk/configurator/src/test/resources/bundles/1.yaml&r1=1774443&r2=1774444&rev=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/test/resources/bundles/1.yaml (original)
+++ felix/trunk/configurator/src/test/resources/bundles/1.json Thu Dec 15 13:44:10 2016
@@ -1,5 +1,12 @@
-configurations:
- - a:
- foo: bar
- - b:
- x: y
+{
+ "configurations" : [
+ {
+ "service.pid" : "a",
+ "foo" : "bar"
+ },
+ {
+ "service.pid" : "b",
+ "x" : "y"
+ }
+ ]
+}
Copied: felix/trunk/configurator/src/test/resources/bundles/2.json (from r1774443, felix/trunk/configurator/src/test/resources/bundles/2.yaml)
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/test/resources/bundles/2.json?p2=felix/trunk/configurator/src/test/resources/bundles/2.json&p1=felix/trunk/configurator/src/test/resources/bundles/2.yaml&r1=1774443&r2=1774444&rev=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/test/resources/bundles/2.yaml (original)
+++ felix/trunk/configurator/src/test/resources/bundles/2.json Thu Dec 15 13:44:10 2016
@@ -1,5 +1,12 @@
-configurations:
- - a:
- foo: bar2
- - b:
- x: y2
+{
+ "configurations" : [
+ {
+ "service.pid" : "a",
+ "foo" : "bar2"
+ },
+ {
+ "service.pid" : "b",
+ "x" : "y2"
+ }
+ ]
+}
Copied: felix/trunk/configurator/src/test/resources/json/simple-types.json (from r1774129, felix/trunk/configurator/src/test/resources/yaml/simple-types.yaml)
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/test/resources/json/simple-types.json?p2=felix/trunk/configurator/src/test/resources/json/simple-types.json&p1=felix/trunk/configurator/src/test/resources/yaml/simple-types.yaml&r1=1774129&r2=1774444&rev=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/test/resources/yaml/simple-types.yaml (original)
+++ felix/trunk/configurator/src/test/resources/json/simple-types.json Thu Dec 15 13:44:10 2016
@@ -1,17 +1,12 @@
-config:
- string: bar
- boolean: true
- number: 17
- float: 5.0
- string.array:
- - a
- - b
- boolean.array:
- - true
- - false
- number.array:
- - 3
- - 4
- float.array:
- - 1.0
- - 2.0
+{
+ "config" : {
+ "string" : "bar",
+ "boolean" : true,
+ "number": 17,
+ "float": 5.0,
+ "string.array" : ["a", "b"],
+ "boolean.array" : [true, false],
+ "number.array" : [3,4],
+ "float.array" : [1.0,2.0]
+ }
+}
Copied: felix/trunk/configurator/src/test/resources/json/valid.json (from r1774129, felix/trunk/configurator/src/test/resources/yaml/valid.yaml)
URL: http://svn.apache.org/viewvc/felix/trunk/configurator/src/test/resources/json/valid.json?p2=felix/trunk/configurator/src/test/resources/json/valid.json&p1=felix/trunk/configurator/src/test/resources/yaml/valid.yaml&r1=1774129&r2=1774444&rev=1774444&view=diff
==============================================================================
--- felix/trunk/configurator/src/test/resources/yaml/valid.yaml (original)
+++ felix/trunk/configurator/src/test/resources/json/valid.json Thu Dec 15 13:44:10 2016
@@ -1,5 +1,12 @@
-configurations:
- - a:
- foo: bar
- - b:
- x: y
+{
+ "configurations" : [
+ {
+ "service.pid" : "a",
+ "foo" : "bar"
+ },
+ {
+ "service.pid" : "b",
+ "x" : "y"
+ }
+ ]
+}