You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by an...@apache.org on 2012/04/25 18:05:09 UTC
svn commit: r1330376 - in /jackrabbit/oak/trunk/oak-core/src:
main/java/org/apache/jackrabbit/oak/util/CoreValueUtil.java
test/java/org/apache/jackrabbit/oak/util/CoreValueUtilTest.java
Author: angela
Date: Wed Apr 25 16:05:09 2012
New Revision: 1330376
URL: http://svn.apache.org/viewvc?rev=1330376&view=rev
Log:
OAK-33 : Values in oak-core (WIP)
- javadoc, tests for CoreValueUtil + minor improvement
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/CoreValueUtil.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/CoreValueUtilTest.java
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/CoreValueUtil.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/CoreValueUtil.java?rev=1330376&r1=1330375&r2=1330376&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/CoreValueUtil.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/CoreValueUtil.java Wed Apr 25 16:05:09 2012
@@ -33,7 +33,7 @@ import java.util.Map;
/**
* CoreValueUtil...
*
- * TODO: review if this should be added to CoreValue/*Factory interfaces
+ * TODO: review if this should be added to CoreValue/*Factory interfaces/implementation
*/
public class CoreValueUtil {
@@ -52,6 +52,17 @@ public class CoreValueUtil {
}
}
+ /**
+ * Returns the internal JSON representation of the specified {@code value}
+ * that is stored in the MicroKernel. All property types that are not
+ * reflected as JSON types are converted to strings and get a type prefix.
+ *
+ * @param value The core value to be converted.
+ * @return The encoded JSON string.
+ * @see JsonBuilder#encode(String)
+ * @see JsonBuilder#encode(long)
+ * @see JsonBuilder#encode(long)
+ */
public static String toJsonValue(CoreValue value) {
String jsonString;
switch (value.getType()) {
@@ -64,18 +75,27 @@ public class CoreValueUtil {
case PropertyType.STRING:
String str = value.getString();
if (startsWithHint(str)) {
- jsonString = buildJsonStringWithType(value);
+ jsonString = buildJsonStringWithHint(value);
} else {
jsonString = JsonBuilder.encode(value.getString());
}
break;
default:
// any other type
- jsonString = buildJsonStringWithType(value);
+ jsonString = buildJsonStringWithHint(value);
}
return jsonString;
}
+ /**
+ * Returns an JSON array containing the JSON representation of the
+ * specified values.
+ *
+ * @param values
+ * @return JSON array containing the JSON representation of the specified
+ * values.
+ * @see #toJsonValue(org.apache.jackrabbit.oak.api.CoreValue)
+ */
public static String toJsonArray(Iterable<CoreValue> values) {
StringBuilder sb = new StringBuilder();
sb.append('[');
@@ -90,6 +110,14 @@ public class CoreValueUtil {
return sb.toString();
}
+ /**
+ * Read a single value from the specified reader and convert it into a
+ * {@code CoreValue}. This method takes type-hint prefixes into account.
+ *
+ * @param reader The JSON reader.
+ * @param valueFactory The factory used to create the value.
+ * @return The value such as defined by the token obtained from the reader.
+ */
public static CoreValue fromJsopReader(JsopReader reader, CoreValueFactory valueFactory) {
CoreValue value;
if (reader.matches(JsopTokenizer.NUMBER)) {
@@ -113,20 +141,24 @@ public class CoreValueUtil {
return value;
}
+ /**
+ * Read the list of values from the specified reader and convert them into
+ * {@link CoreValue}s. This method takes type-hint prefixes into account.
+ *
+ * @param reader The JSON reader.
+ * @param valueFactory The factory used to create the values.
+ * @return A list of values such as defined by the reader.
+ */
public static List<CoreValue> listFromJsopReader(JsopReader reader, CoreValueFactory valueFactory) {
- if (!reader.matches('[')) {
- List<CoreValue> values = new ArrayList<CoreValue>();
- while (!reader.matches(']')) {
- values.add(fromJsopReader(reader, valueFactory));
- reader.matches(',');
- }
- return values;
- } else {
- throw new IllegalArgumentException("Unexpected token: " + reader.getToken());
+ List<CoreValue> values = new ArrayList<CoreValue>();
+ while (!reader.matches(']')) {
+ values.add(fromJsopReader(reader, valueFactory));
+ reader.matches(',');
}
+ return values;
}
- private static String buildJsonStringWithType(CoreValue value) {
+ private static String buildJsonStringWithHint(CoreValue value) {
StringBuilder sb = new StringBuilder();
sb.append(TYPE2HINT.get(value.getType()));
sb.append(':');
Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/CoreValueUtilTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/CoreValueUtilTest.java?rev=1330376&r1=1330375&r2=1330376&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/CoreValueUtilTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/CoreValueUtilTest.java Wed Apr 25 16:05:09 2012
@@ -32,7 +32,10 @@ import javax.jcr.PropertyType;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -52,58 +55,134 @@ public class CoreValueUtilTest {
private MicroKernel microKernel;
private CoreValueFactory valueFactory;
- private Map<CoreValue, String> map;
+ private Map<CoreValue, String> singleValueMap;
+ private Map<String, List<CoreValue>> mvValueMap;
@Before
public void setUp() throws IOException {
microKernel = new SimpleKernelImpl("mem:" + getClass().getName());
valueFactory = new CoreValueFactoryImpl(microKernel);
- map = new HashMap<CoreValue, String>();
- map.put(valueFactory.createValue("abc"), "\"abc\"");
- map.put(valueFactory.createValue("a:bc"), "\"a:bc\"");
- map.put(valueFactory.createValue("a:bc"), "\"a:bc\"");
- map.put(valueFactory.createValue("boo:abc"), "\"str:boo:abc\"");
- map.put(valueFactory.createValue("str:abc"), "\"str:str:abc\"");
- map.put(valueFactory.createValue("str:"), "\"str:str:\"");
-
- map.put(valueFactory.createValue(true), "true");
- map.put(valueFactory.createValue(false), "false");
-
- map.put(valueFactory.createValue(12345), "12345");
- map.put(valueFactory.createValue(1.23), "\"dou:1.23\"");
- BigDecimal bd = new BigDecimal("12345678901234567890");
- map.put(valueFactory.createValue(bd), "\"dec:" + bd.toString() + '\"');
+ singleValueMap = new HashMap<CoreValue, String>();
+ singleValueMap.put(valueFactory.createValue("abc"), "\"abc\"");
+ singleValueMap.put(valueFactory.createValue("a:bc"), "\"a:bc\"");
+ singleValueMap.put(valueFactory.createValue("a:bc"), "\"a:bc\"");
+ singleValueMap.put(valueFactory.createValue("boo:abc"), "\"str:boo:abc\"");
+ singleValueMap.put(valueFactory.createValue("str:abc"), "\"str:str:abc\"");
+ singleValueMap.put(valueFactory.createValue("str:"), "\"str:str:\"");
+
+ singleValueMap.put(valueFactory.createValue(true), "true");
+ singleValueMap.put(valueFactory.createValue(false), "false");
+
+ singleValueMap.put(valueFactory.createValue(12345), "12345");
+ singleValueMap.put(valueFactory.createValue(1.23), "\"dou:1.23\"");
+ BigDecimal decimal = new BigDecimal("12345678901234567890");
+ singleValueMap.put(valueFactory.createValue(decimal), "\"dec:" + decimal.toString() + '\"');
- map.put(valueFactory.createValue("2012-05-01T12:00.000:00GMT", PropertyType.DATE), "\"dat:2012-05-01T12:00.000:00GMT\"");
+ singleValueMap.put(valueFactory.createValue("2012-05-01T12:00.000:00GMT", PropertyType.DATE), "\"dat:2012-05-01T12:00.000:00GMT\"");
- map.put(valueFactory.createValue("jcr:primaryType", PropertyType.NAME), "\"nam:jcr:primaryType\"");
- map.put(valueFactory.createValue("/jcr:system", PropertyType.PATH), "\"pat:/jcr:system\"");
+ singleValueMap.put(valueFactory.createValue("jcr:primaryType", PropertyType.NAME), "\"nam:jcr:primaryType\"");
+ singleValueMap.put(valueFactory.createValue("/jcr:system", PropertyType.PATH), "\"pat:/jcr:system\"");
- map.put(valueFactory.createValue("http://jackrabbit.apache.org", PropertyType.URI), "\"uri:http://jackrabbit.apache.org\"");
+ singleValueMap.put(valueFactory.createValue("http://jackrabbit.apache.org", PropertyType.URI), "\"uri:http://jackrabbit.apache.org\"");
String uuid = UUID.randomUUID().toString();
- map.put(valueFactory.createValue(uuid, PropertyType.REFERENCE), "\"ref:" +uuid+ '\"');
- map.put(valueFactory.createValue(uuid, PropertyType.WEAKREFERENCE), "\"wea:" +uuid+ '\"');
+ singleValueMap.put(valueFactory.createValue(uuid, PropertyType.REFERENCE), "\"ref:" +uuid+ '\"');
+ singleValueMap.put(valueFactory.createValue(uuid, PropertyType.WEAKREFERENCE), "\"wea:" +uuid+ '\"');
CoreValue binary = valueFactory.createValue(new ByteArrayInputStream("123".getBytes()));
- map.put(binary, "\"bin:"+ binary.getString()+ '\"');
+ singleValueMap.put(binary, "\"bin:"+ binary.getString()+ '\"');
+
+ // multi valued properties
+ mvValueMap = new HashMap<String, List<CoreValue>>();
+ mvValueMap.put("[]", Collections.<CoreValue>emptyList());
+
+ List<CoreValue> strValues = new ArrayList<CoreValue>();
+ strValues.add(valueFactory.createValue("abc"));
+ strValues.add(valueFactory.createValue("a:bc"));
+ strValues.add(valueFactory.createValue("boo:abc"));
+ strValues.add(valueFactory.createValue("str:abc"));
+ strValues.add(valueFactory.createValue("str:"));
+ mvValueMap.put("[\"abc\",\"a:bc\",\"str:boo:abc\",\"str:str:abc\",\"str:str:\"]", strValues);
+
+ List<CoreValue> boValues = new ArrayList<CoreValue>();
+ boValues.add(valueFactory.createValue(true));
+ boValues.add(valueFactory.createValue(false));
+ mvValueMap.put("[true,false]", boValues);
+
+ List<CoreValue> longs = new ArrayList<CoreValue>();
+ longs.add(valueFactory.createValue(1));
+ longs.add(valueFactory.createValue(2));
+ longs.add(valueFactory.createValue(3));
+ mvValueMap.put("[1,2,3]", longs);
+
+ List<CoreValue> doubles = new ArrayList<CoreValue>();
+ doubles.add(valueFactory.createValue(1.23));
+ mvValueMap.put("[\"dou:1.23\"]", doubles);
+
+ List<CoreValue> decimals = new ArrayList<CoreValue>();
+ decimals.add(valueFactory.createValue(decimal));
+ decimals.add(valueFactory.createValue(decimal));
+ mvValueMap.put("[\"dec:" + decimal.toString() + "\",\"dec:" + decimal.toString() + "\"]", decimals);
+
+ List<CoreValue> dates = Collections.singletonList(valueFactory.createValue("2012-05-01T12:00.000:00GMT", PropertyType.DATE));
+ mvValueMap.put("[\"dat:2012-05-01T12:00.000:00GMT\"]", dates);
+
+ List<CoreValue> names = Collections.singletonList(valueFactory.createValue("jcr:primaryType", PropertyType.NAME));
+ mvValueMap.put("[\"nam:jcr:primaryType\"]", names);
+
+ List<CoreValue> paths = new ArrayList<CoreValue>();
+ paths.add(valueFactory.createValue("/jcr:system", PropertyType.PATH));
+ paths.add(valueFactory.createValue("../../content", PropertyType.PATH));
+ mvValueMap.put("[\"pat:/jcr:system\",\"pat:../../content\"]", paths);
+
+ List<CoreValue> uris = Collections.singletonList(valueFactory.createValue("http://jackrabbit.apache.org", PropertyType.URI));
+ mvValueMap.put("[\"uri:http://jackrabbit.apache.org\"]", uris);
+
+ List<CoreValue> refs = new ArrayList<CoreValue>();
+ refs.add(valueFactory.createValue(uuid, PropertyType.REFERENCE));
+ mvValueMap.put("[\"ref:" +uuid+ "\"]", refs);
+
+ List<CoreValue> wr = new ArrayList<CoreValue>();
+ wr.add(valueFactory.createValue(uuid, PropertyType.WEAKREFERENCE));
+ mvValueMap.put("[\"wea:" +uuid+ "\"]", wr);
+
+ mvValueMap.put("[\"bin:"+ binary.getString()+ "\"]", Collections.singletonList(binary));
}
@Test
public void testToJsonValue() throws IOException {
- for (CoreValue v : map.keySet()) {
- String json = map.get(v);
+ for (CoreValue v : singleValueMap.keySet()) {
+ String json = singleValueMap.get(v);
assertEquals(json, CoreValueUtil.toJsonValue(v));
}
}
@Test
public void testFromJsonValue() throws IOException {
- for (CoreValue v : map.keySet()) {
- String json = map.get(v);
+ for (CoreValue v : singleValueMap.keySet()) {
+ String json = singleValueMap.get(v);
JsopReader reader = new JsopTokenizer(json);
assertEquals(v, CoreValueUtil.fromJsopReader(reader, valueFactory));
}
}
+
+ @Test
+ public void testToJsonArray() throws IOException {
+ for (String json : mvValueMap.keySet()) {
+ List<CoreValue> values = mvValueMap.get(json);
+ assertEquals(json, CoreValueUtil.toJsonArray(values));
+ }
+ }
+
+ @Test
+ public void testListFromJsopReader() throws IOException {
+ for (String json : mvValueMap.keySet()) {
+ List<CoreValue> values = mvValueMap.get(json);
+ JsopReader reader = new JsopTokenizer(json);
+ if (reader.matches('[')) {
+ assertEquals(values, CoreValueUtil.listFromJsopReader(reader, valueFactory));
+ }
+ }
+ }
}
\ No newline at end of file