Coverage Report - org.apache.maven.plugins.enforcer.utils.DependencyVersionMap
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyVersionMap
0%
0/34
0%
0/16
1.667
 
 1  
 package org.apache.maven.plugins.enforcer.utils;
 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  
 import java.util.ArrayList;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.maven.artifact.Artifact;
 27  
 import org.apache.maven.artifact.ArtifactUtils;
 28  
 import org.apache.maven.plugin.logging.Log;
 29  
 import org.apache.maven.shared.dependency.tree.DependencyNode;
 30  
 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
 31  
 
 32  
 public class DependencyVersionMap
 33  
     implements DependencyNodeVisitor
 34  
 {
 35  
     private Log log;
 36  
     
 37  
     private boolean uniqueVersions;
 38  
 
 39  
     private Map<String, List<DependencyNode>> idsToNode;
 40  
 
 41  
     public DependencyVersionMap( Log log )
 42  0
     {
 43  0
         this.log = log;
 44  0
         idsToNode = new HashMap<String, List<DependencyNode>>();
 45  0
     }
 46  
     
 47  
     public void setUniqueVersions( boolean uniqueVersions )
 48  
     {
 49  0
         this.uniqueVersions = uniqueVersions;
 50  0
     }
 51  
 
 52  
     public boolean visit( DependencyNode node )
 53  
     {
 54  0
         addDependency( node );
 55  0
         return !containsConflicts( node );
 56  
     }
 57  
 
 58  
     public boolean endVisit( DependencyNode node )
 59  
     {
 60  0
         return true;
 61  
     }
 62  
 
 63  
     private String constructKey( DependencyNode node )
 64  
     {
 65  0
         return constructKey( node.getArtifact() );
 66  
     }
 67  
 
 68  
     private String constructKey( Artifact artifact )
 69  
     {
 70  0
         return artifact.getGroupId() + ":" + artifact.getArtifactId();
 71  
     }
 72  
 
 73  
     public void addDependency( DependencyNode node )
 74  
     {
 75  0
         String key = constructKey( node );
 76  0
         List<DependencyNode> nodes = idsToNode.get( key );
 77  0
         if ( nodes == null )
 78  
         {
 79  0
             nodes = new ArrayList<DependencyNode>();
 80  0
             idsToNode.put( key, nodes );
 81  
         }
 82  0
         nodes.add( node );
 83  0
     }
 84  
     
 85  
     private String getVersion( Artifact artifact )
 86  
     {
 87  0
         log.info( ArtifactUtils.versionlessKey( artifact ) + " " + artifact.getVersion() + " " + artifact.getBaseVersion() );
 88  0
         return uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion(); 
 89  
     }
 90  
 
 91  
     private boolean containsConflicts( DependencyNode node )
 92  
     {
 93  0
         return containsConflicts( node.getArtifact() );
 94  
     }
 95  
 
 96  
     private boolean containsConflicts( Artifact artifact )
 97  
     {
 98  0
         return containsConflicts( idsToNode.get( constructKey( artifact ) ) );
 99  
     }
 100  
 
 101  
     private boolean containsConflicts( List<DependencyNode> nodes )
 102  
     {
 103  0
         String version = null;
 104  0
         for ( DependencyNode node : nodes )
 105  
         {
 106  0
             if ( version == null )
 107  
             {
 108  0
                 version = getVersion( node.getArtifact() );
 109  
             }
 110  
             else
 111  
             {
 112  0
                 if ( version.compareTo( getVersion( node.getArtifact() ) ) != 0 )
 113  
                 {
 114  0
                     return true;
 115  
                 }
 116  
             }
 117  
         }
 118  0
         return false;
 119  
     }
 120  
 
 121  
     public List<List<DependencyNode>> getConflictedVersionNumbers()
 122  
     {
 123  0
         List<List<DependencyNode>> output = new ArrayList<List<DependencyNode>>();
 124  0
         for ( List<DependencyNode> nodes : idsToNode.values() )
 125  
         {
 126  0
             if ( containsConflicts( nodes ) )
 127  
             {
 128  0
                 output.add( nodes );
 129  
             }
 130  
         }
 131  0
         return output;
 132  
     }
 133  
 }