Coverage Report - org.apache.maven.plugin.changes.ReleaseUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ReleaseUtils
50%
39/77
50%
27/54
5.25
 
 1  
 package org.apache.maven.plugin.changes;
 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.ArrayList;
 23  
 import java.util.Collections;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.maven.plugin.MojoExecutionException;
 28  
 import org.apache.maven.plugin.logging.Log;
 29  
 import org.apache.maven.plugins.changes.model.Action;
 30  
 import org.apache.maven.plugins.changes.model.Release;
 31  
 
 32  
 /**
 33  
  * A utility class for working with Release objects.
 34  
  *
 35  
  * @author Dennis Lundberg
 36  
  * @version $Id: ReleaseUtils.java 1328886 2012-04-22 14:09:45Z bimargulies $
 37  
  * @since 2.4
 38  
  */
 39  
 public class ReleaseUtils
 40  
 {
 41  
     private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
 42  
 
 43  
     private Log log;
 44  
 
 45  
     public ReleaseUtils( Log log )
 46  2
     {
 47  2
         this.log = log;
 48  2
     }
 49  
 
 50  
     /**
 51  
      * Get the latest release by matching the supplied releases
 52  
      * with the version from the pom.
 53  
      *
 54  
      * @param releases list of releases
 55  
      * @param pomVersion Version of the artifact
 56  
      * @return A <code>Release</code> that matches the next release of the current project
 57  
      * @throws org.apache.maven.plugin.MojoExecutionException If a release can't be found
 58  
      */
 59  
     public Release getLatestRelease( List<Release> releases, String pomVersion )
 60  
         throws MojoExecutionException
 61  
     {
 62  
         // Remove "-SNAPSHOT" from the end, if it's there
 63  1
         if ( pomVersion != null && pomVersion.endsWith( SNAPSHOT_SUFFIX ) )
 64  
         {
 65  0
             pomVersion = pomVersion.substring( 0, pomVersion.length() - SNAPSHOT_SUFFIX.length() );
 66  
         }
 67  1
         getLog().debug( "Found " + releases.size() + " releases." );
 68  
 
 69  1
         Release release = getRelease( releases, pomVersion );
 70  
 
 71  1
         if ( release == null )
 72  
         {
 73  0
             throw new MojoExecutionException( "Couldn't find the release '" + pomVersion
 74  
                 + "' among the supplied releases." );
 75  
         }
 76  
 
 77  1
         return release;
 78  
     }
 79  
 
 80  
     private Log getLog()
 81  
     {
 82  20
         return log;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Get a release with the specified version from the list of releases.
 87  
      *
 88  
      * @param releases A list of releases
 89  
      * @param version The version we want
 90  
      * @return A Release, or null if no release with the specified version can be found
 91  
      */
 92  
     protected Release getRelease( List<Release> releases, String version )
 93  
     {
 94  11
         for ( Release release : releases )
 95  
         {
 96  14
             if ( getLog().isDebugEnabled() )
 97  
             {
 98  0
                 getLog().debug( "The release: " + release.getVersion()
 99  
                     + " has " + release.getActions().size() + " actions." );
 100  
             }
 101  
 
 102  14
             if ( release.getVersion() != null && release.getVersion().equals( version ) )
 103  
             {
 104  5
                 if ( getLog().isDebugEnabled() )
 105  
                 {
 106  0
                     getLog().debug( "Found the correct release: " + release.getVersion() );
 107  0
                     logRelease( release );
 108  
                 }
 109  5
                 return release;
 110  
             }
 111  
         }
 112  6
         return null;
 113  
     }
 114  
 
 115  
     protected void logRelease( Release release )
 116  
     {
 117  
         Action action;
 118  0
         for ( Iterator iterator = release.getActions().iterator(); iterator.hasNext(); )
 119  
         {
 120  0
             action = (Action) iterator.next();
 121  0
             getLog().debug( "o " + action.getType() );
 122  0
             getLog().debug( "issue : " + action.getIssue() );
 123  0
             getLog().debug( "action : " + action.getAction() );
 124  0
             getLog().debug( "dueTo : " + action.getDueTo() );
 125  
         }
 126  0
     }
 127  
 
 128  
     /**
 129  
      * Merge releases from one issue tracker with releases from another issue
 130  
      * tracker. If a release is found in both issue trackers, i.e. they have
 131  
      * the same version, their issues are merged into one release.
 132  
      *
 133  
      * @param firstReleases Releases from the first issue tracker
 134  
      * @param secondReleases Releases from the second issue tracker
 135  
      * @return A list containing the merged releases
 136  
      */
 137  
     public List<Release> mergeReleases( final List<Release> firstReleases, final List<Release> secondReleases )
 138  
     {
 139  6
         if ( firstReleases == null && secondReleases == null )
 140  
         {
 141  0
             return Collections.emptyList();
 142  
         }
 143  6
         if ( firstReleases == null )
 144  
         {
 145  1
             return secondReleases;
 146  
         }
 147  5
         if ( secondReleases == null )
 148  
         {
 149  0
             return firstReleases;
 150  
         }
 151  
 
 152  5
         List<Release> mergedReleases = new ArrayList<Release>();
 153  
 
 154  
         // Loop through the releases from the first issue tracker, merging in
 155  
         // actions from releases with the same version from the second issue
 156  
         // tracker
 157  5
         for ( Release firstRelease : firstReleases )
 158  
         {
 159  6
             Release secondRelease = getRelease( secondReleases, firstRelease.getVersion() );
 160  6
             if ( secondRelease != null )
 161  
             {
 162  2
                 if ( secondRelease.getActions() != null )
 163  
                 {
 164  2
                     firstRelease.getActions().addAll( secondRelease.getActions() );
 165  
                 }
 166  
             }
 167  6
             mergedReleases.add( firstRelease );
 168  6
         }
 169  
 
 170  
         // Handle releases that are only in the second issue tracker
 171  5
         for ( Release secondRelease : secondReleases )
 172  
         {
 173  4
             Release mergedRelease = getRelease( mergedReleases, secondRelease.getVersion() );
 174  4
             if ( mergedRelease == null )
 175  
             {
 176  2
                 mergedReleases.add( secondRelease );
 177  
             }
 178  4
         }
 179  5
         return mergedReleases;
 180  
     }
 181  
 
 182  
     /**
 183  
      * Convert an untyped List of Release objects that comes from changes.xml
 184  
      * into a typed List of Release objects.
 185  
      *
 186  
      * @param changesReleases An untyped List of Release objects
 187  
      * @return A type List of Release objects
 188  
      * @todo When Modello can generate typed collections this method is no longer needed
 189  
      */
 190  
     public List<Release> convertReleaseList( List changesReleases ) {
 191  1
         List<Release> releases = new ArrayList<Release>();
 192  
 
 193  
         // Loop through the List of releases from changes.xml and casting each
 194  
         // release to a Release
 195  1
         for ( Iterator iterator = changesReleases.iterator(); iterator.hasNext(); )
 196  
         {
 197  2
             Release release = (Release) iterator.next();
 198  2
             releases.add( release );
 199  2
         }
 200  1
         return releases;
 201  
     }
 202  
 
 203  
     /**
 204  
      * Merge releases from parent component with releases from child component.
 205  
      * If a release is found in both components, i.e. they have the same version,
 206  
      * their issues are merged into one (parent) release with component marker
 207  
      * for component issues.
 208  
      *
 209  
      * @param releases Releases from the parent component
 210  
      * @param componentName child component name (retrieved from project name)
 211  
      * @param componentReleases Releases from the child component
 212  
      * @return A list containing the merged releases
 213  
      */
 214  
     public List mergeReleases( final List releases, final String componentName, final List componentReleases ) {
 215  0
         if ( releases == null && componentReleases == null )
 216  
         {
 217  0
             return Collections.EMPTY_LIST;
 218  
         }
 219  0
         if ( componentReleases == null )
 220  
         {
 221  0
             return releases;
 222  
         }
 223  
 
 224  0
         final List mergedReleases = new ArrayList();
 225  
 
 226  0
         if ( releases != null )
 227  
         {
 228  0
             for ( Iterator iterator = releases.iterator(); iterator.hasNext(); )
 229  
             {
 230  0
                 final Release release = (Release) iterator.next();
 231  0
                 final Release componentRelease = getRelease( componentReleases, release.getVersion() );
 232  0
                 if ( componentRelease != null ) {
 233  0
                     release.addComponent( componentName, componentRelease );
 234  
                 }
 235  0
                 mergedReleases.add( release );
 236  0
             }
 237  
         }
 238  
 
 239  0
         for ( Iterator iterator = componentReleases.iterator(); iterator.hasNext(); )
 240  
         {
 241  0
             final Release release = (Release) iterator.next();
 242  0
             final Release mergedRelease = getRelease( mergedReleases, release.getVersion() );
 243  0
             if ( mergedRelease == null )
 244  
             {
 245  0
                 final Release componentRelease = new Release();
 246  0
                 componentRelease.setVersion( release.getVersion() );
 247  0
                 componentRelease.setDateRelease( release.getDateRelease() );
 248  0
                 componentRelease.addComponent( componentName, release );
 249  0
                 mergedReleases.add( componentRelease );
 250  
             }
 251  0
         }
 252  
 
 253  0
         return mergedReleases;
 254  
     }
 255  
 }