ConditionalOnJavaTests Class — spring-boot Architecture
Architecture documentation for the ConditionalOnJavaTests class in ConditionalOnJavaTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJavaTests.java lines 42–153
class ConditionalOnJavaTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
private final OnJavaCondition condition = new OnJavaCondition();
@Test
@EnabledOnJre(JRE.JAVA_17)
void doesNotMatchIfBetterVersionIsRequired() {
this.contextRunner.withUserConfiguration(Java18Required.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
@EnabledOnJre(JRE.JAVA_18)
void doesNotMatchIfLowerIsRequired() {
this.contextRunner.withUserConfiguration(OlderThan18Required.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
void matchesIfVersionIsInRange() {
this.contextRunner.withUserConfiguration(Java17Required.class)
.run((context) -> assertThat(context).hasSingleBean(String.class));
}
@Test
void boundsTests() {
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.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.EIGHTEEN,
JavaVersion.SEVENTEEN);
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnJava (older than 17) found 18");
}
@Test
@EnabledOnJre(JRE.JAVA_17)
void java17IsDetected() throws Exception {
assertThat(getJavaVersion()).isEqualTo("17");
}
@Test
@EnabledOnJre(JRE.JAVA_17)
void java17IsTheFallback() throws Exception {
assertThat(getJavaVersion(Console.class)).isEqualTo("17");
}
private String getJavaVersion(Class<?>... hiddenClasses) throws Exception {
FilteredClassLoader classLoader = new FilteredClassLoader(hiddenClasses);
Class<?> javaVersionClass = Class.forName(JavaVersion.class.getName(), false, classLoader);
Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, "getJavaVersion");
assertThat(getJavaVersionMethod).isNotNull();
Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null);
classLoader.close();
assertThat(javaVersion).isNotNull();
return javaVersion.toString();
}
private void testBounds(Range range, JavaVersion runningVersion, JavaVersion version, boolean expected) {
ConditionOutcome outcome = this.condition.getMatchOutcome(range, runningVersion, version);
assertThat(outcome.isMatch()).as(outcome.getMessage()).isEqualTo(expected);
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnJava(JavaVersion.SEVENTEEN)
static class Java17Required {
@Bean
String foo() {
return "foo";
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnJava(range = Range.OLDER_THAN, value = JavaVersion.EIGHTEEN)
static class OlderThan18Required {
@Bean
String foo() {
return "foo";
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnJava(JavaVersion.EIGHTEEN)
static class Java18Required {
@Bean
String foo() {
return "foo";
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free