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.apache.maven.surefire.group.match.AndGroupMatcher;
23  import org.apache.maven.surefire.group.match.GroupMatcher;
24  import org.apache.maven.surefire.group.match.InverseGroupMatcher;
25  import org.junit.experimental.categories.Category;
26  import org.junit.runner.Description;
27  import org.junit.runner.manipulation.Filter;
28  
29  import java.util.ArrayList;
30  import java.util.HashSet;
31  import java.util.Set;
32  
33  import static java.util.Collections.addAll;
34  import static org.junit.runner.Description.createSuiteDescription;
35  
36  final class GroupMatcherCategoryFilter
37      extends Filter
38  {
39      private final AndGroupMatcher matcher;
40  
41      GroupMatcherCategoryFilter( GroupMatcher included, GroupMatcher excluded )
42      {
43          GroupMatcher invertedExclude = excluded == null ? null : new InverseGroupMatcher( excluded );
44          if ( included != null || invertedExclude != null )
45          {
46              matcher = new AndGroupMatcher();
47              if ( included != null )
48              {
49                  matcher.addMatcher( included );
50              }
51  
52              if ( invertedExclude != null )
53              {
54                  matcher.addMatcher( invertedExclude );
55              }
56          }
57          else
58          {
59              matcher = null;
60          }
61      }
62  
63      @Override
64      public boolean shouldRun( Description description )
65      {
66          if ( description.getMethodName() == null || description.getTestClass() == null )
67          {
68              return shouldRun( description, null, null );
69          }
70          else
71          {
72              Class<?> testClass = description.getTestClass();
73              return shouldRun( description, createSuiteDescription( testClass ), testClass );
74          }
75      }
76  
77      private static void findSuperclassCategories( Set<Class<?>> cats, Class<?> clazz )
78      {
79          if ( clazz != null && clazz.getSuperclass() != null )
80          {
81              Category cat = clazz.getSuperclass().getAnnotation( Category.class );
82              if ( cat != null )
83              {
84                  addAll( cats, cat.value() );
85              }
86              else
87              {
88                  findSuperclassCategories( cats, clazz.getSuperclass() );
89              }
90          }
91      }
92  
93      private boolean shouldRun( Description description, Description parent, Class<?> parentClass )
94      {
95          if ( matcher == null )
96          {
97              return true;
98          }
99          else
100         {
101             Set<Class<?>> cats = new HashSet<Class<?>>();
102             Category cat = description.getAnnotation( Category.class );
103             if ( cat != null )
104             {
105                 addAll( cats, cat.value() );
106             }
107 
108             if ( parent != null )
109             {
110                 cat = parent.getAnnotation( Category.class );
111                 if ( cat != null )
112                 {
113                     addAll( cats, cat.value() );
114                 }
115             }
116 
117             if ( parentClass != null )
118             {
119                 findSuperclassCategories( cats, parentClass );
120             }
121 
122             Class<?> testClass = description.getTestClass();
123             if ( testClass != null )
124             {
125                 cat = testClass.getAnnotation( Category.class );
126                 if ( cat != null )
127                 {
128                     addAll( cats, cat.value() );
129                 }
130             }
131 
132             cats.remove( null );
133 
134             boolean result = matcher.enabled( cats.toArray( new Class<?>[cats.size()] ) );
135 
136             if ( !result )
137             {
138                 ArrayList<Description> children = description.getChildren();
139                 if ( children != null )
140                 {
141                     for ( Description child : children )
142                     {
143                         if ( shouldRun( child, description, null ) )
144                         {
145                             result = true;
146                             break;
147                         }
148                     }
149                 }
150             }
151 
152             return result;
153         }
154     }
155 
156     @Override
157     public String describe()
158     {
159         return matcher == null ? "ANY" : matcher.toString();
160     }
161 }