You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ag...@apache.org on 2013/06/16 22:10:10 UTC
git commit: [SUREFIRE-1005] Fix test pattern matching in scanned
dependencies
Updated Branches:
refs/heads/master 7a0783637 -> edfaf2b86
[SUREFIRE-1005] Fix test pattern matching in scanned dependencies
Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/edfaf2b8
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/edfaf2b8
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/edfaf2b8
Branch: refs/heads/master
Commit: edfaf2b8645922fa4be52b47df2207afa97f1ec4
Parents: 7a07836
Author: Andreas Gudian <ag...@apache.org>
Authored: Sun Jun 16 22:09:30 2013 +0200
Committer: Andreas Gudian <ag...@apache.org>
Committed: Sun Jun 16 22:09:30 2013 +0200
----------------------------------------------------------------------
.../plugin/surefire/util/DependencyScanner.java | 255 ++++++++++---------
.../maven/plugin/surefire/util/ScannerUtil.java | 7 +
.../surefire/util/SpecificFileFilter.java | 10 +-
3 files changed, 142 insertions(+), 130 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/edfaf2b8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DependencyScanner.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DependencyScanner.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DependencyScanner.java
index 189ea80..df36f08 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DependencyScanner.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DependencyScanner.java
@@ -39,137 +39,146 @@ import javax.annotation.Nullable;
/**
* Scans dependencies looking for tests.
- *
+ *
* @author Aslak Knutsen
*/
-public class DependencyScanner {
+public class DependencyScanner
+{
- private final List<File> dependenciesToScan;
+ private final List<File> dependenciesToScan;
- protected final List<String> includes;
+ protected final List<String> includes;
- protected final @Nonnull List<String> excludes;
+ protected final @Nonnull List<String> excludes;
- protected final List<String> specificTests;
+ protected final List<String> specificTests;
- public DependencyScanner(List<File> dependenciesToScan, List<String> includes, @Nonnull List<String> excludes, List<String> specificTests)
- {
- this.dependenciesToScan = dependenciesToScan;
+ public DependencyScanner( List<File> dependenciesToScan, List<String> includes, @Nonnull List<String> excludes, List<String> specificTests )
+ {
+ this.dependenciesToScan = dependenciesToScan;
this.includes = includes;
this.excludes = excludes;
this.specificTests = specificTests;
- }
-
- public DefaultScanResult scan() throws MojoExecutionException
- {
- Matcher matcher = new Matcher(includes, excludes, specificTests);
- List<String> found = new ArrayList<String>();
- for(File artifact : dependenciesToScan)
- {
- try
- {
- found.addAll(scanArtifact(artifact, matcher));
- }
- catch (IOException e)
- {
- throw new MojoExecutionException("Could not scan dependency " + artifact.toString(), e);
- }
- }
- return new DefaultScanResult(found);
- }
-
- private List<String> scanArtifact(File artifact, Matcher matcher) throws IOException
- {
- List<String> found = new ArrayList<String>();
-
- if(artifact != null) {
- if(artifact.isFile()) {
- JarFile jar = null;
- try {
- jar = new JarFile(artifact);
- Enumeration<JarEntry> entries = jar.entries();
- while(entries.hasMoreElements())
- {
- JarEntry entry = entries.nextElement();
- if(matcher.shouldInclude(entry.getName()))
- {
- found.add(convertJarFileResourceToJavaClassName( entry.getName() ));
- }
- }
- }
- finally
- {
- if(jar != null)
- {
- jar.close();
- }
- }
- }
- }
- return found;
- }
-
- public static List<File> filter(List<Artifact> artifacts, List<String> groupArtifactIds)
- {
- List<File> matches = new ArrayList<File>();
- if(groupArtifactIds == null || artifacts == null)
- {
- return matches;
- }
- for(Artifact artifact : artifacts)
- {
- for(String groups : groupArtifactIds)
- {
- String[] groupArtifact = groups.split(":");
- if(groupArtifact.length != 2)
- {
- throw new IllegalArgumentException(
- "dependencyToScan argument should be in format 'groupid:artifactid': " + groups);
- }
- if(
- artifact.getGroupId().matches(groupArtifact[0]) &&
- artifact.getArtifactId().matches(groupArtifact[1]))
- {
- matches.add(artifact.getFile());
- }
- }
- }
- return matches;
- }
-
- private class Matcher {
-
- private MatchPatterns includes;
- private MatchPatterns excludes;
-
- private SpecificFileFilter specificTestFilter;
-
- public Matcher(@Nullable List<String> includes, @Nonnull List<String> excludes, @Nullable List<String> specificTests)
- {
- String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
- specificTestFilter = new SpecificFileFilter( specific );
-
- if(includes != null && includes.size() > 0)
- {
- this.includes = MatchPatterns.from(processIncludesExcludes(includes));
- }
- else
- {
- this.includes = MatchPatterns.from("**");
- }
- this.excludes = MatchPatterns.from(processIncludesExcludes(excludes));
- }
-
- public boolean shouldInclude(String name)
- {
- if(!name.endsWith(".class"))
- {
- return false;
- }
- boolean isIncluded = includes.matches(name, false);
- boolean isExcluded = excludes.matches(name, false);
-
- return isIncluded && !isExcluded && specificTestFilter.accept(name);
- }
- }
+ }
+
+ public DefaultScanResult scan()
+ throws MojoExecutionException
+ {
+ Matcher matcher = new Matcher( includes, excludes, specificTests );
+ List<String> found = new ArrayList<String>();
+ for ( File artifact : dependenciesToScan )
+ {
+ try
+ {
+ found.addAll( scanArtifact( artifact, matcher ) );
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "Could not scan dependency " + artifact.toString(), e );
+ }
+ }
+ return new DefaultScanResult( found );
+ }
+
+ private List<String> scanArtifact( File artifact, Matcher matcher )
+ throws IOException
+ {
+ List<String> found = new ArrayList<String>();
+
+ if ( artifact != null )
+ {
+ if ( artifact.isFile() )
+ {
+ JarFile jar = null;
+ try
+ {
+ jar = new JarFile( artifact );
+ Enumeration<JarEntry> entries = jar.entries();
+ while ( entries.hasMoreElements() )
+ {
+ JarEntry entry = entries.nextElement();
+ if ( matcher.shouldInclude( entry.getName() ) )
+ {
+ found.add( convertJarFileResourceToJavaClassName( entry.getName() ) );
+ }
+ }
+ }
+ finally
+ {
+ if ( jar != null )
+ {
+ jar.close();
+ }
+ }
+ }
+ }
+ return found;
+ }
+
+ public static List<File> filter( List<Artifact> artifacts, List<String> groupArtifactIds )
+ {
+ List<File> matches = new ArrayList<File>();
+ if ( groupArtifactIds == null || artifacts == null )
+ {
+ return matches;
+ }
+ for ( Artifact artifact : artifacts )
+ {
+ for ( String groups : groupArtifactIds )
+ {
+ String[] groupArtifact = groups.split( ":" );
+ if ( groupArtifact.length != 2 )
+ {
+ throw new IllegalArgumentException(
+ "dependencyToScan argument should be in format 'groupid:artifactid': "
+ + groups );
+ }
+ if ( artifact.getGroupId().matches( groupArtifact[0] )
+ && artifact.getArtifactId().matches( groupArtifact[1] ) )
+ {
+ matches.add( artifact.getFile() );
+ }
+ }
+ }
+ return matches;
+ }
+
+ private class Matcher
+ {
+
+ private MatchPatterns includes;
+
+ private MatchPatterns excludes;
+
+ private SpecificFileFilter specificTestFilter;
+
+ public Matcher( @Nullable List<String> includes, @Nonnull List<String> excludes, @Nullable List<String> specificTests )
+ {
+ String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
+ specificTestFilter = new SpecificFileFilter( specific );
+
+ if ( includes != null && includes.size() > 0 )
+ {
+ this.includes = MatchPatterns.from( processIncludesExcludes( includes ) );
+ }
+ else
+ {
+ this.includes = MatchPatterns.from( "**" );
+ }
+ this.excludes = MatchPatterns.from( processIncludesExcludes( excludes ) );
+ }
+
+ public boolean shouldInclude( String name )
+ {
+ if ( !name.endsWith( ".class" ) )
+ {
+ return false;
+ }
+ name = ScannerUtil.convertJarFileResourceToSystemFileSeparator( name );
+ boolean isIncluded = includes.matches( name, false );
+ boolean isExcluded = excludes.matches( name, false );
+
+ return isIncluded && !isExcluded && specificTestFilter.accept( name );
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/edfaf2b8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/ScannerUtil.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/ScannerUtil.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/ScannerUtil.java
index 8b80301..8752194 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/ScannerUtil.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/ScannerUtil.java
@@ -36,6 +36,8 @@ final class ScannerUtil {
private static final String JAVA_SOURCE_FILE_EXTENSION = ".java";
private static final String JAVA_CLASS_FILE_EXTENSION = ".class";
+
+ private static final boolean IS_NON_UNIX_FS = (!FS.equals( "/" ));
public static @Nonnull String convertToJavaClassName( @Nonnull String test )
{
@@ -47,6 +49,11 @@ final class ScannerUtil {
return StringUtils.removeEnd( test, ".class" ).replace( "/", "." );
}
+ public static @Nonnull String convertJarFileResourceToSystemFileSeparator( @Nonnull String path )
+ {
+ return ( IS_NON_UNIX_FS ? path.replace( "/", FS ) : path );
+ }
+
public static @Nonnull String stripBaseDir( String basedir, String test )
{
return StringUtils.removeStart( test, basedir );
http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/edfaf2b8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/SpecificFileFilter.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/SpecificFileFilter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/SpecificFileFilter.java
index 73d52e5..272ceb6 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/SpecificFileFilter.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/SpecificFileFilter.java
@@ -19,18 +19,16 @@ package org.apache.maven.plugin.surefire.util;
* under the License.
*/
-import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
-import org.apache.maven.shared.utils.io.SelectorUtils;
import javax.annotation.Nullable;
+import org.apache.maven.shared.utils.io.SelectorUtils;
+
public class SpecificFileFilter
{
- private static final char FS = System.getProperty( "file.separator" ).charAt( 0 );
-
private Set<String> names;
public SpecificFileFilter( @Nullable String[] classNames )
@@ -38,12 +36,10 @@ public class SpecificFileFilter
if ( classNames != null && classNames.length > 0 )
{
this.names = new HashSet<String>();
- boolean isBackslashFs = '\\' == FS;
for ( String name : classNames )
{
- names.add( isBackslashFs ? name.replace( '/', FS ) : name );
+ names.add( ScannerUtil.convertJarFileResourceToSystemFileSeparator( name ) );
}
- Collections.addAll( names, classNames );
}
}