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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.concurrent.ConcurrentHashMap;
46
47 import org.apache.maven.surefire.api.booter.BaseProviderFactory;
48 import org.apache.maven.surefire.api.booter.ProviderParameterNames;
49 import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
50 import org.apache.maven.surefire.common.junit4.Notifier;
51 import org.apache.maven.surefire.api.report.DefaultDirectConsoleReporter;
52 import org.apache.maven.surefire.api.report.ReporterConfiguration;
53 import org.apache.maven.surefire.api.report.ReporterFactory;
54 import org.apache.maven.surefire.api.report.RunListener;
55 import org.apache.maven.surefire.api.suite.RunResult;
56 import org.apache.maven.surefire.api.testset.TestSetFailedException;
57 import org.apache.maven.surefire.api.util.TestsToRun;
58
59 import org.junit.Rule;
60 import org.junit.Test;
61 import org.junit.rules.ExpectedException;
62 import org.junit.runner.Description;
63 import org.junit.runner.RunWith;
64 import org.junit.runner.notification.RunNotifier;
65 import org.junit.runners.BlockJUnit4ClassRunner;
66 import org.junit.runners.model.InitializationError;
67
68 import static junit.framework.Assert.assertEquals;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public class Surefire746Test
103 {
104 @Rule
105 public final ExpectedException exception = ExpectedException.none();
106
107 @Test
108 @SuppressWarnings( "checkstyle:methodname" )
109 public void surefireIsConfused_ByMultipleIgnore_OnClassLevel() throws Exception
110 {
111 ReporterFactory reporterFactory = JUnitCoreTester.defaultNoXml();
112 BaseProviderFactory providerParameters = new BaseProviderFactory( true );
113 providerParameters.setReporterFactory( reporterFactory );
114
115 providerParameters.setReporterConfiguration( new ReporterConfiguration( new File( "" ), false ) );
116 Map<String, String> junitProps = new HashMap<>();
117 junitProps.put( ProviderParameterNames.PARALLEL_PROP, "none" );
118
119 JUnitCoreParameters jUnitCoreParameters = new JUnitCoreParameters( junitProps );
120
121 final Map<String, TestSet> testSetMap = new ConcurrentHashMap<>();
122
123 RunListener listener = ConcurrentRunListener.createInstance( testSetMap, reporterFactory, false, false,
124 new DefaultDirectConsoleReporter( System.out ) );
125
126 TestsToRun testsToRun = new TestsToRun( Collections.<Class<?>>singleton( TestClassTest.class ) );
127
128 org.junit.runner.notification.RunListener jUnit4RunListener = new JUnitCoreRunListener( listener, testSetMap );
129
130 List<org.junit.runner.notification.RunListener> customRunListeners = new ArrayList<>();
131 customRunListeners.add( 0, jUnit4RunListener );
132
133 try
134 {
135
136
137 exception.expect( TestSetFailedException.class );
138 JUnit4RunListener dummy = new JUnit4RunListener( new MockReporter() );
139 new JUnitCoreWrapper( new Notifier( dummy, 0 ), jUnitCoreParameters,
140 new DefaultDirectConsoleReporter( System.out ) ).execute( testsToRun, customRunListeners, null );
141 }
142 finally
143 {
144 RunResult result = reporterFactory.close();
145 assertEquals( "JUnit should report correctly number of test ran(Finished)", 1,
146 result.getCompletedCount() );
147 }
148 }
149
150
151
152
153 @RunWith( TestCaseRunner.class )
154 public static class TestClassTest
155 {
156 @Test
157 public void shouldNeverBeCalled() throws Exception
158 {
159 }
160 }
161
162
163
164
165 public static class TestCaseRunner extends BlockJUnit4ClassRunner
166 {
167 public TestCaseRunner( Class<?> klass ) throws InitializationError
168 {
169 super( klass );
170 }
171
172 @Override
173 public void run( RunNotifier notifier )
174 {
175 notifier.addListener( new TestRunListener() );
176 super.run( notifier );
177 }
178
179 }
180
181 private static class TestRunListener extends org.junit.runner.notification.RunListener
182 {
183 @Override
184 public void testFinished( Description description ) throws Exception
185 {
186 throw new RuntimeException(
187 "This Exception will cause Surefire to receive an internal JUnit Description and fail." );
188 }
189 }
190 }