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

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1703872 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java b/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java
new file mode 100644
index 0000000..997d126
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java
@@ -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()]);
+    }
+}
diff --git a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java
index 5171474..99b194f 100644
--- a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java
+++ b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java
@@ -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 @@
     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 @@
     
     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
         }
     }
 }
diff --git a/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java b/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java
new file mode 100644
index 0000000..e976f39
--- /dev/null
+++ b/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java
@@ -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)));
+    }
+}