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