Coverage Report - org.apache.maven.artifact.resolver.ResolutionNode
 
Classes in this File Line Coverage Branch Coverage Complexity
ResolutionNode
0%
0/84
0%
0/40
1.944
 
 1  
 package org.apache.maven.artifact.resolver;
 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.resolver.filter.ArtifactFilter;
 24  
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 25  
 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
 26  
 
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collections;
 29  
 import java.util.Iterator;
 30  
 import java.util.LinkedList;
 31  
 import java.util.List;
 32  
 import java.util.Set;
 33  
 
 34  
 public class ResolutionNode
 35  
 {
 36  
     private Artifact artifact;
 37  
 
 38  
     private List children;
 39  
 
 40  
     private final List parents;
 41  
 
 42  
     private final int depth;
 43  
 
 44  
     private final ResolutionNode parent;
 45  
 
 46  
     private final List remoteRepositories;
 47  
 
 48  0
     private boolean active = true;
 49  
 
 50  
     private List trail;
 51  
 
 52  
     public ResolutionNode( Artifact artifact, List remoteRepositories )
 53  0
     {
 54  0
         this.artifact = artifact;
 55  0
         this.remoteRepositories = remoteRepositories;
 56  0
         depth = 0;
 57  0
         parents = Collections.EMPTY_LIST;
 58  0
         parent = null;
 59  0
     }
 60  
 
 61  
     public ResolutionNode( Artifact artifact, List remoteRepositories, ResolutionNode parent )
 62  0
     {
 63  0
         this.artifact = artifact;
 64  0
         this.remoteRepositories = remoteRepositories;
 65  0
         depth = parent.depth + 1;
 66  0
         parents = new ArrayList();
 67  0
         parents.addAll( parent.parents );
 68  0
         parents.add( parent.getKey() );
 69  0
         this.parent = parent;
 70  0
     }
 71  
 
 72  
     public void setArtifact( Artifact artifact )
 73  
     {
 74  0
         this.artifact = artifact;
 75  0
     }
 76  
 
 77  
     public Artifact getArtifact()
 78  
     {
 79  0
         return artifact;
 80  
     }
 81  
 
 82  
     public Object getKey()
 83  
     {
 84  0
         return artifact.getDependencyConflictId();
 85  
     }
 86  
 
 87  
     public void addDependencies( Set artifacts, List remoteRepositories, ArtifactFilter filter )
 88  
         throws CyclicDependencyException, OverConstrainedVersionException
 89  
     {
 90  0
         if ( !artifacts.isEmpty() )
 91  
         {
 92  0
             children = new ArrayList( artifacts.size() );
 93  
 
 94  0
             for ( Iterator i = artifacts.iterator(); i.hasNext(); )
 95  
             {
 96  0
                 Artifact a = (Artifact) i.next();
 97  
 
 98  0
                 if ( parents.contains( a.getDependencyConflictId() ) )
 99  
                 {
 100  0
                     a.setDependencyTrail( getDependencyTrail() );
 101  
 
 102  0
                     throw new CyclicDependencyException( "A dependency has introduced a cycle", a );
 103  
                 }
 104  
 
 105  0
                 children.add( new ResolutionNode( a, remoteRepositories, this ) );
 106  0
             }
 107  
         }
 108  
         else
 109  
         {
 110  0
             children = Collections.EMPTY_LIST;
 111  
         }
 112  0
         trail = null;
 113  0
     }
 114  
 
 115  
     /**
 116  
      * @return {@link List} < {@link String} > with artifact ids
 117  
      * @throws OverConstrainedVersionException
 118  
      */
 119  
     public List getDependencyTrail()
 120  
         throws OverConstrainedVersionException
 121  
     {
 122  0
         List trial = getTrail();
 123  
 
 124  0
         List ret = new ArrayList( trial.size() );
 125  0
         for ( Iterator i = trial.iterator(); i.hasNext(); )
 126  
         {
 127  0
             Artifact artifact = (Artifact) i.next();
 128  0
             ret.add( artifact.getId() );
 129  0
         }
 130  0
         return ret;
 131  
     }
 132  
 
 133  
     private List getTrail()
 134  
         throws OverConstrainedVersionException
 135  
     {
 136  0
         if ( trail == null )
 137  
         {
 138  0
             List ids = new LinkedList();
 139  0
             ResolutionNode node = this;
 140  0
             while ( node != null )
 141  
             {
 142  0
                 Artifact artifact = node.getArtifact();
 143  0
                 if ( artifact.getVersion() == null )
 144  
                 {
 145  
                     // set the recommended version
 146  0
                     ArtifactVersion selected = artifact.getSelectedVersion();
 147  
                     //MNG-2123: null is a valid response to getSelectedVersion, don't
 148  
                     //assume it won't ever be.
 149  0
                     if (selected != null)
 150  
                     {
 151  0
                         artifact.selectVersion( selected.toString() );
 152  
                     }
 153  
                     else
 154  
                     {
 155  0
                         throw new OverConstrainedVersionException("Unable to get a selected Version for "+ artifact.getArtifactId(),artifact);
 156  
                     }
 157  
                 }
 158  
 
 159  0
                 ids.add( 0, artifact );
 160  0
                 node = node.parent;
 161  0
             }
 162  0
             trail = ids;
 163  
         }
 164  0
         return trail;
 165  
     }
 166  
 
 167  
     public boolean isResolved()
 168  
     {
 169  0
         return children != null;
 170  
     }
 171  
 
 172  
     public boolean isChildOfRootNode()
 173  
     {
 174  0
         return parent != null && parent.parent == null;
 175  
     }
 176  
 
 177  
     public Iterator getChildrenIterator()
 178  
     {
 179  0
         return children.iterator();
 180  
     }
 181  
 
 182  
     public int getDepth()
 183  
     {
 184  0
         return depth;
 185  
     }
 186  
 
 187  
     public List getRemoteRepositories()
 188  
     {
 189  0
         return remoteRepositories;
 190  
     }
 191  
 
 192  
     public boolean isActive()
 193  
     {
 194  0
         return active;
 195  
     }
 196  
 
 197  
     public void enable()
 198  
     {
 199  0
         active = true;
 200  
         // TODO: if it was null, we really need to go find them now... or is this taken care of by the ordering?
 201  0
         if ( children != null )
 202  
         {
 203  0
             for ( Iterator i = children.iterator(); i.hasNext(); )
 204  
             {
 205  0
                 ResolutionNode node = (ResolutionNode) i.next();
 206  0
                 node.enable();
 207  0
             }
 208  
         }
 209  0
     }
 210  
 
 211  
     public void disable()
 212  
     {
 213  0
         active = false;
 214  0
         if ( children != null )
 215  
         {
 216  0
             for ( Iterator i = children.iterator(); i.hasNext(); )
 217  
             {
 218  0
                 ResolutionNode node = (ResolutionNode) i.next();
 219  0
                 node.disable();
 220  0
             }
 221  
         }
 222  0
     }
 223  
 
 224  
     public boolean filterTrail( ArtifactFilter filter )
 225  
         throws OverConstrainedVersionException
 226  
     {
 227  0
         boolean success = true;
 228  0
         if ( filter != null )
 229  
         {
 230  0
             for ( Iterator i = getTrail().iterator(); i.hasNext() && success; )
 231  
             {
 232  0
                 Artifact artifact = (Artifact) i.next();
 233  0
                 if ( !filter.include( artifact ) )
 234  
                 {
 235  0
                     success = false;
 236  
                 }
 237  0
             }
 238  
         }
 239  0
         return success;
 240  
     }
 241  
 
 242  
     public String toString()
 243  
     {
 244  0
         return artifact.toString() + " (" + depth + "; " + ( active ? "enabled" : "disabled" ) + ")";
 245  
     }
 246  
 
 247  
 }