diff options
Diffstat (limited to 'sangria-lazy')
4 files changed, 51 insertions, 24 deletions
diff --git a/sangria-lazy/pom.xml b/sangria-lazy/pom.xml index 4840af3..e2c806e 100644 --- a/sangria-lazy/pom.xml +++ b/sangria-lazy/pom.xml @@ -38,6 +38,12 @@ </dependency> <dependency> + <groupId>com.tavianator.sangria</groupId> + <artifactId>sangria-test</artifactId> + <scope>test</scope> + </dependency> + + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/SangriaLazyModule.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/SangriaLazyModule.java index ff4a02a..57cf763 100644 --- a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/SangriaLazyModule.java +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/SangriaLazyModule.java @@ -31,14 +31,4 @@ public class SangriaLazyModule extends AbstractModule { protected void configure() { bindScope(LazySingleton.class, LazyScopes.LAZY_SINGLETON); } - - @Override - public boolean equals(Object o) { - return o instanceof SangriaLazyModule; - } - - @Override - public int hashCode() { - return SangriaLazyModule.class.hashCode(); - } } 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(); |