incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r601312 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/helpers/json/JsonItemWriter.java test/java/org/apache/sling/microsling/integration/JsonRenderingTest.java
Date Wed, 05 Dec 2007 12:16:45 GMT
Author: bdelacretaz
Date: Wed Dec  5 04:16:44 2007
New Revision: 601312

URL: http://svn.apache.org/viewvc?rev=601312&view=rev
Log:
SLING-121 - recursive JSON node dump did caused errors in JSONWriter, due to misplaced keys
for child nodes

Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/helpers/json/JsonItemWriter.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/JsonRenderingTest.java

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/helpers/json/JsonItemWriter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/helpers/json/JsonItemWriter.java?rev=601312&r1=601311&r2=601312&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/helpers/json/JsonItemWriter.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/helpers/json/JsonItemWriter.java
Wed Dec  5 04:16:44 2007
@@ -93,10 +93,12 @@
         }
 
         // the child nodes
-        final NodeIterator children = node.getNodes();
-        while(children.hasNext()) {
-            final Node n = children.nextNode();
-            dumpSingleNode(n, w, currentRecursionLevel, maxRecursionLevels);
+        if(recursionLevelActive(currentRecursionLevel, maxRecursionLevels)) {
+            final NodeIterator children = node.getNodes();
+            while(children.hasNext()) {
+                final Node n = children.nextNode();
+                dumpSingleNode(n, w, currentRecursionLevel, maxRecursionLevels);
+            }
         }
 
         w.endObject();
@@ -105,10 +107,15 @@
     /** Dump a single node */
     protected void dumpSingleNode(Node n, JSONWriter w, int currentRecursionLevel, int maxRecursionLevels)
     throws RepositoryException, JSONException {
-        w.key(n.getName());
-        if (maxRecursionLevels == 0 || currentRecursionLevel + 1 < maxRecursionLevels)
{
+        if (recursionLevelActive(currentRecursionLevel, maxRecursionLevels)) {
+            w.key(n.getName());
             dump(n, w, currentRecursionLevel + 1, maxRecursionLevels);
         }
+    }
+    
+    /** true if the current recursion level is active */
+    protected boolean recursionLevelActive(int currentRecursionLevel, int maxRecursionLevels)
{
+        return currentRecursionLevel < maxRecursionLevels;
     }
 
     /**

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/JsonRenderingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/JsonRenderingTest.java?rev=601312&r1=601311&r2=601312&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/JsonRenderingTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/JsonRenderingTest.java
Wed Dec  5 04:16:44 2007
@@ -52,19 +52,44 @@
         assertJavascript(testText, json ,"out.println(data.text)");
     }
     
+    /**  Create a node with children, verify that we get them back in JSON format */ 
     public void testRecursiveOneLevel() throws IOException {
         final Map<String,String> props = new HashMap<String,String>();
         props.put("text", testText);
         
         final String parentNodeUrl = testClient.createNode(postUrl, props);
-        for(String child : new String [] { "A", "B", "C" }) {
+        final String [] children = { "A", "B", "C" };
+        for(String child : children) {
+            props.put("child", child);
+            testClient.createNode(parentNodeUrl + "/" + child, props);
+        }
+        
+        final String json = getContent(parentNodeUrl + ".json?slingItemDumpRecursionLevel=1",
CONTENT_TYPE_JSON);
+        assertJavascript(testText, json, "out.print(data.text)");
+        for(String child : children) {
+            assertJavascript(child, json, "out.print(data['" + child + "'].child)");
+            assertJavascript(testText, json, "out.print(data['" + child + "'].text)");
+        }
+    }
+    
+    /**  Create a node with children, verify that we do not get them back in 
+     *   JSON format if using recursion level=0 */ 
+    public void testRecursiveZeroLevels() throws IOException {
+        final Map<String,String> props = new HashMap<String,String>();
+        props.put("text", testText);
+        
+        final String parentNodeUrl = testClient.createNode(postUrl, props);
+        final String [] children = { "A", "B", "C" };
+        for(String child : children) {
             props.put("child", child);
             testClient.createNode(parentNodeUrl + "/" + child, props);
         }
         
-        // TODO fails if recursion level=1
         final String json = getContent(parentNodeUrl + ".json?slingItemDumpRecursionLevel=0",
CONTENT_TYPE_JSON);
         assertJavascript(testText, json, "out.print(data.text)");
+        for(String child : children) {
+            assertJavascript("undefined", json, "out.print(typeof data['" + child + "'])");
+        }
     }
     
     public void testEscapedStrings() throws IOException {



Mime
View raw message