Skip to content

Deprecate unsupported JRE enum constants and update related conditions #4516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop/6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ repository on GitHub.
- `InvocationInterceptor.interceptDynamicTest(Invocation, ExtensionContext)` method
* The deprecated `junit.jupiter.tempdir.scope` configuration parameter is no longer
supported.
* The `JRE` enum constants for `JAVA_8` to `JAVA_16` have been deprecated because they can
no longer be used at runtime since `JAVA_17` is the new baseline.
* `@EnabledForJreRange` and `@DisabledForJreRange` now use `JAVA_17` as their default
`min` value.

[[release-notes-6.0.0-M1-junit-jupiter-new-features-and-improvements]]
==== New Features and Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

package example;

import static org.junit.jupiter.api.condition.JRE.JAVA_11;
import static org.junit.jupiter.api.condition.JRE.JAVA_17;
import static org.junit.jupiter.api.condition.JRE.JAVA_18;
import static org.junit.jupiter.api.condition.JRE.JAVA_19;
import static org.junit.jupiter.api.condition.JRE.JAVA_21;
import static org.junit.jupiter.api.condition.JRE.JAVA_9;
import static org.junit.jupiter.api.condition.JRE.JAVA_25;
import static org.junit.jupiter.api.condition.OS.LINUX;
import static org.junit.jupiter.api.condition.OS.MAC;
import static org.junit.jupiter.api.condition.OS.WINDOWS;
Expand Down Expand Up @@ -113,44 +114,44 @@ void onJava17And21() {
}

@Test
@EnabledForJreRange(min = JAVA_9, max = JAVA_11)
void fromJava9To11() {
@EnabledForJreRange(min = JAVA_21, max = JAVA_25)
void fromJava21To25() {
// ...
}

@Test
@EnabledForJreRange(min = JAVA_9)
void onJava9AndHigher() {
@EnabledForJreRange(min = JAVA_21)
void onJava21ndHigher() {
// ...
}

@Test
@EnabledForJreRange(max = JAVA_11)
void fromJava8To11() {
@EnabledForJreRange(max = JAVA_18)
void fromJava17To18() {
// ...
}

@Test
@DisabledOnJre(JAVA_9)
void notOnJava9() {
@DisabledOnJre(JAVA_19)
void notOnJava19() {
// ...
}

@Test
@DisabledForJreRange(min = JAVA_9, max = JAVA_11)
void notFromJava9To11() {
@DisabledForJreRange(min = JAVA_17, max = JAVA_17)
void notFromJava17To19() {
// ...
}

@Test
@DisabledForJreRange(min = JAVA_9)
void notOnJava9AndHigher() {
@DisabledForJreRange(min = JAVA_19)
void notOnJava19AndHigher() {
// ...
}

@Test
@DisabledForJreRange(max = JAVA_11)
void notFromJava8To11() {
@DisabledForJreRange(max = JAVA_18)
void notFromJava17To18() {
// ...
}
// end::user_guide_jre[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ abstract class GenerateJreRelatedSourceCode : DefaultTask() {
mapper.registerModule(KotlinModule.Builder().build())
mapper.readValue(input, object : TypeReference<List<JRE>>() {})
}
val minRuntimeVersion = 17
val supportedJres = jres.filter { it.version >= minRuntimeVersion }
val params = mapOf(
"jres" to jres,
"jresSortedByStringValue" to jres.sortedBy { it.version.toString() },
"minRuntimeVersion" to minRuntimeVersion,
"allJres" to jres,
"supportedJres" to supportedJres,
"supportedJresSortedByStringValue" to supportedJres.sortedBy { it.version.toString() },
"licenseHeader" to licenseHeaderFile.asFile.get().readText()
)
templates.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
*/
abstract class AbstractJreRangeCondition<A extends Annotation> extends BooleanExecutionCondition<A> {

private static final JRE DEFAULT_MINIMUM_JRE = JRE.JAVA_17;
private static final JRE DEFAULT_MAXIMUM_JRE = JRE.OTHER;

private final String annotationName;

AbstractJreRangeCondition(Class<A> annotationType, Function<A, String> customDisabledReason) {
Expand Down Expand Up @@ -58,17 +61,17 @@ protected final boolean isCurrentVersionWithinRange(JRE minJre, JRE maxJre, int
// 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;
minJre = DEFAULT_MINIMUM_JRE;
}
if (!maxJreSet) {
maxJre = JRE.OTHER;
maxJre = DEFAULT_MAXIMUM_JRE;
}

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),
Preconditions.condition((min != DEFAULT_MINIMUM_JRE.version() || max != DEFAULT_MAXIMUM_JRE.version()),
() -> "You must declare a non-default value for the minimum or maximum value in @" + this.annotationName);
Preconditions.condition(min <= max,
() -> String.format("@%s's minimum value [%d] must be less than or equal to its maximum value [%d]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
* {@link #minVersion() minVersion} instead.
*
* <p>Defaults to {@link JRE#UNDEFINED UNDEFINED}, which will be interpreted
* as {@link JRE#JAVA_8 JAVA_8} if the {@link #minVersion() minVersion} is
* as {@link JRE#JAVA_17 JAVA_17} if the {@link #minVersion() minVersion} is
* not set.
*
* @see JRE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
* {@link #minVersion() minVersion} instead.
*
* <p>Defaults to {@link JRE#UNDEFINED UNDEFINED}, which will be interpreted
* as {@link JRE#JAVA_8 JAVA_8} if the {@link #minVersion() minVersion} is
* as {@link JRE#JAVA_17 JAVA_17} if the {@link #minVersion() minVersion} is
* not set.
*
* @see JRE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
@import gg.jte.support.ForSupport
@import junitbuild.generator.model.JRE

@param List<JRE> jres
@param int minRuntimeVersion
@param List<JRE> allJres
@param String licenseHeader
${licenseHeader}
package org.junit.jupiter.api.condition;
Expand All @@ -11,26 +12,17 @@ import static org.apiguardian.api.API.Status.DEPRECATED;
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
import static org.apiguardian.api.API.Status.STABLE;

import java.lang.reflect.Method;

import org.apiguardian.api.API;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.support.ReflectionSupport;
import org.junit.platform.commons.util.StringUtils;

/**
* Enumeration of Java Runtime Environment (JRE) versions.
*
* <p>If the current JRE version can be detected but is not one of the predefined
* constants in this enum, {@link #OTHER} will be considered to be the
* {@linkplain #isCurrentVersion current JRE version}. If the current JRE version
* cannot be detected &mdash; for example, if the {@code java.version} JVM system
* property is undefined &mdash; {@link #UNDEFINED} will be considered to be the
* current JRE version.
* {@linkplain #isCurrentVersion current JRE version}.
*
* @since 5.1
@for(JRE jre : jres)<%--
@for(JRE jre : allJres)<%--
--%> * @see #JAVA_${jre.getVersion()}
@endfor<%--
--%> * @see #OTHER
Expand All @@ -54,22 +46,30 @@ public enum JRE {
*/
@API(status = EXPERIMENTAL, since = "5.12")
UNDEFINED(-1),
@for(var jre : jres)
@for(var jre : allJres)
/**
* Java ${jre.getVersion()}.
@if(jre.getSince() != null)<%--
@if(jre.getSince() != null || jre.getVersion() < minRuntimeVersion)<%--
--%> *
* @since ${jre.getSince()}
@endif<%--
--%>@if(jre.getSince() != null)<%--
--%> * @since ${jre.getSince()}
@endif<%--
--%>@if(jre.getVersion() < minRuntimeVersion)<%--
--%> * @deprecated No longer supported at runtime; please use {@link #JAVA_17} or later
@endif<%--
--%> */
@if(jre.getSince() != null)<%--
@if(jre.getVersion() < minRuntimeVersion)<%--
--%>@API(status = DEPRECATED, since = "6.0") //
@Deprecated(since = "6.0", forRemoval = true)
@elseif(jre.getSince() != null)<%--
--%>@API(status = STABLE, since = "${jre.getSince()}")
@endif<%--
--%>JAVA_${jre.getVersion()}(${jre.getVersion()}),
@endfor
/**
* A JRE version other than <%--
--%>@for(var jre : ForSupport.of(jres))<%--
--%>@for(var jre : ForSupport.of(allJres))<%--
--%>@if(jre.isLast())or @endif<%--
--%>{@link #JAVA_${jre.get().getVersion()}}<%--
--%>@if(jre.isLast()).@else,@endif<%--
Expand All @@ -85,59 +85,13 @@ public enum JRE {

static final int UNDEFINED_VERSION = -1;

static final int MINIMUM_VERSION = 8;

private static final Logger logger = LoggerFactory.getLogger(JRE.class);

private static final int CURRENT_VERSION = determineCurrentVersion();

private static final JRE CURRENT_JRE = determineCurrentJre(CURRENT_VERSION);

private static int determineCurrentVersion() {
String javaVersion = System.getProperty("java.version");
boolean javaVersionIsBlank = StringUtils.isBlank(javaVersion);
static final int MINIMUM_VERSION = ${allJres.getFirst().getVersion()};

if (javaVersionIsBlank) {
logger.debug(
() -> "JVM system property 'java.version' is undefined. It is therefore not possible to detect Java 8.");
}

if (!javaVersionIsBlank && javaVersion.startsWith("1.8")) {
return 8;
}

try {
// java.lang.Runtime.version() is a static method available on Java 9+
// that returns an instance of java.lang.Runtime.Version which has the
// following method: public int major()
Method versionMethod = Runtime.class.getMethod("version");
Object version = ReflectionSupport.invokeMethod(versionMethod, null);
Method majorMethod = version.getClass().getMethod("major");
return (int) ReflectionSupport.invokeMethod(majorMethod, version);
}
catch (Exception ex) {
logger.debug(ex, () -> "Failed to determine the current JRE version via java.lang.Runtime.Version.");
}

return UNDEFINED_VERSION;
}

private static JRE determineCurrentJre(int currentVersion) {
switch (currentVersion) {
case UNDEFINED_VERSION:
return UNDEFINED;<%--
--%>@for(var jre : jres)
case ${jre.getVersion()}:
return JAVA_${jre.getVersion()};<%--
--%>@endfor
default:
return OTHER;
}
}
private static final int CURRENT_VERSION = Runtime.version().feature();

private final int version;

private JRE(int version) {
JRE(int version) {
this.version = version;
}

Expand All @@ -161,18 +115,18 @@ public enum JRE {
/**
* @return {@code true} if <em>this</em> {@code JRE} is known to be the
* Java Runtime Environment version for the currently executing JVM or if
* the version is {@link #OTHER} or {@link #UNDEFINED}
* the version is {@link #OTHER}
*
* @see #currentJre()
* @see #currentVersionNumber()
*/
public boolean isCurrentVersion() {
return this == CURRENT_JRE;
return this == currentJre();
}

/**
* @return the {@link JRE} for the currently executing JVM, potentially
* {@link #OTHER} or {@link #UNDEFINED}
* {@link #OTHER}
*
* @since 5.7
* @see #currentVersionNumber()
Expand All @@ -186,14 +140,19 @@ public enum JRE {

/**
* @return the {@link JRE} for the currently executing JVM, potentially
* {@link #OTHER} or {@link #UNDEFINED}
* {@link #OTHER}
*
* @since 5.12
* @see #currentVersionNumber()
*/
@API(status = STABLE, since = "5.12")
public static JRE currentJre() {
return CURRENT_JRE;
return switch (CURRENT_VERSION) {<%--
--%>@for(var jre : allJres)
case ${jre.getVersion()} -> JAVA_${jre.getVersion()};<%--
--%>@endfor
default -> OTHER;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
@import gg.jte.support.ForSupport
@import junitbuild.generator.model.JRE

@param List<JRE> jres
@param List<JRE> supportedJres
@param String licenseHeader
${licenseHeader}
package org.junit.jupiter.api.condition;

public class JavaVersionPredicates {

private static final int JAVA_VERSION = Runtime.version().feature();
@for(JRE jre : jres)
@for(JRE jre : supportedJres)
static boolean onJava${jre.getVersion()}() {
return JAVA_VERSION == ${jre.getVersion()};
}
@endfor
static boolean onKnownVersion() {
return @for(var jre : ForSupport.of(jres))onJava${jre.get().getVersion()}()@if(!jre.isLast()) //
return @for(var jre : ForSupport.of(supportedJres))onJava${jre.get().getVersion()}()@if(!jre.isLast()) //
|| @endif@endfor;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@import java.util.List
@import junitbuild.generator.model.JRE

@param List<JRE> jres
@param List<JRE> jresSortedByStringValue
@param List<JRE> supportedJres
@param List<JRE> supportedJresSortedByStringValue
@param String licenseHeader
${licenseHeader}
package org.junit.jupiter.api.condition;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@for(var jre : jresSortedByStringValue)<%--
@for(var jre : supportedJresSortedByStringValue)<%--
--%>import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava${jre.getVersion()};
@endfor<%--
--%>import static org.junit.jupiter.api.condition.JavaVersionPredicates.onKnownVersion;
Expand Down Expand Up @@ -89,7 +89,7 @@ class DisabledOnJreConditionTests extends AbstractExecutionConditionTests {
assertDisabledOnCurrentJreIf(true);
assertCustomDisabledReasonIs("Disabled on every JRE");
}
@for(var jre : jres)
@for(var jre : supportedJres)
/**
* @see DisabledOnJreIntegrationTests#jre${jre.getVersion()}()
*/
Expand All @@ -99,7 +99,7 @@ class DisabledOnJreConditionTests extends AbstractExecutionConditionTests {
assertDisabledOnCurrentJreIf(onJava${jre.getVersion()}());
}
@endfor<%--
--%>@for(var jre : jres)
--%>@for(var jre : supportedJres)
/**
* @see DisabledOnJreIntegrationTests#version${jre.getVersion()}()
*/
Expand Down
Loading