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;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.surefire.booter.Classpath;
29
30 import static java.util.Collections.addAll;
31 import static org.apache.maven.surefire.shared.utils.StringUtils.split;
32
33 final class TestClassPath {
34 private final Iterable<Artifact> artifacts;
35 private final File classesDirectory;
36 private final File testClassesDirectory;
37 private final Iterable<String> additionalClasspathElements;
38
39 TestClassPath(
40 Iterable<Artifact> artifacts,
41 File classesDirectory,
42 File testClassesDirectory,
43 Iterable<String> additionalClasspathElements) {
44 this.artifacts = artifacts;
45 this.classesDirectory = classesDirectory;
46 this.testClassesDirectory = testClassesDirectory;
47 this.additionalClasspathElements = additionalClasspathElements;
48 }
49
50 Map<String, Artifact> getTestDependencies() {
51 Map<String, Artifact> artifactMapping = new LinkedHashMap<>();
52 for (Artifact artifact : artifacts) {
53 artifactMapping.put(artifact.getGroupId() + ":" + artifact.getArtifactId(), artifact);
54 }
55 return artifactMapping;
56 }
57
58 Classpath toClasspath() {
59 List<String> classpath = new ArrayList<>();
60 classpath.add(testClassesDirectory.getAbsolutePath());
61 classpath.add(classesDirectory.getAbsolutePath());
62 for (Artifact artifact : artifacts) {
63 if (artifact.getArtifactHandler().isAddedToClasspath()) {
64 File file = artifact.getFile();
65 if (file != null) {
66 classpath.add(file.getAbsolutePath());
67 }
68 }
69 }
70 if (additionalClasspathElements != null) {
71 for (String additionalClasspathElement : additionalClasspathElements) {
72 if (additionalClasspathElement != null) {
73 addAll(classpath, split(additionalClasspathElement, ","));
74 }
75 }
76 }
77
78 return new Classpath(classpath);
79 }
80 }