View Javadoc
1   package org.apache.maven.surefire.junitcore;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /*
23   * JBoss, Home of Professional Open Source
24   * Copyright 2009, Red Hat Middleware LLC, and individual contributors
25   * by the @authors tag. See the copyright.txt in the distribution for a
26   * full listing of individual contributors.
27   *
28   * Licensed under the Apache License, Version 2.0 (the "License");
29   * you may not use this file except in compliance with the License.
30   * You may obtain a copy of the License at
31   * http://www.apache.org/licenses/LICENSE-2.0
32   * Unless required by applicable law or agreed to in writing, software
33   * distributed under the License is distributed on an "AS IS" BASIS,
34   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35   * See the License for the specific language governing permissions and
36   * limitations under the License.
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   * {@code
72   * <dependency>
73   * <groupId>junit</groupId>
74   * <artifactId>junit</artifactId>
75   * <version>4.8.1</version>
76   * <scope>test</scope>
77   * </dependency>
78   * <br>
79   * <dependency>
80   * <groupId>org.apache.maven.surefire</groupId>
81   * <artifactId>surefire-booter</artifactId>
82   * <version>2.8.1</version>
83   * <scope>test</scope>
84   * </dependency>
85   * <dependency>
86   * <groupId>org.apache.maven.plugins</groupId>
87   * <artifactId>maven-surefire-plugin</artifactId>
88   * <version>2.8.1</version>
89   * <scope>test</scope>
90   * </dependency>
91   * <dependency>
92   * <groupId>org.apache.maven.surefire</groupId>
93   * <artifactId>surefire-junit47</artifactId>
94   * <version>2.8.1</version>
95   * <scope>test</scope>
96   * </dependency>
97   * }
98   *
99   * @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
100  * @version $Revision: $
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             // JUnitCoreWrapper#execute() is calling JUnit4RunListener#rethrowAnyTestMechanismFailures()
136             // and rethrows a failure which happened in listener
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 }