Remove versions less than 17 from JavaVersion enum

Closes gh-29503
pull/30157/head
Andy Wilkinson 3 years ago
parent a031c0c659
commit f5ca568014

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,10 +16,8 @@
package org.springframework.boot.autoconfigure.condition;
import java.io.Console;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.ServiceLoader;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
@ -48,58 +46,59 @@ class ConditionalOnJavaTests {
private final OnJavaCondition condition = new OnJavaCondition();
@Test
@EnabledOnJre(JRE.JAVA_8)
@EnabledOnJre(JRE.JAVA_17)
void doesNotMatchIfBetterVersionIsRequired() {
this.contextRunner.withUserConfiguration(Java9Required.class)
this.contextRunner.withUserConfiguration(Java18Required.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
@EnabledOnJre(JRE.JAVA_18)
void doesNotMatchIfLowerIsRequired() {
this.contextRunner.withUserConfiguration(Java7Required.class)
this.contextRunner.withUserConfiguration(OlderThan18Required.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
void matchesIfVersionIsInRange() {
this.contextRunner.withUserConfiguration(Java8Required.class)
this.contextRunner.withUserConfiguration(Java17Required.class)
.run((context) -> assertThat(context).hasSingleBean(String.class));
}
@Test
void boundsTests() {
testBounds(Range.EQUAL_OR_NEWER, JavaVersion.NINE, JavaVersion.EIGHT, true);
testBounds(Range.EQUAL_OR_NEWER, JavaVersion.EIGHT, JavaVersion.EIGHT, true);
testBounds(Range.EQUAL_OR_NEWER, JavaVersion.EIGHT, JavaVersion.NINE, false);
testBounds(Range.OLDER_THAN, JavaVersion.NINE, JavaVersion.EIGHT, false);
testBounds(Range.OLDER_THAN, JavaVersion.EIGHT, JavaVersion.EIGHT, false);
testBounds(Range.OLDER_THAN, JavaVersion.EIGHT, JavaVersion.NINE, true);
testBounds(Range.EQUAL_OR_NEWER, JavaVersion.EIGHTEEN, JavaVersion.SEVENTEEN, true);
testBounds(Range.EQUAL_OR_NEWER, JavaVersion.SEVENTEEN, JavaVersion.SEVENTEEN, true);
testBounds(Range.EQUAL_OR_NEWER, JavaVersion.SEVENTEEN, JavaVersion.EIGHTEEN, false);
testBounds(Range.OLDER_THAN, JavaVersion.EIGHTEEN, JavaVersion.SEVENTEEN, false);
testBounds(Range.OLDER_THAN, JavaVersion.SEVENTEEN, JavaVersion.SEVENTEEN, false);
testBounds(Range.OLDER_THAN, JavaVersion.SEVENTEEN, JavaVersion.EIGHTEEN, true);
}
@Test
void equalOrNewerMessage() {
ConditionOutcome outcome = this.condition.getMatchOutcome(Range.EQUAL_OR_NEWER, JavaVersion.NINE,
JavaVersion.EIGHT);
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnJava (1.8 or newer) found 9");
ConditionOutcome outcome = this.condition.getMatchOutcome(Range.EQUAL_OR_NEWER, JavaVersion.EIGHTEEN,
JavaVersion.SEVENTEEN);
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnJava (17 or newer) found 18");
}
@Test
void olderThanMessage() {
ConditionOutcome outcome = this.condition.getMatchOutcome(Range.OLDER_THAN, JavaVersion.NINE,
JavaVersion.EIGHT);
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnJava (older than 1.8) found 9");
ConditionOutcome outcome = this.condition.getMatchOutcome(Range.OLDER_THAN, JavaVersion.EIGHTEEN,
JavaVersion.SEVENTEEN);
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnJava (older than 17) found 18");
}
@Test
@EnabledOnJre(JRE.JAVA_8)
void java8IsDetected() throws Exception {
assertThat(getJavaVersion()).isEqualTo("1.8");
@EnabledOnJre(JRE.JAVA_17)
void java17IsDetected() throws Exception {
assertThat(getJavaVersion()).isEqualTo("17");
}
@Test
@EnabledOnJre(JRE.JAVA_8)
void java8IsTheFallback() throws Exception {
assertThat(getJavaVersion(Function.class, Files.class, ServiceLoader.class)).isEqualTo("1.8");
@EnabledOnJre(JRE.JAVA_17)
void java17IsTheFallback() throws Exception {
assertThat(getJavaVersion(Console.class)).isEqualTo("17");
}
private String getJavaVersion(Class<?>... hiddenClasses) throws Exception {
@ -117,8 +116,8 @@ class ConditionalOnJavaTests {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnJava(JavaVersion.NINE)
static class Java9Required {
@ConditionalOnJava(JavaVersion.SEVENTEEN)
static class Java17Required {
@Bean
String foo() {
@ -128,8 +127,8 @@ class ConditionalOnJavaTests {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnJava(range = Range.OLDER_THAN, value = JavaVersion.EIGHT)
static class Java7Required {
@ConditionalOnJava(range = Range.OLDER_THAN, value = JavaVersion.EIGHTEEN)
static class OlderThan18Required {
@Bean
String foo() {
@ -139,8 +138,8 @@ class ConditionalOnJavaTests {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnJava(JavaVersion.EIGHT)
static class Java8Required {
@ConditionalOnJava(JavaVersion.EIGHTEEN)
static class Java18Required {
@Bean
String foo() {

@ -32,7 +32,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter;
import org.springframework.boot.system.JavaVersion;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@ -152,14 +151,8 @@ class NoSuchBeanDefinitionFailureAnalyzerTests {
@Test
void failureAnalysisForUnmatchedQualifier() {
FailureAnalysis analysis = analyzeFailure(createFailure(QualifiedBeanConfiguration.class));
assertThat(analysis.getDescription()).containsPattern(determineAnnotationValuePattern());
}
private String determineAnnotationValuePattern() {
if (JavaVersion.getJavaVersion().isEqualOrNewerThan(JavaVersion.FOURTEEN)) {
return "@org.springframework.beans.factory.annotation.Qualifier\\(\"*alpha\"*\\)";
}
return "@org.springframework.beans.factory.annotation.Qualifier\\(value=\"*alpha\"*\\)";
assertThat(analysis.getDescription())
.containsPattern("@org.springframework.beans.factory.annotation.Qualifier\\(\"*alpha\"*\\)");
}
private void assertDescriptionConstructorMissingType(FailureAnalysis analysis, Class<?> component, int index,

@ -46,7 +46,6 @@ import org.springframework.boot.devtools.restart.FailureHandler.Outcome;
import org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles;
import org.springframework.boot.devtools.restart.classloader.RestartClassLoader;
import org.springframework.boot.logging.DeferredLog;
import org.springframework.boot.system.JavaVersion;
import org.springframework.cglib.core.ClassNameReader;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
@ -333,9 +332,6 @@ public class Restarter {
cleanCachedIntrospectionResultsCache();
ReflectionUtils.clearCache();
clearAnnotationUtilsCache();
if (!JavaVersion.getJavaVersion().isEqualOrNewerThan(JavaVersion.NINE)) {
clear("com.sun.naming.internal.ResourceManager", "propertiesCache");
}
}
private void cleanCachedIntrospectionResultsCache() throws Exception {

@ -17,13 +17,10 @@
package org.springframework.boot.system;
import java.io.Console;
import java.lang.invoke.MethodHandles;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.springframework.util.ClassUtils;
@ -36,51 +33,6 @@ import org.springframework.util.ClassUtils;
*/
public enum JavaVersion {
/**
* Java 1.8.
*/
EIGHT("1.8", Optional.class, "empty"),
/**
* Java 9.
*/
NINE("9", Optional.class, "stream"),
/**
* Java 10.
*/
TEN("10", Optional.class, "orElseThrow"),
/**
* Java 11.
*/
ELEVEN("11", String.class, "strip"),
/**
* Java 12.
*/
TWELVE("12", String.class, "describeConstable"),
/**
* Java 13.
*/
THIRTEEN("13", String.class, "stripIndent"),
/**
* Java 14.
*/
FOURTEEN("14", MethodHandles.Lookup.class, "hasFullPrivilegeAccess"),
/**
* Java 15.
*/
FIFTEEN("15", CharSequence.class, "isEmpty"),
/**
* Java 16.
*/
SIXTEEN("16", Stream.class, "toList"),
/**
* Java 17.
*/
@ -117,7 +69,7 @@ public enum JavaVersion {
return candidate;
}
}
return EIGHT;
return SEVENTEEN;
}
/**

@ -36,101 +36,47 @@ class JavaVersionTests {
@Test
void compareToWhenComparingSmallerToGreaterShouldBeLessThanZero() {
assertThat(JavaVersion.EIGHT.compareTo(JavaVersion.NINE)).isLessThan(0);
assertThat(JavaVersion.SEVENTEEN.compareTo(JavaVersion.EIGHTEEN)).isLessThan(0);
}
@Test
void compareToWhenComparingGreaterToSmallerShouldBeGreaterThanZero() {
assertThat(JavaVersion.NINE.compareTo(JavaVersion.EIGHT)).isGreaterThan(0);
assertThat(JavaVersion.EIGHTEEN.compareTo(JavaVersion.SEVENTEEN)).isGreaterThan(0);
}
@Test
void compareToWhenComparingSameShouldBeZero() {
assertThat(JavaVersion.EIGHT.compareTo(JavaVersion.EIGHT)).isEqualTo(0);
assertThat(JavaVersion.SEVENTEEN.compareTo(JavaVersion.SEVENTEEN)).isEqualTo(0);
}
@Test
void isEqualOrNewerThanWhenComparingSameShouldBeTrue() {
assertThat(JavaVersion.EIGHT.isEqualOrNewerThan(JavaVersion.EIGHT)).isTrue();
assertThat(JavaVersion.SEVENTEEN.isEqualOrNewerThan(JavaVersion.SEVENTEEN)).isTrue();
}
@Test
void isEqualOrNewerThanWhenSmallerToGreaterShouldBeFalse() {
assertThat(JavaVersion.EIGHT.isEqualOrNewerThan(JavaVersion.NINE)).isFalse();
assertThat(JavaVersion.SEVENTEEN.isEqualOrNewerThan(JavaVersion.EIGHTEEN)).isFalse();
}
@Test
void isEqualOrNewerThanWhenGreaterToSmallerShouldBeTrue() {
assertThat(JavaVersion.NINE.isEqualOrNewerThan(JavaVersion.EIGHT)).isTrue();
assertThat(JavaVersion.EIGHTEEN.isEqualOrNewerThan(JavaVersion.SEVENTEEN)).isTrue();
}
@Test
void isOlderThanThanWhenComparingSameShouldBeFalse() {
assertThat(JavaVersion.EIGHT.isOlderThan(JavaVersion.EIGHT)).isFalse();
assertThat(JavaVersion.SEVENTEEN.isOlderThan(JavaVersion.SEVENTEEN)).isFalse();
}
@Test
void isOlderThanWhenSmallerToGreaterShouldBeTrue() {
assertThat(JavaVersion.EIGHT.isOlderThan(JavaVersion.NINE)).isTrue();
assertThat(JavaVersion.SEVENTEEN.isOlderThan(JavaVersion.EIGHTEEN)).isTrue();
}
@Test
void isOlderThanWhenGreaterToSmallerShouldBeFalse() {
assertThat(JavaVersion.NINE.isOlderThan(JavaVersion.EIGHT)).isFalse();
}
@Test
@EnabledOnJre(JRE.JAVA_8)
void currentJavaVersionEight() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.EIGHT);
}
@Test
@EnabledOnJre(JRE.JAVA_9)
void currentJavaVersionNine() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.NINE);
}
@Test
@EnabledOnJre(JRE.JAVA_10)
void currentJavaVersionTen() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.TEN);
}
@Test
@EnabledOnJre(JRE.JAVA_11)
void currentJavaVersionEleven() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.ELEVEN);
}
@Test
@EnabledOnJre(JRE.JAVA_12)
void currentJavaVersionTwelve() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.TWELVE);
}
@Test
@EnabledOnJre(JRE.JAVA_13)
void currentJavaVersionThirteen() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.THIRTEEN);
}
@Test
@EnabledOnJre(JRE.JAVA_14)
void currentJavaVersionFourteen() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.FOURTEEN);
}
@Test
@EnabledOnJre(JRE.JAVA_15)
void currentJavaVersionFifteen() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.FIFTEEN);
}
@Test
@EnabledOnJre(JRE.JAVA_16)
void currentJavaVersionSixteen() {
assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.SIXTEEN);
assertThat(JavaVersion.EIGHTEEN.isOlderThan(JavaVersion.SEVENTEEN)).isFalse();
}
@Test

@ -108,7 +108,7 @@ class LoaderIntegrationTests {
}
static JavaRuntime openJdk(JavaVersion version) {
String imageVersion = (version != JavaVersion.EIGHT) ? version.toString() : "8";
String imageVersion = version.toString();
DockerImageName image = DockerImageName.parse("bellsoft/liberica-openjdk-debian:" + imageVersion);
return new JavaRuntime("OpenJDK " + imageVersion, version, () -> new GenericContainer<>(image));
}

Loading…
Cancel
Save