View Javadoc
1   package org.apache.maven.surefire.common.junit48;
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.Collection;
26  
27  final class CombinedCategoryFilter
28      extends Filter
29  {
30      private final Collection<Filter> includedFilters;
31  
32      private final Collection<Filter> excludedFilters;
33  
34      CombinedCategoryFilter( Collection<Filter> includedFilters, Collection<Filter> excludedFilters )
35      {
36          this.includedFilters = includedFilters;
37          this.excludedFilters = excludedFilters;
38      }
39  
40      @Override
41      public boolean shouldRun( Description description )
42      {
43          return ( includedFilters.isEmpty() || anyFilterMatchesDescription( includedFilters, description ) )
44              && ( excludedFilters.isEmpty() || allFiltersMatchDescription( excludedFilters, description ) );
45      }
46  
47      private boolean anyFilterMatchesDescription( Collection<Filter> filters, Description description )
48      {
49          for ( Filter f : filters )
50          {
51              if ( f.shouldRun( description ) )
52              {
53                  return true;
54              }
55          }
56          return false;
57      }
58  
59      private boolean allFiltersMatchDescription( Collection<Filter> filters, Description description )
60      {
61          for ( Filter f : filters )
62          {
63              if ( !f.shouldRun( description ) )
64              {
65                  return false;
66              }
67          }
68          return true;
69      }
70  
71      @Override
72      public String describe()
73      {
74          StringBuilder sb = new StringBuilder();
75          if ( !includedFilters.isEmpty() )
76          {
77              sb.append( "(" );
78              sb.append( joinFilters( includedFilters, " OR " ) );
79              sb.append( ")" );
80              if ( !excludedFilters.isEmpty() )
81              {
82                  sb.append( " AND " );
83              }
84          }
85  
86          if ( !excludedFilters.isEmpty() )
87          {
88              sb.append( "NOT (" );
89              sb.append( joinFilters( includedFilters, " OR " ) );
90              sb.append( ")" );
91          }
92  
93          return sb.toString();
94      }
95  
96      private String joinFilters( Collection<Filter> filters, String sep )
97      {
98          boolean isFirst = true;
99          StringBuilder sb = new StringBuilder();
100         for ( Filter f : filters )
101         {
102             if ( !isFirst )
103             {
104                 sb.append( sep );
105             }
106             sb.append( f.describe() );
107             isFirst = false;
108         }
109         return sb.toString();
110     }
111 }