View Javadoc
1   package org.apache.maven.plugin.surefire;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }