1 package org.apache.maven.plugin.surefire;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.surefire.booter.Classpath;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import static java.util.Collections.addAll;
32 import static org.apache.maven.surefire.shared.utils.StringUtils.split;
33
34 final class TestClassPath
35 {
36 private final Iterable<Artifact> artifacts;
37 private final File classesDirectory;
38 private final File testClassesDirectory;
39 private final String[] additionalClasspathElements;
40
41 TestClassPath( Iterable<Artifact> artifacts,
42 File classesDirectory,
43 File testClassesDirectory,
44 String[] additionalClasspathElements )
45 {
46 this.artifacts = artifacts;
47 this.classesDirectory = classesDirectory;
48 this.testClassesDirectory = testClassesDirectory;
49 this.additionalClasspathElements = additionalClasspathElements;
50 }
51
52 Map<String, Artifact> getTestDependencies()
53 {
54 Map<String, Artifact> artifactMapping = new LinkedHashMap<>();
55 for ( Artifact artifact : artifacts )
56 {
57 artifactMapping.put( artifact.getGroupId() + ":" + artifact.getArtifactId(), artifact );
58 }
59 return artifactMapping;
60 }
61
62 Classpath toClasspath()
63 {
64 List<String> classpath = new ArrayList<>();
65 classpath.add( testClassesDirectory.getAbsolutePath() );
66 classpath.add( classesDirectory.getAbsolutePath() );
67 for ( Artifact artifact : artifacts )
68 {
69 if ( artifact.getArtifactHandler().isAddedToClasspath() )
70 {
71 File file = artifact.getFile();
72 if ( file != null )
73 {
74 classpath.add( file.getAbsolutePath() );
75 }
76 }
77 }
78 if ( additionalClasspathElements != null )
79 {
80 for ( String additionalClasspathElement : additionalClasspathElements )
81 {
82 if ( additionalClasspathElement != null )
83 {
84 addAll( classpath, split( additionalClasspathElement, "," ) );
85 }
86 }
87 }
88
89 return new Classpath( classpath );
90 }
91 }