View Javadoc
1   package org.apache.maven.plugin.surefire.util;
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 java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.StringUtils;
27  
28  import javax.annotation.Nonnull;
29  
30  final class ScannerUtil
31  {
32  
33      private ScannerUtil()
34      {
35      }
36  
37      private static final String FS = System.getProperty( "file.separator" );
38  
39      private static final String JAVA_SOURCE_FILE_EXTENSION = ".java";
40  
41      private static final String JAVA_CLASS_FILE_EXTENSION = ".class";
42  
43      private static final boolean IS_NON_UNIX_FS = ( !FS.equals( "/" ) );
44  
45      @SuppressWarnings( "checkstyle:modifierorder" )
46      public static @Nonnull String convertToJavaClassName( @Nonnull String test )
47      {
48          return StringUtils.removeEnd( test, ".class" ).replace( FS, "." );
49      }
50  
51      @SuppressWarnings( "checkstyle:modifierorder" )
52      public static @Nonnull String convertJarFileResourceToJavaClassName( @Nonnull String test )
53      {
54          return StringUtils.removeEnd( test, ".class" ).replace( "/", "." );
55      }
56  
57      @SuppressWarnings( "checkstyle:modifierorder" )
58      public static @Nonnull String convertSlashToSystemFileSeparator( @Nonnull String path )
59      {
60          return ( IS_NON_UNIX_FS ? path.replace( "/", FS ) : path );
61      }
62  
63      @SuppressWarnings( "checkstyle:modifierorder" )
64      public static @Nonnull String stripBaseDir( String basedir, String test )
65      {
66          return StringUtils.removeStart( test, basedir );
67      }
68  
69      @SuppressWarnings( "checkstyle:modifierorder" )
70      public static @Nonnull String[] processIncludesExcludes( @Nonnull List<String> list )
71      {
72          List<String> newList = new ArrayList<String>();
73          for ( Object aList : list )
74          {
75              String include = (String) aList;
76              String[] includes = include.split( "," );
77              Collections.addAll( newList, includes );
78          }
79  
80          String[] incs = new String[newList.size()];
81  
82          for ( int i = 0; i < incs.length; i++ )
83          {
84              String inc = newList.get( i );
85              if ( inc.endsWith( JAVA_SOURCE_FILE_EXTENSION ) )
86              {
87                  inc = StringUtils.removeEnd( inc, JAVA_SOURCE_FILE_EXTENSION ) + JAVA_CLASS_FILE_EXTENSION;
88              }
89              incs[i] = convertSlashToSystemFileSeparator( inc );
90  
91          }
92          return incs;
93      }
94  }