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.junit48;
20  
21  import java.util.Collection;
22  
23  import org.junit.runner.Description;
24  import org.junit.runner.manipulation.Filter;
25  
26  final class CombinedCategoryFilter extends Filter {
27      private final Collection<Filter> includedFilters;
28  
29      private final Collection<Filter> excludedFilters;
30  
31      CombinedCategoryFilter(Collection<Filter> includedFilters, Collection<Filter> excludedFilters) {
32          this.includedFilters = includedFilters;
33          this.excludedFilters = excludedFilters;
34      }
35  
36      @Override
37      public boolean shouldRun(Description description) {
38          return (includedFilters.isEmpty() || anyFilterMatchesDescription(includedFilters, description))
39                  && (excludedFilters.isEmpty() || allFiltersMatchDescription(excludedFilters, description));
40      }
41  
42      private boolean anyFilterMatchesDescription(Collection<Filter> filters, Description description) {
43          for (Filter f : filters) {
44              if (f.shouldRun(description)) {
45                  return true;
46              }
47          }
48          return false;
49      }
50  
51      private boolean allFiltersMatchDescription(Collection<Filter> filters, Description description) {
52          for (Filter f : filters) {
53              if (!f.shouldRun(description)) {
54                  return false;
55              }
56          }
57          return true;
58      }
59  
60      @Override
61      public String describe() {
62          StringBuilder sb = new StringBuilder();
63          if (!includedFilters.isEmpty()) {
64              sb.append("(");
65              sb.append(joinFilters(includedFilters, " OR "));
66              sb.append(")");
67              if (!excludedFilters.isEmpty()) {
68                  sb.append(" AND ");
69              }
70          }
71  
72          if (!excludedFilters.isEmpty()) {
73              sb.append("NOT (");
74              sb.append(joinFilters(includedFilters, " OR "));
75              sb.append(")");
76          }
77  
78          return sb.toString();
79      }
80  
81      private String joinFilters(Collection<Filter> filters, String sep) {
82          boolean isFirst = true;
83          StringBuilder sb = new StringBuilder();
84          for (Filter f : filters) {
85              if (!isFirst) {
86                  sb.append(sep);
87              }
88              sb.append(f.describe());
89              isFirst = false;
90          }
91          return sb.toString();
92      }
93  }