summaryrefslogtreecommitdiffstats
path: root/sangria-lazy/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'sangria-lazy/src/test/java')
-rw-r--r--sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java21
-rw-r--r--sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java38
2 files changed, 45 insertions, 14 deletions
diff --git a/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java
index 69fabce..2895c7d 100644
--- a/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java
+++ b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java
@@ -20,12 +20,14 @@ package com.tavianator.sangria.lazy;
import javax.inject.Inject;
import javax.inject.Provider;
+import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Stage;
import org.junit.Test;
+import static com.tavianator.sangria.test.SangriaMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
@@ -62,11 +64,6 @@ public class LazySingletonTest {
test(Stage.PRODUCTION, new SangriaLazyModule());
}
- @Test
- public void testDuplicateModule() {
- test(Stage.PRODUCTION, new SangriaLazyModule(), new SangriaLazyModule());
- }
-
private void test(Stage stage, Module... modules) {
int before = Scoped.INSTANCES.get();
@@ -80,4 +77,18 @@ public class LazySingletonTest {
assertThat(provider.get(), sameInstance(instance));
assertThat(Scoped.INSTANCES.get(), equalTo(before + 1));
}
+
+ @Test
+ public void testBestPractices() {
+ Module module = new AbstractModule() {
+ @Override
+ protected void configure() {
+ install(new SangriaLazyModule());
+ bind(Scoped.class);
+ }
+ };
+
+ assertThat(module, is(atomic()));
+ assertThat(module, followsBestPractices());
+ }
}
diff --git a/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java
index ff63c03..8bea02d 100644
--- a/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java
+++ b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java
@@ -35,6 +35,7 @@ import com.google.inject.spi.Element;
import com.google.inject.spi.Elements;
import org.junit.Test;
+import static com.tavianator.sangria.test.SangriaMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
@@ -69,7 +70,12 @@ public class LazyTest {
}
private static class HasConcrete {
- @Inject Lazy<Concrete> lazy;
+ final Lazy<Concrete> lazy;
+
+ @Inject
+ HasConcrete(Lazy<Concrete> lazy) {
+ this.lazy = lazy;
+ }
}
private static class HasQualifiedAbstract {
@@ -88,6 +94,8 @@ public class LazyTest {
protected void configure() {
binder().requireExplicitBindings();
+ bind(HasConcrete.class);
+
bind(Concrete.class);
LazyBinder.create(binder())
.bind(Concrete.class);
@@ -95,11 +103,26 @@ public class LazyTest {
}));
}
+ @Test
+ public void testBestPractices() {
+ Module module = new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(HasConcrete.class);
+
+ bind(Concrete.class);
+ LazyBinder.create(binder())
+ .bind(Concrete.class);
+ }
+ };
+ assertThat(module, is(atomic()));
+ assertThat(module, followsBestPractices());
+ }
+
private void testHasConcrete(Injector injector) {
int before = Concrete.INSTANCES.get();
- HasConcrete hasConcrete = new HasConcrete();
- injector.injectMembers(hasConcrete);
+ HasConcrete hasConcrete = injector.getInstance(HasConcrete.class);
assertThat(Concrete.INSTANCES.get(), equalTo(before));
Concrete instance = hasConcrete.lazy.get();
@@ -109,8 +132,7 @@ public class LazyTest {
assertThat(instance2, sameInstance(instance));
assertThat(Concrete.INSTANCES.get(), equalTo(before + 1));
- HasConcrete hasConcrete2 = new HasConcrete();
- injector.injectMembers(hasConcrete2);
+ HasConcrete hasConcrete2 = injector.getInstance(HasConcrete.class);
assertThat(Concrete.INSTANCES.get(), equalTo(before + 1));
Concrete instance3 = hasConcrete2.lazy.get();
@@ -150,8 +172,7 @@ public class LazyTest {
private void testQualifiedAbstract(Injector injector) {
int before = Concrete.INSTANCES.get();
- HasQualifiedAbstract hasQualifiedAbstract = new HasQualifiedAbstract();
- injector.injectMembers(hasQualifiedAbstract);
+ HasQualifiedAbstract hasQualifiedAbstract = injector.getInstance(HasQualifiedAbstract.class);
assertThat(Concrete.INSTANCES.get(), equalTo(before));
Abstract instance = hasQualifiedAbstract.lazy.get();
@@ -161,8 +182,7 @@ public class LazyTest {
assertThat(instance2, sameInstance(instance2));
assertThat(Concrete.INSTANCES.get(), equalTo(before + 1));
- HasQualifiedAbstract hasQualifiedAbstract2 = new HasQualifiedAbstract();
- injector.injectMembers(hasQualifiedAbstract2);
+ HasQualifiedAbstract hasQualifiedAbstract2 = injector.getInstance(HasQualifiedAbstract.class);
assertThat(Concrete.INSTANCES.get(), equalTo(before + 1));
Abstract instance3 = hasQualifiedAbstract2.lazy.get();