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
This commit is contained in:
John Dennis Casey 2007-06-01 19:12:58 +00:00
parent e12fbd08e9
commit 09553c6ff3
2 changed files with 135 additions and 13 deletions

View File

@ -24,10 +24,13 @@ import org.apache.maven.model.Activation;
import org.apache.maven.model.Profile; import org.apache.maven.model.Profile;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
public class JdkPrefixProfileActivator public class JdkPrefixProfileActivator
extends DetectedProfileActivator extends DetectedProfileActivator
{ {
public static final String JDK_VERSION = "java.version"; public static final String JDK_VERSION = "java.version";
public boolean isActive( Profile profile ) public boolean isActive( Profile profile )
@ -35,21 +38,39 @@ public class JdkPrefixProfileActivator
Activation activation = profile.getActivation(); Activation activation = profile.getActivation();
String jdk = activation.getJdk(); String jdk = activation.getJdk();
boolean reverse = false;
if ( jdk.startsWith( "!" ) )
{
reverse = true;
jdk = jdk.substring( 1 );
}
SystemBuildContext systemContext = SystemBuildContext.getSystemBuildContext( getBuildContextManager(), true ); SystemBuildContext systemContext = SystemBuildContext.getSystemBuildContext( getBuildContextManager(), true );
String javaVersion = systemContext.getSystemProperty( JDK_VERSION ); 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. // 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 ) if ( reverse )
{ {
return !result; return !result;
@ -62,7 +83,63 @@ public class JdkPrefixProfileActivator
protected boolean canDetectActivation( Profile profile ) 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;
}
} }

View File

@ -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+" ) );
}
}