1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.surefire.util;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Enumeration;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarFile;
31
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
35 import org.apache.maven.surefire.api.testset.TestFilter;
36 import org.apache.maven.surefire.api.testset.TestListResolver;
37 import org.apache.maven.surefire.api.util.DefaultScanResult;
38
39 import static org.apache.maven.plugin.surefire.util.ScannerUtil.convertJarFileResourceToJavaClassName;
40 import static org.apache.maven.plugin.surefire.util.ScannerUtil.isJavaClassFile;
41
42
43
44
45
46
47 public class DependencyScanner {
48 private final List<File> dependenciesToScan;
49
50 private final TestListResolver filter;
51
52 public DependencyScanner(List<File> dependenciesToScan, TestListResolver filter) {
53 this.dependenciesToScan = dependenciesToScan;
54 this.filter = filter;
55 }
56
57 public DefaultScanResult scan() throws MojoExecutionException {
58 Set<String> classes = new LinkedHashSet<>();
59 for (File artifact : dependenciesToScan) {
60 if (artifact != null && artifact.isFile() && artifact.getName().endsWith(".jar")) {
61 try {
62 scanArtifact(artifact, filter, classes);
63 } catch (IOException e) {
64 throw new MojoExecutionException("Could not scan dependency " + artifact.toString(), e);
65 }
66 }
67 }
68 return new DefaultScanResult(new ArrayList<>(classes));
69 }
70
71 private static void scanArtifact(File artifact, TestFilter<String, String> filter, Set<String> classes)
72 throws IOException {
73 try (JarFile jar = new JarFile(artifact)) {
74 for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); ) {
75 JarEntry entry = entries.nextElement();
76 String path = entry.getName();
77 if (!entry.isDirectory() && isJavaClassFile(path) && filter.shouldRun(path, null)) {
78 classes.add(convertJarFileResourceToJavaClassName(path));
79 }
80 }
81 }
82 }
83
84
85
86
87
88
89
90
91
92 public static List<Artifact> filter(List<Artifact> artifacts, List<String> artifactPatterns) {
93 if (artifactPatterns == null || artifacts == null || artifacts.isEmpty()) {
94 return Collections.emptyList();
95 }
96
97 PatternIncludesArtifactFilter artifactFilter = new PatternIncludesArtifactFilter(artifactPatterns);
98
99 List<Artifact> matches = new ArrayList<>();
100 for (Artifact artifact : artifacts) {
101 if (artifactFilter.include(artifact)) {
102 matches.add(artifact);
103 }
104 }
105 return matches;
106 }
107 }