From 4b386cdf97ebfbf0e9bb3909ce42b6d171628787 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Tue, 3 Mar 2009 14:58:44 +0000 Subject: [PATCH] o Added guard construct that marks a single test method as skipped (in contrast to matchesMavenVersion) git-svn-id: https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk@749623 13f79535-47bb-0310-9956-ffa450edef68 --- .../it/AbstractMavenIntegrationTestCase.java | 57 +++++++++++++++++++ .../it/MavenIntegrationTestCaseTest.java | 21 +++++++ 2 files changed, 78 insertions(+) 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 f89d2ed863..be70699b1a 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 @@ -177,6 +177,12 @@ public abstract class AbstractMavenIntegrationTestCase String result = "OK " + formatTime( milliseconds ); out.println( pad( RESULT_COLUMN - line.length() ) + result ); } + catch ( UnsupportedMavenVersionException e ) + { + String result = "SKIPPED - version " + e.mavenVersion + " not in range " + e.supportedRange; + out.println( pad( RESULT_COLUMN - line.length() ) + result ); + return; + } catch ( Throwable t ) { milliseconds = System.currentTimeMillis() - milliseconds; @@ -186,6 +192,57 @@ public abstract class AbstractMavenIntegrationTestCase } } + /** + * 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. + * + * @param versionRange The version range that specifies the acceptable Maven versions for the test, must not be + * null. + */ + protected void requiresMavenVersion( String versionRange ) + { + VersionRange range; + try + { + range = VersionRange.createFromVersionSpec( versionRange ); + } + catch ( InvalidVersionSpecificationException e ) + { + throw (RuntimeException) new IllegalArgumentException( "Invalid version range: " + versionRange ).initCause( e ); + } + + ArtifactVersion version = getMavenVersion(); + if ( version != null ) + { + if ( !range.containsVersion( removePattern( version ) ) ) + { + throw new UnsupportedMavenVersionException( version, range ); + } + } + else + { + out.println( "WARNING: " + getITName() + ": version range '" + versionRange + + "' supplied but no Maven version found - not skipping test." ); + } + } + + private class UnsupportedMavenVersionException + extends RuntimeException + { + + public ArtifactVersion mavenVersion; + + public VersionRange supportedRange; + + public UnsupportedMavenVersionException( ArtifactVersion mavenVersion, VersionRange supportedRange ) + { + this.mavenVersion = mavenVersion; + this.supportedRange = supportedRange; + } + + } + private String getITName() { String simpleName = getClass().getName(); diff --git a/its/core-it-support/maven-it-helper/src/test/java/org/apache/maven/it/MavenIntegrationTestCaseTest.java b/its/core-it-support/maven-it-helper/src/test/java/org/apache/maven/it/MavenIntegrationTestCaseTest.java index fc442e8b4f..69509cced7 100644 --- a/its/core-it-support/maven-it-helper/src/test/java/org/apache/maven/it/MavenIntegrationTestCaseTest.java +++ b/its/core-it-support/maven-it-helper/src/test/java/org/apache/maven/it/MavenIntegrationTestCaseTest.java @@ -49,4 +49,25 @@ public class MavenIntegrationTestCaseTest assertEquals( expected, test.removePattern( new DefaultArtifactVersion( version ) ).toString() ); } + public void testRequiresMavenVersion() + { + System.setProperty( "maven.version", "2.1" ); + + AbstractMavenIntegrationTestCase test = new AbstractMavenIntegrationTestCase( "[2.0,)" ) + { + // test case with version range + }; + + try + { + test.requiresMavenVersion( "[3.0,)" ); + } + catch ( RuntimeException e ) + { + // expected + } + + test.requiresMavenVersion( "[2.0,)" ); + } + }