pull/10447/head
Phillip Webb 7 years ago
parent 52babaa2d2
commit 93f6168fd0

@ -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);
}

@ -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<Class<?>> 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 {

@ -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));
}
}

@ -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;
}
}

@ -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();
}
}

Loading…
Cancel
Save