incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1072970 - in /sling/trunk/testing/junit/core: pom.xml src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
Date Mon, 21 Feb 2011 14:02:06 GMT
Author: bdelacretaz
Date: Mon Feb 21 14:02:06 2011
New Revision: 1072970

URL: http://svn.apache.org/viewvc?rev=1072970&view=rev
Log:
SLING-1963 - json renderer added

Added:
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
  (with props)
Modified:
    sling/trunk/testing/junit/core/pom.xml
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java

Modified: sling/trunk/testing/junit/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/pom.xml?rev=1072970&r1=1072969&r2=1072970&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/pom.xml (original)
+++ sling/trunk/testing/junit/core/pom.xml Mon Feb 21 14:02:06 2011
@@ -90,6 +90,11 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
+            <version>2.0.6</version>
+        </dependency>
+        <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
         </dependency>

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java?rev=1072970&r1=1072969&r2=1072970&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java
(original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java
Mon Feb 21 14:02:06 2011
@@ -94,6 +94,8 @@ public class JUnitServlet extends HttpSe
             return new PlainTextRenderer();
         } else if(".xml".equals(requestInfo.extension)) {
             return new XmlRenderer();
+        } else if(".json".equals(requestInfo.extension)) {
+            return new JsonRenderer();
         } else {
             return new HtmlRenderer();
         }

Added: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java?rev=1072970&view=auto
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
(added)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
Mon Feb 21 14:02:06 2011
@@ -0,0 +1,133 @@
+/*
+ * 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.sling.junit.impl.servlet;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.io.JSONWriter;
+import org.junit.runner.Description;
+import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class JsonRenderer extends Renderer {
+
+    public static final String INFO_TYPE_KEY = "INFO_TYPE";
+    public static final String INFO_SUBTYPE_KEY = "INFO_SUBTYPE";
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private JSONWriter writer;
+    private int counter;
+    
+    @Override
+    void setup(HttpServletResponse response, String pageTitle) throws IOException, UnsupportedEncodingException
{
+        response.setContentType("application/json");
+        response.setCharacterEncoding("UTF-8");
+        writer = new JSONWriter(response.getWriter());
+        writer.setTidy(true);
+        try {
+            writer.array();
+        } catch(JSONException jex) {
+            throw new IOException(jex);
+        }
+    }
+
+    @Override
+    void cleanup() {
+        if(writer != null) {
+            try {
+                writer.endArray();
+            } catch(JSONException jex) {
+                log.warn("JSONException in cleanup()", jex);
+            }
+        }
+        writer = null;
+    }
+
+    @Override
+    void info(String cssClass, String info) {
+        try {
+            startItem("info");
+            writer.key(INFO_SUBTYPE_KEY).value(cssClass);
+            writer.key("info").value(info);
+            endItem();
+        } catch(JSONException jex) {
+            log.warn("JSONException in info()", jex);
+        }
+    }
+
+    @Override
+    void list(String cssClass, List<String> data) {
+        try {
+            startItem("list");
+            writer.key(INFO_SUBTYPE_KEY).value(cssClass);
+            writer.key("data");
+            writer.array();
+            for(String str : data) {
+                writer.value(str);
+            }
+            writer.endArray();
+            endItem();
+        } catch(JSONException jex) {
+            log.warn("JSONException in list()", jex);
+        }
+    }
+
+    @Override
+    void title(int level, String title) {
+        // Titles are not needed
+    }
+    
+    @Override
+    public void testStarted(Description description) throws Exception {
+        super.testStarted(description);
+        startItem("test");
+        writer.key("description").value(description.toString());
+    }
+    
+    @Override
+    public void testFinished(Description description) throws Exception {
+        super.testFinished(description);
+        endItem();
+    }
+
+    
+    @Override
+    public void testFailure(Failure failure) throws Exception {
+        writer.key("failure").value(failure.toString());
+    }
+    
+    @Override
+    public void testRunFinished(Result result) throws Exception {
+        // Not needed, info is already present in the output
+    }
+    
+    void startItem(String name) throws JSONException {
+        ++counter;
+        writer.object();
+        writer.key(INFO_TYPE_KEY).value(name);
+    }
+    
+    void endItem() throws JSONException {
+        writer.endObject();
+    }
+}

Propchange: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message