From 0ccd57285f50ff84429c8d80234e61f75c3e0e51 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 28 Sep 2017 10:38:33 +0200 Subject: [PATCH] Move JavaVersion to a reusable location. --- .../condition/ConditionalOnJava.java | 68 +---------------- .../condition/OnJavaCondition.java | 22 +++++- .../condition/ConditionalOnJavaTests.java | 4 +- .../boot/system/JavaVersion.java | 73 +++++++++++++++++++ .../boot/system/JavaVersionTests.java | 50 +++++++++++++ 5 files changed, 146 insertions(+), 71 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJava.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJava.java index 735fe2f987..84adc6bcf4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJava.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJava.java @@ -22,9 +22,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.boot.system.JavaVersion; import org.springframework.context.annotation.Conditional; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; /** * {@link Conditional} that matches based on the JVM version the application is running @@ -73,69 +72,4 @@ public @interface ConditionalOnJava { } - /** - * Java versions. - */ - enum JavaVersion { - - /** - * Java 1.9. - */ - NINE(9, "1.9", "java.security.cert.URICertStoreParameters"), - - /** - * Java 1.8. - */ - EIGHT(8, "1.8", "java.util.function.Function"); - - private final int value; - - private final String name; - - private final boolean available; - - JavaVersion(int value, String name, String className) { - this.value = value; - this.name = name; - this.available = ClassUtils.isPresent(className, getClass().getClassLoader()); - } - - /** - * Determines if this version is within the specified range of versions. - * @param range the range - * @param version the bounds of the range - * @return if this version is within the specified range - */ - public boolean isWithin(Range range, JavaVersion version) { - Assert.notNull(range, "Range must not be null"); - Assert.notNull(version, "Version must not be null"); - switch (range) { - case EQUAL_OR_NEWER: - return this.value >= version.value; - case OLDER_THAN: - return this.value < version.value; - } - throw new IllegalStateException("Unknown range " + range); - } - - @Override - public String toString() { - return this.name; - } - - /** - * Returns the {@link JavaVersion} of the current runtime. - * @return the {@link JavaVersion} - */ - public static JavaVersion getJavaVersion() { - for (JavaVersion candidate : JavaVersion.values()) { - if (candidate.available) { - return candidate; - } - } - return EIGHT; - } - - } - } 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 a37b05461a..999bf0d9fe 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 @@ -18,8 +18,8 @@ package org.springframework.boot.autoconfigure.condition; import java.util.Map; -import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion; import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range; +import org.springframework.boot.system.JavaVersion; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.Ordered; @@ -51,7 +51,7 @@ class OnJavaCondition extends SpringBootCondition { protected ConditionOutcome getMatchOutcome(Range range, JavaVersion runningVersion, JavaVersion version) { - boolean match = runningVersion.isWithin(range, version); + boolean match = isWithin(runningVersion, range, version); String expected = String.format( range == Range.EQUAL_OR_NEWER ? "(%s or newer)" : "(older than %s)", version); @@ -61,4 +61,22 @@ class OnJavaCondition extends SpringBootCondition { return new ConditionOutcome(match, message); } + /** + * Determines if the {@code runningVersion} is within the specified range of versions. + * @param runningVersion the current version. + * @param range the range + * @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); + if (range == Range.EQUAL_OR_NEWER) { + return i >= 0; + } + else if (range == Range.OLDER_THAN) { + return i < 0; + } + 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 2f97af2896..dd61dc4e23 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 @@ -27,8 +27,8 @@ import java.util.function.Function; import org.junit.Test; -import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion; import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range; +import org.springframework.boot.system.JavaVersion; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -107,7 +107,7 @@ public class ConditionalOnJavaTests { URL[] urls = ((URLClassLoader) getClass().getClassLoader()).getURLs(); URLClassLoader classLoader = new ClassHidingClassLoader(urls, hiddenClasses); Class javaVersionClass = classLoader - .loadClass(ConditionalOnJava.JavaVersion.class.getName()); + .loadClass(JavaVersion.class.getName()); Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, "getJavaVersion"); Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null); 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 new file mode 100644 index 0000000000..db7f485981 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java @@ -0,0 +1,73 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.system; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.springframework.util.ClassUtils; + +/** + * Supported Java versions. + * + * @author Oliver Gierke + * @author Phillip Webb + * @since 2.0.0 + */ +public enum JavaVersion { + + /** + * Java 1.8. + */ + EIGHT("1.8", "java.util.function.Function"), + + /** + * Java 1.9. + */ + NINE("1.9", "java.security.cert.URICertStoreParameters"); + + private final String name; + + private final boolean available; + + JavaVersion(String name, String className) { + this.name = name; + this.available = ClassUtils.isPresent(className, getClass().getClassLoader()); + } + + @Override + public String toString() { + return this.name; + } + + /** + * Returns the {@link JavaVersion} of the current runtime. + * @return the {@link JavaVersion} + */ + public static JavaVersion getJavaVersion() { + List candidates = Arrays.asList(JavaVersion.values()); + Collections.reverse(candidates); + for (JavaVersion candidate : candidates) { + if (candidate.available) { + return candidate; + } + } + return EIGHT; + } + +} 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 new file mode 100644 index 0000000000..d32834ab86 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.system; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link JavaVersion}. + * + * @author Stephane Nicoll + */ +public class JavaVersionTests { + + @Test + public void currentVersionIsAvailable() { + assertThat(JavaVersion.getJavaVersion()).isNotNull(); + } + + @Test + public void java8IsOlderThanJava9() { + assertThat(JavaVersion.EIGHT.compareTo(JavaVersion.NINE)).isLessThan(0); + } + + @Test + public void java9IsNewerThanJava8() { + assertThat(JavaVersion.NINE.compareTo(JavaVersion.EIGHT)).isGreaterThan(0); + } + + @Test + public void comparisonOfSameVersion() { + assertThat(JavaVersion.EIGHT.compareTo(JavaVersion.EIGHT)).isEqualTo(0); + } + +}