You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2015/09/18 16:33:37 UTC

svn commit: r1703872 - in /sling/trunk/testing/junit: core/src/main/java/org/apache/sling/junit/impl/ core/src/main/java/org/apache/sling/junit/impl/servlet/ remote/src/main/java/org/apache/sling/junit/remote/testrunner/ remote/src/test/ remote/src/tes...

Author: kwin
Date: Fri Sep 18 14:33:36 2015
New Revision: 1703872

URL: http://svn.apache.org/viewvc?rev=1703872&view=rev
Log:
SLING-4322 expose stack trace for failures in integration tests

embed stacktrace in all responses from the JUnit servlet (plain text, Json, HTML), log a warning with stacktrace on the server, expose the stacktrace on the client when using the SlingRemoteTestRunner

Added:
    sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java
    sling/trunk/testing/junit/remote/src/test/
    sling/trunk/testing/junit/remote/src/test/java/
    sling/trunk/testing/junit/remote/src/test/java/org/
    sling/trunk/testing/junit/remote/src/test/java/org/apache/
    sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/
    sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/junit/
    sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/junit/remote/
    sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/junit/remote/testrunner/
    sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java
Modified:
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/HtmlRenderer.java
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/PlainTextRenderer.java
    sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java?rev=1703872&r1=1703871&r2=1703872&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java Fri Sep 18 14:33:36 2015
@@ -21,10 +21,13 @@ import org.junit.runner.Description;
 import org.junit.runner.Result;
 import org.junit.runner.notification.Failure;
 import org.junit.runner.notification.RunListener;
+import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
 
 public class TestContextRunListenerWrapper extends RunListener {
     private final RunListener wrapped;
     private long testStartTime;
+    private static final Logger log = LoggerFactory.getLogger(TestContextRunListenerWrapper.class);
     
     TestContextRunListenerWrapper(RunListener toWrap) {
         wrapped = toWrap;
@@ -37,6 +40,7 @@ public class TestContextRunListenerWrapp
 
     @Override
     public void testFailure(Failure failure) throws Exception {
+        log.warn("JUnit test execution failed: {}", failure.toString(), failure.getException());
         wrapped.testFailure(failure);
     }
 

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/HtmlRenderer.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/HtmlRenderer.java?rev=1703872&r1=1703871&r2=1703872&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/HtmlRenderer.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/HtmlRenderer.java Fri Sep 18 14:33:36 2015
@@ -143,8 +143,11 @@ public class HtmlRenderer extends RunLis
         output.print("TEST FAILED: ");
         HtmlFilter.escape(output, failure.getTestHeader());
         output.print("</h3><div class='failureDetails'>");
+        output.print("<div>");
         HtmlFilter.escape(output, failure.toString());
-        output.println("</div></div>");
+        output.print("</div><div>Stack Trace: ");
+        HtmlFilter.escape(output, failure.getTrace());
+        output.println("</div></div></div>");
     }
 
     @Override

Modified: 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=1703872&r1=1703871&r2=1703872&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java Fri Sep 18 14:33:36 2015
@@ -165,6 +165,7 @@ public class JsonRenderer extends RunLis
     @Override
     public void testFailure(Failure failure) throws Exception {
         writer.key("failure").value(failure.toString());
+        writer.key("trace").value(failure.getTrace());
     }
     
     @Override

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/PlainTextRenderer.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/PlainTextRenderer.java?rev=1703872&r1=1703871&r2=1703872&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/PlainTextRenderer.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/PlainTextRenderer.java Fri Sep 18 14:33:36 2015
@@ -109,6 +109,7 @@ public class PlainTextRenderer extends R
     public void testFailure(Failure failure) throws Exception {
         super.testFailure(failure);
         output.println("FAILURE " + failure);
+        output.println("Stack Trace: " + failure.getTrace());
     }
 
     @Override

Added: sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java?rev=1703872&view=auto
==============================================================================
--- sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java (added)
+++ sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java Fri Sep 18 14:33:36 2015
@@ -0,0 +1,157 @@
+/*
+ * 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.remote.testrunner;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.nio.CharBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class RemoteExecutionException extends RuntimeException {
+    private final String trace;
+
+    private final StackTraceElement[] stackTrace;
+
+    /**
+     * Matches on lines in the format {@code at package.class.method(source.java:123)} with 4 (1,2,3,4) or
+     * {@code at package.class.method(Native method)} with 3 different groups (1,2,5)
+     */
+    private static final Pattern TRACE_PATTERN = Pattern
+            .compile("\\s*at\\s+([\\w\\.$_]+)\\.([\\w$_]+)(?:\\((.*\\.java):(\\d+)\\)|(\\(.*\\)))");
+    /**
+     * Matches on lines in the format {@code Caused by: java.io.IOException: Some message} with 1 group containing the
+     * part after the first colon
+     */
+    private static final Pattern CAUSED_BY_PATTERN = Pattern.compile("\\s*Caused by:\\s+(.*)");
+
+    private static final int NATIVE_METHOD_LINE_NUMBER = -2;
+
+    public static RemoteExecutionException getExceptionFromTrace(String trace) throws IOException {
+        // first line of trace is something like "java.lang.RuntimeException: Wrapper exception"
+        BufferedReader reader = new BufferedReader(new StringReader(trace));
+        final String firstLine;
+        try {
+            firstLine = reader.readLine();
+        } finally {
+            reader.close();
+        }
+        return new RemoteExecutionException(firstLine, trace);
+    }
+
+    public RemoteExecutionException(String failure, String trace) throws NumberFormatException, IOException {
+        super(failure);
+        this.trace = trace;
+        this.stackTrace = getStackTraceFromString(trace);
+    }
+
+    @Override
+    public void printStackTrace(PrintStream s) {
+        if (trace != null) {
+            s.print(trace);
+        }
+    }
+    
+    @Override
+    public void printStackTrace(PrintWriter s) {
+        if (trace != null) {
+            s.print(trace);
+        }
+    }
+
+    @Override
+    public StackTraceElement[] getStackTrace() {
+        return stackTrace;
+    }
+
+    /**
+     * Returns all StackTraceElement created from the given String. Also evaluates the cause of an exception by setting {@link #initCause(Throwable)}.
+     * Example format for given trace:
+     * 
+     * <pre>
+     * {@code
+     *  java.lang.RuntimeException: Wrapper exception
+     *         at org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest.testGetNestedStackTraceFromString(RemoteExecutionExceptionTest.java:55)
+     *         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
+     *         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
+     *         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
+     *         at java.lang.reflect.Method.invoke(Method.java:606)
+     *         ...
+     * Caused by: java.lang.IllegalStateException: Some message
+     *         at org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest.testGetNestedStackTraceFromString(RemoteExecutionExceptionTest.java:53)
+     *         ... 23 more
+     * }
+     * </pre>
+     * 
+     * @param trace a serialized stack trace.
+     * @return an array of {@link StackTraceElement}s.
+     * @throws IOException
+     * @throws NumberFormatException
+     */
+    private final StackTraceElement[] getStackTraceFromString(String trace) throws NumberFormatException, IOException {
+        if (trace == null) {
+            return new StackTraceElement[0];
+        }
+
+        List<StackTraceElement> stackTraceElements = new ArrayList<StackTraceElement>();
+        BufferedReader reader = new BufferedReader(new StringReader(trace));
+        try {
+            String line = null;
+            while ((line = reader.readLine()) != null) {
+                Matcher traceMatcher = TRACE_PATTERN.matcher(line);
+                if (traceMatcher.find()) {
+                    String className = traceMatcher.group(1);
+                    String methodName = traceMatcher.group(2);
+                    String sourceFile;
+                    int lineNumber;
+                    if (traceMatcher.group(3) != null) {
+                        // java file with line number
+                        sourceFile = traceMatcher.group(3);
+                        if (traceMatcher.group(4) != null) {
+                            lineNumber = Integer.parseInt(traceMatcher.group(4));
+                        } else {
+                            lineNumber = -1;
+                        }
+                    } else {
+                        // probably a native method
+                        sourceFile = traceMatcher.group(5);
+                        lineNumber = NATIVE_METHOD_LINE_NUMBER;
+                    }
+                    // null checks
+                    stackTraceElements.add(new StackTraceElement(className, methodName, sourceFile, lineNumber));
+                }
+                // is this a caused by
+                Matcher causedByMatcher = CAUSED_BY_PATTERN.matcher(line);
+                if (causedByMatcher.find()) {
+                    // all remaining lines of the trace should be given to the wrapped exception
+                    char[] cbuf = new char[trace.length()];
+                    if (reader.read(cbuf) > 0) {
+                        this.initCause(new RemoteExecutionException(causedByMatcher.group(1), new String(cbuf)));
+                    }
+                }
+            }
+        } finally {
+            reader.close();
+        }
+        return stackTraceElements.toArray(new StackTraceElement[stackTraceElements.size()]);
+    }
+}

Modified: sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java?rev=1703872&r1=1703871&r2=1703872&view=diff
==============================================================================
--- sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java (original)
+++ sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java Fri Sep 18 14:33:36 2015
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.junit.remote.testrunner;
 
+import java.io.IOException;
+
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
 import org.junit.runner.Description;
@@ -25,14 +27,21 @@ class SlingRemoteTest {
     private final Class<?> testClass;
     private final String description;
     private final String failure;
+    private final String trace;
     
     public static final String DESCRIPTION = "description";
     public static final String FAILURE = "failure";
+    public static final String TRACE = "trace";
     
     SlingRemoteTest(Class<?> testClass, JSONObject json) throws JSONException {
         this.testClass = testClass;
         description = json.getString(DESCRIPTION);
         failure = json.has(FAILURE) ? json.getString(FAILURE) : null;
+        if (failure != null) {
+            trace = json.has(TRACE) ? json.getString(TRACE) : null;
+        } else {
+            trace = null;
+        }
     }
     
     Description describe() {
@@ -41,7 +50,14 @@ class SlingRemoteTest {
     
     void run() {
         if(failure != null && failure.trim().length() > 0) {
-            throw new AssertionError(failure);
+            try {
+                throw new RemoteExecutionException(failure, trace);
+            } catch (NumberFormatException e) {
+                // error reading stack
+            } catch (IOException e) {
+                // error reading stack
+            }
+            // TODO: distinguish between assumption failures and regular exceptions
         }
     }
 }

Added: sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java?rev=1703872&view=auto
==============================================================================
--- sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java (added)
+++ sling/trunk/testing/junit/remote/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java Fri Sep 18 14:33:36 2015
@@ -0,0 +1,81 @@
+/*
+ * 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.remote.testrunner;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Arrays;
+import java.util.List;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class RemoteExecutionExceptionTest {
+
+    @Test
+    public void testGetStackTraceFromString() throws NumberFormatException, IOException {
+        String trace = null;
+        try {
+            throw new IllegalStateException("Some message");
+        } catch(Exception e) {
+            StringWriter writer = new StringWriter();
+            e.printStackTrace(new PrintWriter(writer));
+            trace = writer.toString();
+        }
+        RemoteExecutionException e = RemoteExecutionException.getExceptionFromTrace(trace);
+        Assert.assertThat(e.getMessage(), Matchers.equalTo("java.lang.IllegalStateException: Some message"));
+        List<StackTraceElement> stackTraceElements = Arrays.asList(new RemoteExecutionException("some failure", trace).getStackTrace());
+        Assert.assertThat(stackTraceElements, Matchers.hasItem(new StackTraceElement("org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", "testGetStackTraceFromString", "RemoteExecutionExceptionTest.java", 36)));
+        // compare original stacktrace with newly generated one from the exception
+        StringWriter writer = new StringWriter();
+        e.printStackTrace(new PrintWriter(writer));
+        String newTrace = writer.toString();
+        Assert.assertEquals(trace, newTrace);
+    }
+
+    @Test
+    public void testGetStackTraceFromStringWithNestedException() throws NumberFormatException, IOException {
+        String trace = null;
+        try {
+            try {
+                throw new IllegalStateException("Some message");
+            } catch(Exception e) {
+                throw new RuntimeException("Wrapper exception", e);
+            }
+        } catch (Exception e) {
+            StringWriter writer = new StringWriter();
+            e.printStackTrace(new PrintWriter(writer));
+            trace = writer.toString();
+        }
+        
+        RemoteExecutionException e = RemoteExecutionException.getExceptionFromTrace(trace);
+        Assert.assertThat(e.getMessage(), Matchers.equalTo("java.lang.RuntimeException: Wrapper exception"));
+        List<StackTraceElement> stackTraceElements = Arrays.asList(e.getStackTrace());
+        Assert.assertThat(stackTraceElements, Matchers.hasItem(new StackTraceElement("org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", "testGetStackTraceFromStringWithNestedException", "RemoteExecutionExceptionTest.java", 60)));
+        // no original exception in the stack trace
+        Assert.assertThat(stackTraceElements, Matchers.not(Matchers.hasItem(new StackTraceElement("org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", "testGetStackTraceFromStringWithNestedException", "RemoteExecutionExceptionTest.java", 58))));
+        
+        // cause must be set
+        Assert.assertNotNull("Cause must be set on the exception", e.getCause());
+        Assert.assertThat(e.getCause().getMessage(), Matchers.equalTo("java.lang.IllegalStateException: Some message"));
+        stackTraceElements = Arrays.asList(e.getCause().getStackTrace());
+        Assert.assertThat(stackTraceElements, Matchers.hasItem(new StackTraceElement("org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", "testGetStackTraceFromStringWithNestedException", "RemoteExecutionExceptionTest.java", 58)));
+    }
+}