jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1189796 [2/2] - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/ main/java/org/apache/jackrabbit/mk/index/ main/java/org/apache/jackrabbit/mk/json/ main/java/org/apache/jackrabbit/mk/mem/ main/java/org/apache/jac...
Date Thu, 27 Oct 2011 15:00:16 GMT
Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopStreamTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopStreamTest.java?rev=1189796&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopStreamTest.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopStreamTest.java
Thu Oct 27 15:00:15 2011
@@ -0,0 +1,215 @@
+/*
+ * 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.jackrabbit.mk.json;
+
+import java.io.IOException;
+import junit.framework.TestCase;
+
+public class JsopStreamTest extends TestCase {
+
+    // run the micro-benchmark
+    public static void main(String... args) {
+        for (int k = 0; k < 5; k++) {
+            String s = "Hello \"World\" Hello \"World\" Hello \"World\" Hello \"World\" Hello
\"World\" Hello \"World\" ";
+            long t = System.currentTimeMillis();
+            JsopWriter w = k % 2 == 1 ? new JsopBuilder() : new JsopStream();
+            for (int i = 0; i < 1000000; i++) {
+                w.value(s);
+                if (i % 100 == 0) {
+                    w.reset();
+                }
+            }
+            System.out.println(w.getClass() + ": " + (System.currentTimeMillis() - t));
+        }
+        // JsopStream: 20
+        // JsopBuilder: 1150
+    }
+
+    public void testNested() {
+        JsopStream s = new JsopStream().key("x");
+        JsopStream nested = new JsopStream().array().value(1).value(null).value(true).value(false).value("Hello").endArray();
+        s.append(nested);
+        assertEquals("\"x\":[1,null,true,false,\"Hello\"]", s.toString());
+    }
+
+    public void testJsopReader() {
+        JsopStream s = new JsopStream().tag('+').
+            key("x").object().
+                key("y").value(1).
+                key("n").value("").
+                key("z").encodedValue("n10").
+                endObject();
+        s.setLineLength(-1);
+        assertEquals("+\"x\":{\"y\":1,\"n\":\"\",\"z\":n10}", s.toString());
+        assertFalse(s.matches('-'));
+        assertTrue(s.matches('+'));
+        assertEquals("x", s.read(JsopTokenizer.STRING));
+        try {
+            s.read('}');
+            fail();
+        } catch (Exception e) {
+            // expected
+        }
+        s.read(':');
+        assertEquals("{", s.read('{'));
+        assertEquals("y", s.readString());
+        s.read(':');
+        assertEquals("1", s.readRawValue());
+        s.read(',');
+        assertEquals("n", s.readString());
+        s.read(':');
+        assertEquals("\"\"", s.readRawValue());
+        s.read(',');
+        assertEquals("z", s.readString());
+        s.read(':');
+        assertEquals("n10", s.readRawValue());
+        s.read('}');
+    }
+
+    public void testTokenizer() {
+        test("+ \"x\": {}",
+                new JsopStream().tag('+').
+                    key("x").object().endObject());
+        test("[\"-1\": -1, " +
+                "\"true\": true, " +
+                "\"false\": false, " +
+                "\"null\": null]",
+                new JsopStream().array().
+                    key("-1").value(-1).
+                    key("true").value(true).
+                    key("false").value(false).
+                    key("null").value(null).
+                    endArray());
+
+    }
+
+    static void test(String expected, JsopReader t) {
+        String j2 = prettyPrint(t);
+        assertEquals(expected, j2);
+        j2 = prettyPrint(new JsopTokenizer(j2));
+        assertEquals(expected, j2);
+    }
+
+    public static String prettyPrint(JsopReader t) {
+        StringBuilder buff = new StringBuilder();
+        while (true) {
+            prettyPrint(buff, t, "  ");
+            if (t.getTokenType() == JsopTokenizer.END) {
+                return buff.toString();
+            }
+        }
+    }
+
+    static String prettyPrint(StringBuilder buff, JsopReader t, String ident) {
+        String space = "";
+        boolean inArray = false;
+        while (true) {
+            int token = t.read();
+            switch (token) {
+                case JsopTokenizer.END:
+                    return buff.toString();
+                case JsopTokenizer.STRING:
+                    buff.append(JsopBuilder.encode(t.getToken()));
+                    break;
+                case JsopTokenizer.NUMBER:
+                case JsopTokenizer.TRUE:
+                case JsopTokenizer.FALSE:
+                case JsopTokenizer.NULL:
+                case JsopTokenizer.IDENTIFIER:
+                case JsopTokenizer.ERROR:
+                    buff.append(t.getToken());
+                    break;
+                case '{':
+                    if (t.matches('}')) {
+                        buff.append("{}");
+                    } else {
+                        buff.append("{\n").append(space += ident);
+                    }
+                    break;
+                case '}':
+                    space = space.substring(0, space.length() - ident.length());
+                    buff.append('\n').append(space).append("}");
+                    break;
+                case '[':
+                    inArray = true;
+                    buff.append("[");
+                    break;
+                case ']':
+                    inArray = false;
+                    buff.append("]");
+                    break;
+                case ',':
+                    if (!inArray) {
+                        buff.append(",\n").append(space);
+                    } else {
+                        buff.append(", ");
+                    }
+                    break;
+                default:
+                    buff.append((char) token).append(' ');
+                    break;
+            }
+        }
+    }
+
+    public void testBuilder() throws IOException {
+
+        JsopWriter buff = new JsopStream();
+        buff.tag('+').object().
+                key("foo").value("bar").
+                key("int").value(3).
+                key("decimal").encodedValue("3.0").
+                key("obj").object().
+                key("boolean").value(true).
+                key("null").value(null).
+                key("arr").array().
+                array().
+                value(1).
+                value("\u001f ~ \u007f \u0080").
+                value("42").
+                endArray().
+                array().
+                endArray().
+                endArray().
+                endObject().
+                key("some").value("more").
+                endObject();
+
+        String json = buff.toString();
+        assertEquals("+{\"foo\":\"bar\",\"int\":3,\"decimal\":3.0," +
+                "\"obj\":{\"boolean\":true,\"null\":null," +
+                "\"arr\":[[1,\"\\u001f ~ \\u007f \\u0080\",\"42\"],[]]},\"some\":\"more\"}",
json);
+
+        buff.reset();
+        buff.array().
+                object().key("x").value("1").endObject().newline().
+                object().key("y").value("2").endObject().newline().
+                endArray();
+        json = buff.toString();
+        assertEquals("[{\"x\":\"1\"},{\"y\":\"2\"}]", json);
+
+        buff = new JsopStream();
+        buff.tag('+').key("x").value("1").newline();
+        buff.tag('+').key("y").value("2").newline();
+        buff.tag('+').key("z").value(false).newline();
+        json = buff.toString();
+        assertEquals("+\"x\":\"1\"+\"y\":\"2\"+\"z\":false", json);
+
+    }
+
+}
+

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java?rev=1189796&r1=1189795&r2=1189796&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
Thu Oct 27 15:00:15 2011
@@ -284,7 +284,7 @@ public class JsopTest extends TestCase {
     public void testBuilder() throws IOException {
 
         JsopBuilder buff = new JsopBuilder();
-        buff.appendTag("+ ").object().
+        buff.tag('+').object().
                 key("foo").value("bar").
                 key("int").value(3).
                 key("decimal").encodedValue("3.0").
@@ -305,7 +305,7 @@ public class JsopTest extends TestCase {
                 endObject();
 
         String json = buff.toString();
-        assertEquals("+ {\"foo\":\"bar\",\"int\":3,\"decimal\":3.0," +
+        assertEquals("+{\"foo\":\"bar\",\"int\":3,\"decimal\":3.0," +
                 "\"obj\":{\"boolean\":true,\"null\":null," +
                 "\"arr\":[[1,\"\\u001f ~ \\u007f \\u0080\",\"42\"],[]]},\"some\":\"more\"}",
json);
 
@@ -318,10 +318,10 @@ public class JsopTest extends TestCase {
         assertEquals("[{\"x\":\"1\"}\n,{\"y\":\"2\"}\n]", json);
 
         buff = new JsopBuilder();
-        buff.appendTag("+ ").key("x").value("1").newline();
-        buff.appendTag("+ ").key("y").value("2").newline();
+        buff.tag('+').key("x").value("1").newline();
+        buff.tag('+').key("y").value("2").newline();
         json = buff.toString();
-        assertEquals("+ \"x\":\"1\"\n+ \"y\":\"2\"\n", json);
+        assertEquals("+\"x\":\"1\"\n+\"y\":\"2\"\n", json);
 
     }
 

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/TestSecurityWrapper.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/TestSecurityWrapper.java?rev=1189796&r1=1189795&r2=1189796&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/TestSecurityWrapper.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/wrapper/TestSecurityWrapper.java
Thu Oct 27 15:00:15 2011
@@ -108,16 +108,16 @@ public class TestSecurityWrapper extends
         toRevision = mkAdmin.commit("/", "^ \":rights\": \"read\"", fromRevision, "");
         String j3 = mkGuest.getJournal(fromRevision, toRevision);
         assertEquals(
-                "+ \"/test\":{\"data\":\"Hello\"}\n" +
-                "^ \"/test/data\":\"Hallo\"\n" +
-                "^ \"/test/data\":null\n",
+                "+\"/test\":{\"data\":\"Hello\"}\n" +
+                "^\"/test/data\":\"Hallo\"\n" +
+                "^\"/test/data\":null\n",
                 filterJournal(j3));
         String journal = mkAdmin.getJournal(fromRevision, toRevision);
         assertEquals(
-                "+ \"/test\":{\"data\":\"Hello\"}\n" +
-                "^ \"/test/data\":\"Hallo\"\n" +
-                "^ \"/test/data\":null\n" +
-                "+ \"/:rights\":\"read\"",
+                "+\"/test\":{\"data\":\"Hello\"}\n" +
+                "^\"/test/data\":\"Hallo\"\n" +
+                "^\"/test/data\":null\n" +
+                "+\"/:rights\":\"read\"",
                 filterJournal(journal));
     }
 



Mime
View raw message