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  /**
27   * @author Kristian Rosenvold
28   */
29  public class LegacyPojoStackTraceWriterTest
30      extends TestCase
31  {
32  
33      public void testWriteTrimmedTraceToString()
34      {
35          String stackTrace = "junit.framework.AssertionFailedError: blah\n" +
36              "    at junit.framework.Assert.fail(Assert.java:47)\n" +
37              "    at TestSurefire3.testQuote(TestSurefire3.java:23)\n" +
38              "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
39              "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" +
40              "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" +
41              "    at java.lang.reflect.Method.invoke(Method.java:585)\n" +
42              "    at junit.framework.TestCase.runTest(TestCase.java:154)\n" +
43              "    at junit.framework.TestCase.runBare(TestCase.java:127)\n" +
44              "    at junit.framework.TestResult$1.protect(TestResult.java:106)\n" +
45              "    at junit.framework.TestResult.runProtected(TestResult.java:124)\n" +
46              "    at junit.framework.TestResult.run(TestResult.java:109)\n" +
47              "    at junit.framework.TestCase.run(TestCase.java:118)\n" +
48              "    at junit.framework.TestSuite.runTest(TestSuite.java:208)\n" +
49              "    at junit.framework.TestSuite.run(TestSuite.java:203)\n" +
50              "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
51              "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" +
52              "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" +
53              "    at java.lang.reflect.Method.invoke(Method.java:585)\n" +
54              "    at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)\n" +
55              "    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)\n"
56              +
57              "    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)\n"
58              +
59              "    at org.apache.maven.surefire.Surefire.run(Surefire.java:132)\n" +
60              "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
61              "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" +
62              "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" +
63              "    at java.lang.reflect.Method.invoke(Method.java:585)\n" +
64              "    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:318)\n" +
65              "    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:956)\n";
66          MockThrowable t = new MockThrowable( stackTrace );
67          LegacyPojoStackTraceWriter w = new LegacyPojoStackTraceWriter( "TestSurefire3", "testQuote", t );
68          String out = w.writeTrimmedTraceToString();
69          String expected = "junit.framework.AssertionFailedError: blah\n" +
70              "    at junit.framework.Assert.fail(Assert.java:47)\n" +
71              "    at TestSurefire3.testQuote(TestSurefire3.java:23)\n";
72          assertEquals( expected, out );
73      }
74  
75      public void testCausedBy()
76      {
77          String stackTrace = "java.lang.RuntimeException: blah\n" +
78              "    at TestSurefire3.testBlah(TestSurefire3.java:45)\n" +
79              "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
80              "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" +
81              "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" +
82              "    at java.lang.reflect.Method.invoke(Method.java:585)\n" +
83              "    at junit.framework.TestCase.runTest(TestCase.java:154)\n" +
84              "    at junit.framework.TestCase.runBare(TestCase.java:127)\n" +
85              "    at junit.framework.TestResult$1.protect(TestResult.java:106)\n" +
86              "    at junit.framework.TestResult.runProtected(TestResult.java:124)\n" +
87              "    at junit.framework.TestResult.run(TestResult.java:109)\n" +
88              "    at junit.framework.TestCase.run(TestCase.java:118)\n" +
89              "    at junit.framework.TestSuite.runTest(TestSuite.java:208)\n" +
90              "    at junit.framework.TestSuite.run(TestSuite.java:203)\n" +
91              "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
92              "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" +
93              "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" +
94              "    at java.lang.reflect.Method.invoke(Method.java:585)\n" +
95              "    at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)\n" +
96              "    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)\n"
97              +
98              "    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)\n"
99              +
100             "    at org.apache.maven.surefire.Surefire.run(Surefire.java:132)\n" +
101             "    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
102             "    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n" +
103             "    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n" +
104             "    at java.lang.reflect.Method.invoke(Method.java:585)\n" +
105             "    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:318)\n" +
106             "    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:956)\n" +
107             "Caused by: junit.framework.AssertionFailedError: \"\n" +
108             "    at junit.framework.Assert.fail(Assert.java:47)\n" +
109             "    at TestSurefire3.testQuote(TestSurefire3.java:23)\n" +
110             "    at TestSurefire3.testBlah(TestSurefire3.java:43)\n" +
111             "    at TestSurefire3.testBlah(TestSurefire3.java:43)\n" +
112             "    ... 26 more\n";
113         MockThrowable t = new MockThrowable( stackTrace );
114         LegacyPojoStackTraceWriter w = new LegacyPojoStackTraceWriter( "TestSurefire3", "testBlah", t );
115         String out = w.writeTrimmedTraceToString();
116         String expected = "java.lang.RuntimeException: blah\n" +
117             "    at TestSurefire3.testBlah(TestSurefire3.java:45)\n" +
118             "Caused by: junit.framework.AssertionFailedError: \"\n" +
119             "    at junit.framework.Assert.fail(Assert.java:47)\n" +
120             "    at TestSurefire3.testQuote(TestSurefire3.java:23)\n" +
121             "    at TestSurefire3.testBlah(TestSurefire3.java:43)\n" +
122             "    at TestSurefire3.testBlah(TestSurefire3.java:43)\n" +
123             "    ... 26 more\n";
124         assertEquals( expected, out );
125     }
126 
127     class MockThrowable
128         extends Throwable
129     {
130         private static final long serialVersionUID = 1L;
131 
132         private final String stackTrace;
133 
134         public MockThrowable( String stackTrace )
135         {
136             this.stackTrace = stackTrace;
137         }
138 
139         public void printStackTrace( PrintWriter s )
140         {
141             s.write( stackTrace );
142         }
143     }
144 
145 }