Coverage Report - org.apache.maven.plugin.surefire.SurefireDependencyResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
SurefireDependencyResolver
0%
0/47
0%
0/16
4,2
 
 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 java.util.ArrayList;
 23  
 import java.util.Collections;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 import org.apache.maven.artifact.Artifact;
 27  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 28  
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 29  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 30  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 31  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 32  
 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 33  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 34  
 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
 35  
 import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
 36  
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 37  
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 38  
 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
 39  
 import org.apache.maven.artifact.versioning.VersionRange;
 40  
 import org.apache.maven.plugin.logging.Log;
 41  
 import org.apache.maven.surefire.booter.Classpath;
 42  
 
 43  
 /**
 44  
  * Does dependency resolution and artifact handling for the surefire plugin.
 45  
  *
 46  
  * @author Stephen Connolly
 47  
  * @author Kristian Rosenvold
 48  
  */
 49  
 public class SurefireDependencyResolver
 50  
 {
 51  
 
 52  
     private final ArtifactResolver artifactResolver;
 53  
 
 54  
     private final ArtifactFactory artifactFactory;
 55  
 
 56  
     private final org.apache.maven.plugin.logging.Log log;
 57  
 
 58  
     private final ArtifactRepository localRepository;
 59  
 
 60  
     private final List<ArtifactRepository> remoteRepositories;
 61  
 
 62  
     private final ArtifactMetadataSource artifactMetadataSource;
 63  
 
 64  
     private final String pluginName;
 65  
 
 66  
     protected SurefireDependencyResolver( ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, Log log,
 67  
                                           ArtifactRepository localRepository,
 68  
                                           List<ArtifactRepository> remoteRepositories,
 69  
                                           ArtifactMetadataSource artifactMetadataSource, String pluginName )
 70  0
     {
 71  0
         this.artifactResolver = artifactResolver;
 72  0
         this.artifactFactory = artifactFactory;
 73  0
         this.log = log;
 74  0
         this.localRepository = localRepository;
 75  0
         this.remoteRepositories = remoteRepositories;
 76  0
         this.artifactMetadataSource = artifactMetadataSource;
 77  0
         this.pluginName = pluginName;
 78  0
     }
 79  
 
 80  
 
 81  
     public boolean isWithinVersionSpec( Artifact artifact, String versionSpec )
 82  
     {
 83  0
         if ( artifact == null )
 84  
         {
 85  0
             return false;
 86  
         }
 87  
         try
 88  
         {
 89  0
             VersionRange range = VersionRange.createFromVersionSpec( versionSpec );
 90  
             try
 91  
             {
 92  0
                 return range.containsVersion( artifact.getSelectedVersion() );
 93  
             }
 94  0
             catch ( NullPointerException e )
 95  
             {
 96  0
                 return range.containsVersion( new DefaultArtifactVersion( artifact.getBaseVersion() ) );
 97  
             }
 98  
         }
 99  0
         catch ( InvalidVersionSpecificationException e )
 100  
         {
 101  0
             throw new RuntimeException( "Bug in plugin. Please report with stacktrace" );
 102  
         }
 103  0
         catch ( OverConstrainedVersionException e )
 104  
         {
 105  0
             throw new RuntimeException( "Bug in plugin. Please report with stacktrace" );
 106  
         }
 107  
     }
 108  
 
 109  
 
 110  
     public ArtifactResolutionResult resolveArtifact( Artifact filteredArtifact, Artifact providerArtifact )
 111  
         throws ArtifactResolutionException, ArtifactNotFoundException
 112  
     {
 113  0
         ArtifactFilter filter = null;
 114  0
         if ( filteredArtifact != null )
 115  
         {
 116  0
             filter = new ExcludesArtifactFilter(
 117  
                 Collections.singletonList( filteredArtifact.getGroupId() + ":" + filteredArtifact.getArtifactId() ) );
 118  
         }
 119  
 
 120  0
         Artifact originatingArtifact = artifactFactory.createBuildArtifact( "dummy", "dummy", "1.0", "jar" );
 121  
 
 122  0
         return artifactResolver.resolveTransitively( Collections.singleton( providerArtifact ), originatingArtifact,
 123  
                                                      localRepository, remoteRepositories, artifactMetadataSource,
 124  
                                                      filter );
 125  
     }
 126  
 
 127  
     public Classpath getProviderClasspath( String provider, String version, Artifact filteredArtifact )
 128  
         throws ArtifactNotFoundException, ArtifactResolutionException
 129  
     {
 130  0
         Classpath classPath = ClasspathCache.getCachedClassPath( provider );
 131  0
         if ( classPath == null )
 132  
         {
 133  0
             Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider,
 134  
                                                                                   VersionRange.createFromVersion(
 135  
                                                                                       version ), "jar", null,
 136  
                                                                                   Artifact.SCOPE_TEST );
 137  0
             ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );
 138  0
             List<String> files = new ArrayList<String>();
 139  
 
 140  0
             for ( Object o : result.getArtifacts() )
 141  
             {
 142  0
                 Artifact artifact = (Artifact) o;
 143  
 
 144  0
                 log.debug(
 145  
                     "Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: "
 146  
                         + artifact.getScope() );
 147  
 
 148  0
                 files.add( artifact.getFile().getAbsolutePath() );
 149  0
             }
 150  0
             classPath = new Classpath( files );
 151  0
             ClasspathCache.setCachedClasspath( provider, classPath );
 152  
         }
 153  0
         return classPath;
 154  
     }
 155  
 
 156  
     public Classpath addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact )
 157  
         throws ArtifactResolutionException, ArtifactNotFoundException
 158  
     {
 159  0
         List<String> files = new ArrayList<String>();
 160  0
         if ( surefireArtifact != null )
 161  
         {
 162  0
             final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
 163  0
             for ( Artifact artifact : pluginArtifactMap.values() )
 164  
             {
 165  0
                 if ( !artifactResolutionResult.getArtifacts().contains( artifact ) )
 166  
                 {
 167  0
                     files.add( artifact.getFile().getAbsolutePath() );
 168  
                 }
 169  
             }
 170  0
         }
 171  
         else
 172  
         {
 173  
             // Bit of a brute force strategy if not found. Should probably be improved
 174  0
             for ( Artifact artifact : pluginArtifactMap.values() )
 175  
             {
 176  0
                 files.add( artifact.getFile().getPath() );
 177  
             }
 178  
         }
 179  0
         return new Classpath( files );
 180  
     }
 181  
 
 182  
 }