Coverage Report - org.apache.maven.plugins.enforcer.RequireJavaVersion
 
Classes in this File Line Coverage Branch Coverage Complexity
RequireJavaVersion
0%
0/26
0%
0/8
3
 
 1  
 package org.apache.maven.plugins.enforcer;
 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.Arrays;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.commons.lang.SystemUtils;
 27  
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 28  
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 29  
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 30  
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
 31  
 import org.apache.maven.plugin.logging.Log;
 32  
 import org.codehaus.plexus.util.StringUtils;
 33  
 
 34  
 /**
 35  
  * This rule checks that the Java version is allowed.
 36  
  *
 37  
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 38  
  * @version $Id: RequireJavaVersion.java 1345283 2012-06-01 17:34:35Z rfscholte $
 39  
  */
 40  0
 public class RequireJavaVersion
 41  
     extends AbstractVersionEnforcer
 42  
 {
 43  
 
 44  
     /*
 45  
      * (non-Javadoc)
 46  
      *
 47  
      * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
 48  
      */
 49  
     public void execute( EnforcerRuleHelper helper )
 50  
         throws EnforcerRuleException
 51  
     {
 52  0
         String java_version = SystemUtils.JAVA_VERSION_TRIMMED;
 53  0
         Log log = helper.getLog();
 54  
 
 55  0
         log.debug( "Detected Java String: " + java_version );
 56  0
         java_version = normalizeJDKVersion( java_version );
 57  0
         log.debug( "Normalized Java String: " + java_version );
 58  
 
 59  0
         ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion( java_version );
 60  
 
 61  0
         log.debug( "Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: " +
 62  
             detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion() +
 63  
             " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: " + detectedJdkVersion.getQualifier() );
 64  
 
 65  0
         enforceVersion( helper.getLog(), "JDK", version, detectedJdkVersion );
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Converts a jdk string from 1.5.0-11b12 to a single 3 digit version like 1.5.0-11
 70  
      *
 71  
      * @param theJdkVersion to be converted.
 72  
      * @return the converted string.
 73  
      */
 74  
     public static String normalizeJDKVersion( String theJdkVersion )
 75  
     {
 76  
 
 77  0
         theJdkVersion = theJdkVersion.replaceAll( "_|-", "." );
 78  0
         String tokenArray[] = StringUtils.split( theJdkVersion, "." );
 79  0
         List<String> tokens = Arrays.asList( tokenArray );
 80  0
         StringBuffer buffer = new StringBuffer( theJdkVersion.length() );
 81  
 
 82  0
         Iterator<String> iter = tokens.iterator();
 83  0
         for ( int i = 0; i < tokens.size() && i < 4; i++ )
 84  
         {
 85  0
             String section = (String) iter.next();
 86  0
             section = section.replaceAll( "[^0-9]", "" );
 87  
 
 88  0
             if ( StringUtils.isNotEmpty( section ) )
 89  
             {
 90  0
                 buffer.append( Integer.parseInt( section ) );
 91  
 
 92  0
                 if ( i != 2 )
 93  
                 {
 94  0
                     buffer.append( '.' );
 95  
                 }
 96  
                 else
 97  
                 {
 98  0
                     buffer.append( '-' );
 99  
                 }
 100  
             }
 101  
         }
 102  
 
 103  0
         String version = buffer.toString();
 104  0
         version = StringUtils.stripEnd( version, "-" );
 105  0
         return StringUtils.stripEnd( version, "." );
 106  
     }
 107  
 }