From c9b6d83c5730a857defe2960ff0aed878ded8c4a Mon Sep 17 00:00:00 2001
From: John Dennis Casey <jdcasey@apache.org>
Date: Tue, 13 Sep 2005 03:23:16 +0000
Subject: [PATCH] Resolving: MNG-835. Using activateByDefault within activation
 in the profile.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@280476 13f79535-47bb-0310-9956-ffa450edef68
---
 maven-model/maven.mdo                         |   6 +
 maven-profile/profiles.mdo                    |   6 +
 .../profiles/ProfilesConversionUtils.java     |   2 +
 .../maven/profiles/DefaultProfileManager.java |  26 +++
 .../apache/maven/profiles/ProfileManager.java |   2 +
 .../activation/JdkPrefixProfileActivator.java |  19 ++-
 .../SystemPropertyProfileActivator.java       |  40 ++++-
 .../project/DefaultMavenProjectBuilder.java   |  11 +-
 .../profiles/DefaultProfileManagerTest.java   | 159 ++++++++++++++++++
 maven-settings/settings.mdo                   |   6 +
 .../apache/maven/settings/SettingsUtils.java  |   2 +
 11 files changed, 274 insertions(+), 5 deletions(-)
 create mode 100644 maven-project/src/test/java/org/apache/maven/profiles/DefaultProfileManagerTest.java

diff --git a/maven-model/maven.mdo b/maven-model/maven.mdo
index 4bc6bb567d..a1b13abbff 100644
--- a/maven-model/maven.mdo
+++ b/maven-model/maven.mdo
@@ -2610,6 +2610,12 @@
         the automatic inclusion of the parent build profile.
       ]]></description>
       <fields>
+        <field>
+          <name>activeByDefault</name>
+          <version>4.0.0</version>
+          <type>boolean</type>
+          <description>Flag specifying whether this profile is active as a default.</description>
+        </field>
         <field>
           <name>jdk</name>
           <version>4.0.0</version>
diff --git a/maven-profile/profiles.mdo b/maven-profile/profiles.mdo
index b32d5b6e9d..d0a3fad2c4 100644
--- a/maven-profile/profiles.mdo
+++ b/maven-profile/profiles.mdo
@@ -112,6 +112,12 @@
         the automatic inclusion of the parent build profile.
       ]]></description>
       <fields>
+        <field>
+          <name>activeByDefault</name>
+          <version>1.0.0</version>
+          <type>boolean</type>
+          <description>Flag specifying whether this profile is active as a default.</description>
+        </field>
         <field>
           <name>jdk</name>
           <version>1.0.0</version>
diff --git a/maven-profile/src/main/java/org/apache/maven/profiles/ProfilesConversionUtils.java b/maven-profile/src/main/java/org/apache/maven/profiles/ProfilesConversionUtils.java
index f91760c3f5..d2940e5baa 100644
--- a/maven-profile/src/main/java/org/apache/maven/profiles/ProfilesConversionUtils.java
+++ b/maven-profile/src/main/java/org/apache/maven/profiles/ProfilesConversionUtils.java
@@ -44,6 +44,8 @@ public class ProfilesConversionUtils
         if ( profileActivation != null )
         {
             Activation activation = new Activation();
+            
+            activation.setActiveByDefault( profileActivation.isActiveByDefault() );
 
             activation.setJdk( profileActivation.getJdk() );
             
diff --git a/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java b/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java
index afa732c5a5..22f7e89d0c 100644
--- a/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java
+++ b/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java
@@ -1,5 +1,6 @@
 package org.apache.maven.profiles;
 
+import org.apache.maven.model.Activation;
 import org.apache.maven.model.Profile;
 import org.apache.maven.profiles.activation.ProfileActivationException;
 import org.apache.maven.profiles.activation.ProfileActivator;
@@ -38,6 +39,7 @@ public class DefaultProfileManager implements ProfileManager
 
     private Set activatedIds = new HashSet();
     private Set deactivatedIds = new HashSet();
+    private Set defaultIds = new HashSet();
     
     private Map profilesById = new HashMap();
     
@@ -86,6 +88,13 @@ public class DefaultProfileManager implements ProfileManager
         }
         
         profilesById.put( profile.getId(), profile );
+        
+        Activation activation = profile.getActivation();
+        
+        if ( activation != null && activation.isActiveByDefault() )
+        {
+            activateAsDefault( profileId );
+        }
     }
     
     /* (non-Javadoc)
@@ -158,6 +167,18 @@ public class DefaultProfileManager implements ProfileManager
             }
         }
         
+        if ( active.isEmpty() )
+        {
+            for ( Iterator it = defaultIds.iterator(); it.hasNext(); )
+            {
+                String profileId = (String) it.next();
+                
+                Profile profile = (Profile) profilesById.get( profileId );
+                
+                active.add( profile );
+            }
+        }
+        
         return active;
     }
     
@@ -210,5 +231,10 @@ public class DefaultProfileManager implements ProfileManager
             addProfile( profile );
         }
     }
+
+    public void activateAsDefault( String profileId )
+    {
+        defaultIds.add( profileId );
+    }
     
 }
diff --git a/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java b/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java
index aa8ec1fd6c..0ee0c7a2bd 100644
--- a/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java
+++ b/maven-project/src/main/java/org/apache/maven/profiles/ProfileManager.java
@@ -19,6 +19,8 @@ public interface ProfileManager
     void explicitlyDeactivate( String profileId );
 
     void explicitlyDeactivate( List profileIds );
+    
+    void activateAsDefault( String profileId );
 
     List getActiveProfiles()
         throws ProfileActivationException;
diff --git a/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java b/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java
index 82cac4a1ea..b5abaed151 100644
--- a/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java
+++ b/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java
@@ -30,9 +30,26 @@ public class JdkPrefixProfileActivator
         Activation activation = profile.getActivation();
 
         String jdk = activation.getJdk();
+        
+        boolean reverse = false;
+        
+        if ( jdk.startsWith( "!" ) )
+        {
+            reverse = true;
+            jdk = jdk.substring( 1 );
+        }
 
         // null case is covered by canDetermineActivation(), so we can do a straight startsWith() here.
-        return JDK_VERSION.startsWith( jdk );
+        boolean result = JDK_VERSION.startsWith( jdk );
+        
+        if ( reverse )
+        {
+            return !result;
+        }
+        else
+        {
+            return result;
+        }
     }
 
     protected boolean canDetectActivation( Profile profile )
diff --git a/maven-project/src/main/java/org/apache/maven/profiles/activation/SystemPropertyProfileActivator.java b/maven-project/src/main/java/org/apache/maven/profiles/activation/SystemPropertyProfileActivator.java
index 1c162cb5c5..d1d0c337a9 100644
--- a/maven-project/src/main/java/org/apache/maven/profiles/activation/SystemPropertyProfileActivator.java
+++ b/maven-project/src/main/java/org/apache/maven/profiles/activation/SystemPropertyProfileActivator.java
@@ -37,17 +37,51 @@ public class SystemPropertyProfileActivator
 
         if ( property != null )
         {
-            String sysValue = System.getProperty( property.getName() );
+            String name = property.getName();
+            boolean reverseName = false;
+            
+            if ( name.startsWith("!") )
+            {
+                reverseName = true;
+                name = name.substring( 1 );
+            }
+            
+            String sysValue = System.getProperty( name );
 
             String propValue = property.getValue();
             if ( StringUtils.isNotEmpty( propValue ) )
             {
+                boolean reverseValue = false;
+                if ( propValue.startsWith( "!" ) )
+                {
+                    reverseValue = true;
+                    propValue = propValue.substring( 1 );
+                }
+                
                 // we have a value, so it has to match the system value...
-                return propValue.equals( sysValue );
+                boolean result = propValue.equals( sysValue );
+                
+                if ( reverseValue )
+                {
+                    return !result;
+                }
+                else
+                {
+                    return result;
+                }
             }
             else
             {
-                return StringUtils.isNotEmpty( sysValue );
+                boolean result = StringUtils.isNotEmpty( sysValue );
+                
+                if ( reverseName )
+                {
+                    return !result;
+                }
+                else
+                {
+                    return result;
+                }
             }
         }
 
diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java
index 32037523d0..981e22bdb9 100644
--- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java
+++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java
@@ -861,11 +861,20 @@ public class DefaultMavenProjectBuilder
 
                 if ( root != null )
                 {
+                    List active = root.getActiveProfiles();
+                    
+                    if( active != null && !active.isEmpty() )
+                    {
+                        profileManager.explicitlyActivate( root.getActiveProfiles() );
+                    }
+                    
                     for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); )
                     {
                         org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next();
 
-                        profileManager.addProfile( ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile ) );
+                        Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile );
+                        
+                        profileManager.addProfile( converted );
                     }
                 }
             }
diff --git a/maven-project/src/test/java/org/apache/maven/profiles/DefaultProfileManagerTest.java b/maven-project/src/test/java/org/apache/maven/profiles/DefaultProfileManagerTest.java
new file mode 100644
index 0000000000..19b6c733c5
--- /dev/null
+++ b/maven-project/src/test/java/org/apache/maven/profiles/DefaultProfileManagerTest.java
@@ -0,0 +1,159 @@
+package org.apache.maven.profiles;
+
+import org.apache.maven.model.Activation;
+import org.apache.maven.model.ActivationProperty;
+import org.apache.maven.model.Profile;
+import org.apache.maven.profiles.activation.ProfileActivationException;
+import org.codehaus.plexus.PlexusTestCase;
+
+import java.util.List;
+
+public class DefaultProfileManagerTest
+    extends PlexusTestCase
+{
+    
+    public void testShouldActivateDefaultProfile() throws ProfileActivationException
+    {
+        Profile notActivated = new Profile();
+        notActivated.setId("notActivated");
+        
+        Activation nonActivation = new Activation();
+        
+        nonActivation.setJdk("19.2");
+        
+        notActivated.setActivation( nonActivation );
+        
+        Profile defaultActivated = new Profile();
+        defaultActivated.setId("defaultActivated");
+        
+        Activation defaultActivation = new Activation();
+        
+        defaultActivation.setActiveByDefault(true);
+        
+        defaultActivated.setActivation( defaultActivation );
+        
+        ProfileManager profileManager = new DefaultProfileManager(getContainer());
+        
+        profileManager.addProfile(notActivated);
+        profileManager.addProfile(defaultActivated);
+        
+        List active = profileManager.getActiveProfiles();
+        
+        assertNotNull( active );
+        assertEquals( 1, active.size() );
+        assertEquals("defaultActivated", ((Profile)active.get(0)).getId());
+    }
+
+    public void testShouldNotActivateDefaultProfile() throws ProfileActivationException
+    {
+        Profile syspropActivated = new Profile();
+        syspropActivated.setId("syspropActivated");
+        
+        Activation syspropActivation = new Activation();
+        
+        ActivationProperty syspropProperty = new ActivationProperty();
+        syspropProperty.setName("java.version");
+        
+        syspropActivation.setProperty(syspropProperty);
+        
+        syspropActivated.setActivation( syspropActivation );
+        
+        Profile defaultActivated = new Profile();
+        defaultActivated.setId("defaultActivated");
+        
+        Activation defaultActivation = new Activation();
+        
+        defaultActivation.setActiveByDefault(true);
+        
+        defaultActivated.setActivation( defaultActivation );
+        
+        ProfileManager profileManager = new DefaultProfileManager(getContainer());
+        
+        profileManager.addProfile(syspropActivated);
+        profileManager.addProfile(defaultActivated);
+        
+        List active = profileManager.getActiveProfiles();
+        
+        assertNotNull( active );
+        assertEquals( 1, active.size() );
+        assertEquals("syspropActivated", ((Profile)active.get(0)).getId());
+    }
+
+    public void testShouldNotActivateReversalOfPresentSystemProperty() throws ProfileActivationException
+    {
+        Profile syspropActivated = new Profile();
+        syspropActivated.setId("syspropActivated");
+        
+        Activation syspropActivation = new Activation();
+        
+        ActivationProperty syspropProperty = new ActivationProperty();
+        syspropProperty.setName("!java.version");
+        
+        syspropActivation.setProperty(syspropProperty);
+        
+        syspropActivated.setActivation( syspropActivation );
+        
+        ProfileManager profileManager = new DefaultProfileManager(getContainer());
+        
+        profileManager.addProfile(syspropActivated);
+        
+        List active = profileManager.getActiveProfiles();
+        
+        assertNotNull( active );
+        assertEquals( 0, active.size() );
+    }
+
+    public void testShouldOverrideAndActivateInactiveProfile() throws ProfileActivationException
+    {
+        Profile syspropActivated = new Profile();
+        syspropActivated.setId("syspropActivated");
+        
+        Activation syspropActivation = new Activation();
+        
+        ActivationProperty syspropProperty = new ActivationProperty();
+        syspropProperty.setName("!java.version");
+        
+        syspropActivation.setProperty(syspropProperty);
+        
+        syspropActivated.setActivation( syspropActivation );
+        
+        ProfileManager profileManager = new DefaultProfileManager(getContainer());
+        
+        profileManager.addProfile(syspropActivated);
+        
+        profileManager.explicitlyActivate("syspropActivated");
+        
+        List active = profileManager.getActiveProfiles();
+        
+        assertNotNull( active );
+        assertEquals( 1, active.size() );
+        assertEquals( "syspropActivated", ((Profile)active.get(0)).getId());
+    }
+
+    public void testShouldOverrideAndDeactivateActiveProfile() throws ProfileActivationException
+    {
+        Profile syspropActivated = new Profile();
+        syspropActivated.setId("syspropActivated");
+        
+        Activation syspropActivation = new Activation();
+        
+        ActivationProperty syspropProperty = new ActivationProperty();
+        syspropProperty.setName("java.version");
+        
+        syspropActivation.setProperty(syspropProperty);
+        
+        syspropActivated.setActivation( syspropActivation );
+        
+        ProfileManager profileManager = new DefaultProfileManager(getContainer());
+        
+        profileManager.addProfile(syspropActivated);
+        
+        profileManager.explicitlyDeactivate("syspropActivated");
+        
+        List active = profileManager.getActiveProfiles();
+        
+        assertNotNull( active );
+        assertEquals( 0, active.size() );
+    }
+
+}
diff --git a/maven-settings/settings.mdo b/maven-settings/settings.mdo
index f48142d747..8c49817c6b 100644
--- a/maven-settings/settings.mdo
+++ b/maven-settings/settings.mdo
@@ -559,6 +559,12 @@
         the automatic inclusion of the parent build profile.
       ]]></description>
       <fields>
+        <field>
+          <name>activeByDefault</name>
+          <version>1.0.0</version>
+          <type>boolean</type>
+          <description>Flag specifying whether this profile is active as a default.</description>
+        </field>
         <field>
           <name>jdk</name>
           <version>1.0.0</version>
diff --git a/maven-settings/src/main/java/org/apache/maven/settings/SettingsUtils.java b/maven-settings/src/main/java/org/apache/maven/settings/SettingsUtils.java
index efacc98fe7..615816cb0d 100644
--- a/maven-settings/src/main/java/org/apache/maven/settings/SettingsUtils.java
+++ b/maven-settings/src/main/java/org/apache/maven/settings/SettingsUtils.java
@@ -147,6 +147,8 @@ public final class SettingsUtils
         {
             org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation();
 
+            activation.setActiveByDefault( settingsActivation.isActiveByDefault() );
+            
             activation.setJdk( settingsActivation.getJdk() );
 
             ActivationProperty settingsProp = settingsActivation.getProperty();