mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 17:05:43 +01:00
added modules, classloading and view packages to sonia.scm.lifecycle
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package sonia.scm.lifecycle;
|
||||
package sonia.scm.lifecycle.classloading;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -0,0 +1,32 @@
|
||||
package sonia.scm.lifecycle.modules;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class CloseableModuleTest {
|
||||
|
||||
@Test
|
||||
void shouldCloseCloseables() {
|
||||
Injector injector = Guice.createInjector(new CloseableModule());
|
||||
CloseMe closeMe = injector.getInstance(CloseMe.class);
|
||||
|
||||
injector.getInstance(CloseableModule.class).closeAll();
|
||||
assertThat(closeMe.closed).isTrue();
|
||||
}
|
||||
|
||||
public static class CloseMe implements Closeable {
|
||||
|
||||
private boolean closed = false;
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
this.closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package sonia.scm.lifecycle.modules;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sonia.scm.EagerSingleton;
|
||||
|
||||
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!";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package sonia.scm.lifecycle;
|
||||
package sonia.scm.lifecycle.modules;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.inject.AbstractModule;
|
||||
@@ -10,10 +10,11 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.CloseableModule;
|
||||
import sonia.scm.lifecycle.modules.CloseableModule;
|
||||
import sonia.scm.Default;
|
||||
import sonia.scm.EagerSingleton;
|
||||
import sonia.scm.EagerSingletonModule;
|
||||
import sonia.scm.lifecycle.modules.EagerSingletonModule;
|
||||
import sonia.scm.lifecycle.modules.InjectionLifeCycle;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
@@ -0,0 +1,40 @@
|
||||
package sonia.scm.lifecycle.modules;
|
||||
|
||||
import com.google.inject.TypeLiteral;
|
||||
import com.google.inject.matcher.Matcher;
|
||||
import org.assertj.core.api.AbstractBooleanAssert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
import java.io.Serializable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MoreMatchersTest {
|
||||
|
||||
@Test
|
||||
void shouldMatchSubTypes() {
|
||||
Matcher<TypeLiteral> matcher = MoreMatchers.isSubtypeOf(Serializable.class);
|
||||
assertBoolean(matcher, One.class).isTrue();
|
||||
assertBoolean(matcher, Two.class).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMatchIfAnnotated() {
|
||||
Matcher<TypeLiteral> matcher = MoreMatchers.isAnnotatedWith(Singleton.class);
|
||||
assertBoolean(matcher, One.class).isFalse();
|
||||
assertBoolean(matcher, Two.class).isTrue();
|
||||
}
|
||||
|
||||
private AbstractBooleanAssert<?> assertBoolean(Matcher<TypeLiteral> matcher, Class<?> clazz) {
|
||||
return assertThat(matcher.matches(TypeLiteral.get(clazz)));
|
||||
}
|
||||
|
||||
public static class One implements Serializable {
|
||||
}
|
||||
|
||||
@Singleton
|
||||
public static class Two {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package sonia.scm.lifecycle.modules;
|
||||
|
||||
import com.github.legman.Subscribe;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sonia.scm.event.LegmanScmEventBus;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ScmEventBusModuleTest {
|
||||
|
||||
@Test
|
||||
void shouldRegisterInstance() {
|
||||
LegmanScmEventBus eventBus = new LegmanScmEventBus();
|
||||
|
||||
Injector injector = Guice.createInjector(new ScmEventBusModule(eventBus));
|
||||
Listener listener = injector.getInstance(Listener.class);
|
||||
|
||||
eventBus.post("hello");
|
||||
|
||||
assertThat(listener.message).isEqualTo("hello");
|
||||
}
|
||||
|
||||
public static class Listener {
|
||||
|
||||
private String message;
|
||||
|
||||
@Subscribe(async = false)
|
||||
public void receive(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package sonia.scm.lifecycle.modules;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sonia.scm.Initable;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ScmInitializerModuleTest {
|
||||
|
||||
@Test
|
||||
void shouldInitializeInstances() {
|
||||
Injector injector = Guice.createInjector(new ScmInitializerModule());
|
||||
InitializeMe instance = injector.getInstance(InitializeMe.class);
|
||||
|
||||
assertThat(instance.initialized).isTrue();
|
||||
}
|
||||
|
||||
public static class InitializeMe implements Initable {
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
@Override
|
||||
public void init(SCMContextProvider context) {
|
||||
this.initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package sonia.scm.lifecycle.modules;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ServletContextListenerHolderTest {
|
||||
|
||||
@Test
|
||||
void shouldInitializeEveryContextListener() {
|
||||
CountingListener one = new CountingListener(41);
|
||||
CountingListener two = new CountingListener(41);
|
||||
|
||||
ServletContextListenerHolder holder = createHolder(one, two);
|
||||
holder.contextInitialized(null);
|
||||
|
||||
assertThat(one.counter).isEqualTo(42);
|
||||
assertThat(one.counter).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDestroyEveryContextListener() {
|
||||
CountingListener one = new CountingListener(43);
|
||||
CountingListener two = new CountingListener(43);
|
||||
|
||||
ServletContextListenerHolder holder = createHolder(one, two);
|
||||
holder.contextDestroyed(null);
|
||||
|
||||
assertThat(one.counter).isEqualTo(42);
|
||||
assertThat(one.counter).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotFailWithoutServletContextListenerBound(){
|
||||
Injector injector = Guice.createInjector();
|
||||
ServletContextListenerHolder holder = injector.getInstance(ServletContextListenerHolder.class);
|
||||
holder.contextInitialized(null);
|
||||
holder.contextDestroyed(null);
|
||||
}
|
||||
|
||||
private ServletContextListenerHolder createHolder(CountingListener one, CountingListener two) {
|
||||
Injector injector = Guice.createInjector(new ListenerModule(one, two));
|
||||
return injector.getInstance(ServletContextListenerHolder.class);
|
||||
}
|
||||
|
||||
public static class ListenerModule extends AbstractModule {
|
||||
|
||||
private final Set<ServletContextListener> listeners;
|
||||
|
||||
ListenerModule(ServletContextListener... listeners) {
|
||||
this.listeners = new HashSet<>(Arrays.asList(listeners));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(new Key<Set<ServletContextListener>>(){}).toInstance(listeners);
|
||||
}
|
||||
}
|
||||
|
||||
public static class CountingListener implements ServletContextListener {
|
||||
|
||||
private int counter;
|
||||
|
||||
CountingListener(int counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
counter++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
counter--;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package sonia.scm.lifecycle;
|
||||
package sonia.scm.lifecycle.modules;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
@@ -1,4 +1,4 @@
|
||||
package sonia.scm.lifecycle;
|
||||
package sonia.scm.lifecycle.view;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -1,4 +1,4 @@
|
||||
package sonia.scm.lifecycle;
|
||||
package sonia.scm.lifecycle.view;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.StaticResourceServlet;
|
||||
import sonia.scm.lifecycle.modules.ModuleProvider;
|
||||
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
Reference in New Issue
Block a user