1 package org.apache.maven.surefire.common.junit4;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.concurrent.CountDownLatch;
23
24 import junit.framework.TestCase;
25 import org.apache.maven.surefire.junit4.MockReporter;
26
27 import junit.framework.Assert;
28 import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
29 import org.junit.Test;
30 import org.junit.runner.Description;
31 import org.junit.runner.Request;
32 import org.junit.runner.Runner;
33 import org.junit.runner.notification.Failure;
34 import org.junit.runner.notification.RunListener;
35 import org.junit.runner.notification.RunNotifier;
36
37
38
39
40 public class JUnit4RunListenerTest
41 extends TestCase
42 {
43 public void testTestStarted()
44 throws Exception
45 {
46 RunListener jUnit4TestSetReporter = new JUnit4RunListener( new MockReporter() );
47 Runner junitTestRunner = Request.classes( "abc", STest1.class, STest2.class ).getRunner();
48 RunNotifier runNotifier = new RunNotifier();
49 runNotifier.addListener( jUnit4TestSetReporter );
50 junitTestRunner.run( runNotifier );
51 }
52
53 public void testParallelInvocations()
54 throws Exception
55 {
56 final MockReporter reporter = new MockReporter();
57 final RunListener jUnit4TestSetReporter = new JUnit4RunListener( reporter );
58 final CountDownLatch countDownLatch = new CountDownLatch( 1 );
59 final Description testSomething = Description.createTestDescription( STest1.class, "testSomething" );
60 final Description testSomething2 = Description.createTestDescription( STest2.class, "testSomething2" );
61
62 jUnit4TestSetReporter.testStarted( testSomething );
63
64 DaemonThreadFactory.newDaemonThread( new Runnable()
65 {
66 @Override
67 public void run()
68 {
69 try
70 {
71 jUnit4TestSetReporter.testStarted( testSomething2 );
72 jUnit4TestSetReporter.testFailure( new Failure( testSomething2, new AssertionError( "Fud" ) ) );
73 jUnit4TestSetReporter.testFinished( testSomething2 );
74 countDownLatch.countDown();
75 }
76 catch ( Exception e )
77 {
78 throw new RuntimeException( e );
79 }
80 }
81 } ).start();
82
83 countDownLatch.await();
84 jUnit4TestSetReporter.testFinished( testSomething );
85
86 Assert.assertEquals( "Failing tests", 1, reporter.getTestFailed() );
87 Assert.assertEquals( "Succeeded tests", 1, reporter.getTestSucceeded() );
88 }
89
90
91 public static class STest1
92 {
93 @Test
94 public void testSomething()
95 {
96
97 }
98 }
99
100 public static class STest2
101 {
102 @Test
103 public void testSomething2()
104 {
105
106 }
107 }
108
109 }