Coverage Report - org.apache.maven.plugin.surefire.SurefireDependencyResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
SurefireDependencyResolver
0%
0/47
0%
0/14
4
 
 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.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import org.apache.maven.artifact.Artifact;
 28  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 29  
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 30  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 31  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 32  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 33  
 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 34  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 35  
 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
 36  
 import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
 37  
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 38  
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 39  
 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
 40  
 import org.apache.maven.artifact.versioning.VersionRange;
 41  
 import org.apache.maven.plugin.logging.Log;
 42  
 import org.apache.maven.surefire.booter.Classpath;
 43  
 
 44  
 /**
 45  
  * Does dependency resolution and artifact handling for the surefire plugin.
 46  
  *
 47  
  * @author Stephen Connolly
 48  
  * @author Kristian Rosenvold
 49  
  */
 50  
 public class SurefireDependencyResolver
 51  
 {
 52  
 
 53  
     private final ArtifactResolver artifactResolver;
 54  
 
 55  
     private final ArtifactFactory artifactFactory;
 56  
 
 57  
     private final org.apache.maven.plugin.logging.Log log;
 58  
 
 59  
     private final ArtifactRepository localRepository;
 60  
 
 61  
     private final List remoteRepositories;
 62  
 
 63  
     private final ArtifactMetadataSource artifactMetadataSource;
 64  
 
 65  
     private final String pluginName;
 66  
 
 67  
     protected SurefireDependencyResolver( ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, Log log,
 68  
                                           ArtifactRepository localRepository, List 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
         Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider,
 131  
                                                                               VersionRange.createFromVersion( version ),
 132  
                                                                               "jar", null, Artifact.SCOPE_TEST );
 133  0
         ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact );
 134  0
         List files = new ArrayList();
 135  
 
 136  0
         for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
 137  
         {
 138  0
             Artifact artifact = (Artifact) i.next();
 139  
 
 140  0
             log.debug(
 141  
                 "Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: "
 142  
                     + artifact.getScope() );
 143  
 
 144  0
             files.add( artifact.getFile().getAbsolutePath() );
 145  0
         }
 146  0
         return new Classpath( files );
 147  
     }
 148  
 
 149  
     public Classpath addProviderToClasspath( Map pluginArtifactMap, Artifact surefireArtifact )
 150  
         throws ArtifactResolutionException, ArtifactNotFoundException
 151  
     {
 152  0
         List files = new ArrayList();
 153  0
         if ( surefireArtifact != null )
 154  
         {
 155  0
             final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact );
 156  0
             for ( Iterator iterator = pluginArtifactMap.values().iterator(); iterator.hasNext(); )
 157  
             {
 158  0
                 Artifact artifact = (Artifact) iterator.next();
 159  0
                 if ( !artifactResolutionResult.getArtifacts().contains( artifact ) )
 160  
                 {
 161  0
                     files.add( artifact.getFile().getAbsolutePath() );
 162  
                 }
 163  0
             }
 164  0
         }
 165  
         else
 166  
         {
 167  
             // Bit of a brute force strategy if not found. Should probably be improved
 168  0
             for ( Iterator iterator = pluginArtifactMap.values().iterator(); iterator.hasNext(); )
 169  
             {
 170  0
                 Artifact artifact = (Artifact) iterator.next();
 171  0
                 files.add( artifact.getFile().getPath() );
 172  0
             }
 173  
         }
 174  0
         return new Classpath( files );
 175  
     }
 176  
 
 177  
 }