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.testset.ResolvedTest;
23  import org.apache.maven.surefire.testset.TestListResolver;
24  import org.junit.runner.Description;
25  import org.junit.runner.manipulation.Filter;
26  
27  import java.util.Collection;
28  import java.util.LinkedHashSet;
29  
30  final class MethodFilter
31      extends Filter
32  {
33      private final CombinedCategoryFilter combinedFilter;
34  
35      MethodFilter( String requestString )
36      {
37          this( new TestListResolver( requestString ) );
38      }
39  
40      MethodFilter( TestListResolver testResolver )
41      {
42          Collection<Filter> includedFilters = new LinkedHashSet<Filter>();
43          Collection<Filter> excludedFilters = new LinkedHashSet<Filter>();
44          for ( ResolvedTest test : testResolver.getIncludedPatterns() )
45          {
46              includedFilters.add( new RequestedTest( test, true ) );
47          }
48          for ( ResolvedTest test : testResolver.getExcludedPatterns() )
49          {
50              excludedFilters.add( new RequestedTest( test, false ) );
51          }
52          combinedFilter = new CombinedCategoryFilter( includedFilters, excludedFilters );
53      }
54  
55      @Override
56      public boolean shouldRun( Description description )
57      {
58          if ( description.isEmpty() )
59          {
60              return false;
61          }
62          else if ( description.isTest() )
63          {
64              return combinedFilter.shouldRun( description );
65          }
66          else
67          {
68              for ( Description o : description.getChildren() )
69              {
70                  if ( combinedFilter.shouldRun( o ) || shouldRun( o ) )
71                  {
72                      return true;
73                  }
74              }
75              return false;
76          }
77      }
78  
79      @Override
80      public String describe()
81      {
82          return combinedFilter.describe();
83      }
84  }