close every registered Closeable, instead of explicitly close a special set of instances

This commit is contained in:
Sebastian Sdorra
2019-06-21 08:41:26 +02:00
parent 43777f1e27
commit bacdf4d711
6 changed files with 193 additions and 15 deletions

View File

@@ -0,0 +1,32 @@
package sonia.scm;
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;
}
}
}

View File

@@ -0,0 +1,40 @@
package sonia.scm;
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 {
}
}