1 package org.apache.maven.surefire.junitcore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
23 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
24 import org.apache.maven.surefire.report.RunListener;
25 import org.apache.maven.surefire.report.SimpleReportEntry;
26 import org.apache.maven.surefire.testset.TestSetFailedException;
27
28 import org.junit.runner.Description;
29 import org.junit.runner.Result;
30 import org.junit.runner.notification.Failure;
31
32
33
34
35
36
37 public class NonConcurrentRunListener
38 extends JUnit4RunListener
39 implements ConsoleOutputReceiver
40 {
41
42 private java.lang.Class<?> currentTestClass;
43
44 private Description lastFinishedDescription;
45
46 public NonConcurrentRunListener( RunListener reporter )
47 throws TestSetFailedException
48 {
49 super( reporter );
50 }
51
52 public synchronized void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
53 {
54
55 ( (ConsoleOutputReceiver) reporter ).writeTestOutput( buf, off, len, stdout );
56 }
57
58 protected SimpleReportEntry createReportEntry( Description description )
59 {
60 return new SimpleReportEntry( description.getClassName(), description.getDisplayName()
61 );
62 }
63
64 protected SimpleReportEntry createReportEntryForTestSet( Description description )
65 {
66 return new SimpleReportEntry( description.getClassName(), description.getClassName()
67 );
68 }
69
70 @Override
71 public void testStarted( Description description )
72 throws Exception
73 {
74 finishLastTestSetIfNeccessary( description );
75 super.testStarted( description );
76 }
77
78 private void finishLastTestSetIfNeccessary( Description description )
79 {
80 if ( !description.getTestClass().equals( currentTestClass ) )
81 {
82 currentTestClass = description.getTestClass();
83 if ( lastFinishedDescription != null )
84 {
85 reporter.testSetCompleted( createReportEntryForTestSet( lastFinishedDescription ) );
86 lastFinishedDescription = null;
87 }
88 reporter.testSetStarting( createReportEntryForTestSet( description ) );
89 }
90 }
91
92 @Override
93 public void testFinished( Description description )
94 throws Exception
95 {
96 super.testFinished( description );
97 this.lastFinishedDescription = description;
98 }
99
100 @Override
101 public void testIgnored( Description description )
102 throws Exception
103 {
104 finishLastTestSetIfNeccessary( description );
105
106 super.testIgnored( description );
107 this.lastFinishedDescription = description;
108 }
109
110 @Override
111 public void testFailure( Failure failure )
112 throws Exception
113 {
114 super.testFailure( failure );
115 this.lastFinishedDescription = failure.getDescription();
116 }
117
118 @Override
119 public void testAssumptionFailure( Failure failure )
120 {
121 super.testAssumptionFailure( failure );
122 this.lastFinishedDescription = failure.getDescription();
123 }
124
125 @Override
126 public void testRunStarted( Description description )
127 throws Exception
128 {
129 }
130
131 @Override
132 public void testRunFinished( Result result )
133 throws Exception
134 {
135 if ( lastFinishedDescription != null )
136 {
137 reporter.testSetCompleted( createReportEntryForTestSet( lastFinishedDescription ) );
138 lastFinishedDescription = null;
139 }
140 }
141 }