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