ConditionalOnJndiTests Class — spring-boot Architecture
Architecture documentation for the ConditionalOnJndiTests class in ConditionalOnJndiTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJndiTests.java lines 48–181
class ConditionalOnJndiTests {
private ClassLoader threadContextClassLoader;
private @Nullable String initialContextFactory;
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
private final MockableOnJndi condition = new MockableOnJndi();
@BeforeEach
void setupThreadContextClassLoader() {
this.threadContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(new JndiPropertiesHidingClassLoader(getClass().getClassLoader()));
}
@AfterEach
void close() {
TestableInitialContextFactory.clearAll();
if (this.initialContextFactory != null) {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, this.initialContextFactory);
}
else {
System.clearProperty(Context.INITIAL_CONTEXT_FACTORY);
}
Thread.currentThread().setContextClassLoader(this.threadContextClassLoader);
}
@Test
void jndiNotAvailable() {
this.contextRunner.withUserConfiguration(JndiAvailableConfiguration.class, JndiConditionConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
void jndiAvailable() {
setupJndi();
this.contextRunner.withUserConfiguration(JndiAvailableConfiguration.class, JndiConditionConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(String.class));
}
@Test
void jndiLocationNotBound() {
setupJndi();
this.contextRunner.withUserConfiguration(JndiConditionConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
void jndiLocationBound() {
setupJndi();
TestableInitialContextFactory.bind("java:/FooManager", new Object());
this.contextRunner.withUserConfiguration(JndiConditionConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(String.class));
}
@Test
void jndiLocationNotFound() {
ConditionOutcome outcome = this.condition.getMatchOutcome(mock(ConditionContext.class),
mockMetadata("java:/a"));
assertThat(outcome.isMatch()).isFalse();
}
@Test
void jndiLocationFound() {
this.condition.setFoundLocation("java:/b");
ConditionOutcome outcome = this.condition.getMatchOutcome(mock(ConditionContext.class),
mockMetadata("java:/a", "java:/b"));
assertThat(outcome.isMatch()).isTrue();
}
private void setupJndi() {
this.initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, TestableInitialContextFactory.class.getName());
}
private AnnotatedTypeMetadata mockMetadata(String... value) {
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class);
Map<String, Object> attributes = new HashMap<>();
attributes.put("value", value);
given(metadata.getAnnotationAttributes(ConditionalOnJndi.class.getName())).willReturn(attributes);
return metadata;
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnJndi
static class JndiAvailableConfiguration {
@Bean
String foo() {
return "foo";
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnJndi("java:/FooManager")
static class JndiConditionConfiguration {
@Bean
String foo() {
return "foo";
}
}
static class MockableOnJndi extends OnJndiCondition {
private final boolean jndiAvailable = true;
private @Nullable String foundLocation;
@Override
protected boolean isJndiAvailable() {
return this.jndiAvailable;
}
@Override
protected JndiLocator getJndiLocator(String[] locations) {
return new JndiLocator(locations) {
@Override
public @Nullable String lookupFirstLocation() {
return MockableOnJndi.this.foundLocation;
}
};
}
void setFoundLocation(String foundLocation) {
this.foundLocation = foundLocation;
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free