66import static org .junit .Assert .fail ;
77import static org .junit .Assume .assumeTrue ;
88
9- import java .lang .reflect .Method ;
109import java .util .concurrent .Callable ;
1110import java .util .concurrent .ExecutionException ;
1211import java .util .concurrent .ExecutorService ;
1312import java .util .concurrent .Executors ;
1413import java .util .concurrent .Future ;
1514import java .util .regex .Pattern ;
1615
17- import junit .framework . TestCase ;
16+ import junit .tests . SampleJUnit3Tests ;
1817import org .hamcrest .CoreMatchers ;
1918import org .hamcrest .Description ;
2019import org .hamcrest .Matcher ;
2120import org .hamcrest .StringDescription ;
2221import org .hamcrest .TypeSafeMatcher ;
2322import org .junit .AfterClass ;
24- import org .junit .Before ;
2523import org .junit .BeforeClass ;
26- import org .junit .ClassRule ;
27- import org .junit .Rule ;
2824import org .junit .Test ;
29- import org .junit .rules .MethodRule ;
30- import org .junit .rules .TestRule ;
3125import org .junit .runner .JUnitCore ;
3226import org .junit .runner .Result ;
3327import org .junit .runner .notification .Failure ;
34- import org .junit .runners .model .FrameworkMethod ;
35- import org .junit .runners .model .Statement ;
28+ import org .junit .tests .SampleJUnit4Tests .*;
3629
3730public class StackTracesTest {
3831 private static final String EOL = System .getProperty ("line.separator" , "\n " );
@@ -58,9 +51,9 @@ public void getTrimmedStackForJUnit4TestFailingInTestMethod() {
5851
5952 assertHasTrimmedTrace (failure ,
6053 message ("java.lang.RuntimeException: cause" ),
61- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
62- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
63- at ("org.junit.internal.StackTracesTest $TestWithOneThrowingTestMethod.alwaysThrows" ));
54+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
55+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
56+ at ("org.junit.tests.SampleJUnit4Tests $TestWithOneThrowingTestMethod.alwaysThrows" ));
6457 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
6558 }
6659
@@ -73,14 +66,14 @@ public void getTrimmedStackForJUnit4TestFailingInTestMethodWithCause() {
7366
7467 assertHasTrimmedTrace (failure ,
7568 message ("java.lang.RuntimeException: outer" ),
76- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithCause" ),
77- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithCause" ),
78- at ("org.junit.internal.StackTracesTest $TestWithOneThrowingTestMethodWithCause.alwaysThrows" ),
69+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithCause" ),
70+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithCause" ),
71+ at ("org.junit.tests.SampleJUnit4Tests $TestWithOneThrowingTestMethodWithCause.alwaysThrows" ),
7972 framesTrimmed (),
8073 message ("Caused by: java.lang.RuntimeException: cause" ),
81- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
82- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
83- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithCause" ),
74+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
75+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
76+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithCause" ),
8477 framesInCommon ());
8578 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
8679 }
@@ -94,39 +87,39 @@ public void getTrimmedStackForJUnit4TestFailingInBeforeMethod() {
9487
9588 assertHasTrimmedTrace (failure ,
9689 message ("java.lang.RuntimeException: cause" ),
97- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
98- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
99- at ("org.junit.internal.StackTracesTest $TestWithThrowingBeforeMethod.alwaysThrows" ));
90+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
91+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
92+ at ("org.junit.tests.SampleJUnit4Tests $TestWithThrowingBeforeMethod.alwaysThrows" ));
10093 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
10194 }
10295
10396 @ Test
10497 public void getTrimmedStackForJUnit3TestFailingInTestMethod () {
105- Result result = runTest (JUnit3TestWithOneThrowingTestMethod .class );
98+ Result result = runTest (SampleJUnit3Tests . TestWithOneThrowingTestMethod .class );
10699 assertEquals ("Should run the test" , 1 , result .getRunCount ());
107100 assertEquals ("One test should fail" , 1 , result .getFailureCount ());
108101 Failure failure = result .getFailures ().get (0 );
109102
110103 assertHasTrimmedTrace (failure ,
111104 message ("java.lang.RuntimeException: cause" ),
112- at ("org. junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
113- at ("org. junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
114- at ("org. junit.internal.StackTracesTest$JUnit3TestWithOneThrowingTestMethod .testAlwaysThrows" ));
105+ at ("junit.tests.SampleJUnit3Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
106+ at ("junit.tests.SampleJUnit3Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
107+ at ("junit.tests.SampleJUnit3Tests$TestWithOneThrowingTestMethod .testAlwaysThrows" ));
115108 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
116109 }
117110
118111 @ Test
119112 public void getTrimmedStackForJUnit3TestFailingInSetupMethod () {
120- Result result = runTest (JUnit3TestWithThrowingSetUpMethod .class );
113+ Result result = runTest (SampleJUnit3Tests . TestWithThrowingSetUpMethod .class );
121114 assertEquals ("Should run the test" , 1 , result .getRunCount ());
122115 assertEquals ("One test should fail" , 1 , result .getFailureCount ());
123116 Failure failure = result .getFailures ().get (0 );
124117
125118 assertHasTrimmedTrace (failure ,
126119 message ("java.lang.RuntimeException: cause" ),
127- at ("org. junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
128- at ("org. junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
129- at ("org. junit.internal.StackTracesTest$JUnit3TestWithThrowingSetUpMethod .setUp" ));
120+ at ("junit.tests.SampleJUnit3Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
121+ at ("junit.tests.SampleJUnit3Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
122+ at ("junit.tests.SampleJUnit3Tests$TestWithThrowingSetUpMethod .setUp" ));
130123 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
131124 }
132125
@@ -139,9 +132,9 @@ public void getTrimmedStackForJUnit4TestFailingInTestRule() {
139132
140133 assertHasTrimmedTrace (failure ,
141134 message ("java.lang.RuntimeException: cause" ),
142- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
143- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
144- at ("org.junit.internal.StackTracesTest $ThrowingTestRule.apply" ));
135+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
136+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
137+ at ("org.junit.tests.SampleJUnit4Tests $ThrowingTestRule.apply" ));
145138 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
146139 }
147140
@@ -154,9 +147,9 @@ public void getTrimmedStackForJUnit4TestFailingInClassRule() {
154147
155148 assertHasTrimmedTrace (failure ,
156149 message ("java.lang.RuntimeException: cause" ),
157- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
158- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
159- at ("org.junit.internal.StackTracesTest $ThrowingTestRule.apply" ));
150+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
151+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
152+ at ("org.junit.tests.SampleJUnit4Tests $ThrowingTestRule.apply" ));
160153 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
161154 }
162155
@@ -169,9 +162,9 @@ public void getTrimmedStackForJUnit4TestFailingInMethodRule() {
169162
170163 assertHasTrimmedTrace (failure ,
171164 message ("java.lang.RuntimeException: cause" ),
172- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
173- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
174- at ("org.junit.internal.StackTracesTest $ThrowingMethodRule.apply" ));
165+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
166+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
167+ at ("org.junit.tests.SampleJUnit4Tests $ThrowingMethodRule.apply" ));
175168 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
176169 }
177170
@@ -185,9 +178,9 @@ public void getTrimmedStackWithSuppressedExceptions() {
185178
186179 assertHasTrimmedTrace (failure ,
187180 message ("java.lang.RuntimeException: error" ),
188- at ("org.junit.internal.StackTracesTest $TestWithSuppressedException.alwaysThrows" ),
181+ at ("org.junit.tests.SampleJUnit4Tests $TestWithSuppressedException.alwaysThrows" ),
189182 message ("\t Suppressed: java.lang.RuntimeException: suppressed" ),
190- at ("org.junit.internal.StackTracesTest $TestWithSuppressedException.alwaysThrows" ),
183+ at ("org.junit.tests.SampleJUnit4Tests $TestWithSuppressedException.alwaysThrows" ),
191184 framesInCommon ());
192185 assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
193186 }
@@ -336,141 +329,4 @@ private static void assertHasTrimmedTrace(Failure failure, StringMatcher... matc
336329 fail ("Missing line in trimmed trace: " + description .toString ());
337330 }
338331 }
339-
340- public static class TestWithOneThrowingTestMethod {
341-
342- @ Test
343- public void alwaysThrows () {
344- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
345- }
346- }
347-
348- public static class JUnit3TestWithOneThrowingTestMethod extends TestCase {
349-
350- public void testAlwaysThrows () {
351- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
352- }
353- }
354-
355- public static class TestWithOneThrowingTestMethodWithCause {
356-
357- @ Test
358- public void alwaysThrows () {
359- new FakeClassUnderTest ().throwsExceptionWithCause ();
360- }
361- }
362-
363- public static class TestWithThrowingBeforeMethod {
364-
365- @ Before
366- public void alwaysThrows () {
367- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
368- }
369-
370- @ Test
371- public void alwaysPasses () {
372- }
373- }
374-
375- public static class JUnit3TestWithThrowingSetUpMethod extends TestCase {
376-
377- @ Override
378- protected void setUp () throws Exception {
379- super .setUp ();
380- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
381- }
382-
383- public void testAlwaysPasses () {
384- }
385- }
386-
387- public static class ThrowingTestRule implements TestRule {
388-
389- public Statement apply (
390- Statement base , org .junit .runner .Description description ) {
391- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
392- return base ;
393- }
394- }
395-
396- public static class TestWithThrowingTestRule {
397-
398- @ Rule
399- public final TestRule rule = new ThrowingTestRule ();
400-
401- @ Test
402- public void alwaysPasses () {
403- }
404- }
405-
406- public static class TestWithThrowingClassRule {
407-
408- @ ClassRule
409- public static final TestRule rule = new ThrowingTestRule ();
410-
411- @ Test
412- public void alwaysPasses () {
413- }
414- }
415-
416- public static class ThrowingMethodRule implements MethodRule {
417-
418- public Statement apply (
419- Statement base , FrameworkMethod method , Object target ) {
420- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
421- return base ;
422- }
423- }
424-
425- public static class TestWithThrowingMethodRule {
426-
427- @ Rule
428- public final ThrowingMethodRule rule = new ThrowingMethodRule ();
429-
430- @ Test
431- public void alwaysPasses () {
432- }
433- }
434-
435- private static class FakeClassUnderTest {
436-
437- public void throwsExceptionWithCause () {
438- doThrowExceptionWithCause ();
439- }
440-
441- public void throwsExceptionWithoutCause () {
442- doThrowExceptionWithoutCause ();
443- }
444-
445- private void doThrowExceptionWithCause () {
446- try {
447- throwsExceptionWithoutCause ();
448- } catch (Exception e ) {
449- throw new RuntimeException ("outer" , e );
450- }
451- }
452-
453- private void doThrowExceptionWithoutCause () {
454- throw new RuntimeException ("cause" );
455- }
456- }
457-
458- public static class TestWithSuppressedException {
459- static final Method addSuppressed = initAddSuppressed ();
460-
461- static Method initAddSuppressed () {
462- try {
463- return Throwable .class .getMethod ("addSuppressed" , Throwable .class );
464- } catch (Throwable e ) {
465- return null ;
466- }
467- }
468-
469- @ Test
470- public void alwaysThrows () throws Exception {
471- final RuntimeException exception = new RuntimeException ("error" );
472- addSuppressed .invoke (exception , new RuntimeException ("suppressed" ));
473- throw exception ;
474- }
475- }
476332}
0 commit comments