Coverage Report - fr.paris.lutece.plugins.releaser.util.version.Version
 
Classes in this File Line Coverage Branch Coverage Complexity
Version
71 %
85/119
75 %
27/36
2,037
 
 1  
 /*
 2  
  * Copyright (c) 2002-2017, Mairie de Paris
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  *
 9  
  *  1. Redistributions of source code must retain the above copyright notice
 10  
  *     and the following disclaimer.
 11  
  *
 12  
  *  2. Redistributions in binary form must reproduce the above copyright notice
 13  
  *     and the following disclaimer in the documentation and/or other materials
 14  
  *     provided with the distribution.
 15  
  *
 16  
  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 17  
  *     contributors may be used to endorse or promote products derived from
 18  
  *     this software without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  *
 32  
  * License 1.0
 33  
  */
 34  
 package fr.paris.lutece.plugins.releaser.util.version;
 35  
 
 36  
 import fr.paris.lutece.portal.service.util.AppLogService;
 37  
 import java.util.regex.Matcher;
 38  
 import java.util.regex.Pattern;
 39  
 import java.util.ArrayList;
 40  
 import java.util.List;
 41  
 
 42  
 /**
 43  
  * Version
 44  
  */
 45  
 public class Version implements Comparable
 46  
 {
 47  
     public static final String NOT_AVAILABLE = "Not available";
 48  
     private static final String QUALIFIER_SNAPSHOT = "SNAPSHOT";
 49  
     private static final String QUALIFIER_CANDIDATE = "RC";
 50  
     private static final String PATTERN_NUMBER = "\\d+";
 51  
     private static final String QUALIFIER_VERSION_FORMAT = "%02d";
 52  
 
 53  
     private int _nMajor;
 54  
     private int _nMinor;
 55  
     private int _nPatch;
 56  
     private String _strQualifier;
 57  
     private String _strQualifierRadix;
 58  
     private int _nQualifierNumber;
 59  
 
 60  
     /** Constructor */
 61  
     public Version( )
 62  31
     {
 63  31
     }
 64  
 
 65  
     /**
 66  
      * Constructor
 67  
      * 
 68  
      * @param nMajor
 69  
      *            major digit
 70  
      * @param nMinor
 71  
      *            minor digit
 72  
      * @param nPatch
 73  
      *            patch digit
 74  
      * @param strQualifier
 75  
      *            qualifier
 76  
      */
 77  
     public Version( int nMajor, int nMinor, int nPatch, String strQualifier )
 78  44
     {
 79  44
         _nMajor = nMajor;
 80  44
         _nMinor = nMinor;
 81  44
         _nPatch = nPatch;
 82  44
         _strQualifier = strQualifier;
 83  44
     }
 84  
 
 85  
     /**
 86  
      * @return the nMajor
 87  
      */
 88  
     public int getMajor( )
 89  
     {
 90  0
         return _nMajor;
 91  
     }
 92  
 
 93  
     /**
 94  
      * @param nMajor
 95  
      *            the nMajor to set
 96  
      */
 97  
     public void setMajor( int nMajor )
 98  
     {
 99  31
         _nMajor = nMajor;
 100  31
     }
 101  
 
 102  
     /**
 103  
      * @return the nMinor
 104  
      */
 105  
     public int getMinor( )
 106  
     {
 107  0
         return _nMinor;
 108  
     }
 109  
 
 110  
     /**
 111  
      * @param nMinor
 112  
      *            the nMinor to set
 113  
      */
 114  
     public void setMinor( int nMinor )
 115  
     {
 116  31
         _nMinor = nMinor;
 117  31
     }
 118  
 
 119  
     /**
 120  
      * @return the nPatch
 121  
      */
 122  
     public int getPatch( )
 123  
     {
 124  0
         return _nPatch;
 125  
     }
 126  
 
 127  
     /**
 128  
      * @param nPatch
 129  
      *            the nPatch to set
 130  
      */
 131  
     public void setPatch( int nPatch )
 132  
     {
 133  29
         _nPatch = nPatch;
 134  29
     }
 135  
 
 136  
     /**
 137  
      * @return the Qualifier
 138  
      */
 139  
     public String getQualifier( )
 140  
     {
 141  0
         return _strQualifier;
 142  
     }
 143  
 
 144  
     /**
 145  
      * @param strQualifier
 146  
      *            the Qualifier to set
 147  
      */
 148  
     public void setQualifier( String strQualifier )
 149  
     {
 150  14
         Pattern pattern = Pattern.compile( PATTERN_NUMBER );
 151  14
         Matcher matcher = pattern.matcher( strQualifier );
 152  14
         if( matcher.find() )
 153  
         {
 154  8
             String strNumber = matcher.group();
 155  8
             _nQualifierNumber = Integer.parseInt( strNumber );
 156  8
             _strQualifierRadix = strQualifier.substring( 0 , strQualifier.indexOf( strNumber ));
 157  
         }
 158  14
         _strQualifier = strQualifier;
 159  14
     }
 160  
 
 161  
     @Override
 162  
     public int compareTo( Object object )
 163  
     {
 164  0
         Version version = (Version) object;
 165  0
         int nDiff = _nMajor - version.getMajor( );
 166  0
         if ( nDiff != 0 )
 167  
         {
 168  0
             return nDiff;
 169  
         }
 170  0
         nDiff = _nMinor - version.getMinor( );
 171  0
         if ( nDiff != 0 )
 172  
         {
 173  0
             return nDiff;
 174  
         }
 175  0
         nDiff = _nPatch - version.getPatch( );
 176  0
         return nDiff;
 177  
     }
 178  
 
 179  
     public String getVersion( )
 180  
     {
 181  54
         StringBuilder sbVersion = new StringBuilder( );
 182  54
         sbVersion.append( _nMajor ).append( '.' ).append( _nMinor ).append( '.' ).append( _nPatch );
 183  54
         if ( _strQualifier != null )
 184  
         {
 185  29
             if( _strQualifierRadix != null )
 186  
             {
 187  2
                 sbVersion.append( '-' ).append( _strQualifierRadix ).append( String.format( QUALIFIER_VERSION_FORMAT , _nQualifierNumber ));
 188  
             }
 189  
             else
 190  
             {
 191  27
                 sbVersion.append( '-' ).append( _strQualifier );
 192  
             }
 193  
         }
 194  54
         return sbVersion.toString( );
 195  
     }
 196  
 
 197  
     /**
 198  
      * {@inheritDoc }
 199  
      */
 200  
     @Override
 201  
     public String toString( )
 202  
     {
 203  0
         return getVersion( );
 204  
     }
 205  
 
 206  
     /**
 207  
      * Parse a string to extract version
 208  
      * 
 209  
      * @param strSource
 210  
      *            The source
 211  
      * @return The version object
 212  
      * @throws VersionParsingException
 213  
      *             if parsing failed
 214  
      */
 215  
     public static Version parse( String strSource ) throws VersionParsingException
 216  
     {
 217  31
         Version version = new Version( );
 218  
 
 219  
         try
 220  
         {
 221  31
             String strCurrent = strSource.trim( );
 222  
 
 223  
             // Search for qualifier
 224  31
             int nPos = strCurrent.indexOf( '-' );
 225  31
             if ( nPos != -1 )
 226  
             {
 227  14
                 version.setQualifier( strCurrent.substring( nPos + 1 ) );
 228  14
                 strCurrent = strCurrent.substring( 0, nPos );
 229  
             }
 230  
 
 231  
             // Search for major digits
 232  31
             nPos = strCurrent.indexOf( '.' );
 233  
 
 234  31
             String strMajor = strCurrent.substring( 0, nPos );
 235  31
             version.setMajor( Integer.parseInt( strMajor ) );
 236  
 
 237  
             // Search for minor digits
 238  31
             strCurrent = strCurrent.substring( nPos + 1 );
 239  31
             nPos = strCurrent.indexOf( '.' );
 240  
 
 241  31
             if ( nPos != -1 )
 242  
             {
 243  29
                 String strMinor = strCurrent.substring( 0, nPos );
 244  29
                 version.setMinor( Integer.parseInt( strMinor ) );
 245  
 
 246  29
                 strCurrent = strCurrent.substring( nPos + 1 );
 247  29
                 version.setPatch( Integer.parseInt( strCurrent ) );
 248  29
             }
 249  
             else
 250  
             {
 251  2
                 version.setMinor( Integer.parseInt( strCurrent ) );
 252  
             }
 253  
         }
 254  0
         catch( Exception e )
 255  
         {
 256  0
             throw new VersionParsingException( "Error parsing version : '" + strSource + "' : " + e.getMessage( ), e );
 257  31
         }
 258  31
         return version;
 259  
     }
 260  
 
 261  
     /**
 262  
      * returns the snapshot version 
 263  
      * @return the snapshot version 
 264  
      */
 265  
     public Version snapshot()
 266  
     {
 267  4
         return new Version( _nMajor, _nMinor, _nPatch, QUALIFIER_SNAPSHOT );
 268  
     }
 269  
 
 270  
     /**
 271  
      * Build a new version object with major digit incremented
 272  
      * 
 273  
      * @param bSnapshot
 274  
      *            if snapshot qualifier needed
 275  
      * @return The next version object
 276  
      */
 277  
     public Version nextMajor( boolean bSnapshot )
 278  
     {
 279  7
         String strQualifier = ( bSnapshot ) ? QUALIFIER_SNAPSHOT : null;
 280  
 
 281  7
         return new Version( _nMajor + 1, 0 , 0, strQualifier );
 282  
     }
 283  
 
 284  
     /**
 285  
      * Build a new version object with minor digit incremented
 286  
      * 
 287  
      * @param bSnapshot
 288  
      *            if snapshot qualifier needed
 289  
      * @return The next version object
 290  
      */
 291  
     public Version nextMinor( boolean bSnapshot )
 292  
     {
 293  7
         String strQualifier = ( bSnapshot ) ? QUALIFIER_SNAPSHOT : null;
 294  
 
 295  7
         return new Version( _nMajor, _nMinor + 1, 0 , strQualifier );
 296  
     }
 297  
 
 298  
     /**
 299  
      * Build a new version object with patch digit incremented
 300  
      * 
 301  
      * @param bSnapshot
 302  
      *            if snapshot qualifier needed
 303  
      * @return The next version object
 304  
      */
 305  
     public Version nextPatch( boolean bSnapshot )
 306  
     {
 307  12
         String strQualifier = ( bSnapshot ) ? QUALIFIER_SNAPSHOT : null;
 308  
 
 309  12
         return new Version( _nMajor, _nMinor, _nPatch + 1, strQualifier );
 310  
     }
 311  
 
 312  
     /**
 313  
      * Build a new version object with no qualifier
 314  
      * 
 315  
      * @return The next version object
 316  
      */
 317  
     public Version nextRelease( )
 318  
     {
 319  7
         int nPatch = ( isSnapshot() || isCandidate() ) ? _nPatch : _nPatch + 1;
 320  7
         return new Version( _nMajor, _nMinor, nPatch, null );
 321  
     }
 322  
 
 323  
     private Version nextCandidate()
 324  
     {
 325  
         String strQualifier;
 326  7
         int nPatch = _nPatch;
 327  7
         if( (_strQualifierRadix != null) && (_strQualifierRadix.equals( "RC-")) )
 328  
         {
 329  2
             strQualifier = String.format( "RC-%02d", _nQualifierNumber + 1 );
 330  
         }
 331  
         else
 332  
         {
 333  5
             if(  ! QUALIFIER_SNAPSHOT.equals( _strQualifier ))
 334  
             {
 335  1
                 nPatch += 1;
 336  
             }
 337  5
             strQualifier = "RC-01";
 338  
         }
 339  7
         return new Version( _nMajor, _nMinor, nPatch, strQualifier );
 340  
     }
 341  
     
 342  
     /**
 343  
      * Returns true if the version is qualified as Snapshot
 344  
      * @return true if the version is qualified as Snapshot
 345  
      */
 346  
     public boolean isSnapshot()
 347  
     {
 348  7
         return QUALIFIER_SNAPSHOT.equals( _strQualifier );
 349  
     }
 350  
     
 351  
     /**
 352  
      * Returns true if the version is qualified as Candidate
 353  
      * @return true if the version is qualified as Candidate
 354  
      */
 355  
     public boolean isCandidate()
 356  
     {
 357  22
         return ( _strQualifier != null ) && ( _strQualifier.startsWith( QUALIFIER_CANDIDATE ));
 358  
     }
 359  
     
 360  
     /**
 361  
      * Check if a given version is a SNAPSHOT
 362  
      * 
 363  
      * @param strVersion
 364  
      *            The version to check
 365  
      * @return True if snapshot otherwise false
 366  
      */
 367  
     public static boolean isSnapshot( String strVersion )
 368  
     {
 369  
         try
 370  
         {
 371  0
             Version version = parse( strVersion );
 372  0
             return version.isSnapshot();
 373  
         }
 374  0
         catch( VersionParsingException ex )
 375  
         {
 376  0
             AppLogService.error( "Error parsing version " + strVersion + " : " + ex.getMessage( ), ex );
 377  
         }
 378  0
         return false;
 379  
     }
 380  
 
 381  
     /**
 382  
      * Check if a given version is a RELEASE CANDIDATE
 383  
      * 
 384  
      * @param strVersion
 385  
      *            The version to check
 386  
      * @return True if candidate otherwise false
 387  
      */
 388  
     public static boolean isCandidate( String strVersion )
 389  
     {
 390  
         try
 391  
         {
 392  3
             Version version = parse( strVersion );
 393  3
             return version.isCandidate();
 394  
        }
 395  0
         catch( VersionParsingException ex )
 396  
         {
 397  0
             AppLogService.error( "Error parsing version " + strVersion + " : " + ex.getMessage( ), ex );
 398  
         }
 399  0
         return false;
 400  
     }
 401  
 
 402  
     /**
 403  
      * Get a list of next versions for a given version
 404  
      * @param strPreviousReleaseVersion The current version
 405  
      * @return The list
 406  
      */
 407  
     public static List<String> getNextReleaseVersions( String strPreviousReleaseVersion )
 408  
     {
 409  7
         List<String> listVersions = new ArrayList<>();
 410  
         try
 411  
         {
 412  7
             Version version = parse( strPreviousReleaseVersion );
 413  7
             listVersions.add( version.nextCandidate().getVersion() );
 414  7
             listVersions.add( version.nextRelease().getVersion() );
 415  7
             listVersions.add( version.nextMinor( false ).getVersion() );
 416  7
             listVersions.add( version.nextMajor( false ).getVersion() );
 417  
         }
 418  0
         catch( VersionParsingException ex )
 419  
         {
 420  0
             AppLogService.error( "Error parsing version " + strPreviousReleaseVersion + " : " + ex.getMessage( ), ex );
 421  7
         }
 422  7
         return listVersions;
 423  
     }
 424  
     
 425  
     /**
 426  
      * Get the next snapshot version for a given version
 427  
      * @param strVersion The current version
 428  
      * @return The version
 429  
      */
 430  
     public static String getNextSnapshotVersion( String strVersion )
 431  
     {
 432  16
         String strSnapshotVersion = NOT_AVAILABLE;
 433  
         try
 434  
         {
 435  16
             Version version = Version.parse( strVersion );
 436  16
             boolean bSnapshot = true;
 437  16
             if( version.isCandidate() )
 438  
             {
 439  4
                 strSnapshotVersion = version.snapshot( ).getVersion();
 440  
             }   
 441  
             else
 442  
             {
 443  12
                 strSnapshotVersion = version.nextPatch( bSnapshot ).getVersion();
 444  
             }
 445  
         }
 446  0
         catch( VersionParsingException ex )
 447  
         {
 448  0
             AppLogService.error( "Error parsing version " + strVersion + " : " + ex.getMessage( ), ex );
 449  16
         }
 450  16
         return strSnapshotVersion;
 451  
     }
 452  
     
 453  
     /**
 454  
      * Get the next release version for a given version
 455  
      * @param strVersion The current version
 456  
      * @return The version
 457  
      */
 458  
     public static String getReleaseVersion( String strVersion )
 459  
     {
 460  0
         String strTargetVersion = Version.NOT_AVAILABLE;
 461  
         try
 462  
         {
 463  0
             strTargetVersion = Version.parse( strVersion ).nextRelease( ).getVersion( );
 464  
         }
 465  0
         catch( VersionParsingException ex )
 466  
         {
 467  0
             AppLogService.error( "Error parsing version " + strVersion + " : " + ex.getMessage( ), ex );
 468  0
         }
 469  0
         return strTargetVersion;
 470  
     }
 471  
 
 472  
 
 473  
 }