diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2014-10-04 12:41:07 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2014-10-04 12:46:46 -0400 |
commit | 8e24a47b69f940c31ac95451b4584b95de7c8670 (patch) | |
tree | 2ed52205fa53ea6f9ed1ec089722d13a521bb3dc /sangria-lazy/src | |
parent | 17a7490cabfd4428cfdb5d9106dc076b1dc4b905 (diff) | |
download | sangria-8e24a47b69f940c31ac95451b4584b95de7c8670.tar.xz |
lazy: Test best practices.
Diffstat (limited to 'sangria-lazy/src')
3 files changed, 45 insertions, 24 deletions
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(); |