View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  /**
34   * this class is part of internal implementation and should not be used.
35   * There is mot guarantee about backward compatibility.
36   */
37  public final class TestClassPath {
38      private final Iterable<Artifact> artifacts;
39      private final File classesDirectory;
40      private final File testClassesDirectory;
41      private final Iterable<String> additionalClasspathElements;
42  
43      TestClassPath(
44              Iterable<Artifact> artifacts,
45              File classesDirectory,
46              File testClassesDirectory,
47              Iterable<String> additionalClasspathElements) {
48          this.artifacts = artifacts;
49          this.classesDirectory = classesDirectory;
50          this.testClassesDirectory = testClassesDirectory;
51          this.additionalClasspathElements = additionalClasspathElements;
52      }
53  
54      Map<String, Artifact> getTestDependencies() {
55          Map<String, Artifact> artifactMapping = new LinkedHashMap<>();
56          for (Artifact artifact : artifacts) {
57              artifactMapping.put(artifact.getGroupId() + ":" + artifact.getArtifactId(), artifact);
58          }
59          return artifactMapping;
60      }
61  
62      Classpath toClasspath() {
63          List<String> classpath = new ArrayList<>();
64          classpath.add(testClassesDirectory.getAbsolutePath());
65          classpath.add(classesDirectory.getAbsolutePath());
66          for (Artifact artifact : artifacts) {
67              if (artifact.getArtifactHandler().isAddedToClasspath()) {
68                  File file = artifact.getFile();
69                  if (file != null) {
70                      classpath.add(file.getAbsolutePath());
71                  }
72              }
73          }
74          if (additionalClasspathElements != null) {
75              for (String additionalClasspathElement : additionalClasspathElements) {
76                  if (additionalClasspathElement != null) {
77                      addAll(classpath, split(additionalClasspathElement, ","));
78                  }
79              }
80          }
81  
82          return new Classpath(classpath);
83      }
84  
85      public Iterable<Artifact> getArtifacts() {
86          return artifacts;
87      }
88  
89      public File getClassesDirectory() {
90          return classesDirectory;
91      }
92  
93      public File getTestClassesDirectory() {
94          return testClassesDirectory;
95      }
96  
97      public Iterable<String> getAdditionalClasspathElements() {
98          return additionalClasspathElements;
99      }
100 }