Skip to content

Commit 95cfb7b

Browse files
committed
Add additional validation for minimum runtime version
1 parent 9d86c3b commit 95cfb7b

File tree

11 files changed

+92
-17
lines changed

11 files changed

+92
-17
lines changed

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,23 @@ protected final IntStream validatedVersions(JRE[] jres, int[] versions) {
4242
Preconditions.condition(jres.length > 0 || versions.length > 0,
4343
() -> "You must declare at least one JRE or version in @" + this.annotationName);
4444

45-
return IntStream.concat(//
45+
var allVersions = IntStream.concat(//
4646
Arrays.stream(jres).mapToInt(jre -> {
4747
Preconditions.condition(jre != JRE.UNDEFINED,
4848
() -> "JRE.UNDEFINED is not supported in @" + this.annotationName);
4949
return jre.version();
5050
}), //
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-
).distinct();
51+
Arrays.stream(versions).peek(version -> Preconditions.condition(version >= JRE.MINIMUM_DECLARED_VERSION,
52+
() -> String.format("Version [%d] in @%s must be greater than or equal to %d", version,
53+
this.annotationName, JRE.MINIMUM_DECLARED_VERSION)))//
54+
).sorted().distinct().toArray();
55+
56+
var max = allVersions[allVersions.length - 1];
57+
Preconditions.condition(max >= JRE.MINIMUM_RUNTIME_VERSION,
58+
() -> String.format("Versions in @%s must contain at least one value greater than or equal to %d",
59+
this.annotationName, JRE.MINIMUM_RUNTIME_VERSION));
60+
61+
return IntStream.of(allVersions);
5862
}
5963

6064
}

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ protected final boolean isCurrentVersionWithinRange(JRE minJre, JRE maxJre, int
4848
this.annotationName));
4949

5050
// Users must supply valid values for minVersion and maxVersion.
51-
Preconditions.condition(!minVersionSet || (minVersion >= JRE.MINIMUM_VERSION),
51+
Preconditions.condition(!minVersionSet || (minVersion >= JRE.MINIMUM_DECLARED_VERSION),
5252
() -> 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),
53+
minVersion, JRE.MINIMUM_DECLARED_VERSION));
54+
Preconditions.condition(!maxVersionSet || (maxVersion >= JRE.MINIMUM_DECLARED_VERSION),
5555
() -> String.format("@%s's maxVersion [%d] must be greater than or equal to %d", this.annotationName,
56-
maxVersion, JRE.MINIMUM_VERSION));
56+
maxVersion, JRE.MINIMUM_DECLARED_VERSION));
5757

5858
// Now that we have checked the basic preconditions, we need to ensure that we are
5959
// using valid JRE enum constants.
@@ -68,14 +68,17 @@ protected final boolean isCurrentVersionWithinRange(JRE minJre, JRE maxJre, int
6868
int max = (maxVersionSet ? maxVersion : maxJre.version());
6969

7070
// Finally, we need to validate the effective minimum and maximum values.
71-
Preconditions.condition((min != JRE.MINIMUM_VERSION || max != Integer.MAX_VALUE),
71+
Preconditions.condition((min != JRE.MINIMUM_DECLARED_VERSION || max != Integer.MAX_VALUE),
7272
() -> "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));
73+
Preconditions.condition(min >= JRE.MINIMUM_DECLARED_VERSION,
74+
() -> String.format("@%s's minimum value [%d] must be greater than or equal to %d", this.annotationName,
75+
min, JRE.MINIMUM_DECLARED_VERSION));
7676
Preconditions.condition(min <= max,
7777
() -> String.format("@%s's minimum value [%d] must be less than or equal to its maximum value [%d]",
7878
this.annotationName, min, max));
79+
Preconditions.condition(max >= JRE.MINIMUM_RUNTIME_VERSION,
80+
() -> String.format("@%s's maximum value [%d] must be greater than or equal to %d", this.annotationName,
81+
max, JRE.MINIMUM_RUNTIME_VERSION));
7982

8083
return JRE.isCurrentVersionWithinRange(min, max);
8184
}

junit-jupiter-api/src/templates/resources/main/org/junit/jupiter/api/condition/JRE.java.jte

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public enum JRE {
8888

8989
static final int UNDEFINED_VERSION = -1;
9090

91-
static final int MINIMUM_VERSION = ${jres.getFirst().getVersion()};
91+
static final int MINIMUM_DECLARED_VERSION = ${jres.getFirst().getVersion()};
92+
93+
static final int MINIMUM_RUNTIME_VERSION = ${minRuntimeVersion};
9294

9395
private static final int CURRENT_VERSION = Runtime.version().feature();
9496

jupiter-tests/src/templates/resources/test/org/junit/jupiter/api/condition/DisabledOnJreConditionTests.java.jte

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ class DisabledOnJreConditionTests extends AbstractExecutionConditionTests {
8080
.withMessage("Version [7] in @DisabledOnJre must be greater than or equal to 8");
8181
}
8282

83+
/**
84+
* @see DisabledOnJreIntegrationTests#version16()
85+
*/
86+
@Test
87+
void version16() {
88+
assertThatExceptionOfType(PreconditionViolationException.class)//
89+
.isThrownBy(this::evaluateCondition)//
90+
.withMessage("Versions in @DisabledOnJre must contain at least one value greater than or equal to 17");
91+
}
92+
8393
/**
8494
* @see DisabledOnJreIntegrationTests#disabledOnAllJavaVersions()
8595
*/

jupiter-tests/src/templates/resources/test/org/junit/jupiter/api/condition/DisabledOnJreIntegrationTests.java.jte

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class DisabledOnJreIntegrationTests {
5454
void version7() {
5555
}
5656

57+
@Test
58+
@Disabled("Only used in a unit test via reflection")
59+
@DisabledOnJre(versions = 16)
60+
void version16() {
61+
}
62+
5763
@Test
5864
@DisabledOnJre(disabledReason = "Disabled on every JRE", value = { //
5965
@for(var jre : supportedJres)<%--

jupiter-tests/src/templates/resources/test/org/junit/jupiter/api/condition/EnabledOnJreConditionTests.java.jte

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ class EnabledOnJreConditionTests extends AbstractExecutionConditionTests {
8080
.withMessage("Version [7] in @EnabledOnJre must be greater than or equal to 8");
8181
}
8282

83+
/**
84+
* @see EnabledOnJreIntegrationTests#version16()
85+
*/
86+
@Test
87+
void version16() {
88+
assertThatExceptionOfType(PreconditionViolationException.class)//
89+
.isThrownBy(this::evaluateCondition)//
90+
.withMessage("Versions in @EnabledOnJre must contain at least one value greater than or equal to 17");
91+
}
92+
8393
/**
8494
* @see EnabledOnJreIntegrationTests#enabledOnAllJavaVersions()
8595
*/

jupiter-tests/src/templates/resources/test/org/junit/jupiter/api/condition/EnabledOnJreIntegrationTests.java.jte

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class EnabledOnJreIntegrationTests {
5353
void version7() {
5454
}
5555

56+
@Test
57+
@Disabled("Only used in a unit test via reflection")
58+
@EnabledOnJre(versions = 16)
59+
void version16() {
60+
}
61+
5662
@Test
5763
@EnabledOnJre({ //
5864
@for(var jre : supportedJres)<%--

jupiter-tests/src/test/java/org/junit/jupiter/api/condition/DisabledForJreRangeConditionTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ void minVersion8() {
9595
defaultValues();
9696
}
9797

98+
/**
99+
* @see DisabledForJreRangeIntegrationTests#minVersion8Max11()
100+
*/
101+
@Test
102+
void minVersion8Max11() {
103+
assertThatExceptionOfType(PreconditionViolationException.class)//
104+
.isThrownBy(this::evaluateCondition)//
105+
.withMessage("@DisabledForJreRange's maximum value [11] must be greater than or equal to 17");
106+
}
107+
98108
/**
99109
* @see DisabledForJreRangeIntegrationTests#maxOther()
100110
*/

jupiter-tests/src/test/java/org/junit/jupiter/api/condition/DisabledForJreRangeIntegrationTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ void minVersion8() {
7474
fail("should result in a configuration exception");
7575
}
7676

77+
@Test
78+
@Disabled("Only used in a unit test via reflection")
79+
@DisabledForJreRange(minVersion = 8, maxVersion = 11)
80+
void minVersion8Max11() {
81+
fail("should result in a configuration exception");
82+
}
83+
7784
@Test
7885
@Disabled("Only used in a unit test via reflection")
7986
@DisabledForJreRange(max = OTHER)

jupiter-tests/src/test/java/org/junit/jupiter/api/condition/EnabledForJreRangeConditionTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ void minVersion8() {
101101
defaultValues();
102102
}
103103

104+
/**
105+
* @see EnabledForJreRangeIntegrationTests#minVersion8Max11()
106+
*/
107+
@Test
108+
void minVersion8Max11() {
109+
assertThatExceptionOfType(PreconditionViolationException.class)//
110+
.isThrownBy(this::evaluateCondition)//
111+
.withMessage("@EnabledForJreRange's maximum value [11] must be greater than or equal to 17");
112+
}
113+
104114
/**
105115
* @see EnabledForJreRangeIntegrationTests#maxOther()
106116
*/

jupiter-tests/src/test/java/org/junit/jupiter/api/condition/EnabledForJreRangeIntegrationTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ void minVersion8() {
8181
fail("should result in a configuration exception");
8282
}
8383

84+
@Test
85+
@Disabled("Only used in a unit test via reflection")
86+
@EnabledForJreRange(minVersion = 8, maxVersion = 11)
87+
void minVersion8Max11() {
88+
fail("should result in a configuration exception");
89+
}
90+
8491
@Test
8592
@Disabled("Only used in a unit test via reflection")
8693
@EnabledForJreRange(max = OTHER)

0 commit comments

Comments
 (0)