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 ju...@apache.org on 2013/05/27 13:06:15 UTC

svn commit: r1486573 - in /jackrabbit/oak/trunk: oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/ oak-core/src/test/java/org/apache/jackrabbit/oak/query/ oak-core/src/test/java/org/apache/jackrabbit/oak/util/ oak-lucene/src/test...

Author: jukka
Date: Mon May 27 11:06:15 2013
New Revision: 1486573

URL: http://svn.apache.org/r1486573
Log:
OAK-838: Refactor JsopUtil to not duplicate code from KernelNodeState

Removed:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/JsopUtil.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/JsopUtilTest.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexQueryTest.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/AbstractQueryTest.java
    jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java
    jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrIndexQueryTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexQueryTest.java?rev=1486573&r1=1486572&r2=1486573&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexQueryTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexQueryTest.java Mon May 27 11:06:15 2013
@@ -27,11 +27,11 @@ import org.apache.jackrabbit.oak.Oak;
 import org.apache.jackrabbit.oak.api.ContentRepository;
 import org.apache.jackrabbit.oak.api.PropertyValue;
 import org.apache.jackrabbit.oak.api.ResultRow;
+import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
 import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexProvider;
 import org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent;
 import org.apache.jackrabbit.oak.query.AbstractQueryTest;
-import org.apache.jackrabbit.oak.query.JsopUtil;
 import org.apache.jackrabbit.oak.spi.query.PropertyValues;
 import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
 import org.junit.Test;
@@ -58,9 +58,10 @@ public class PropertyIndexQueryTest exte
 
     @Test
     public void bindVariableTest() throws Exception {
-        JsopUtil.apply(
-                root,
-                "/ + \"test\": { \"hello\": {\"id\": \"1\"}, \"world\": {\"id\": \"2\"}}");
+        Tree tree = root.getTree("/");
+        Tree test = tree.addChild("test");
+        test.addChild("hello").setProperty("id", "1");
+        test.addChild("world").setProperty("id", "2");
         root.commit();
 
         Map<String, PropertyValue> sv = new HashMap<String, PropertyValue>();

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/AbstractQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/AbstractQueryTest.java?rev=1486573&r1=1486572&r2=1486573&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/AbstractQueryTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/AbstractQueryTest.java Mon May 27 11:06:15 2013
@@ -29,9 +29,14 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 
+import javax.jcr.PropertyType;
+
 import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.mk.json.JsopReader;
+import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.oak.api.ContentRepository;
 import org.apache.jackrabbit.oak.api.ContentSession;
+import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.PropertyValue;
 import org.apache.jackrabbit.oak.api.Result;
 import org.apache.jackrabbit.oak.api.ResultRow;
@@ -39,12 +44,20 @@ import org.apache.jackrabbit.oak.api.Roo
 import org.apache.jackrabbit.oak.api.QueryEngine;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.kernel.TypeCodes;
+import org.apache.jackrabbit.oak.plugins.memory.BooleanPropertyState;
+import org.apache.jackrabbit.oak.plugins.memory.StringPropertyState;
+import org.apache.jackrabbit.oak.plugins.value.Conversions;
 import org.junit.Before;
 
+import com.google.common.collect.Lists;
+
 import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NAME;
 import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NODE_TYPE;
 import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.REINDEX_PROPERTY_NAME;
 import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.TYPE_PROPERTY_NAME;
+import static org.apache.jackrabbit.oak.plugins.memory.PropertyStates.createProperty;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -187,7 +200,7 @@ public abstract class AbstractQueryTest 
                 } else if (line.startsWith("commit")) {
                     w.println(line);
                     line = line.substring("commit".length()).trim();
-                    JsopUtil.apply(root, line);
+                    apply(root, line);
                     root.commit();
                 }
                 w.flush();
@@ -276,4 +289,154 @@ public abstract class AbstractQueryTest 
                 .getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
     }
 
+    /**
+     * Applies the commit string to a given Root instance
+     *
+     * The commit string represents a sequence of operations, jsonp style:
+     *
+     * <p>
+     * / + "test": { "a": { "id": "ref:123" }, "b": { "id" : "str:123" }}
+     * <p>
+     * or
+     * <p>
+     * "/ - "test"
+     * </p>
+     *
+     * @param root
+     * @param commit the commit string
+     * @throws UnsupportedOperationException if the operation is not supported
+     */
+    private static void apply(Root root, String commit)
+            throws UnsupportedOperationException {
+        int index = commit.indexOf(' ');
+        String path = commit.substring(0, index).trim();
+        Tree c = root.getTree(path);
+        if (!c.exists()) {
+            // TODO create intermediary?
+            throw new UnsupportedOperationException("Non existing path " + path);
+        }
+        commit = commit.substring(index);
+        JsopTokenizer tokenizer = new JsopTokenizer(commit);
+        if (tokenizer.matches('-')) {
+            removeTree(c, tokenizer);
+        } else if (tokenizer.matches('+')) {
+            addTree(c, tokenizer);
+        } else {
+            throw new UnsupportedOperationException(
+                    "Unsupported " + (char) tokenizer.read()
+                    + ". This should be either '+' or '-'.");
+        }
+    }
+
+    private static void removeTree(Tree t, JsopTokenizer tokenizer) {
+        String path = tokenizer.readString();
+        for (String p : PathUtils.elements(path)) {
+            if (!t.hasChild(p)) {
+                return;
+            }
+            t = t.getChild(p);
+        }
+        t.remove();
+    }
+
+    private static void addTree(Tree t, JsopTokenizer tokenizer) {
+        do {
+            String key = tokenizer.readString();
+            tokenizer.read(':');
+            if (tokenizer.matches('{')) {
+                Tree c = t.addChild(key);
+                if (!tokenizer.matches('}')) {
+                    addTree(c, tokenizer);
+                    tokenizer.read('}');
+                }
+            } else if (tokenizer.matches('[')) {
+                t.setProperty(readArrayProperty(key, tokenizer));
+            } else {
+                t.setProperty(readProperty(key, tokenizer));
+            }
+        } while (tokenizer.matches(','));
+    }
+
+    /**
+     * Read a {@code PropertyState} from a {@link JsopReader}
+     * @param name  The name of the property state
+     * @param reader  The reader
+     * @return new property state
+     */
+    private static PropertyState readProperty(String name, JsopReader reader) {
+        if (reader.matches(JsopReader.NUMBER)) {
+            String number = reader.getToken();
+            return createProperty(name, number, PropertyType.LONG);
+        } else if (reader.matches(JsopReader.TRUE)) {
+            return BooleanPropertyState.booleanProperty(name, true);
+        } else if (reader.matches(JsopReader.FALSE)) {
+            return BooleanPropertyState.booleanProperty(name, false);
+        } else if (reader.matches(JsopReader.STRING)) {
+            String jsonString = reader.getToken();
+            int split = TypeCodes.split(jsonString);
+            if (split != -1) {
+                int type = TypeCodes.decodeType(split, jsonString);
+                String value = TypeCodes.decodeName(split, jsonString);
+                if (type == PropertyType.BINARY) {
+                    throw new UnsupportedOperationException();
+                } else {
+                    return createProperty(name, value, type);
+                }
+            } else {
+                return StringPropertyState.stringProperty(name, jsonString);
+            }
+        } else {
+            throw new IllegalArgumentException("Unexpected token: " + reader.getToken());
+        }
+    }
+
+    /**
+     * Read a multi valued {@code PropertyState} from a {@link JsopReader}
+     * @param name  The name of the property state
+     * @param reader  The reader
+     * @return new property state
+     */
+    private static PropertyState readArrayProperty(String name, JsopReader reader) {
+        int type = PropertyType.STRING;
+        List<Object> values = Lists.newArrayList();
+        while (!reader.matches(']')) {
+            if (reader.matches(JsopReader.NUMBER)) {
+                String number = reader.getToken();
+                type = PropertyType.LONG;
+                values.add(Conversions.convert(number).toLong());
+            } else if (reader.matches(JsopReader.TRUE)) {
+                type = PropertyType.BOOLEAN;
+                values.add(true);
+            } else if (reader.matches(JsopReader.FALSE)) {
+                type = PropertyType.BOOLEAN;
+                values.add(false);
+            } else if (reader.matches(JsopReader.STRING)) {
+                String jsonString = reader.getToken();
+                int split = TypeCodes.split(jsonString);
+                if (split != -1) {
+                    type = TypeCodes.decodeType(split, jsonString);
+                    String value = TypeCodes.decodeName(split, jsonString);
+                    if (type == PropertyType.BINARY) {
+                        throw new UnsupportedOperationException();
+                    } else if (type == PropertyType.DOUBLE) {
+                        values.add(Conversions.convert(value).toDouble());
+                    } else if (type == PropertyType.DECIMAL) {
+                        values.add(Conversions.convert(value).toDecimal());
+                    } else if(type == PropertyType.DATE) {
+                        values.add(Conversions.convert(value).toCalendar().getTimeInMillis());
+                    } else {
+                        values.add(value);
+                    }
+                } else {
+                    type = PropertyType.STRING;
+                    values.add(jsonString);
+                }
+            } else {
+                throw new IllegalArgumentException("Unexpected token: " + reader.getToken());
+            }
+            reader.matches(',');
+        }
+        return createProperty(name, values, Type.fromTag(type, true));
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java?rev=1486573&r1=1486572&r2=1486573&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java Mon May 27 11:06:15 2013
@@ -16,7 +16,9 @@
  */
 package org.apache.jackrabbit.oak.plugins.index.lucene;
 
+import static java.util.Arrays.asList;
 import static junit.framework.Assert.assertEquals;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
@@ -27,7 +29,6 @@ import org.apache.jackrabbit.oak.api.Con
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent;
 import org.apache.jackrabbit.oak.query.AbstractQueryTest;
-import org.apache.jackrabbit.oak.query.JsopUtil;
 import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
 import org.junit.Test;
 
@@ -67,7 +68,9 @@ public class LuceneIndexQueryTest extend
 
     @Test
     public void descendantTest() throws Exception {
-        JsopUtil.apply(root, "/ + \"test\": { \"a\": {}, \"b\": {} }");
+        Tree test = root.getTree("/").addChild("test");
+        test.addChild("a");
+        test.addChild("b");
         root.commit();
 
         Iterator<String> result = executeQuery(
@@ -81,9 +84,9 @@ public class LuceneIndexQueryTest extend
 
     @Test
     public void descendantTest2() throws Exception {
-        JsopUtil.apply(
-                root,
-                "/ + \"test\": { \"a\": { \"name\": [\"Hello\", \"World\" ] }, \"b\": { \"name\" : \"Hello\" }}");
+        Tree test = root.getTree("/").addChild("test");
+        test.addChild("a").setProperty("name", asList("Hello", "World"), STRINGS);
+        test.addChild("b").setProperty("name", "Hello");
         root.commit();
 
         Iterator<String> result = executeQuery(
@@ -96,12 +99,16 @@ public class LuceneIndexQueryTest extend
 
     @Test
     public void ischildnodeTest() throws Exception {
-        JsopUtil.apply(
-                root,
-                "/ + \"parents\": { \"p0\": {\"id\": \"0\"}, \"p1\": {\"id\": \"1\"}, \"p2\": {\"id\": \"2\"}}");
-        JsopUtil.apply(
-                root,
-                "/ + \"children\": { \"c1\": {\"p\": \"1\"}, \"c2\": {\"p\": \"1\"}, \"c3\": {\"p\": \"2\"}, \"c4\": {\"p\": \"3\"}}");
+        Tree tree = root.getTree("/");
+        Tree parents = tree.addChild("parents");
+        parents.addChild("p0").setProperty("id", "0");
+        parents.addChild("p1").setProperty("id", "1");
+        parents.addChild("p2").setProperty("id", "2");
+        Tree children = tree.addChild("children");
+        children.addChild("c1").setProperty("p", "1");
+        children.addChild("c2").setProperty("p", "2");
+        children.addChild("c3").setProperty("p", "3");
+        children.addChild("c4").setProperty("p", "4");
         root.commit();
 
         Iterator<String> result = executeQuery(
@@ -120,8 +127,9 @@ public class LuceneIndexQueryTest extend
         String h = "Hello" + System.currentTimeMillis();
         String w = "World" + System.currentTimeMillis();
 
-        JsopUtil.apply(root, "/ + \"test\": { \"a\": { \"name\": [\"" + h
-                + "\", \"" + w + "\" ] }, \"b\": { \"name\" : \"" + h + "\" }}");
+        Tree test = root.getTree("/").addChild("test");
+        test.addChild("a").setProperty("name", asList(h, w), STRINGS);
+        test.addChild("b").setProperty("name", h);
         root.commit();
 
         // query 'hello'

Modified: jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrIndexQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrIndexQueryTest.java?rev=1486573&r1=1486572&r2=1486573&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrIndexQueryTest.java (original)
+++ jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrIndexQueryTest.java Mon May 27 11:06:15 2013
@@ -16,7 +16,9 @@
  */
 package org.apache.jackrabbit.oak.plugins.index.solr.query;
 
+import static java.util.Arrays.asList;
 import static junit.framework.Assert.assertEquals;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
@@ -29,7 +31,6 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.plugins.index.solr.TestUtils;
 import org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent;
 import org.apache.jackrabbit.oak.query.AbstractQueryTest;
-import org.apache.jackrabbit.oak.query.JsopUtil;
 import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
 import org.apache.solr.client.solrj.SolrServer;
 import org.junit.After;
@@ -85,7 +86,9 @@ public class SolrIndexQueryTest extends 
 
     @Test
     public void descendantTest() throws Exception {
-        JsopUtil.apply(root, "/ + \"test\": { \"a\": {}, \"b\": {} }");
+        Tree test = root.getTree("/").addChild("test");
+        test.addChild("a");
+        test.addChild("b");
         root.commit();
 
         Iterator<String> result = executeQuery(
@@ -99,9 +102,9 @@ public class SolrIndexQueryTest extends 
 
     @Test
     public void descendantTest2() throws Exception {
-        JsopUtil.apply(
-                root,
-                "/ + \"test\": { \"a\": { \"name\": [\"Hello\", \"World\" ] }, \"b\": { \"name\" : \"Hello\" }}");
+        Tree test = root.getTree("/").addChild("test");
+        test.addChild("a").setProperty("name", asList("Hello", "World"), STRINGS);
+        test.addChild("b").setProperty("name", "Hello");
         root.commit();
 
         Iterator<String> result = executeQuery(
@@ -114,12 +117,16 @@ public class SolrIndexQueryTest extends 
 
     @Test
     public void ischildnodeTest() throws Exception {
-        JsopUtil.apply(
-                root,
-                "/ + \"parents\": { \"p0\": {\"id\": \"0\"}, \"p1\": {\"id\": \"1\"}, \"p2\": {\"id\": \"2\"}}");
-        JsopUtil.apply(
-                root,
-                "/ + \"children\": { \"c1\": {\"p\": \"1\"}, \"c2\": {\"p\": \"1\"}, \"c3\": {\"p\": \"2\"}, \"c4\": {\"p\": \"3\"}}");
+        Tree tree = root.getTree("/");
+        Tree parents = tree.addChild("parents");
+        parents.addChild("p0").setProperty("id", "0");
+        parents.addChild("p1").setProperty("id", "1");
+        parents.addChild("p2").setProperty("id", "2");
+        Tree children = tree.addChild("children");
+        children.addChild("c1").setProperty("p", "1");
+        children.addChild("c2").setProperty("p", "2");
+        children.addChild("c3").setProperty("p", "3");
+        children.addChild("c4").setProperty("p", "4");
         root.commit();
 
         Iterator<String> result = executeQuery(