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.plugin.surefire.log.api.ConsoleLogger;
24  import org.apache.maven.surefire.booter.Classpath;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Set;
31  
32  import static java.util.Collections.addAll;
33  import static org.apache.maven.shared.utils.StringUtils.split;
34  
35  final class TestClassPath
36  {
37      private final Iterable<Artifact> artifacts;
38      private final File classesDirectory;
39      private final File testClassesDirectory;
40      private final String[] additionalClasspathElements;
41      private final ConsoleLogger logger;
42  
43      TestClassPath( Iterable<Artifact> artifacts,
44                     File classesDirectory,
45                     File testClassesDirectory,
46                     String[] additionalClasspathElements,
47                     ConsoleLogger logger )
48      {
49          this.artifacts = artifacts;
50          this.classesDirectory = classesDirectory;
51          this.testClassesDirectory = testClassesDirectory;
52          this.additionalClasspathElements = additionalClasspathElements;
53          this.logger = logger;
54      }
55  
56      void avoidArtifactDuplicates( Set<Artifact> providerArtifacts )
57      {
58          for ( Artifact artifact : artifacts )
59          {
60              Iterator<Artifact> it = providerArtifacts.iterator();
61              while ( it.hasNext() )
62              {
63                  Artifact providerArtifact = it.next();
64                  String classifier1 = providerArtifact.getClassifier();
65                  String classifier2 = artifact.getClassifier();
66                  if ( providerArtifact.getGroupId().equals( artifact.getGroupId() )
67                          && providerArtifact.getArtifactId().equals( artifact.getArtifactId() )
68                          && providerArtifact.getType().equals( artifact.getType() )
69                          && ( classifier1 == null ? classifier2 == null : classifier1.equals( classifier2 ) ) )
70                  {
71                      it.remove();
72                      if ( logger.isDebugEnabled() )
73                      {
74                          logger.debug( "Removed artifact " + providerArtifact + " from provider. "
75                                  + "Already appears in test classpath." );
76                      }
77                  }
78              }
79          }
80      }
81  
82      Classpath toClasspath()
83      {
84          List<String> classpath = new ArrayList<>();
85          classpath.add( testClassesDirectory.getAbsolutePath() );
86          classpath.add( classesDirectory.getAbsolutePath() );
87          for ( Artifact artifact : artifacts )
88          {
89              if ( artifact.getArtifactHandler().isAddedToClasspath() )
90              {
91                  File file = artifact.getFile();
92                  if ( file != null )
93                  {
94                      classpath.add( file.getAbsolutePath() );
95                  }
96              }
97          }
98          if ( additionalClasspathElements != null )
99          {
100             for ( String additionalClasspathElement : additionalClasspathElements )
101             {
102                 if ( additionalClasspathElement != null )
103                 {
104                     addAll( classpath, split( additionalClasspathElement, "," ) );
105                 }
106             }
107         }
108 
109         return new Classpath( classpath );
110     }
111 }