Coverage Report - org.apache.maven.profiles.activation.JdkPrefixProfileActivator
 
Classes in this File Line Coverage Branch Coverage Complexity
JdkPrefixProfileActivator
100 %
23/23
94 %
15/16
2,8
 
 1  
 package org.apache.maven.profiles.activation;
 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.maven.artifact.versioning.DefaultArtifactVersion;
 23  
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 24  
 import org.apache.maven.artifact.versioning.VersionRange;
 25  
 import org.apache.maven.model.Activation;
 26  
 import org.apache.maven.model.Profile;
 27  
 import org.codehaus.plexus.util.StringUtils;
 28  
 
 29  129
 public class JdkPrefixProfileActivator
 30  
     extends DetectedProfileActivator
 31  
 {
 32  1
     private static final String JDK_VERSION = System.getProperty( "java.version" );
 33  
 
 34  
     public boolean isActive( Profile profile )
 35  
         throws ProfileActivationException
 36  
     {
 37  8
         Activation activation = profile.getActivation();
 38  
 
 39  8
         String jdk = activation.getJdk();
 40  
 
 41  
         // null case is covered by canDetermineActivation(), so we can do a straight startsWith() here.
 42  8
         if ( jdk.startsWith( "[" ) || jdk.startsWith( "(" ) )
 43  
         {
 44  
             try
 45  
             {
 46  3
                 if ( matchJdkVersionRange( jdk ) )
 47  
                 {
 48  1
                     return true;
 49  
                 }
 50  
                 else
 51  
                 {
 52  1
                     return false;
 53  
                 }
 54  
             }
 55  1
             catch ( InvalidVersionSpecificationException e )
 56  
             {
 57  1
                 throw new ProfileActivationException( "Invalid JDK version in profile '" + profile.getId() + "': "
 58  
                     + e.getMessage() );
 59  
             }
 60  
         }
 61  
 
 62  5
         boolean reverse = false;
 63  
 
 64  5
         if ( jdk.startsWith( "!" ) )
 65  
         {
 66  2
             reverse = true;
 67  2
             jdk = jdk.substring( 1 );
 68  
         }
 69  
 
 70  5
         if ( getJdkVersion().startsWith( jdk ) )
 71  
         {
 72  2
             return !reverse;
 73  
         }
 74  
         else
 75  
         {
 76  3
             return reverse;
 77  
         }
 78  
     }
 79  
 
 80  
     private boolean matchJdkVersionRange( String jdk )
 81  
         throws InvalidVersionSpecificationException
 82  
     {
 83  3
         VersionRange jdkVersionRange = VersionRange.createFromVersionSpec( convertJdkToMavenVersion( jdk ) );
 84  2
         DefaultArtifactVersion jdkVersion = new DefaultArtifactVersion( convertJdkToMavenVersion( getJdkVersion() ) );
 85  2
         return jdkVersionRange.containsVersion( jdkVersion );
 86  
     }
 87  
 
 88  
     private String convertJdkToMavenVersion( String jdk )
 89  
     {
 90  5
         return jdk.replaceAll( "_", "-" );
 91  
     }
 92  
 
 93  
     protected String getJdkVersion()
 94  
     {
 95  1
         return JDK_VERSION;
 96  
     }
 97  
 
 98  
     protected boolean canDetectActivation( Profile profile )
 99  
     {
 100  121
         return profile.getActivation() != null && StringUtils.isNotEmpty( profile.getActivation().getJdk() );
 101  
     }
 102  
 
 103  
 }