View Javadoc
1   package org.apache.maven.surefire.junitcore;
2   
3   /*
4    * JBoss, Home of Professional Open Source
5    * Copyright 2009, Red Hat Middleware LLC, and individual contributors
6    * by the @authors tag. See the copyright.txt in the distribution for a
7    * full listing of individual contributors.
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import java.io.File;
21  import java.util.*;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
25  import org.apache.maven.surefire.booter.BaseProviderFactory;
26  import org.apache.maven.surefire.booter.ProviderParameterNames;
27  import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
28  import org.apache.maven.surefire.common.junit4.Notifier;
29  import org.apache.maven.surefire.junit4.MockReporter;
30  import org.apache.maven.surefire.report.DefaultDirectConsoleReporter;
31  import org.apache.maven.surefire.report.ReporterConfiguration;
32  import org.apache.maven.surefire.report.ReporterFactory;
33  import org.apache.maven.surefire.report.RunListener;
34  import org.apache.maven.surefire.suite.RunResult;
35  import org.apache.maven.surefire.testset.TestSetFailedException;
36  import org.apache.maven.surefire.util.TestsToRun;
37  
38  import org.junit.Rule;
39  import org.junit.Test;
40  import org.junit.rules.ExpectedException;
41  import org.junit.runner.Description;
42  import org.junit.runner.RunWith;
43  import org.junit.runner.notification.RunNotifier;
44  import org.junit.runners.BlockJUnit4ClassRunner;
45  import org.junit.runners.model.InitializationError;
46  
47  import static junit.framework.Assert.assertEquals;
48  
49  /**
50   * {@code
51   * <dependency>
52   * <groupId>junit</groupId>
53   * <artifactId>junit</artifactId>
54   * <version>4.8.1</version>
55   * <scope>test</scope>
56   * </dependency>
57   * <br>
58   * <dependency>
59   * <groupId>org.apache.maven.surefire</groupId>
60   * <artifactId>surefire-booter</artifactId>
61   * <version>2.8.1</version>
62   * <scope>test</scope>
63   * </dependency>
64   * <dependency>
65   * <groupId>org.apache.maven.plugins</groupId>
66   * <artifactId>maven-surefire-plugin</artifactId>
67   * <version>2.8.1</version>
68   * <scope>test</scope>
69   * </dependency>
70   * <dependency>
71   * <groupId>org.apache.maven.surefire</groupId>
72   * <artifactId>surefire-junit47</artifactId>
73   * <version>2.8.1</version>
74   * <scope>test</scope>
75   * </dependency>
76   * }
77   *
78   * @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
79   * @version $Revision: $
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  
93          providerParameters.setReporterConfiguration( new ReporterConfiguration( new File( "" ), false ) );
94          Map<String, String> junitProps = new HashMap<String, String>();
95          junitProps.put( ProviderParameterNames.PARALLEL_PROP, "none" );
96  
97          JUnitCoreParameters jUnitCoreParameters = new JUnitCoreParameters( junitProps );
98  
99          final Map<String, TestSet> testSetMap = new ConcurrentHashMap<String, TestSet>();
100 
101         RunListener listener =
102             ConcurrentRunListener.createInstance( testSetMap, reporterFactory, false, false,
103                                                         new DefaultDirectConsoleReporter( System.out ) );
104 
105         TestsToRun testsToRun = new TestsToRun( Collections.<Class<?>>singleton( 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             // JUnitCoreWrapper#execute() is calling JUnit4RunListener#rethrowAnyTestMechanismFailures()
116             // and rethrows a failure which happened in listener
117             exception.expect( TestSetFailedException.class );
118             JUnit4RunListener dummy = new JUnit4RunListener( new MockReporter() );
119             new JUnitCoreWrapper( new Notifier( dummy, 0 ), jUnitCoreParameters,
120                                         new DefaultDirectConsoleReporter( System.out ) )
121                 .execute( testsToRun, customRunListeners, null );
122         }
123         finally
124         {
125             RunResult result = reporterFactory.close();
126             assertEquals( "JUnit should report correctly number of test ran(Finished)", 1, result.getCompletedCount() );
127         }
128     }
129 
130     @RunWith( TestCaseRunner.class )
131     public static class TestClassTest
132     {
133         @Test
134         public void shouldNeverBeCalled()
135             throws Exception
136         {
137         }
138     }
139 
140     public static class TestCaseRunner
141         extends BlockJUnit4ClassRunner
142     {
143         public TestCaseRunner( Class<?> klass )
144             throws InitializationError
145         {
146             super( klass );
147         }
148 
149         @Override
150         public void run( RunNotifier notifier )
151         {
152             notifier.addListener( new TestRunListener() );
153             super.run( notifier );
154         }
155 
156     }
157 
158     private static class TestRunListener
159         extends org.junit.runner.notification.RunListener
160     {
161         @Override
162         public void testFinished( Description description )
163             throws Exception
164         {
165             throw new RuntimeException(
166                 "This Exception will cause Surefire to receive an internal JUnit Description and fail." );
167         }
168     }
169 }