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