1 package org.apache.maven.surefire.common.junit4;
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 import org.apache.maven.surefire.util.TestsToRun;
23 import org.apache.maven.surefire.util.internal.StringUtils;
24
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.junit.runner.Description;
33 import org.junit.runner.notification.Failure;
34
35 import static org.apache.maven.surefire.common.junit4.JUnit4RunListener.isFailureInsideJUnitItself;
36
37 /**
38 *
39 * Utility method used among all JUnit4 providers
40 *
41 * @author Qingzhou Luo
42 *
43 */
44 public class JUnit4ProviderUtil
45 {
46 /**
47 * Organize all the failures in previous run into a map between test classes and corresponding failing test methods
48 *
49 * @param allFailures all the failures in previous run
50 * @param testsToRun all the test classes
51 * @return a map between failing test classes and their corresponding failing test methods
52 */
53 public static Map<Class<?>, Set<String>> generateFailingTests( List<Failure> allFailures, TestsToRun testsToRun )
54 {
55 Map<Class<?>, Set<String>> testClassMethods = new HashMap<Class<?>, Set<String>>();
56
57 for ( Failure failure : allFailures )
58 {
59 if ( !isFailureInsideJUnitItself( failure ) )
60 {
61 // failure.getTestHeader() is in the format: method(class)
62 String[] testMethodClass = StringUtils.split( failure.getTestHeader(), "(" );
63 String testMethod = testMethodClass[0];
64 String testClass = StringUtils.split( testMethodClass[1], ")" )[0];
65 Class testClassObj = testsToRun.getClassByName( testClass );
66
67 if ( testClassObj == null )
68 {
69 continue;
70 }
71
72 Set<String> failingMethods = testClassMethods.get( testClassObj );
73 if ( failingMethods == null )
74 {
75 failingMethods = new HashSet<String>();
76 failingMethods.add( testMethod );
77 testClassMethods.put( testClassObj, failingMethods );
78 }
79 else
80 {
81 failingMethods.add( testMethod );
82 }
83 }
84 }
85 return testClassMethods;
86 }
87
88 /**
89 * Get the name of all test methods from a list of Failures
90 *
91 * @param allFailures the list of failures for a given test class
92 * @return the list of test method names
93 */
94 public static Set<String> generateFailingTests( List<Failure> allFailures )
95 {
96 Set<String> failingMethods = new HashSet<String>();
97
98 for ( Failure failure : allFailures )
99 {
100 if ( !isFailureInsideJUnitItself( failure ) )
101 {
102 // failure.getTestHeader() is in the format: method(class)
103 String testMethod = StringUtils.split( failure.getTestHeader(), "(" )[0];
104 failingMethods.add( testMethod );
105 }
106 }
107 return failingMethods;
108 }
109
110 public static Description createSuiteDescription( Collection<Class<?>> classes )
111 {
112 JUnit4Reflector reflector = new JUnit4Reflector();
113 return reflector.createRequest( classes.toArray( new Class[classes.size()] ) )
114 .getRunner()
115 .getDescription();
116 }
117
118 }