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