Author: bentmann Date: Fri May 1 11:24:53 2009 New Revision: 770638 URL: http://svn.apache.org/viewvc?rev=770638&view=rev Log: o Added support to skip tests based on Java version
Modified: maven/core-integration-testing/trunk/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java Modified: maven/core-integration-testing/trunk/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java?rev=770638&r1=770637&r2=770638&view=diff ============================================================================== --- maven/core-integration-testing/trunk/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java (original) +++ maven/core-integration-testing/trunk/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java Fri May 1 11:24:53 2009 @@ -61,6 +61,8 @@ private boolean skip; + private static ArtifactVersion javaVersion; + private ArtifactVersion mavenVersion; private VersionRange versionRange; @@ -102,6 +104,27 @@ } /** + * 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. * * @return The Maven version or <code>null</code> if unknown. @@ -177,6 +200,12 @@ 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; @@ -193,6 +222,33 @@ } /** + * 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 * would usually call this method right at the start of a test method. @@ -227,6 +283,22 @@ } } + 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 {