Author: enorman
Date: Sat Aug  7 21:15:37 2010
New Revision: 983310

URL: http://svn.apache.org/viewvc?rev=983310&view=rev
Log:
SLING-1632 The JsonQueryServlet should support the tidy selector to provided 
pretty printed results

Modified:
    
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java

Modified: 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java?rev=983310&r1=983309&r2=983310&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
 (original)
+++ 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
 Sat Aug  7 21:15:37 2010
@@ -82,11 +82,23 @@ public class JsonQueryServlet extends Sl
     /** rep:exerpt */
     private static final String REP_EXCERPT = "rep:excerpt()";
 
+    public static final String TIDY = "tidy";
+    
     private final JsonResourceWriter itemWriter;
 
     public JsonQueryServlet() {
         itemWriter = new JsonResourceWriter(null);
     }
+    
+    /** True if our request wants the "tidy" pretty-printed format */
+    protected boolean isTidy(SlingHttpServletRequest req) {
+        for(String selector : req.getRequestPathInfo().getSelectors()) {
+            if(TIDY.equals(selector)) {
+                return true;
+            }
+        }
+        return false;
+    }
 
     @Override
     protected void doGet(SlingHttpServletRequest req,
@@ -151,6 +163,8 @@ public class JsonQueryServlet extends Sl
             resp.setCharacterEncoding("UTF-8");
 
             final JSONWriter w = new JSONWriter(resp.getWriter());
+            w.setTidy(isTidy(req));
+            
             w.array();
 
             long count = -1;

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java?rev=983310&r1=983309&r2=983310&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
 Sat Aug  7 21:15:37 2010
@@ -23,6 +23,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.httpclient.NameValuePair;
+import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.testing.integration.HttpTestBase;
 
 
@@ -66,9 +67,13 @@ public class JsonQueryServletTest extend
             testClient.delete(WEBDAV_BASE_URL + testPath);
         }
     }
-    
     private void assertCount(int expectedCount, String statement, String 
queryType, int offset, int rows) 
     throws IOException {
+       assertCount(expectedCount, statement, queryType, offset, rows, false);
+    }    
+    private void assertCount(int expectedCount, String statement, String 
queryType, int offset, int rows,
+               boolean tidy) 
+    throws IOException {
         final List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new NameValuePair("statement", statement));
         if(queryType != null) {
@@ -80,7 +85,7 @@ public class JsonQueryServletTest extend
         if(rows > 0) {
             params.add(new NameValuePair("rows", String.valueOf(rows)));
         }
-        final String json = getContent(testFolderUrl + ".query.json", 
CONTENT_TYPE_JSON, params);
+        final String json = getContent(testFolderUrl + ".query" + (tidy ? 
".tidy" : "") + ".json", CONTENT_TYPE_JSON, params);
         assertJavascript(
                 expectedCount + ".0", 
                 json, 
@@ -148,4 +153,56 @@ public class JsonQueryServletTest extend
         
         
     }
+    
+    /**
+     * Test for SLING-1632: tidy rendering of query results
+     */
+    public void testTidyResultFormat() throws IOException, JSONException {
+       boolean tidy = true;
+       //query should function the same when the output is tidy'ed.
+        String statement = "/" + testPath + "/folderA/*";
+               String queryType = "xpath";
+               assertCount(5, statement, queryType, 0, 0, tidy);
+       
+        final List<NameValuePair> params = new ArrayList<NameValuePair>();
+        params.add(new NameValuePair("statement", statement));
+        params.add(new NameValuePair("queryType", queryType));
+        final String json = getContent(testFolderUrl + ".query.json", 
CONTENT_TYPE_JSON, params);
+        final String tidyJson = getContent(testFolderUrl + ".query.tidy.json", 
CONTENT_TYPE_JSON, params);
+        
+        //tidy json text should have whitespace that makes it not be 
equivalent to the untidy version
+        assertNotSame(json, tidyJson);
+
+        //compare expected with actual
+        String expectedTidyJson = 
+               "[{\n" +
+               "    \"name\": \"node0\",\n" +
+               "    \"jcr:score\": 1000,\n" +
+               "    \"jcr:primaryType\": \"nt:unstructured\",\n" +
+               "    \"jcr:path\": \"" + testPath + "/folderA/node0\"\n" +
+               "  },{\n" +
+               "    \"name\": \"node1\",\n" +
+               "    \"jcr:score\": 1000,\n" +
+               "    \"jcr:primaryType\": \"nt:unstructured\",\n" +
+               "    \"jcr:path\": \"" + testPath + "/folderA/node1\"\n" +
+               "  },{\n" +
+               "    \"name\": \"node2\",\n" +
+               "    \"jcr:score\": 1000,\n" +
+               "    \"jcr:primaryType\": \"nt:unstructured\",\n" +
+               "    \"jcr:path\": \"" + testPath + "/folderA/node2\"\n" +
+               "  },{\n" +
+               "    \"name\": \"node3\",\n" +
+               "    \"jcr:score\": 1000,\n" +
+               "    \"jcr:primaryType\": \"nt:unstructured\",\n" +
+               "    \"jcr:path\": \"" + testPath + "/folderA/node3\"\n" +
+               "  },{\n" +
+               "    \"name\": \"node4\",\n" +
+               "    \"jcr:score\": 1000,\n" +
+               "    \"jcr:primaryType\": \"nt:unstructured\",\n" +
+               "    \"jcr:path\": \"" + testPath + "/folderA/node4\"\n" +
+               "  }\n" +
+               "]";
+               assertEquals(expectedTidyJson.length(), tidyJson.length());
+        assertEquals(expectedTidyJson, tidyJson);
+    }
 }


Reply via email to