o Added support to skip tests based on Java version

git-svn-id: https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk@770638 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-05-01 11:24:53 +00:00
parent 61dd562ac3
commit 5c936d7675

View File

@ -61,6 +61,8 @@ public abstract class AbstractMavenIntegrationTestCase
private boolean skip; private boolean skip;
private static ArtifactVersion javaVersion;
private ArtifactVersion mavenVersion; private ArtifactVersion mavenVersion;
private VersionRange versionRange; private VersionRange versionRange;
@ -101,6 +103,27 @@ protected AbstractMavenIntegrationTestCase( String versionRangeStr, String match
} }
} }
/**
* Gets the Java version used to run this test.
*
* @return The Java version, never <code>null</code>.
*/
private ArtifactVersion getJavaVersion()
{
if ( javaVersion == null )
{
String version = System.getProperty( "java.version" );
version = version.replaceAll( "[_-]", "." );
Matcher matcher = Pattern.compile( "(?s).*?(([0-9]+\\.[0-9]+)(\\.[0-9]+)?).*" ).matcher( version );
if ( matcher.matches() )
{
version = matcher.group( 1 );
}
javaVersion = new DefaultArtifactVersion( version );
}
return javaVersion;
}
/** /**
* Gets the Maven version used to run this test. * Gets the Maven version used to run this test.
* *
@ -177,6 +200,12 @@ protected void runTest()
String result = "OK " + formatTime( milliseconds ); String result = "OK " + formatTime( milliseconds );
out.println( pad( RESULT_COLUMN - line.length() ) + result ); out.println( pad( RESULT_COLUMN - line.length() ) + result );
} }
catch ( UnsupportedJavaVersionException e )
{
String result = "SKIPPED - Java version " + e.javaVersion + " not in range " + e.supportedRange;
out.println( pad( RESULT_COLUMN - line.length() ) + result );
return;
}
catch ( UnsupportedMavenVersionException e ) catch ( UnsupportedMavenVersionException e )
{ {
String result = "SKIPPED - version " + e.mavenVersion + " not in range " + e.supportedRange; String result = "SKIPPED - version " + e.mavenVersion + " not in range " + e.supportedRange;
@ -192,6 +221,33 @@ protected void runTest()
} }
} }
/**
* Guards the execution of a test case by checking that the current Java version matches the specified version
* range. If the check fails, an exception will be thrown which aborts the current test and marks it as skipped. One
* would usually call this method right at the start of a test method.
*
* @param versionRange The version range that specifies the acceptable Java versions for the test, must not be
* <code>null</code>.
*/
protected void requiresJavaVersion( String versionRange )
{
VersionRange range;
try
{
range = VersionRange.createFromVersionSpec( versionRange );
}
catch ( InvalidVersionSpecificationException e )
{
throw (RuntimeException) new IllegalArgumentException( "Invalid version range: " + versionRange ).initCause( e );
}
ArtifactVersion version = getJavaVersion();
if ( !range.containsVersion( version ) )
{
throw new UnsupportedJavaVersionException( version, range );
}
}
/** /**
* Guards the execution of a test case by checking that the current Maven version matches the specified version * Guards the execution of a test case by checking that the current Maven version matches the specified version
* range. If the check fails, an exception will be thrown which aborts the current test and marks it as skipped. One * range. If the check fails, an exception will be thrown which aborts the current test and marks it as skipped. One
@ -227,6 +283,22 @@ protected void requiresMavenVersion( String versionRange )
} }
} }
private class UnsupportedJavaVersionException
extends RuntimeException
{
public ArtifactVersion javaVersion;
public VersionRange supportedRange;
public UnsupportedJavaVersionException( ArtifactVersion javaVersion, VersionRange supportedRange )
{
this.javaVersion = javaVersion;
this.supportedRange = supportedRange;
}
}
private class UnsupportedMavenVersionException private class UnsupportedMavenVersionException
extends RuntimeException extends RuntimeException
{ {