View Javadoc

1   package org.apache.maven.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.PrintWriter;
23  
24  import junit.framework.TestCase;
25  
26  public class PojoStackTraceWriterTest
27      extends TestCase
28  {
29  
30      public void testWriteTrimmedTraceToString()
31      {
32          String stackTrace = "junit.framework.AssertionFailedError: blah\n" + 
33          "    at junit.framework.Assert.fail(Assert.java:47)\n" + 
34          "    at TestSurefire3.testQuote(TestSurefire3.java:23)\n" + 
35          "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" + 
36          "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" + 
37          "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" + 
38          "    at java.lang.reflect.Method.invoke(Method.java:585)\n" + 
39          "    at junit.framework.TestCase.runTest(TestCase.java:154)\n" + 
40          "    at junit.framework.TestCase.runBare(TestCase.java:127)\n" + 
41          "    at junit.framework.TestResult$1.protect(TestResult.java:106)\n" + 
42          "    at junit.framework.TestResult.runProtected(TestResult.java:124)\n" + 
43          "    at junit.framework.TestResult.run(TestResult.java:109)\n" + 
44          "    at junit.framework.TestCase.run(TestCase.java:118)\n" + 
45          "    at junit.framework.TestSuite.runTest(TestSuite.java:208)\n" + 
46          "    at junit.framework.TestSuite.run(TestSuite.java:203)\n" + 
47          "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" + 
48          "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" + 
49          "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" + 
50          "    at java.lang.reflect.Method.invoke(Method.java:585)\n" + 
51          "    at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)\n" + 
52          "    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)\n" + 
53          "    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)\n" + 
54          "    at org.apache.maven.surefire.Surefire.run(Surefire.java:132)\n" + 
55          "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" + 
56          "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" + 
57          "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" + 
58          "    at java.lang.reflect.Method.invoke(Method.java:585)\n" + 
59          "    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:318)\n" + 
60          "    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:956)\n";
61          MockThrowable t = new MockThrowable(stackTrace);
62          PojoStackTraceWriter w = new PojoStackTraceWriter("TestSurefire3", "testQuote", t);
63          String out = w.writeTrimmedTraceToString();
64          String expected = "junit.framework.AssertionFailedError: blah\n" + 
65          		"    at junit.framework.Assert.fail(Assert.java:47)\n" + 
66          		"    at TestSurefire3.testQuote(TestSurefire3.java:23)\n";
67          assertEquals( expected, out );
68      }
69  
70      public void testCausedBy() {
71          String stackTrace = "java.lang.RuntimeException: blah\n" + 
72          "    at TestSurefire3.testBlah(TestSurefire3.java:45)\n" + 
73          "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" + 
74          "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" + 
75          "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" + 
76          "    at java.lang.reflect.Method.invoke(Method.java:585)\n" + 
77          "    at junit.framework.TestCase.runTest(TestCase.java:154)\n" + 
78          "    at junit.framework.TestCase.runBare(TestCase.java:127)\n" + 
79          "    at junit.framework.TestResult$1.protect(TestResult.java:106)\n" + 
80          "    at junit.framework.TestResult.runProtected(TestResult.java:124)\n" + 
81          "    at junit.framework.TestResult.run(TestResult.java:109)\n" + 
82          "    at junit.framework.TestCase.run(TestCase.java:118)\n" + 
83          "    at junit.framework.TestSuite.runTest(TestSuite.java:208)\n" + 
84          "    at junit.framework.TestSuite.run(TestSuite.java:203)\n" + 
85          "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" + 
86          "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" + 
87          "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" + 
88          "    at java.lang.reflect.Method.invoke(Method.java:585)\n" + 
89          "    at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)\n" + 
90          "    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)\n" + 
91          "    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)\n" + 
92          "    at org.apache.maven.surefire.Surefire.run(Surefire.java:132)\n" + 
93          "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" + 
94          "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" + 
95          "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" + 
96          "    at java.lang.reflect.Method.invoke(Method.java:585)\n" + 
97          "    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:318)\n" + 
98          "    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:956)\n" + 
99          "Caused by: junit.framework.AssertionFailedError: \"\n" + 
100         "    at junit.framework.Assert.fail(Assert.java:47)\n" + 
101         "    at TestSurefire3.testQuote(TestSurefire3.java:23)\n" + 
102         "    at TestSurefire3.testBlah(TestSurefire3.java:43)\n" + 
103         "    at TestSurefire3.testBlah(TestSurefire3.java:43)\n" + 
104         "    ... 26 more\n";
105         MockThrowable t = new MockThrowable(stackTrace);
106         PojoStackTraceWriter w = new PojoStackTraceWriter("TestSurefire3", "testBlah", t);
107         String out = w.writeTrimmedTraceToString();
108         String expected = "java.lang.RuntimeException: blah\n" + 
109         "    at TestSurefire3.testBlah(TestSurefire3.java:45)\n" + 
110         "Caused by: junit.framework.AssertionFailedError: \"\n" + 
111         "    at junit.framework.Assert.fail(Assert.java:47)\n" + 
112         "    at TestSurefire3.testQuote(TestSurefire3.java:23)\n" + 
113         "    at TestSurefire3.testBlah(TestSurefire3.java:43)\n" + 
114         "    at TestSurefire3.testBlah(TestSurefire3.java:43)\n" + 
115         "    ... 26 more\n";
116         assertEquals( expected, out );
117     }
118     
119     class MockThrowable extends Throwable {
120         private static final long serialVersionUID = 1L;
121         private String stackTrace;
122         
123         public MockThrowable(String stackTrace) {
124             this.stackTrace = stackTrace;
125         }
126         
127         public void printStackTrace( PrintWriter s )
128         {
129             s.write( stackTrace );
130         }
131     }
132 
133 }