jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1371637 - in /jackrabbit/oak/trunk/oak-mk/src: main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java test/java/org/apache/jackrabbit/mk/json/JsopTest.java
Date Fri, 10 Aug 2012 10:12:20 GMT
Author: thomasm
Date: Fri Aug 10 10:12:20 2012
New Revision: 1371637

URL: http://svn.apache.org/viewvc?rev=1371637&view=rev
Log:
OAK-19 Consolidate JSON utilities - bugfix in readRawValue()

Modified:
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
    jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java?rev=1371637&r1=1371636&r2=1371637&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
Fri Aug 10 10:12:20 2012
@@ -373,7 +373,7 @@ public class JsopTokenizer implements Js
      * Add an asterisk ('[*]') at the given position. This format is used to
      * show where parsing failed in a statement.
      *
-     * @param s     the text
+     * @param s the text
      * @param index the position
      * @return the text with asterisk
      */
@@ -386,13 +386,14 @@ public class JsopTokenizer implements Js
     }
 
     /**
-     * Read a value and return the raw Json representation.
+     * Read a value and return the raw Json representation. This includes arrays
+     * and nested arrays.
      *
      * @return the Json representation of the value
      */
     public String readRawValue() {
         int start = lastPos;
-        while (jsop.charAt(start) <= ' ') {
+        while (start < length && jsop.charAt(start) <= ' ') {
             start++;
         }
         skipRawValue();
@@ -402,13 +403,16 @@ public class JsopTokenizer implements Js
     private void skipRawValue() {
         switch (currentType) {
             case '[': {
-                read();
                 int level = 0;
                 while (true) {
-                    if (matches(']') && level-- == 0) {
-                        break;
+                    if (matches(']')) {
+                        if (--level == 0) {
+                            break;
+                        }
                     } else if (matches('[')) {
                         level++;
+                    } else if (matches(JsopReader.END)) {
+                        throw getFormatException(jsop, pos, "value");
                     } else {
                         read();
                     }

Modified: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java?rev=1371637&r1=1371636&r2=1371637&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
Fri Aug 10 10:12:20 2012
@@ -31,7 +31,7 @@ public class JsopTest extends TestCase {
             String s = "Hello \"World\" Hello \"World\" Hello \"World\" Hello \"World\" Hello
\"World\" Hello \"World\" ";
             StopWatch timer = new StopWatch();
             int t2 = 0;
-            for (int i = 0; i < 10000000; i++) {
+            for (int i = 0; i < 1000000; i++) {
                 t2 += JsopBuilder.encode(s).length();
             }
             System.out.println(timer.seconds() + " dummy: " + t2);
@@ -61,8 +61,9 @@ public class JsopTest extends TestCase {
         assertEquals("\\/Date(0)\\/", t.getEscapedToken());
     }
 
-    public void testNull() {
-        JsopTokenizer t = new JsopTokenizer("null, 1, null, true, false");
+    public void testNullTrueFalse() {
+        JsopTokenizer t;
+        t = new JsopTokenizer("null, 1, null, true, false");
         assertEquals(null, t.read(JsopReader.NULL));
         assertEquals(",", t.read(','));
         assertEquals("1", t.read(JsopReader.NUMBER));
@@ -72,6 +73,14 @@ public class JsopTest extends TestCase {
         assertEquals("true", t.read(JsopReader.TRUE));
         assertEquals(",", t.read(','));
         assertEquals("false", t.read(JsopReader.FALSE));
+        t = new JsopTokenizer("true, false");
+        assertEquals("true", t.read(JsopReader.TRUE));
+        assertEquals(",", t.read(','));
+        assertEquals("false", t.read(JsopReader.FALSE));
+        t = new JsopTokenizer("false, true");
+        assertEquals("false", t.read(JsopReader.FALSE));
+        assertEquals(",", t.read(','));
+        assertEquals("true", t.read(JsopReader.TRUE));
     }
 
     public void testLineLength() {
@@ -103,7 +112,22 @@ public class JsopTest extends TestCase {
     }
 
     public void testRawValue() {
-        JsopTokenizer t = new JsopTokenizer("{\"x\": [1], null, true, {\"y\": 1}, error}");
+        JsopTokenizer t;
+        t = new JsopTokenizer("");
+        try {
+            t.readRawValue();
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        t = new JsopTokenizer("[unclosed");
+        try {
+            t.readRawValue();
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        t = new JsopTokenizer("{\"x\": [1], null, true, {\"y\": 1}, [1, 2], [], [[1]], +error+}");
         t.read('{');
         assertEquals("x", t.readString());
         t.read(':');
@@ -114,6 +138,13 @@ public class JsopTest extends TestCase {
         assertEquals("true", t.readRawValue());
         t.read(',');
         assertEquals("{\"y\": 1}", t.readRawValue());
+        t.read(',');
+        assertEquals("[1, 2]", t.readRawValue());
+        t.read(',');
+        assertEquals("[]", t.readRawValue());
+        t.read(',');
+        assertEquals("[[1]]", t.readRawValue());
+        t.read(',');
         try {
             t.readRawValue();
             fail();



Mime
View raw message