Coverage Report - org.apache.maven.shared.artifact.filter.collection.ArtifactTransitivityFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactTransitivityFilter
0%
0/33
0%
0/10
2.25
 
 1  
 package org.apache.maven.shared.artifact.filter.collection;
 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.Collection;
 23  
 import java.util.HashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Set;
 27  
 
 28  
 import org.apache.maven.artifact.Artifact;
 29  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 30  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 31  
 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
 32  
 import org.apache.maven.model.Dependency;
 33  
 import org.apache.maven.project.MavenProject;
 34  
 import org.apache.maven.project.MavenProjectBuilder;
 35  
 import org.apache.maven.project.ProjectBuildingException;
 36  
 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
 37  
 
 38  
 /**
 39  
  * This filter will exclude everything that is not a dependency of the selected artifact.
 40  
  * 
 41  
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 42  
  * @version $Id: ArtifactTransitivityFilter.java 744326 2009-02-14 01:09:41Z brianf $
 43  
  */
 44  
 public class ArtifactTransitivityFilter
 45  
     extends AbstractArtifactsFilter
 46  
 {
 47  
 
 48  
     Collection transitiveArtifacts;
 49  
 
 50  
     ArtifactFactory factory;
 51  
 
 52  
     ArtifactRepository local;
 53  
 
 54  
     List remote;
 55  
 
 56  
     public ArtifactTransitivityFilter( Artifact artifact, ArtifactFactory factory, ArtifactRepository local,
 57  
                                        List remote, MavenProjectBuilder builder )
 58  
         throws ProjectBuildingException, InvalidDependencyVersionException
 59  0
     {
 60  0
         this.factory = factory;
 61  0
         this.local = local;
 62  0
         this.remote = remote;
 63  
 
 64  0
         Artifact rootArtifactPom =
 65  
             factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "", "pom" );
 66  
 
 67  0
         MavenProject rootArtifactProject = builder.buildFromRepository( rootArtifactPom, remote, local );
 68  
 
 69  
         // load all the artifacts.
 70  0
         transitiveArtifacts =
 71  
             rootArtifactProject.createArtifacts( this.factory, Artifact.SCOPE_TEST,
 72  
                                                  new ScopeArtifactFilter( Artifact.SCOPE_TEST ) );
 73  
 
 74  0
     }
 75  
 
 76  
     public ArtifactTransitivityFilter( Dependency dependency, ArtifactFactory factory, ArtifactRepository local,
 77  
                                        List remote, MavenProjectBuilder builder )
 78  
         throws ProjectBuildingException, InvalidDependencyVersionException
 79  0
     {
 80  
 
 81  0
         this.factory = factory;
 82  0
         this.local = local;
 83  0
         this.remote = remote;
 84  
 
 85  0
         Artifact rootArtifactPom =
 86  
             factory.createArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), "",
 87  
                                     "pom" );
 88  
 
 89  0
         MavenProject rootArtifactProject = builder.buildFromRepository( rootArtifactPom, remote, local );
 90  
 
 91  
         // load all the artifacts.
 92  0
         transitiveArtifacts =
 93  
             rootArtifactProject.createArtifacts( this.factory, Artifact.SCOPE_TEST,
 94  
                                                  new ScopeArtifactFilter( Artifact.SCOPE_TEST ) );
 95  
 
 96  0
     }
 97  
 
 98  
     public Set filter( Set artifacts )
 99  
     {
 100  
 
 101  0
         Set result = new HashSet();
 102  0
         Iterator iterator = artifacts.iterator();
 103  0
         while ( iterator.hasNext() )
 104  
         {
 105  0
             Artifact artifact = (Artifact) iterator.next();
 106  0
             if ( artifactIsATransitiveDependency( artifact ) )
 107  
             {
 108  0
                 result.add( artifact );
 109  
             }
 110  0
         }
 111  0
         return result;
 112  
     }
 113  
 
 114  
     /**
 115  
      * Compares the artifact to the list of dependencies to see if it is directly included by this project
 116  
      * 
 117  
      * @param artifact representing the item to compare.
 118  
      * @return true if artifact is a transitive dependency
 119  
      */
 120  
     public boolean artifactIsATransitiveDependency( Artifact artifact )
 121  
     {
 122  0
         boolean result = false;
 123  0
         Iterator iterator = transitiveArtifacts.iterator();
 124  0
         while ( iterator.hasNext() )
 125  
         {
 126  0
             Artifact trans = (Artifact) iterator.next();
 127  0
             if ( trans.getGroupId().equals( artifact.getGroupId() ) &&
 128  
                 trans.getArtifactId().equals( artifact.getArtifactId() ) )
 129  
             {
 130  0
                 result = true;
 131  0
                 break;
 132  
             }
 133  0
         }
 134  0
         return result;
 135  
     }
 136  
 }