Coverage Report - org.apache.maven.archiva.common.utils.VersionUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
VersionUtil
0%
0/38
0%
0/18
2.857
 
 1  
 package org.apache.maven.archiva.common.utils;
 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 org.apache.commons.lang.StringUtils;
 23  
 
 24  
 import java.util.regex.Matcher;
 25  
 import java.util.regex.Pattern;
 26  
 
 27  
 /**
 28  
  * Version utility methods. 
 29  
  *
 30  
  * @version $Id: VersionUtil.java 772151 2009-05-06 11:20:06Z oching $
 31  
  */
 32  0
 public class VersionUtil
 33  
 {
 34  
     /**
 35  
      * These are the version patterns found in the filenames of the various artifact's versions IDs.
 36  
      * These patterns are all tackling lowercase version IDs.
 37  
      */
 38  0
     private static final String versionPatterns[] = new String[] {
 39  
         "([0-9][_.0-9a-z]*)",
 40  
         "(snapshot)",
 41  
         "(g?[_.0-9ab]*(pre|rc|g|m)[_.0-9]*)",
 42  
         "(dev[_.0-9]*)",
 43  
         "(alpha[_.0-9]*)",
 44  
         "(beta[_.0-9]*)",
 45  
         "(rc[_.0-9]*)",
 46  
 //        "(test[_.0-9]*)", -- omitted for MRM-681, can be reinstated as part of MRM-712
 47  
         "(debug[_.0-9]*)",
 48  
         "(unofficial[_.0-9]*)",
 49  
         "(current)",
 50  
         "(latest)",
 51  
         "(fcs)",
 52  
         "(release[_.0-9]*)",
 53  
         "(nightly)",
 54  
         "(final)",
 55  
         "(incubating)",
 56  
         "(incubator)",
 57  
         "([ab][_.0-9]+)" };
 58  
 
 59  
     public static final String SNAPSHOT = "SNAPSHOT";
 60  
 
 61  0
     public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-([0-9]{8}\\.[0-9]{6})-([0-9]+)$" );
 62  
 
 63  0
     public static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "^([0-9]{8})\\.([0-9]{6})$" );
 64  
 
 65  0
     public static final Pattern GENERIC_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-" + SNAPSHOT );
 66  
 
 67  0
     private static final Pattern VERSION_MEGA_PATTERN = Pattern.compile( StringUtils.join( versionPatterns, '|' ), Pattern.CASE_INSENSITIVE );
 68  
 
 69  
     /**
 70  
      * <p>
 71  
      * Tests if the unknown string contains elements that identify it as a version string (or not).
 72  
      * </p>
 73  
      * 
 74  
      * <p>
 75  
      * The algorithm tests each part of the string that is delimited by a '-' (dash) character.
 76  
      * If 75% or more of the sections are identified as 'version' strings, the result is
 77  
      * determined to be of a high probability to be version identifier string.
 78  
      * </p>
 79  
      * 
 80  
      * @param unknown the unknown string to test.
 81  
      * @return true if the unknown string is likely a version string.
 82  
      */
 83  
     public static boolean isVersion( String unknown )
 84  
     {
 85  0
         String versionParts[] = StringUtils.split( unknown, '-' );
 86  
 
 87  
         Matcher mat;
 88  
 
 89  0
         int countValidParts = 0;
 90  
 
 91  0
         for ( int i = 0; i < versionParts.length; i++ )
 92  
         {
 93  0
             String part = versionParts[i];
 94  0
             mat = VERSION_MEGA_PATTERN.matcher( part );
 95  
 
 96  0
             if ( mat.matches() )
 97  
             {
 98  0
                 if ( i == 0 ) // loosen rule to return true if first token matches
 99  
                 {
 100  0
                     return true;
 101  
                 }
 102  0
                 countValidParts++;
 103  
             }
 104  
         }
 105  
 
 106  
         /* Calculate version probability as true if 3/4's of the input string has pieces of
 107  
          * of known version identifier strings.
 108  
          */
 109  0
         int threshold = (int) Math.floor( Math.max( (double) 1.0, (double) ( versionParts.length * 0.75 ) ) );
 110  
 
 111  0
         return ( countValidParts >= threshold );
 112  
     }
 113  
 
 114  
     /**
 115  
      * <p>
 116  
      * Tests if the identifier is a known simple version keyword.
 117  
      * </p>
 118  
      * 
 119  
      * <p>
 120  
      * This method is different from {@link #isVersion(String)} in that it tests the whole input string in
 121  
      * one go as a simple identifier. (eg "alpha", "1.0", "beta", "debug", "latest", "rc#", etc...)
 122  
      * </p>
 123  
      * 
 124  
      * @param identifier the identifier to test.
 125  
      * @return true if the unknown string is likely a version string.
 126  
      */
 127  
     public static boolean isSimpleVersionKeyword( String identifier )
 128  
     {
 129  0
         Matcher mat = VERSION_MEGA_PATTERN.matcher( identifier );
 130  
 
 131  0
         return mat.matches();
 132  
     }
 133  
 
 134  
     public static boolean isSnapshot( String version )
 135  
     {
 136  0
         Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
 137  0
         if ( m.matches() )
 138  
         {
 139  0
             return true;
 140  
         }
 141  
         else
 142  
         {
 143  0
             return isGenericSnapshot(version);
 144  
         }
 145  
     }
 146  
 
 147  
     public static String getBaseVersion( String version )
 148  
     {
 149  0
         Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
 150  0
         if ( m.matches() )
 151  
         {
 152  0
             return m.group( 1 ) + "-" + SNAPSHOT;
 153  
         }
 154  
         else
 155  
         {
 156  0
             return version;
 157  
         }
 158  
     }
 159  
     
 160  
     /**
 161  
      * <p>
 162  
      * Get the release version of the snapshot version.
 163  
      * </p>
 164  
      * 
 165  
      * <p>
 166  
      * If snapshot version is 1.0-SNAPSHOT, then release version would be 1.0
 167  
      * And if snapshot version is 1.0-20070113.163208-1.jar, then release version would still be 1.0
 168  
      * </p>
 169  
      * 
 170  
      * @param snapshotVersion
 171  
      * @return
 172  
      */
 173  
     public static String getReleaseVersion( String snapshotVersion )
 174  
     {
 175  0
         Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( snapshotVersion );
 176  
         
 177  0
         if( isGenericSnapshot( snapshotVersion ) )
 178  
         {
 179  0
             m = GENERIC_SNAPSHOT_PATTERN.matcher( snapshotVersion );
 180  
         }
 181  
                 
 182  0
         if ( m.matches() )
 183  
         {   
 184  0
             return m.group( 1 );
 185  
         }
 186  
         else
 187  
         {        
 188  0
             return snapshotVersion;
 189  
         }
 190  
     }
 191  
 
 192  
     public static boolean isUniqueSnapshot( String version )
 193  
     {             
 194  0
         Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
 195  0
         if( m.matches() )
 196  
         {
 197  0
             return true;
 198  
         }
 199  
 
 200  0
         return false;
 201  
     }
 202  
 
 203  
     public static boolean isGenericSnapshot( String version )
 204  
     {
 205  0
         return version.endsWith( SNAPSHOT );    
 206  
     }
 207  
 }