|
|
|
@ -24,6 +24,7 @@ import org.assertj.core.api.AssertProvider;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
|
|
|
|
|
import org.springframework.boot.BootstrapRegistry.Scope;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.ApplicationListener;
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
@ -74,6 +75,24 @@ class DefaultBootstrapContextTests {
|
|
|
|
|
assertThat(this.context.get(Integer.class)).isEqualTo(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void registerWhenSingletonAlreadyCreatedThrowsException() {
|
|
|
|
|
this.context.register(Integer.class, InstanceSupplier.from(this.counter::getAndIncrement));
|
|
|
|
|
this.context.get(Integer.class);
|
|
|
|
|
assertThatIllegalStateException()
|
|
|
|
|
.isThrownBy(() -> this.context.register(Integer.class, InstanceSupplier.of(100)))
|
|
|
|
|
.withMessage("java.lang.Integer has already been created");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void registerWhenPrototypeAlreadyCreatedReplacesInstance() {
|
|
|
|
|
this.context.register(Integer.class,
|
|
|
|
|
InstanceSupplier.from(this.counter::getAndIncrement).withScope(Scope.PROTOTYPE));
|
|
|
|
|
this.context.get(Integer.class);
|
|
|
|
|
this.context.register(Integer.class, InstanceSupplier.of(100));
|
|
|
|
|
assertThat(this.context.get(Integer.class)).isEqualTo(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void registerWhenAlreadyCreatedThrowsException() {
|
|
|
|
|
this.context.register(Integer.class, InstanceSupplier.from(this.counter::getAndIncrement));
|
|
|
|
@ -146,12 +165,25 @@ class DefaultBootstrapContextTests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void getCreatesOnlyOneInstance() {
|
|
|
|
|
void getWhenSingletonCreatesOnlyOneInstance() {
|
|
|
|
|
this.context.register(Integer.class, InstanceSupplier.from(this.counter::getAndIncrement));
|
|
|
|
|
assertThat(this.context.get(Integer.class)).isEqualTo(0);
|
|
|
|
|
assertThat(this.context.get(Integer.class)).isEqualTo(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void getWhenPrototypeCreatesOnlyNewInstances() {
|
|
|
|
|
this.context.register(Integer.class,
|
|
|
|
|
InstanceSupplier.from(this.counter::getAndIncrement).withScope(Scope.PROTOTYPE));
|
|
|
|
|
assertThat(this.context.get(Integer.class)).isEqualTo(0);
|
|
|
|
|
assertThat(this.context.get(Integer.class)).isEqualTo(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testName() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void getOrElseWhenNoRegistrationReturnsOther() {
|
|
|
|
|
this.context.register(Number.class, InstanceSupplier.of(1));
|
|
|
|
@ -228,6 +260,20 @@ class DefaultBootstrapContextTests {
|
|
|
|
|
assertThat(listener).wasCalledOnlyOnce();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void instanceSupplierGetScopeWhenNotConfiguredReturnsSingleton() {
|
|
|
|
|
InstanceSupplier<String> supplier = InstanceSupplier.of("test");
|
|
|
|
|
assertThat(supplier.getScope()).isEqualTo(Scope.SINGLETON);
|
|
|
|
|
assertThat(supplier.get(null)).isEqualTo("test");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void instanceSupplierWithScopeChangesScope() {
|
|
|
|
|
InstanceSupplier<String> supplier = InstanceSupplier.of("test").withScope(Scope.PROTOTYPE);
|
|
|
|
|
assertThat(supplier.getScope()).isEqualTo(Scope.PROTOTYPE);
|
|
|
|
|
assertThat(supplier.get(null)).isEqualTo("test");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class TestCloseListener
|
|
|
|
|
implements ApplicationListener<BootstrapContextClosedEvent>, AssertProvider<CloseListenerAssert> {
|
|
|
|
|
|
|
|
|
|