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.junit.runner.Description;
23 import org.junit.runner.manipulation.Filter;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29 * Only run test methods in the given failure set.
30 *
31 * @author mpkorstanje
32 */
33 public final class MatchDescriptions
34 extends Filter
35 {
36 private final List<Filter> filters = new ArrayList<Filter>();
37
38 public MatchDescriptions( Iterable<Description> descriptions )
39 {
40 for ( Description description : descriptions )
41 {
42 filters.add( matchDescription( description ) );
43 }
44 }
45
46 @Override
47 public boolean shouldRun( Description description )
48 {
49 for ( Filter filter : filters )
50 {
51 if ( filter.shouldRun( description ) )
52 {
53 return true;
54 }
55 }
56 return false;
57 }
58
59 @Override
60 public String describe()
61 {
62 StringBuilder description = new StringBuilder( "Matching description " );
63 for ( int i = 0; i < filters.size(); i++ )
64 {
65 description.append( filters.get( i ).describe() );
66 if ( i != filters.size() - 1 )
67 {
68 description.append( " OR " );
69 }
70 }
71 return description.toString();
72 }
73
74 private static Filter matchDescription( final Description desiredDescription )
75 {
76 return new Filter()
77 {
78 @Override
79 public boolean shouldRun( Description description )
80 {
81 if ( description.isTest() )
82 {
83 return desiredDescription.equals( description );
84 }
85
86 for ( Description each : description.getChildren() )
87 {
88 if ( shouldRun( each ) )
89 {
90 return true;
91 }
92 }
93
94 return false;
95 }
96
97 @Override
98 public String describe()
99 {
100 return String.format( "Method %s", desiredDescription.getDisplayName() );
101 }
102 };
103 }
104 }