From 09553c6ff3684449784be5e73551c8e8bd18431b Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Fri, 1 Jun 2007 19:12:58 +0000 Subject: [PATCH] REOPENED - issue MNG-1910: Allow jdk 1.4+ as profile requirement http://jira.codehaus.org/browse/MNG-1910 Applied. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@543579 13f79535-47bb-0310-9956-ffa450edef68 --- .../activation/JdkPrefixProfileActivator.java | 103 +++++++++++++++--- .../JdkPrefixProfileActivatorTest.java | 45 ++++++++ 2 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java 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 18ecf9a90c..d8a2ea8434 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 @@ -24,10 +24,13 @@ import org.apache.maven.model.Activation; import org.apache.maven.model.Profile; import org.codehaus.plexus.util.StringUtils; +import java.util.ArrayList; +import java.util.List; + public class JdkPrefixProfileActivator extends DetectedProfileActivator { - + public static final String JDK_VERSION = "java.version"; public boolean isActive( Profile profile ) @@ -35,21 +38,39 @@ public class JdkPrefixProfileActivator Activation activation = profile.getActivation(); String jdk = activation.getJdk(); - - boolean reverse = false; - - if ( jdk.startsWith( "!" ) ) - { - reverse = true; - jdk = jdk.substring( 1 ); - } - + SystemBuildContext systemContext = SystemBuildContext.getSystemBuildContext( getBuildContextManager(), true ); String javaVersion = systemContext.getSystemProperty( JDK_VERSION ); + return isActive( javaVersion, jdk ); + } + + public boolean isActive( String jdkVersion, String expression ) + { + boolean reverse = false; + + if ( expression.startsWith( "!" ) ) + { + reverse = true; + expression = expression.substring( 1 ); + } + // null case is covered by canDetermineActivation(), so we can do a straight startsWith() here. - boolean result = javaVersion.startsWith( jdk ); - + boolean result = false; + if ( expression.endsWith( "+" ) ) + { + result = compareTo( asIntArray( jdkVersion ), asIntArray( expression ) ) >= 0; + } + else if ( expression.endsWith( "-" ) ) + { + result = compareTo( asIntArray( jdkVersion ), asIntArray( expression ) ) <= 0; + } + else + { + // null case is covered by canDetermineActivation(), so we can do a straight startsWith() here. + result = jdkVersion.startsWith( expression ); + } + if ( reverse ) { return !result; @@ -62,7 +83,63 @@ public class JdkPrefixProfileActivator protected boolean canDetectActivation( Profile profile ) { - return profile.getActivation() != null && StringUtils.isNotEmpty( profile.getActivation().getJdk() ); + return ( profile.getActivation() != null ) && StringUtils.isNotEmpty( profile.getActivation().getJdk() ); } + private static void parseNum( List pList, StringBuffer pBuffer ) + { + if ( pBuffer.length() > 0 ) + { + pList.add( new Integer( pBuffer.toString() ) ); + pBuffer.setLength( 0 ); + } + } + + /** This method transforms a string like "1.5.0_06" into + * new int[]{1, 5, 0, 6}. + */ + private static int[] asIntArray( String pVersion ) + { + List nums = new ArrayList(); + StringBuffer sb = new StringBuffer(); + while ( pVersion.length() > 0 ) + { + char c = pVersion.charAt( 0 ); + pVersion = pVersion.substring( 1 ); + if ( Character.isDigit( c ) ) + { + sb.append( c ); + } + else + { + parseNum( nums, sb ); + } + } + parseNum( nums, sb ); + int[] result = new int[nums.size()]; + for ( int i = 0; i < result.length; i++ ) + { + result[i] = ( (Integer) nums.get( i ) ).intValue(); + } + return result; + } + + /** This method compares to integer arrays, as created + * by {@link #asIntArray(String)}. + */ + private static int compareTo( int[] pVersion1, int[] pVersion2 ) + { + int len = Math.max( pVersion1.length, pVersion2.length ); + for ( int i = 0; i < len; i++ ) + { + int n1 = pVersion1.length > i ? pVersion1[i] : 0; + int n2 = pVersion2.length > i ? pVersion2[i] : 0; + int result = n1 - n2; + if ( result != 0 ) + { + return result; + } + } + return 0; + } } diff --git a/maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java b/maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java new file mode 100644 index 0000000000..6569823480 --- /dev/null +++ b/maven-project/src/test/java/org/apache/maven/profiles/activation/JdkPrefixProfileActivatorTest.java @@ -0,0 +1,45 @@ +package org.apache.maven.profiles.activation; + +import junit.framework.TestCase; + + +/** Test case for the {@link JdkPrefixProfileActivator}. + */ +public class JdkPrefixProfileActivatorTest extends TestCase +{ + /** Test for the basic match "equals". + */ + public void testBasicMatch() + { + JdkPrefixProfileActivator activator = new JdkPrefixProfileActivator(); + assertTrue( activator.isActive( "1.5", "1.5" ) ); + assertFalse( activator.isActive( "1.4", "1.5" ) ); + assertFalse( activator.isActive( "1.6", "1.5" ) ); + assertTrue( activator.isActive( "1.5.0_06", "1.5" ) ); + assertFalse( activator.isActive( "1.5.0_06", "1.5.1" ) ); + assertFalse( activator.isActive( "1.5", "!1.5" ) ); + assertTrue( activator.isActive( "1.4", "!1.5" ) ); + assertTrue( activator.isActive( "1.6", "!1.5" ) ); + assertFalse( activator.isActive( "1.5.0_06", "!1.5" ) ); + assertTrue( activator.isActive( "1.5.0_06", "!1.5.1" ) ); + } + + /** Test for the match "greather than or equal". + */ + public void testGreatherThanOrEqualMatch() + { + JdkPrefixProfileActivator activator = new JdkPrefixProfileActivator(); + assertTrue( activator.isActive( "1.5.0_06", "1.5+" ) ); + assertFalse( activator.isActive( "1.5.0_06", "!1.5+" ) ); + assertTrue( activator.isActive( "1.5.0_06", "1.4+" ) ); + assertFalse( activator.isActive( "1.5.0_06", "!1.4+" ) ); + assertFalse( activator.isActive( "1.5.0_06", "1.6+" ) ); + assertTrue( activator.isActive( "1.5.0_06", "!1.6+" ) ); + assertTrue( activator.isActive( "1.5.0_06", "1.5.0.0+" ) ); + assertFalse( activator.isActive( "1.5.0_06", "!1.5.0.0+" ) ); + assertTrue( activator.isActive( "1.5.0_06", "1.5.0.6+" ) ); + assertFalse( activator.isActive( "1.5.0_06", "!1.5.0.6+" ) ); + assertFalse( activator.isActive( "1.5.0_06", "1.5.0.7+" ) ); + assertTrue( activator.isActive( "1.5.0_06", "!1.5.0.7+" ) ); + } +}