Coverage Report - org.apache.maven.report.projectinfo.dependencies.renderer.DependencyManagementRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyManagementRenderer
87%
55/63
71%
10/14
2.7
DependencyManagementRenderer$1
6%
1/18
0%
0/14
2.7
 
 1  
 package org.apache.maven.report.projectinfo.dependencies.renderer;
 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.Collections;
 23  
 import java.util.Comparator;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Locale;
 27  
 import java.util.Map;
 28  
 
 29  
 import org.apache.maven.artifact.Artifact;
 30  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 31  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 32  
 import org.apache.maven.doxia.sink.Sink;
 33  
 import org.apache.maven.model.Dependency;
 34  
 import org.apache.maven.plugin.logging.Log;
 35  
 import org.apache.maven.project.MavenProjectBuilder;
 36  
 import org.apache.maven.report.projectinfo.AbstractProjectInfoRenderer;
 37  
 import org.apache.maven.report.projectinfo.ProjectInfoReportUtils;
 38  
 import org.apache.maven.report.projectinfo.dependencies.ManagementDependencies;
 39  
 import org.codehaus.plexus.i18n.I18N;
 40  
 import org.codehaus.plexus.util.StringUtils;
 41  
 
 42  
 /**
 43  
  * @author Nick Stolwijk
 44  
  * @version $Id: DependencyManagementRenderer.java 940663 2010-05-03 22:58:15Z hboutemy $
 45  
  * @since 2.1
 46  
  */
 47  
 public class DependencyManagementRenderer
 48  
     extends AbstractProjectInfoRenderer
 49  
 {
 50  
     private final ManagementDependencies dependencies;
 51  
 
 52  
     private final Log log;
 53  
 
 54  
     private final ArtifactFactory artifactFactory;
 55  
 
 56  
     private final MavenProjectBuilder mavenProjectBuilder;
 57  
 
 58  
     private final List remoteRepositories;
 59  
 
 60  
     private final ArtifactRepository localRepository;
 61  
 
 62  
     /**
 63  
      * Default constructor
 64  
      *
 65  
      * @param sink
 66  
      * @param locale
 67  
      * @param i18n
 68  
      * @param log
 69  
      * @param artifactFactory
 70  
      * @param dependencies
 71  
      * @param mavenProjectBuilder
 72  
      * @param remoteRepositories
 73  
      * @param localRepository
 74  
      */
 75  
     public DependencyManagementRenderer( Sink sink, Locale locale, I18N i18n, Log log,
 76  
                                          ManagementDependencies dependencies, ArtifactFactory artifactFactory,
 77  
                                          MavenProjectBuilder mavenProjectBuilder, List remoteRepositories,
 78  
                                          ArtifactRepository localRepository )
 79  
     {
 80  1
         super( sink, i18n, locale );
 81  
 
 82  1
         this.log = log;
 83  1
         this.dependencies = dependencies;
 84  1
         this.artifactFactory = artifactFactory;
 85  1
         this.mavenProjectBuilder = mavenProjectBuilder;
 86  1
         this.remoteRepositories = remoteRepositories;
 87  1
         this.localRepository = localRepository;
 88  1
     }
 89  
 
 90  
     // ----------------------------------------------------------------------
 91  
     // Public methods
 92  
     // ----------------------------------------------------------------------
 93  
 
 94  
     protected String getI18Nsection()
 95  
     {
 96  8
         return "dependencyManagement";
 97  
     }
 98  
 
 99  
     /** {@inheritDoc} */
 100  
     public void renderBody()
 101  
     {
 102  
         // Dependencies report
 103  
 
 104  1
         if ( !dependencies.hasDependencies() )
 105  
         {
 106  0
             startSection( getTitle() );
 107  
 
 108  0
             paragraph( getI18nString( "nolist" ) );
 109  
 
 110  0
             endSection();
 111  
 
 112  0
             return;
 113  
         }
 114  
 
 115  
         // === Section: Project Dependencies.
 116  1
         renderSectionProjectDependencies();
 117  1
     }
 118  
 
 119  
     // ----------------------------------------------------------------------
 120  
     // Private methods
 121  
     // ----------------------------------------------------------------------
 122  
 
 123  
     private void renderSectionProjectDependencies()
 124  
     {
 125  1
         startSection( getTitle() );
 126  
 
 127  
         // collect dependencies by scope
 128  1
         Map dependenciesByScope = dependencies.getManagementDependenciesByScope();
 129  
 
 130  1
         renderDependenciesForAllScopes( dependenciesByScope );
 131  
 
 132  1
         endSection();
 133  1
     }
 134  
 
 135  
     private void renderDependenciesForAllScopes( Map dependenciesByScope )
 136  
     {
 137  1
         renderDependenciesForScope( Artifact.SCOPE_COMPILE, (List) dependenciesByScope.get( Artifact.SCOPE_COMPILE ) );
 138  1
         renderDependenciesForScope( Artifact.SCOPE_RUNTIME, (List) dependenciesByScope.get( Artifact.SCOPE_RUNTIME ) );
 139  1
         renderDependenciesForScope( Artifact.SCOPE_TEST, (List) dependenciesByScope.get( Artifact.SCOPE_TEST ) );
 140  1
         renderDependenciesForScope( Artifact.SCOPE_PROVIDED,
 141  
                                     (List) dependenciesByScope.get( Artifact.SCOPE_PROVIDED ) );
 142  1
         renderDependenciesForScope( Artifact.SCOPE_SYSTEM, (List) dependenciesByScope.get( Artifact.SCOPE_SYSTEM ) );
 143  1
     }
 144  
 
 145  
     private String[] getDependencyTableHeader( boolean hasClassifier )
 146  
     {
 147  1
         String groupId = getI18nString( "column.groupId" );
 148  1
         String artifactId = getI18nString( "column.artifactId" );
 149  1
         String version = getI18nString( "column.version" );
 150  1
         String classifier = getI18nString( "column.classifier" );
 151  1
         String type = getI18nString( "column.type" );
 152  
 
 153  1
         if ( hasClassifier )
 154  
         {
 155  0
             return new String[] { groupId, artifactId, version, classifier, type };
 156  
         }
 157  
 
 158  1
         return new String[] { groupId, artifactId, version, type };
 159  
     }
 160  
 
 161  
     private void renderDependenciesForScope( String scope, List artifacts )
 162  
     {
 163  5
         if ( artifacts != null )
 164  
         {
 165  
             // can't use straight artifact comparison because we want optional last
 166  1
             Collections.sort( artifacts, getDependencyComparator() );
 167  
 
 168  1
             startSection( scope );
 169  
 
 170  1
             paragraph( getI18nString( "intro." + scope ) );
 171  1
             startTable();
 172  
 
 173  1
             boolean hasClassifier = false;
 174  1
             for ( Iterator iterator = artifacts.iterator(); iterator.hasNext(); )
 175  
             {
 176  1
                 Dependency dependency = (Dependency) iterator.next();
 177  1
                 if ( StringUtils.isNotEmpty( dependency.getClassifier() ) )
 178  
                 {
 179  0
                     hasClassifier = true;
 180  0
                     break;
 181  
                 }
 182  1
             }
 183  
 
 184  1
             String[] tableHeader = getDependencyTableHeader( hasClassifier );
 185  1
             tableHeader( tableHeader );
 186  
 
 187  1
             for ( Iterator iterator = artifacts.iterator(); iterator.hasNext(); )
 188  
             {
 189  1
                 Dependency dependency = (Dependency) iterator.next();
 190  1
                 tableRow( getDependencyRow( dependency, hasClassifier ) );
 191  1
             }
 192  1
             endTable();
 193  
 
 194  1
             endSection();
 195  
         }
 196  5
     }
 197  
 
 198  
     private String[] getDependencyRow( Dependency dependency, boolean hasClassifier )
 199  
     {
 200  1
         Artifact artifact = artifactFactory.createParentArtifact( dependency.getGroupId(), dependency.getArtifactId(),
 201  
                                                                   dependency.getVersion() );
 202  1
         String url =
 203  
             ProjectInfoReportUtils.getArtifactUrl( artifactFactory, artifact, mavenProjectBuilder, remoteRepositories,
 204  
                                                    localRepository );
 205  1
         String artifactIdCell = ProjectInfoReportUtils.getArtifactIdCell( artifact.getArtifactId(), url );
 206  
 
 207  1
         if ( hasClassifier )
 208  
         {
 209  0
             return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(),
 210  
                 dependency.getClassifier(), dependency.getType() };
 211  
         }
 212  
 
 213  1
         return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(),
 214  
             dependency.getType() };
 215  
     }
 216  
 
 217  
     private Comparator getDependencyComparator()
 218  
     {
 219  1
         return new Comparator()
 220  1
         {
 221  
             public int compare( Object o1, Object o2 )
 222  
             {
 223  0
                 Dependency a1 = (Dependency) o1;
 224  0
                 Dependency a2 = (Dependency) o2;
 225  
 
 226  0
                 int result = a1.getGroupId().compareTo( a2.getGroupId() );
 227  0
                 if ( result == 0 )
 228  
                 {
 229  0
                     result = a1.getArtifactId().compareTo( a2.getArtifactId() );
 230  0
                     if ( result == 0 )
 231  
                     {
 232  0
                         result = a1.getType().compareTo( a2.getType() );
 233  0
                         if ( result == 0 )
 234  
                         {
 235  0
                             if ( a1.getClassifier() == null )
 236  
                             {
 237  0
                                 if ( a2.getClassifier() != null )
 238  
                                 {
 239  0
                                     result = 1;
 240  
                                 }
 241  
                             }
 242  
                             else
 243  
                             {
 244  0
                                 if ( a2.getClassifier() != null )
 245  
                                 {
 246  0
                                     result = a1.getClassifier().compareTo( a2.getClassifier() );
 247  
                                 }
 248  
                                 else
 249  
                                 {
 250  0
                                     result = -1;
 251  
                                 }
 252  
                             }
 253  0
                             if ( result == 0 )
 254  
                             {
 255  
                                 // We don't consider the version range in the comparison, just the resolved version
 256  0
                                 result = a1.getVersion().compareTo( a2.getVersion() );
 257  
                             }
 258  
                         }
 259  
                     }
 260  
                 }
 261  
 
 262  0
                 return result;
 263  
             }
 264  
         };
 265  
     }
 266  
 }