From 8e24a47b69f940c31ac95451b4584b95de7c8670 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 4 Oct 2014 12:41:07 -0400 Subject: lazy: Test best practices. --- sangria-lazy/pom.xml | 6 ++++ .../tavianator/sangria/lazy/SangriaLazyModule.java | 10 ------ .../tavianator/sangria/lazy/LazySingletonTest.java | 21 +++++++++--- .../java/com/tavianator/sangria/lazy/LazyTest.java | 38 +++++++++++++++++----- 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 @@ -37,6 +37,12 @@ true + + com.tavianator.sangria + sangria-test + test + + junit junit 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 lazy; + final Lazy lazy; + + @Inject + HasConcrete(Lazy 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(); -- cgit v1.2.3