2019-06-25 09:49:52 +02:00
|
|
|
package sonia.scm.lifecycle.modules;
|
2019-06-21 09:29:06 +02:00
|
|
|
|
|
|
|
|
import com.google.inject.AbstractModule;
|
|
|
|
|
import com.google.inject.Guice;
|
|
|
|
|
import com.google.inject.Injector;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
2019-06-25 09:49:52 +02:00
|
|
|
import sonia.scm.EagerSingleton;
|
2019-06-21 09:29:06 +02:00
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
|
class EagerSingletonModuleTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldInitializeEagerSingletons() {
|
|
|
|
|
Injector injector = Guice.createInjector(new EagerSingletonModule(), new EagerTestModule());
|
|
|
|
|
injector.getInstance(EagerSingletonModule.class).initialize(injector);
|
|
|
|
|
|
|
|
|
|
Capturer capturer = injector.getInstance(Capturer.class);
|
|
|
|
|
assertThat(capturer.value).isEqualTo("eager!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class EagerTestModule extends AbstractModule {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void configure() {
|
|
|
|
|
bind(Capturer.class);
|
|
|
|
|
bind(Eager.class);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
|
public static class Capturer {
|
|
|
|
|
private String value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EagerSingleton
|
|
|
|
|
public static class Eager {
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
public Eager(Capturer capturer) {
|
|
|
|
|
capturer.value = "eager!";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|