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