-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support arbitrary Java versions with JRE conditions
Prior to JUnit Jupiter 5.12, JRE-based conditions could only rely on predefined constants in the JRE enum. Furthermore, those constants are only updated as long as the particular JUnit Jupiter branch is supported. For example, once JUnit Jupiter 5.12 is released, there is no guarantee that the JRE enum constants will be updated in the 5.11.x branch. Consequently, users previously did not have the ability to enable or disable tests for Java versions released after a particular JUnit Jupiter branch was no longer supported. To address that, this commit introduces support for arbitrary Java versions in the JRE enum and related condition annotations. Users can now specify arbitrary Java versions via the `versions` attributes in @EnabledOnJre and @DisabledOnJre and via the `minVersion` and `maxVersion` attributes in @EnabledForJreRange and @DisabledForJreRange. Closes: #3930 Closes: #3931
- Loading branch information
Showing
29 changed files
with
1,580 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/AbstractJreCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.jupiter.api.condition; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Arrays; | ||
import java.util.function.Function; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.platform.commons.util.Preconditions; | ||
|
||
/** | ||
* Abstract base class for {@link EnabledOnJreCondition} and | ||
* {@link DisabledOnJreCondition}. | ||
* | ||
* @since 5.12 | ||
*/ | ||
abstract class AbstractJreCondition<A extends Annotation> extends BooleanExecutionCondition<A> { | ||
|
||
static final String ENABLED_ON_CURRENT_JRE = // | ||
"Enabled on JRE version: " + System.getProperty("java.version"); | ||
|
||
static final String DISABLED_ON_CURRENT_JRE = // | ||
"Disabled on JRE version: " + System.getProperty("java.version"); | ||
|
||
private final String annotationName; | ||
|
||
AbstractJreCondition(Class<A> annotationType, Function<A, String> customDisabledReason) { | ||
super(annotationType, ENABLED_ON_CURRENT_JRE, DISABLED_ON_CURRENT_JRE, customDisabledReason); | ||
this.annotationName = annotationType.getSimpleName(); | ||
} | ||
|
||
protected final IntStream validatedVersions(JRE[] jres, int[] versions) { | ||
Preconditions.condition(jres.length > 0 || versions.length > 0, | ||
() -> "You must declare at least one JRE or version in @" + this.annotationName); | ||
|
||
return IntStream.concat(// | ||
Arrays.stream(jres).mapToInt(jre -> { | ||
Preconditions.condition(jre != JRE.UNDEFINED, | ||
() -> "JRE.UNDEFINED is not supported in @" + this.annotationName); | ||
return jre.version(); | ||
}), // | ||
Arrays.stream(versions).map(version -> { | ||
Preconditions.condition(version >= JRE.MINIMUM_VERSION, | ||
() -> String.format("Version [%d] in @%s must be greater than or equal to %d", version, | ||
this.annotationName, JRE.MINIMUM_VERSION)); | ||
return version; | ||
})// | ||
); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...-jupiter-api/src/main/java/org/junit/jupiter/api/condition/AbstractJreRangeCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.jupiter.api.condition; | ||
|
||
import static org.junit.jupiter.api.condition.AbstractJreCondition.DISABLED_ON_CURRENT_JRE; | ||
import static org.junit.jupiter.api.condition.AbstractJreCondition.ENABLED_ON_CURRENT_JRE; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.function.Function; | ||
|
||
import org.junit.platform.commons.util.Preconditions; | ||
|
||
/** | ||
* Abstract base class for {@link EnabledForJreRangeCondition} and | ||
* {@link DisabledForJreRangeCondition}. | ||
* | ||
* @since 5.12 | ||
*/ | ||
abstract class AbstractJreRangeCondition<A extends Annotation> extends BooleanExecutionCondition<A> { | ||
|
||
private final String annotationName; | ||
|
||
AbstractJreRangeCondition(Class<A> annotationType, Function<A, String> customDisabledReason) { | ||
super(annotationType, ENABLED_ON_CURRENT_JRE, DISABLED_ON_CURRENT_JRE, customDisabledReason); | ||
this.annotationName = annotationType.getSimpleName(); | ||
} | ||
|
||
protected final boolean isCurrentVersionWithinRange(JRE minJre, JRE maxJre, int minVersion, int maxVersion) { | ||
boolean minJreSet = minJre != JRE.UNDEFINED; | ||
boolean maxJreSet = maxJre != JRE.UNDEFINED; | ||
boolean minVersionSet = minVersion != JRE.UNDEFINED_VERSION; | ||
boolean maxVersionSet = maxVersion != JRE.UNDEFINED_VERSION; | ||
|
||
// Users must choose between JRE enum constants and version numbers. | ||
Preconditions.condition(!minJreSet || !minVersionSet, () -> String.format( | ||
"@%s's minimum value must be configured with either a JRE enum constant or numeric version, but not both", | ||
this.annotationName)); | ||
Preconditions.condition(!maxJreSet || !maxVersionSet, () -> String.format( | ||
"@%s's maximum value must be configured with either a JRE enum constant or numeric version, but not both", | ||
this.annotationName)); | ||
|
||
// Users must supply valid values for minVersion and maxVersion. | ||
Preconditions.condition(!minVersionSet || (minVersion >= JRE.MINIMUM_VERSION), | ||
() -> String.format("@%s's minVersion [%d] must be greater than or equal to %d", this.annotationName, | ||
minVersion, JRE.MINIMUM_VERSION)); | ||
Preconditions.condition(!maxVersionSet || (maxVersion >= JRE.MINIMUM_VERSION), | ||
() -> String.format("@%s's maxVersion [%d] must be greater than or equal to %d", this.annotationName, | ||
maxVersion, JRE.MINIMUM_VERSION)); | ||
|
||
// Now that we have checked the basic preconditions, we need to ensure that we are | ||
// using valid JRE enum constants. | ||
if (!minJreSet) { | ||
minJre = JRE.JAVA_8; | ||
} | ||
if (!maxJreSet) { | ||
maxJre = JRE.OTHER; | ||
} | ||
|
||
int min = (minVersionSet ? minVersion : minJre.version()); | ||
int max = (maxVersionSet ? maxVersion : maxJre.version()); | ||
|
||
// Finally, we need to validate the effective minimum and maximum values. | ||
Preconditions.condition((min != JRE.MINIMUM_VERSION || max != Integer.MAX_VALUE), | ||
() -> "You must declare a non-default value for the minimum or maximum value in @" + this.annotationName); | ||
Preconditions.condition(min >= JRE.MINIMUM_VERSION, | ||
() -> String.format("@%s's minimum value [%d] must greater than or equal to %d", this.annotationName, min, | ||
JRE.MINIMUM_VERSION)); | ||
Preconditions.condition(min <= max, | ||
() -> String.format("@%s's minimum value [%d] must be less than or equal to its maximum value [%d]", | ||
this.annotationName, min, max)); | ||
|
||
return JRE.isCurrentVersionWithinRange(min, max); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.