Skip to content

Commit 2d9a10e

Browse files
committed
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
1 parent 907a13b commit 2d9a10e

29 files changed

+1580
-261
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-RC1.adoc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
*Date of Release:* ❓
55

6-
*Scope:*
6+
*Scope:* Minor enhancements since JUnit 5.12 M1.
77

88
For a complete list of all _closed_ issues and pull requests for this release, consult the
9-
link:{junit5-repo}+/milestone/88?closed=1+[5.12.0-RC1] milestone page in the
10-
JUnit repository on GitHub.
9+
link:{junit5-repo}+/milestone/88?closed=1+[5.12.0-RC1] milestone page in the JUnit
10+
repository on GitHub.
1111

1212

1313
[[release-notes-5.12.0-RC1-junit-platform]]
@@ -45,7 +45,10 @@ JUnit repository on GitHub.
4545
[[release-notes-5.12.0-RC1-junit-jupiter-new-features-and-improvements]]
4646
==== New Features and Improvements
4747

48-
* ❓
48+
* `JRE`-based conditions such as `@EnabledOnJre` and `@DisabledForJreRange` now support
49+
arbitrary Java versions. See the
50+
<<../user-guide/index.adoc#writing-tests-conditional-execution-jre, User Guide>> for
51+
details.
4952

5053

5154
[[release-notes-5.12.0-RC1-junit-vintage]]

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -646,18 +646,36 @@ include::{testDir}/example/ConditionalTestExecutionDemo.java[tags=user_guide_arc
646646
[[writing-tests-conditional-execution-jre]]
647647
==== Java Runtime Environment Conditions
648648

649-
A container or test may be enabled or disabled on particular versions of the Java
650-
Runtime Environment (JRE) via the `{EnabledOnJre}` and `{DisabledOnJre}` annotations
651-
or on a particular range of versions of the JRE via the `{EnabledForJreRange}` and
652-
`{DisabledForJreRange}` annotations. The range defaults to `{JRE}.JAVA_8` as the lower
653-
border (`min`) and `{JRE}.OTHER` as the higher border (`max`), which allows usage of
654-
half open ranges.
649+
A container or test may be enabled or disabled on particular versions of the Java Runtime
650+
Environment (JRE) via the `{EnabledOnJre}` and `{DisabledOnJre}` annotations or on a
651+
particular range of versions of the JRE via the `{EnabledForJreRange}` and
652+
`{DisabledForJreRange}` annotations. The range effectively defaults to `JRE.JAVA_8` as the
653+
lower bound and `JRE.OTHER` as the upper bound, which allows usage of half open ranges.
654+
655+
The following listing demonstrates the use of these annotations with predefined {JRE} enum
656+
constants.
655657

656658
[source,java,indent=0]
657659
----
658660
include::{testDir}/example/ConditionalTestExecutionDemo.java[tags=user_guide_jre]
659661
----
660662

663+
Since the enum constants defined in {JRE} are static for any given JUnit release, you
664+
might find that you need to configure a Java version that is not supported by the `JRE`
665+
enum. For example, as of JUnit Jupiter 5.12 the `JRE` enum defines `JAVA_25` as the
666+
highest supported Java version. However, you may wish to run your tests against later
667+
versions of Java. To support such use cases, you can specify arbitrary Java versions via
668+
the `versions` attributes in `@EnabledOnJre` and `@DisabledOnJre` and via the `minVersion`
669+
and `maxVersion` attributes in `@EnabledForJreRange` and `@DisabledForJreRange`.
670+
671+
The following listing demonstrates the use of these annotations with arbitrary Java
672+
versions.
673+
674+
[source,java,indent=0]
675+
----
676+
include::{testDir}/example/ConditionalTestExecutionDemo.java[tags=user_guide_jre_arbitrary_versions]
677+
----
678+
661679
[[writing-tests-conditional-execution-native]]
662680
==== Native Image Conditions
663681

documentation/src/test/java/example/ConditionalTestExecutionDemo.java

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
package example;
1212

13-
import static org.junit.jupiter.api.condition.JRE.JAVA_10;
1413
import static org.junit.jupiter.api.condition.JRE.JAVA_11;
15-
import static org.junit.jupiter.api.condition.JRE.JAVA_8;
14+
import static org.junit.jupiter.api.condition.JRE.JAVA_17;
15+
import static org.junit.jupiter.api.condition.JRE.JAVA_21;
16+
import static org.junit.jupiter.api.condition.JRE.JAVA_25;
1617
import static org.junit.jupiter.api.condition.JRE.JAVA_9;
1718
import static org.junit.jupiter.api.condition.OS.LINUX;
1819
import static org.junit.jupiter.api.condition.OS.MAC;
@@ -101,26 +102,26 @@ void notOnNewMacs() {
101102

102103
// tag::user_guide_jre[]
103104
@Test
104-
@EnabledOnJre(JAVA_8)
105-
void onlyOnJava8() {
105+
@EnabledOnJre(JAVA_17)
106+
void onlyOnJava17() {
106107
// ...
107108
}
108109

109110
@Test
110-
@EnabledOnJre({ JAVA_9, JAVA_10 })
111-
void onJava9Or10() {
111+
@EnabledOnJre({ JAVA_17, JAVA_21 })
112+
void onJava17And21() {
112113
// ...
113114
}
114115

115116
@Test
116117
@EnabledForJreRange(min = JAVA_9, max = JAVA_11)
117-
void fromJava9to11() {
118+
void fromJava9To11() {
118119
// ...
119120
}
120121

121122
@Test
122123
@EnabledForJreRange(min = JAVA_9)
123-
void fromJava9toCurrentJavaFeatureNumber() {
124+
void onJava9AndHigher() {
124125
// ...
125126
}
126127

@@ -138,23 +139,73 @@ void notOnJava9() {
138139

139140
@Test
140141
@DisabledForJreRange(min = JAVA_9, max = JAVA_11)
141-
void notFromJava9to11() {
142+
void notFromJava9To11() {
142143
// ...
143144
}
144145

145146
@Test
146147
@DisabledForJreRange(min = JAVA_9)
147-
void notFromJava9toCurrentJavaFeatureNumber() {
148+
void notOnJava9AndHigher() {
148149
// ...
149150
}
150151

151152
@Test
152153
@DisabledForJreRange(max = JAVA_11)
153-
void notFromJava8to11() {
154+
void notFromJava8To11() {
154155
// ...
155156
}
156157
// end::user_guide_jre[]
157158

159+
// tag::user_guide_jre_arbitrary_versions[]
160+
@Test
161+
@EnabledOnJre(versions = 26)
162+
void onlyOnJava26() {
163+
// ...
164+
}
165+
166+
@Test
167+
@EnabledOnJre(value = JAVA_25, versions = 26)
168+
void onJava25And26() {
169+
// ...
170+
}
171+
172+
@Test
173+
@EnabledForJreRange(minVersion = 26)
174+
void onJava26AndHigher() {
175+
// ...
176+
}
177+
178+
@Test
179+
@EnabledForJreRange(min = JAVA_25, maxVersion = 27)
180+
void fromJava25To27() {
181+
// ...
182+
}
183+
184+
@Test
185+
@DisabledOnJre(versions = 26)
186+
void notOnJava26() {
187+
// ...
188+
}
189+
190+
@Test
191+
@DisabledOnJre(value = JAVA_25, versions = 26)
192+
void notOnJava25And26() {
193+
// ...
194+
}
195+
196+
@Test
197+
@DisabledForJreRange(minVersion = 26)
198+
void notOnJava26AndHigher() {
199+
// ...
200+
}
201+
202+
@Test
203+
@DisabledForJreRange(min = JAVA_25, maxVersion = 27)
204+
void notFromJava25To27() {
205+
// ...
206+
}
207+
// end::user_guide_jre_arbitrary_versions[]
208+
158209
// tag::user_guide_native[]
159210
@Test
160211
@EnabledInNativeImage
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.jupiter.api.condition;
12+
13+
import java.lang.annotation.Annotation;
14+
import java.util.Arrays;
15+
import java.util.function.Function;
16+
import java.util.stream.IntStream;
17+
18+
import org.junit.platform.commons.util.Preconditions;
19+
20+
/**
21+
* Abstract base class for {@link EnabledOnJreCondition} and
22+
* {@link DisabledOnJreCondition}.
23+
*
24+
* @since 5.12
25+
*/
26+
abstract class AbstractJreCondition<A extends Annotation> extends BooleanExecutionCondition<A> {
27+
28+
static final String ENABLED_ON_CURRENT_JRE = //
29+
"Enabled on JRE version: " + System.getProperty("java.version");
30+
31+
static final String DISABLED_ON_CURRENT_JRE = //
32+
"Disabled on JRE version: " + System.getProperty("java.version");
33+
34+
private final String annotationName;
35+
36+
AbstractJreCondition(Class<A> annotationType, Function<A, String> customDisabledReason) {
37+
super(annotationType, ENABLED_ON_CURRENT_JRE, DISABLED_ON_CURRENT_JRE, customDisabledReason);
38+
this.annotationName = annotationType.getSimpleName();
39+
}
40+
41+
protected final IntStream validatedVersions(JRE[] jres, int[] versions) {
42+
Preconditions.condition(jres.length > 0 || versions.length > 0,
43+
() -> "You must declare at least one JRE or version in @" + this.annotationName);
44+
45+
return IntStream.concat(//
46+
Arrays.stream(jres).mapToInt(jre -> {
47+
Preconditions.condition(jre != JRE.UNDEFINED,
48+
() -> "JRE.UNDEFINED is not supported in @" + this.annotationName);
49+
return jre.version();
50+
}), //
51+
Arrays.stream(versions).map(version -> {
52+
Preconditions.condition(version >= JRE.MINIMUM_VERSION,
53+
() -> String.format("Version [%d] in @%s must be greater than or equal to %d", version,
54+
this.annotationName, JRE.MINIMUM_VERSION));
55+
return version;
56+
})//
57+
);
58+
}
59+
60+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.jupiter.api.condition;
12+
13+
import static org.junit.jupiter.api.condition.AbstractJreCondition.DISABLED_ON_CURRENT_JRE;
14+
import static org.junit.jupiter.api.condition.AbstractJreCondition.ENABLED_ON_CURRENT_JRE;
15+
16+
import java.lang.annotation.Annotation;
17+
import java.util.function.Function;
18+
19+
import org.junit.platform.commons.util.Preconditions;
20+
21+
/**
22+
* Abstract base class for {@link EnabledForJreRangeCondition} and
23+
* {@link DisabledForJreRangeCondition}.
24+
*
25+
* @since 5.12
26+
*/
27+
abstract class AbstractJreRangeCondition<A extends Annotation> extends BooleanExecutionCondition<A> {
28+
29+
private final String annotationName;
30+
31+
AbstractJreRangeCondition(Class<A> annotationType, Function<A, String> customDisabledReason) {
32+
super(annotationType, ENABLED_ON_CURRENT_JRE, DISABLED_ON_CURRENT_JRE, customDisabledReason);
33+
this.annotationName = annotationType.getSimpleName();
34+
}
35+
36+
protected final boolean isCurrentVersionWithinRange(JRE minJre, JRE maxJre, int minVersion, int maxVersion) {
37+
boolean minJreSet = minJre != JRE.UNDEFINED;
38+
boolean maxJreSet = maxJre != JRE.UNDEFINED;
39+
boolean minVersionSet = minVersion != JRE.UNDEFINED_VERSION;
40+
boolean maxVersionSet = maxVersion != JRE.UNDEFINED_VERSION;
41+
42+
// Users must choose between JRE enum constants and version numbers.
43+
Preconditions.condition(!minJreSet || !minVersionSet, () -> String.format(
44+
"@%s's minimum value must be configured with either a JRE enum constant or numeric version, but not both",
45+
this.annotationName));
46+
Preconditions.condition(!maxJreSet || !maxVersionSet, () -> String.format(
47+
"@%s's maximum value must be configured with either a JRE enum constant or numeric version, but not both",
48+
this.annotationName));
49+
50+
// Users must supply valid values for minVersion and maxVersion.
51+
Preconditions.condition(!minVersionSet || (minVersion >= JRE.MINIMUM_VERSION),
52+
() -> String.format("@%s's minVersion [%d] must be greater than or equal to %d", this.annotationName,
53+
minVersion, JRE.MINIMUM_VERSION));
54+
Preconditions.condition(!maxVersionSet || (maxVersion >= JRE.MINIMUM_VERSION),
55+
() -> String.format("@%s's maxVersion [%d] must be greater than or equal to %d", this.annotationName,
56+
maxVersion, JRE.MINIMUM_VERSION));
57+
58+
// Now that we have checked the basic preconditions, we need to ensure that we are
59+
// using valid JRE enum constants.
60+
if (!minJreSet) {
61+
minJre = JRE.JAVA_8;
62+
}
63+
if (!maxJreSet) {
64+
maxJre = JRE.OTHER;
65+
}
66+
67+
int min = (minVersionSet ? minVersion : minJre.version());
68+
int max = (maxVersionSet ? maxVersion : maxJre.version());
69+
70+
// Finally, we need to validate the effective minimum and maximum values.
71+
Preconditions.condition((min != JRE.MINIMUM_VERSION || max != Integer.MAX_VALUE),
72+
() -> "You must declare a non-default value for the minimum or maximum value in @" + this.annotationName);
73+
Preconditions.condition(min >= JRE.MINIMUM_VERSION,
74+
() -> String.format("@%s's minimum value [%d] must greater than or equal to %d", this.annotationName, min,
75+
JRE.MINIMUM_VERSION));
76+
Preconditions.condition(min <= max,
77+
() -> String.format("@%s's minimum value [%d] must be less than or equal to its maximum value [%d]",
78+
this.annotationName, min, max));
79+
80+
return JRE.isCurrentVersionWithinRange(min, max);
81+
}
82+
83+
}

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/BooleanExecutionCondition.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,32 @@
2323

2424
abstract class BooleanExecutionCondition<A extends Annotation> implements ExecutionCondition {
2525

26-
private final Class<A> annotationType;
26+
protected final Class<A> annotationType;
2727
private final String enabledReason;
2828
private final String disabledReason;
2929
private final Function<A, String> customDisabledReason;
3030

3131
BooleanExecutionCondition(Class<A> annotationType, String enabledReason, String disabledReason,
3232
Function<A, String> customDisabledReason) {
33+
3334
this.annotationType = annotationType;
3435
this.enabledReason = enabledReason;
3536
this.disabledReason = disabledReason;
3637
this.customDisabledReason = customDisabledReason;
3738
}
3839

39-
abstract boolean isEnabled(A annotation);
40-
4140
@Override
4241
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
43-
return findAnnotation(context.getElement(), annotationType) //
44-
.map(annotation -> isEnabled(annotation) ? enabled(enabledReason)
45-
: disabled(disabledReason, customDisabledReason.apply(annotation))) //
42+
return findAnnotation(context.getElement(), this.annotationType) //
43+
.map(annotation -> isEnabled(annotation) ? enabled(this.enabledReason)
44+
: disabled(this.disabledReason, this.customDisabledReason.apply(annotation))) //
4645
.orElseGet(this::enabledByDefault);
4746
}
4847

48+
abstract boolean isEnabled(A annotation);
49+
4950
private ConditionEvaluationResult enabledByDefault() {
50-
String reason = String.format("@%s is not present", annotationType.getSimpleName());
51+
String reason = String.format("@%s is not present", this.annotationType.getSimpleName());
5152
return enabled(reason);
5253
}
5354

0 commit comments

Comments
 (0)