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  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  }