Coverage Report - org.apache.maven.plugin.changes.ReleaseUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ReleaseUtils
73%
37/51
69%
25/36
4,333
 
 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: org.apache.maven.plugin.changes.ReleaseUtils.html 816598 2012-05-08 12:46:49Z hboutemy $
 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  4
     {
 47  4
         this.log = log;
 48  4
     }
 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 releases, String pomVersion )
 60  
         throws MojoExecutionException
 61  
     {
 62  
         // Remove "-SNAPSHOT" from the end, if it's there
 63  2
         if ( pomVersion != null && pomVersion.endsWith( SNAPSHOT_SUFFIX ) )
 64  
         {
 65  0
             pomVersion = pomVersion.substring( 0, pomVersion.length() - SNAPSHOT_SUFFIX.length() );
 66  
         }
 67  2
         getLog().debug( "Found " + releases.size() + " releases." );
 68  
 
 69  2
         Release release = getRelease( releases, pomVersion );
 70  
 
 71  2
         if ( release == null )
 72  
         {
 73  0
             throw new MojoExecutionException( "Couldn't find the release '" + pomVersion
 74  
                 + "' among the supplied releases." );
 75  
         }
 76  
 
 77  2
         return release;
 78  
     }
 79  
 
 80  
     private Log getLog()
 81  
     {
 82  40
         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 releases, String version )
 93  
     {
 94  22
         Release release = null;
 95  40
         for ( int i = 0; i < releases.size(); i++ )
 96  
         {
 97  28
             release = (Release) releases.get( i );
 98  28
             if ( getLog().isDebugEnabled() )
 99  
             {
 100  0
                 getLog().debug( "The release: " + release.getVersion()
 101  
                     + " has " + release.getActions().size() + " actions." );
 102  
             }
 103  
 
 104  28
             if ( release.getVersion() != null && release.getVersion().equals( version ) )
 105  
             {
 106  10
                 if ( getLog().isDebugEnabled() )
 107  
                 {
 108  0
                     getLog().debug( "Found the correct release: " + release.getVersion() );
 109  0
                     logRelease( release );
 110  
                 }
 111  10
                 return release;
 112  
             }
 113  
         }
 114  12
         return null;
 115  
     }
 116  
 
 117  
     protected void logRelease( Release release )
 118  
     {
 119  
         Action action;
 120  0
         for ( Iterator iterator = release.getActions().iterator(); iterator.hasNext(); )
 121  
         {
 122  0
             action = (Action) iterator.next();
 123  0
             getLog().debug( "o " + action.getType() );
 124  0
             getLog().debug( "issue : " + action.getIssue() );
 125  0
             getLog().debug( "action : " + action.getAction() );
 126  0
             getLog().debug( "dueTo : " + action.getDueTo() );
 127  
         }
 128  0
     }
 129  
 
 130  
     /**
 131  
      * Merge releases from one issue tracker with releases from another issue
 132  
      * tracker. If a release is found in both issue trackers, i.e. they have
 133  
      * the same version, their issues are merged into one release.
 134  
      *
 135  
      * @param firstReleases Releases from the first issue tracker
 136  
      * @param secondReleases Releases from the second issue tracker
 137  
      * @return A list containing the merged releases
 138  
      */
 139  
     public List mergeReleases( final List firstReleases, final List secondReleases )
 140  
     {
 141  12
         if ( firstReleases == null && secondReleases == null )
 142  
         {
 143  0
             return Collections.EMPTY_LIST;
 144  
         }
 145  12
         if ( firstReleases == null )
 146  
         {
 147  2
             return secondReleases;
 148  
         }
 149  10
         if ( secondReleases == null )
 150  
         {
 151  0
             return firstReleases;
 152  
         }
 153  
 
 154  10
         List mergedReleases = new ArrayList();
 155  
 
 156  
         // Loop through the releases from the first issue tracker, merging in
 157  
         // actions from releases with the same version from the second issue
 158  
         // tracker
 159  10
         for ( Iterator iterator = firstReleases.iterator(); iterator.hasNext(); )
 160  
         {
 161  12
             Release firstRelease = (Release) iterator.next();
 162  12
             Release secondRelease = getRelease( secondReleases, firstRelease.getVersion() );
 163  12
             if ( secondRelease != null )
 164  
             {
 165  4
                 if ( secondRelease.getActions() != null )
 166  
                 {
 167  4
                     firstRelease.getActions().addAll( secondRelease.getActions() );
 168  
                 }
 169  
             }
 170  12
             mergedReleases.add( firstRelease );
 171  12
         }
 172  
 
 173  
         // Handle releases that are only in the second issue tracker
 174  10
         for ( Iterator iterator = secondReleases.iterator(); iterator.hasNext(); )
 175  
         {
 176  8
             Release secondRelease = (Release) iterator.next();
 177  8
             Release mergedRelease = getRelease( mergedReleases, secondRelease.getVersion() );
 178  8
             if ( mergedRelease == null )
 179  
             {
 180  4
                 mergedReleases.add( secondRelease );
 181  
             }
 182  8
         }
 183  10
         return mergedReleases;
 184  
     }
 185  
 }