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