Author: brett
Date: Wed Feb  4 14:21:03 2009
New Revision: 740753

URL: http://svn.apache.org/viewvc?rev=740753&view=rev
Log:
[MNG-1957] support Maven version ranges for JDK profile activation
Submitted by: Torben S. Giesselmann

Modified:
    
maven/components/branches/maven-2.1.x/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java
    
maven/components/branches/maven-2.1.x/maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java

Modified: 
maven/components/branches/maven-2.1.x/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java
URL: 
http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java?rev=740753&r1=740752&r2=740753&view=diff
==============================================================================
--- 
maven/components/branches/maven-2.1.x/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java
 (original)
+++ 
maven/components/branches/maven-2.1.x/maven-project/src/main/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivator.java
 Wed Feb  4 14:21:03 2009
@@ -19,6 +19,9 @@
  * under the License.
  */
 
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
+import 
org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.artifact.versioning.VersionRange;
 import org.apache.maven.model.Activation;
 import org.apache.maven.model.Profile;
 import org.codehaus.plexus.util.StringUtils;
@@ -29,32 +32,64 @@
     private static final String JDK_VERSION = System.getProperty( 
"java.version" );
 
     public boolean isActive( Profile profile )
+        throws ProfileActivationException
     {
         Activation activation = profile.getActivation();
 
         String jdk = activation.getJdk();
-        
+
+        // null case is covered by canDetermineActivation(), so we can do a 
straight startsWith() here.
+        if ( jdk.startsWith( "[" ) || jdk.startsWith( "(" ) )
+        {
+            try
+            {
+                if ( matchJdkVersionRange( jdk ) )
+                {
+                    return true;
+                }
+                else
+                {
+                    return false;
+                }
+            }
+            catch ( InvalidVersionSpecificationException e )
+            {
+                throw new ProfileActivationException( "Invalid JDK version in 
profile '" + profile.getId() + "': "
+                    + e.getMessage() );
+            }
+        }
+
         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.
-        boolean result = getJdkVersion().startsWith( jdk );
-        
-        if ( reverse )
+        if ( getJdkVersion().startsWith( jdk ) )
         {
-            return !result;
+            return !reverse;
         }
         else
         {
-            return result;
+            return reverse;
         }
     }
 
+    private boolean matchJdkVersionRange( String jdk )
+        throws InvalidVersionSpecificationException
+    {
+        VersionRange jdkVersionRange = VersionRange.createFromVersionSpec( 
convertJdkToMavenVersion( jdk ) );
+        DefaultArtifactVersion jdkVersion = new DefaultArtifactVersion( 
convertJdkToMavenVersion( getJdkVersion() ) );
+        return jdkVersionRange.containsVersion( jdkVersion );
+    }
+
+    private String convertJdkToMavenVersion( String jdk )
+    {
+        return jdk.replaceAll( "_", "-" );
+    }
+
     protected String getJdkVersion()
     {
         return JDK_VERSION;

Modified: 
maven/components/branches/maven-2.1.x/maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java
URL: 
http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java?rev=740753&r1=740752&r2=740753&view=diff
==============================================================================
--- 
maven/components/branches/maven-2.1.x/maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java
 (original)
+++ 
maven/components/branches/maven-2.1.x/maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java
 Wed Feb  4 14:21:03 2009
@@ -55,6 +55,7 @@
     }
 
     public void testActivationWithMatchingJdkVersion()
+        throws ProfileActivationException
     {
         JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
 
@@ -63,7 +64,8 @@
         assertTrue( activator.isActive( profile ) );
     }
 
-    public void testActivationWithoutMatchingJdkVersion()
+    public void testActivationWithDifferentJdkVersion()
+        throws ProfileActivationException
     {
         JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
 
@@ -73,6 +75,7 @@
     }
 
     public void testActivationWithMatchingNegatedJdkVersion()
+        throws ProfileActivationException
     {
         JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
 
@@ -81,7 +84,8 @@
         assertTrue( activator.isActive( profile ) );
     }
 
-    public void testActivationWithoutMatchingNegatedJdkVersion()
+    public void testActivationWithDifferentNegatedJdkVersion()
+        throws ProfileActivationException
     {
         JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
 
@@ -90,6 +94,43 @@
         assertFalse( activator.isActive( profile ) );
     }
 
+    public void testActivationWithMatchingRangeJdkVersion()
+        throws ProfileActivationException
+    {
+        JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
+
+        Profile profile = createProfile( "[1.4,1.6)" );
+
+        assertTrue( activator.isActive( profile ) );
+    }
+
+    public void testActivationWithDifferentRangeJdkVersion()
+        throws ProfileActivationException
+    {
+        JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
+
+        Profile profile = createProfile( "[1.4,1.5]" );
+
+        assertFalse( activator.isActive( profile ) );
+    }
+
+    public void testActivationWithBadRangeJdkVersion()
+        throws ProfileActivationException
+    {
+        JdkPrefixProfileActivator activator = createActivator( "1.5.0_15" );
+
+        Profile profile = createProfile( "[1.4," );
+        try
+        {
+            activator.isActive( profile );
+            fail( "Should have raised an exception for invalid format" );
+        }
+        catch ( ProfileActivationException e )
+        {
+            assertTrue( true );
+        }
+    }
+
     private static JdkPrefixProfileActivator createActivator()
     {
         return new JdkPrefixProfileActivator();


Reply via email to