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.lang.reflect.Field;
23  import java.util.List;
24  
25  import junit.framework.Assert;
26  import junit.framework.AssertionFailedError;
27  import junit.framework.ComparisonFailure;
28  import junit.framework.TestCase;
29  
30  @SuppressWarnings( "ThrowableResultOfMethodCallIgnored" )
31  public class SmartStackTraceParserTest
32      extends TestCase
33  {
34  
35      public void testGetString()
36          throws Exception
37      {
38          ATestClass aTestClass = new ATestClass();
39          try
40          {
41              aTestClass.failInAssert();
42          }
43          catch ( AssertionError e )
44          {
45              SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e );
46              String res = smartStackTraceParser.getString();
47              assertEquals( "ATestClass.failInAssert:30 X is not Z", res );
48  
49          }
50  
51      }
52  
53      public void testNestedFailure()
54          throws Exception
55      {
56          ATestClass aTestClass = new ATestClass();
57          try
58          {
59              aTestClass.nestedFailInAssert();
60          }
61          catch ( AssertionError e )
62          {
63              SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e );
64              String res = smartStackTraceParser.getString();
65              assertEquals( "ATestClass.nestedFailInAssert:35->failInAssert:30 X is not Z", res );
66          }
67      }
68  
69      public void testNestedNpe()
70          throws Exception
71      {
72          ATestClass aTestClass = new ATestClass();
73          try
74          {
75              aTestClass.nestedNpe();
76          }
77          catch ( NullPointerException e )
78          {
79              SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e );
80              String res = smartStackTraceParser.getString();
81              assertEquals( "ATestClass.nestedNpe:45->npe:40 NullPointer It was null", res );
82  
83          }
84      }
85  
86      public void testNestedNpeOutsideTest()
87          throws Exception
88      {
89          ATestClass aTestClass = new ATestClass();
90          try
91          {
92              aTestClass.nestedNpeOutsideTest();
93          }
94          catch ( NullPointerException e )
95          {
96              SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e );
97              String res = smartStackTraceParser.getString();
98              assertEquals( "ATestClass.nestedNpeOutsideTest:55->npeOutsideTest:50 » NullPointer", res );
99  
100         }
101     }
102 
103     public void testLongMessageTruncation()
104         throws Exception
105     {
106         ATestClass aTestClass = new ATestClass();
107         try
108         {
109             aTestClass.aLongTestErrorMessage();
110         }
111         catch ( RuntimeException e )
112         {
113             SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e );
114             String res = smartStackTraceParser.getString();
115             assertEquals( "ATestClass.aLongTestErrorMessage:60 Runtime This message will be truncated, so...", res );
116 
117         }
118     }
119 
120     public void testFailureInBaseClass()
121         throws Exception
122     {
123         ASubClass aTestClass = new ASubClass();
124         try
125         {
126             aTestClass.npe();
127         }
128         catch ( NullPointerException e )
129         {
130             SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ASubClass.class, e );
131             String res = smartStackTraceParser.getString();
132             assertEquals( "ASubClass>ABaseClass.npe:27 » NullPointer It was null", res );
133         }
134     }
135 
136     public void testClassThatWillFail()
137         throws Exception
138     {
139         CaseThatWillFail aTestClass = new CaseThatWillFail();
140         try
141         {
142             aTestClass.testThatWillFail();
143         }
144         catch ( ComparisonFailure e )
145         {
146             SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( CaseThatWillFail.class, e );
147             String res = smartStackTraceParser.getString();
148             assertEquals( "CaseThatWillFail.testThatWillFail:29 expected:<abc> but was:<def>", res );
149         }
150     }
151 
152     public Throwable getAThrownException()
153     {
154         try
155         {
156             TestClass1.InnerBTestClass.throwSomething();
157         }
158         catch ( Throwable t )
159         {
160             return t;
161         }
162         return null;
163     }
164 
165     public void testCollections()
166     {
167         Throwable aThrownException = getAThrownException();
168 
169         List<StackTraceElement> innerMost =
170             SmartStackTraceParser.focusInsideClass( aThrownException.getCause().getStackTrace(),
171                                                     TestClass1.InnerBTestClass.class.getName() );
172         assertEquals( 3, innerMost.size() );
173         StackTraceElement inner = innerMost.get( 0 );
174         assertEquals( TestClass2.InnerCTestClass.class.getName(), inner.getClassName() );
175         StackTraceElement outer = innerMost.get( 2 );
176         assertEquals( TestClass1.InnerBTestClass.class.getName(), outer.getClassName() );
177     }
178 
179     public void testAssertionWithNoMessage()
180     {
181         try
182         {
183             new AssertionNoMessage().testThrowSomething();
184         }
185         catch ( ComparisonFailure e )
186         {
187             SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( AssertionNoMessage.class, e );
188             String res = smartStackTraceParser.getString();
189             assertEquals( "AssertionNoMessage.testThrowSomething:29 expected:<abc> but was:<xyz>", res );
190         }
191     }
192 
193     public void testFailWithFail()
194     {
195         try
196         {
197             new FailWithFail().testThatWillFail();
198         }
199         catch ( AssertionFailedError e )
200         {
201             SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( FailWithFail.class, e );
202             String res = smartStackTraceParser.getString();
203             assertEquals( "FailWithFail.testThatWillFail:29 abc", res );
204         }
205     }
206 
207     public void testCollectorWithNested()
208     {
209         try
210         {
211             InnerATestClass.testFake();
212         }
213         catch ( Throwable t )
214         {
215             List<StackTraceElement> stackTraceElements =
216                 SmartStackTraceParser.focusInsideClass( t.getStackTrace(), InnerATestClass.class.getName() );
217             assertNotNull( stackTraceElements );
218             assertEquals( 5, stackTraceElements.size() );
219             StackTraceElement innerMost = stackTraceElements.get( 0 );
220             assertEquals( Assert.class.getName(), innerMost.getClassName() );
221             StackTraceElement outer = stackTraceElements.get( 4 );
222             assertEquals( InnerATestClass.class.getName(), outer.getClassName() );
223         }
224     }
225 
226     public void testNonClassNameStacktrace()
227     {
228         SmartStackTraceParser smartStackTraceParser =
229             new SmartStackTraceParser( "Not a class name", new Throwable( "my message" ) );
230         assertEquals( "my message", smartStackTraceParser.getString() );
231     }
232 
233     public void testNullElementInStackTrace()
234             throws Exception
235     {
236         ATestClass aTestClass = new ATestClass();
237         try
238         {
239             aTestClass.failInAssert();
240         }
241         catch ( AssertionError e )
242         {
243             SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e );
244             Field stackTrace = SmartStackTraceParser.class.getDeclaredField("stackTrace");
245             stackTrace.setAccessible(true);
246             stackTrace.set(smartStackTraceParser, new StackTraceElement[0]);
247             String res = smartStackTraceParser.getString();
248             assertEquals( "ATestClass X is not Z", res );
249         }
250 
251     }
252 
253 }