diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnJavaCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnJavaCondition.java index 999bf0d9fe..f99eafe7c1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnJavaCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnJavaCondition.java @@ -68,13 +68,13 @@ class OnJavaCondition extends SpringBootCondition { * @param version the bounds of the range * @return if this version is within the specified range */ - private boolean isWithin(JavaVersion runningVersion, Range range, JavaVersion version) { - int i = runningVersion.compareTo(version); + private boolean isWithin(JavaVersion runningVersion, Range range, + JavaVersion version) { if (range == Range.EQUAL_OR_NEWER) { - return i >= 0; + return runningVersion.isEqualOrNewerThan(version); } - else if (range == Range.OLDER_THAN) { - return i < 0; + if (range == Range.OLDER_THAN) { + return runningVersion.isOlderThan(version); } throw new IllegalStateException("Unknown range " + range); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJavaTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJavaTests.java index 590d647ad6..1dafd14291 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJavaTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJavaTests.java @@ -17,11 +17,7 @@ package org.springframework.boot.autoconfigure.condition; import java.lang.reflect.Method; -import java.net.URL; -import java.net.URLClassLoader; import java.nio.file.Files; -import java.util.Arrays; -import java.util.List; import java.util.ServiceLoader; import java.util.function.Function; @@ -110,8 +106,7 @@ public class ConditionalOnJavaTests { private String getJavaVersion(Class... hiddenClasses) throws Exception { HideClassesClassLoader classLoader = new HideClassesClassLoader(hiddenClasses); - Class javaVersionClass = classLoader - .loadClass(JavaVersion.class.getName()); + Class javaVersionClass = classLoader.loadClass(JavaVersion.class.getName()); Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, "getJavaVersion"); Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null); @@ -126,34 +121,6 @@ public class ConditionalOnJavaTests { assertThat(outcome.isMatch()).as(outcome.getMessage()).isEqualTo(expected); } - private final class ClassHidingClassLoader extends URLClassLoader { - - private final List> hiddenClasses; - - private ClassHidingClassLoader(URL[] urls, Class... hiddenClasses) { - super(urls, null); - this.hiddenClasses = Arrays.asList(hiddenClasses); - } - - @Override - public Class loadClass(String name) throws ClassNotFoundException { - if (isHidden(name)) { - throw new ClassNotFoundException(); - } - return super.loadClass(name); - } - - private boolean isHidden(String name) { - for (Class hiddenClass : this.hiddenClasses) { - if (hiddenClass.getName().equals(name)) { - return true; - } - } - return false; - } - - } - @Configuration @ConditionalOnJava(JavaVersion.NINE) static class Java9Required { diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/Assume.java b/spring-boot-test/src/main/java/org/springframework/boot/test/Assume.java index 4bfcb2c91a..e1928db57a 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/Assume.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/Assume.java @@ -36,10 +36,10 @@ public abstract class Assume { * @throws AssumptionViolatedException if the assumption fails */ public static void javaVersion(JavaVersion version) { - JavaVersion currentVersion = JavaVersion.getJavaVersion(); - boolean outcome = currentVersion.compareTo(JavaVersion.NINE) < 0; - org.junit.Assume.assumeTrue(String.format( - "This test should run on %s (got %s)", version, currentVersion), outcome); + JavaVersion current = JavaVersion.getJavaVersion(); + org.junit.Assume.assumeTrue( + String.format("This test should run on %s (got %s)", version, current), + current.isOlderThan(version)); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java b/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java index db7f485981..abd1ca4d4e 100644 --- a/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java +++ b/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java @@ -70,4 +70,22 @@ public enum JavaVersion { return EIGHT; } + /** + * Return if this version is equal to or newer than a given version. + * @param version the version to compare + * @return {@code true} if this version is equal to or newer than {@code version} + */ + public boolean isEqualOrNewerThan(JavaVersion version) { + return compareTo(version) >= 0; + } + + /** + * Return if this version is older than a given version. + * @param version the version to compare + * @return {@code true} if this version is older than {@code version} + */ + public boolean isOlderThan(JavaVersion version) { + return compareTo(version) < 0; + } + } diff --git a/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java b/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java index d32834ab86..dacccb4497 100644 --- a/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java @@ -28,23 +28,53 @@ import static org.assertj.core.api.Assertions.assertThat; public class JavaVersionTests { @Test - public void currentVersionIsAvailable() { + public void getJavaVersionShouldBeAvailable() { assertThat(JavaVersion.getJavaVersion()).isNotNull(); } @Test - public void java8IsOlderThanJava9() { + public void compareToWhenComparingSmallerToGreaterShouldBeLessThanZero() { assertThat(JavaVersion.EIGHT.compareTo(JavaVersion.NINE)).isLessThan(0); } @Test - public void java9IsNewerThanJava8() { + public void compareToWhenComparingGreaterToSmallerShouldBeGreaterThanZero() { assertThat(JavaVersion.NINE.compareTo(JavaVersion.EIGHT)).isGreaterThan(0); } @Test - public void comparisonOfSameVersion() { + public void compareToWhenComparingSameShouldBeZero() { assertThat(JavaVersion.EIGHT.compareTo(JavaVersion.EIGHT)).isEqualTo(0); } + @Test + public void isEqualOrNewerThanWhenComparingSameShouldBeTrue() throws Exception { + assertThat(JavaVersion.EIGHT.isEqualOrNewerThan(JavaVersion.EIGHT)).isTrue(); + } + + @Test + public void isEqualOrNewerThanWhenSmallerToGreaterShouldBeFalse() throws Exception { + assertThat(JavaVersion.EIGHT.isEqualOrNewerThan(JavaVersion.NINE)).isFalse(); + } + + @Test + public void isEqualOrNewerThanWhenGreaterToSmallerShouldBeTrue() throws Exception { + assertThat(JavaVersion.NINE.isEqualOrNewerThan(JavaVersion.EIGHT)).isTrue(); + } + + @Test + public void isOlderThanThanWhenComparingSameShouldBeFalse() throws Exception { + assertThat(JavaVersion.EIGHT.isOlderThan(JavaVersion.EIGHT)).isFalse(); + } + + @Test + public void isOlderThanWhenSmallerToGreaterShouldBeTrue() throws Exception { + assertThat(JavaVersion.EIGHT.isOlderThan(JavaVersion.NINE)).isTrue(); + } + + @Test + public void isOlderThanWhenGreaterToSmallerShouldBeFalse() throws Exception { + assertThat(JavaVersion.NINE.isOlderThan(JavaVersion.EIGHT)).isFalse(); + } + }