mirror of https://github.com/apache/maven.git
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:
parent
61dd562ac3
commit
5c936d7675
|
@ -61,6 +61,8 @@ public abstract class AbstractMavenIntegrationTestCase
|
|||
|
||||
private boolean skip;
|
||||
|
||||
private static ArtifactVersion javaVersion;
|
||||
|
||||
private ArtifactVersion mavenVersion;
|
||||
|
||||
private VersionRange versionRange;
|
||||
|
@ -101,6 +103,27 @@ public abstract class AbstractMavenIntegrationTestCase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
@ -177,6 +200,12 @@ public abstract class AbstractMavenIntegrationTestCase
|
|||
String result = "OK " + formatTime( milliseconds );
|
||||
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 )
|
||||
{
|
||||
String result = "SKIPPED - version " + e.mavenVersion + " not in range " + e.supportedRange;
|
||||
|
@ -192,6 +221,33 @@ public abstract class AbstractMavenIntegrationTestCase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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 @@ public abstract class AbstractMavenIntegrationTestCase
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
extends RuntimeException
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue