1 package org.apache.maven.plugin.surefire.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.maven.plugin.surefire.util.ScannerUtil.convertJarFileResourceToJavaClassName;
23 import static org.apache.maven.plugin.surefire.util.ScannerUtil.isJavaClassFile;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Set;
32 import java.util.jar.JarEntry;
33 import java.util.jar.JarFile;
34
35 import org.apache.maven.artifact.Artifact;
36 import org.apache.maven.plugin.MojoExecutionException;
37 import org.apache.maven.surefire.testset.TestFilter;
38 import org.apache.maven.surefire.testset.TestListResolver;
39 import org.apache.maven.surefire.util.DefaultScanResult;
40
41
42
43
44
45
46 public class DependencyScanner
47 {
48 private final List<File> dependenciesToScan;
49
50 private final TestListResolver filter;
51
52 public DependencyScanner( List<File> dependenciesToScan, TestListResolver filter )
53 {
54 this.dependenciesToScan = dependenciesToScan;
55 this.filter = filter;
56 }
57
58 public DefaultScanResult scan()
59 throws MojoExecutionException
60 {
61 Set<String> classes = new LinkedHashSet<String>();
62 for ( File artifact : dependenciesToScan )
63 {
64 if ( artifact != null && artifact.isFile() && artifact.getName().endsWith( ".jar" ) )
65 {
66 try
67 {
68 scanArtifact( artifact, filter, classes );
69 }
70 catch ( IOException e )
71 {
72 throw new MojoExecutionException( "Could not scan dependency " + artifact.toString(), e );
73 }
74 }
75 }
76 return new DefaultScanResult( new ArrayList<String>( classes ) );
77 }
78
79 private static void scanArtifact( File artifact, TestFilter<String, String> filter, Set<String> classes )
80 throws IOException
81 {
82 JarFile jar = null;
83 try
84 {
85 jar = new JarFile( artifact );
86 for ( Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); )
87 {
88 JarEntry entry = entries.nextElement();
89 String path = entry.getName();
90 if ( !entry.isDirectory() && isJavaClassFile( path ) && filter.shouldRun( path, null ) )
91 {
92 classes.add( convertJarFileResourceToJavaClassName( path ) );
93 }
94 }
95 }
96 finally
97 {
98 if ( jar != null )
99 {
100 jar.close();
101 }
102 }
103 }
104
105 public static List<Artifact> filter( List<Artifact> artifacts, List<String> groupArtifactIds )
106 {
107 List<Artifact> matches = new ArrayList<Artifact>();
108 if ( groupArtifactIds == null || artifacts == null )
109 {
110 return matches;
111 }
112 for ( Artifact artifact : artifacts )
113 {
114 for ( String groups : groupArtifactIds )
115 {
116 String[] groupArtifact = groups.split( ":" );
117 if ( groupArtifact.length != 2 )
118 {
119 throw new IllegalArgumentException( "dependencyToScan argument should be in format"
120 + " 'groupid:artifactid': " + groups );
121 }
122 if ( artifact.getGroupId().matches( groupArtifact[0] )
123 && artifact.getArtifactId().matches( groupArtifact[1] ) )
124 {
125 matches.add( artifact );
126 }
127 }
128 }
129 return matches;
130 }
131 }