diff --git a/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java b/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java index be70699b1a..e5bb0d7eac 100644 --- a/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java +++ b/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java @@ -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 null. + */ + 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 + * null. + */ + 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 {