Coverage Report - org.apache.maven.profiles.activation.SystemPropertyProfileActivator
 
Classes in this File Line Coverage Branch Coverage Complexity
SystemPropertyProfileActivator
83 %
25/30
64 %
14/22
5
 
 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 java.util.Properties;
 23  
 import org.apache.maven.model.Activation;
 24  
 import org.apache.maven.model.ActivationProperty;
 25  
 import org.apache.maven.model.Profile;
 26  
 import org.codehaus.plexus.context.Context;
 27  
 import org.codehaus.plexus.context.ContextException;
 28  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
 29  
 import org.codehaus.plexus.util.StringUtils;
 30  
 
 31  119
 public class SystemPropertyProfileActivator
 32  
     extends DetectedProfileActivator implements Contextualizable
 33  
 {
 34  
     private Properties properties;
 35  
     
 36  
     public void contextualize(Context context) throws ContextException 
 37  
     {
 38  119
         properties = (Properties)context.get("SystemProperties");
 39  119
     }
 40  
     
 41  
     protected boolean canDetectActivation( Profile profile )
 42  
     {
 43  117
         return profile.getActivation() != null && profile.getActivation().getProperty() != null;
 44  
     }
 45  
 
 46  
     public boolean isActive( Profile profile )
 47  
         throws ProfileActivationException
 48  
     {
 49  114
         Activation activation = profile.getActivation();
 50  
 
 51  114
         ActivationProperty property = activation.getProperty();
 52  
 
 53  114
         if ( property != null )
 54  
         {
 55  114
             String name = property.getName();
 56  114
             boolean reverseName = false;
 57  
             
 58  114
             if ( name == null )
 59  
             {
 60  0
                 throw new ProfileActivationException( "The property name is required to activate the profile '"
 61  
                     + profile.getId() + "'" );
 62  
             }
 63  
             
 64  114
             if ( name.startsWith("!") )
 65  
             {
 66  1
                 reverseName = true;
 67  1
                 name = name.substring( 1 );
 68  
             }
 69  
             
 70  114
             String sysValue = properties.getProperty( name );
 71  
 
 72  114
             String propValue = property.getValue();
 73  114
             if ( StringUtils.isNotEmpty( propValue ) )
 74  
             {
 75  111
                 boolean reverseValue = false;
 76  111
                 if ( propValue.startsWith( "!" ) )
 77  
                 {
 78  0
                     reverseValue = true;
 79  0
                     propValue = propValue.substring( 1 );
 80  
                 }
 81  
                 
 82  
                 // we have a value, so it has to match the system value...
 83  111
                 boolean result = propValue.equals( sysValue );
 84  
                 
 85  111
                 if ( reverseValue )
 86  
                 {
 87  0
                     return !result;
 88  
                 }
 89  
                 else
 90  
                 {
 91  111
                     return result;
 92  
                 }
 93  
             }
 94  
             else
 95  
             {
 96  3
                 boolean result = StringUtils.isNotEmpty( sysValue );
 97  
                 
 98  3
                 if ( reverseName )
 99  
                 {
 100  1
                     return !result;
 101  
                 }
 102  
                 else
 103  
                 {
 104  2
                     return result;
 105  
                 }
 106  
             }
 107  
         }
 108  
 
 109  0
         return false;
 110  
     }
 111  
 
 112  
 }