View Javadoc

1   package org.apache.maven.surefire;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.Set;
24  import org.apache.maven.shared.utils.io.SelectorUtils;
25  import org.apache.maven.surefire.util.ScannerFilter;
26  
27  public class SpecificTestClassFilter
28      implements ScannerFilter
29  {
30  
31      private static final char FS = System.getProperty( "file.separator" ).charAt( 0 );
32  
33      private static final String JAVA_CLASS_FILE_EXTENSION = ".class";
34  
35      private Set<String> names;
36  
37      public SpecificTestClassFilter( String[] classNames )
38      {
39          if ( classNames != null && classNames.length > 0 )
40          {
41              this.names = new HashSet<String>();
42              Collections.addAll( names, classNames );
43          }
44      }
45  
46      public boolean accept( Class testClass )
47      {
48          // If the tests enumeration is empty, allow anything.
49          boolean result = true;
50  
51          if ( names != null && !names.isEmpty() )
52          {
53              String className = testClass.getName().replace( '.', FS ) + JAVA_CLASS_FILE_EXTENSION;
54  
55              boolean found = false;
56              for ( String pattern : names )
57              {
58                  if ( '\\' == FS )
59                  {
60                      pattern = pattern.replace( '/', FS );
61                  }
62  
63                  // This is the same utility used under the covers in the plexus DirectoryScanner, and
64                  // therefore in the surefire DefaultDirectoryScanner implementation.
65                  if ( SelectorUtils.matchPath( pattern, className, true ) )
66                  {
67                      found = true;
68                      break;
69                  }
70              }
71  
72              if ( !found )
73              {
74                  result = false;
75              }
76          }
77  
78          return result;
79      }
80  
81  }