From ba03ed231044de12662be3a616dfb9a52297c514 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 26 Sep 2014 09:24:43 -0400 Subject: Upgrade Guice dependency to 4.0-beta5. --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 3ae6035..b57fb55 100644 --- a/pom.xml +++ b/pom.xml @@ -87,13 +87,13 @@ com.google.inject guice - 4.0-beta4 + 4.0-beta5 com.google.inject.extensions guice-multibindings - 4.0-beta4 + 4.0-beta5 -- cgit v1.2.3 From 5a08861fcb8c5bc5831a6b99c192aba0a7933952 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 4 Oct 2014 11:46:20 -0400 Subject: test: New sangria-test project. --- pom.xml | 7 ++ sangria-test/pom.xml | 51 ++++++++++++ .../com/tavianator/sangria/test/AtomicMatcher.java | 38 +++++++++ .../sangria/test/BestPracticesMatcher.java | 42 ++++++++++ .../tavianator/sangria/test/SangriaMatchers.java | 31 ++++++++ .../com/tavianator/sangria/test/package-info.java | 25 ++++++ .../sangria/test/SangriaMatchersTest.java | 91 ++++++++++++++++++++++ 7 files changed, 285 insertions(+) create mode 100644 sangria-test/pom.xml create mode 100644 sangria-test/src/main/java/com/tavianator/sangria/test/AtomicMatcher.java create mode 100644 sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java create mode 100644 sangria-test/src/main/java/com/tavianator/sangria/test/SangriaMatchers.java create mode 100644 sangria-test/src/main/java/com/tavianator/sangria/test/package-info.java create mode 100644 sangria-test/src/test/java/com/tavianator/sangria/test/SangriaMatchersTest.java diff --git a/pom.xml b/pom.xml index b57fb55..90d166e 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,12 @@ ${project.version} + + com.tavianator.sangria + sangria-test + ${project.version} + + com.tavianator.sangria sangria-contextual @@ -290,6 +296,7 @@ sangria-core + sangria-test sangria-contextual sangria-slf4j sangria-log4j diff --git a/sangria-test/pom.xml b/sangria-test/pom.xml new file mode 100644 index 0000000..cdb835d --- /dev/null +++ b/sangria-test/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + + com.tavianator.sangria + sangria + 1.2-SNAPSHOT + + + sangria-test + jar + Sangria Test + Utilities for testing with Guice + + + + com.google.inject + guice + + + + com.google.guava + guava + + + + com.google.code.findbugs + jsr305 + true + + + + junit + junit + + + + org.hamcrest + hamcrest-integration + + + + org.mockito + mockito-core + test + + + diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/AtomicMatcher.java b/sangria-test/src/main/java/com/tavianator/sangria/test/AtomicMatcher.java new file mode 100644 index 0000000..03c897e --- /dev/null +++ b/sangria-test/src/main/java/com/tavianator/sangria/test/AtomicMatcher.java @@ -0,0 +1,38 @@ +package com.tavianator.sangria.test; + +import com.google.inject.CreationException; +import com.google.inject.Guice; +import com.google.inject.Module; +import com.google.inject.spi.Elements; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +/** + * Matcher that checks whether a {@link Module} can be installed multiple times. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +final class AtomicMatcher extends TypeSafeDiagnosingMatcher { + @Override + protected boolean matchesSafely(Module item, Description mismatchDescription) { + // Pass through the SPI to make sure the Module is atomic regardless of its equals() implementation + // This ensures atomicity even through Modules.override(), for example + Module copy1 = Elements.getModule(Elements.getElements(item)); + Module copy2 = Elements.getModule(Elements.getElements(item)); + + try { + Guice.createInjector(copy1, copy2); + return true; + } catch (CreationException e) { + mismatchDescription.appendValue(e); + return false; + } + } + + @Override + public void describeTo(Description description) { + description.appendText("an atomic Module"); + } +} diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java b/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java new file mode 100644 index 0000000..36eea97 --- /dev/null +++ b/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java @@ -0,0 +1,42 @@ +package com.tavianator.sangria.test; + +import com.google.inject.AbstractModule; +import com.google.inject.CreationException; +import com.google.inject.Guice; +import com.google.inject.Module; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +/** + * Matcher that checks whether a {@link Module} follows Guice best practices. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +final class BestPracticesMatcher extends TypeSafeDiagnosingMatcher { + private static final class EnforcerModule extends AbstractModule { + @Override + protected void configure() { + binder().requireAtInjectOnConstructors(); + binder().requireExactBindingAnnotations(); + binder().requireExplicitBindings(); + } + } + + @Override + protected boolean matchesSafely(Module item, Description mismatchDescription) { + try { + Guice.createInjector(item, new EnforcerModule()); + return true; + } catch (CreationException e) { + mismatchDescription.appendValue(e); + return false; + } + } + + @Override + public void describeTo(Description description) { + description.appendText("a Module following Guice best practices"); + } +} diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/SangriaMatchers.java b/sangria-test/src/main/java/com/tavianator/sangria/test/SangriaMatchers.java new file mode 100644 index 0000000..da8dd74 --- /dev/null +++ b/sangria-test/src/main/java/com/tavianator/sangria/test/SangriaMatchers.java @@ -0,0 +1,31 @@ +package com.tavianator.sangria.test; + +import com.google.inject.Module; +import org.hamcrest.Matcher; + +/** + * Guice-related Hamcrest matchers. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public final class SangriaMatchers { + private SangriaMatchers() { + // Not for instantiating + } + + /** + * @return A {@link Matcher} that checks whether a {@link Module}'s bindings can be de-duplicated successfully. + */ + public static Matcher atomic() { + return new AtomicMatcher(); + } + + /** + * @return A {@link Matcher} that checks whether a {@link Module} follows Guice best practices. + */ + public static Matcher followsBestPractices() { + return new BestPracticesMatcher(); + } +} diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/package-info.java b/sangria-test/src/main/java/com/tavianator/sangria/test/package-info.java new file mode 100644 index 0000000..0375640 --- /dev/null +++ b/sangria-test/src/main/java/com/tavianator/sangria/test/package-info.java @@ -0,0 +1,25 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +/** + * {@code sangria-test}: Utilities for testing with Guice. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +package com.tavianator.sangria.test; diff --git a/sangria-test/src/test/java/com/tavianator/sangria/test/SangriaMatchersTest.java b/sangria-test/src/test/java/com/tavianator/sangria/test/SangriaMatchersTest.java new file mode 100644 index 0000000..06fb45e --- /dev/null +++ b/sangria-test/src/test/java/com/tavianator/sangria/test/SangriaMatchersTest.java @@ -0,0 +1,91 @@ +package com.tavianator.sangria.test; + +import javax.inject.Inject; +import javax.inject.Named; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import org.junit.Test; + +import static com.tavianator.sangria.test.SangriaMatchers.*; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * Tests for {@link SangriaMatchers}. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public class SangriaMatchersTest { + private static class AtomicModule extends AbstractModule { + private static final Object INSTANCE = new Object(); + + @Override + protected void configure() { + bind(Object.class) + .toInstance(INSTANCE); + } + } + + private static class NonAtomicModule extends AbstractModule { + @Override + protected void configure() { + bind(Object.class) + .toInstance(new Object()); + } + } + + @Test + public void testAtomic() { + assertThat(new AtomicModule(), is(atomic())); + assertThat(new NonAtomicModule(), is(not(atomic()))); + } + + private static class NoAtInjectModule extends AbstractModule { + @Override + protected void configure() { + bind(String.class); + } + } + + private static class InexactBindingAnnotationModule extends AbstractModule { + @Override + protected void configure() { + bind(String.class) + .annotatedWith(Named.class) + .toInstance("test"); + } + + @Provides + String getString(@Named("test") String test) { + return test; + } + } + + private static class Injectable { + @Inject + Injectable() { + } + } + + private static class JustInTimeModule extends AbstractModule { + @Override + protected void configure() { + } + + @Provides + String getString(Injectable injectable) { + return "test"; + } + } + + @Test + public void testFollowsBestPractices() { + assertThat(new AtomicModule(), followsBestPractices()); + assertThat(new NoAtInjectModule(), not(followsBestPractices())); + assertThat(new InexactBindingAnnotationModule(), not(followsBestPractices())); + assertThat(new JustInTimeModule(), not(followsBestPractices())); + } +} -- cgit v1.2.3 From b8c1ca531321dbcee3ae237bad20fa8691b31f83 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 4 Oct 2014 12:24:51 -0400 Subject: contextual: Make ContextSensitiveBinder work with requireExplicitBindings(). Same as regular toProvider() bindings, an explicit binding for the provider is *not* required, unless to provider itself is injected somewhere. This is accomplished by generating a unique linked-key binding for each ContextSensitiveProvider. --- sangria-contextual/pom.xml | 6 ++++++ .../sangria/contextual/ContextSensitiveBinder.java | 18 +++++++++++++++--- .../contextual/ContextSensitiveBinderTest.java | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/sangria-contextual/pom.xml b/sangria-contextual/pom.xml index c939076..702616f 100644 --- a/sangria-contextual/pom.xml +++ b/sangria-contextual/pom.xml @@ -37,6 +37,12 @@ true + + com.tavianator.sangria + sangria-test + test + + junit junit diff --git a/sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java b/sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java index 6d5e502..fdc1ad0 100644 --- a/sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java +++ b/sangria-contextual/src/main/java/com/tavianator/sangria/contextual/ContextSensitiveBinder.java @@ -40,6 +40,7 @@ import com.google.inject.spi.ProviderWithExtensionVisitor; import com.google.inject.spi.ProvisionListener; import com.tavianator.sangria.core.DelayedError; +import com.tavianator.sangria.core.UniqueAnnotations; /** * A binder for {@link ContextSensitiveProvider}s. @@ -139,10 +140,17 @@ public class ContextSensitiveBinder { public void toContextSensitiveProvider(Key> key) { error.cancel(); - binder.bind(bindingKey).toProvider(new ProviderKeyAdapter<>(key)); + binder.bind(bindingKey).toProvider(new ProviderKeyAdapter<>(key, makeLinkedKey(key))); binder.bindListener(new BindingMatcher(bindingKey), new Trigger(bindingKey)); } + private Key makeLinkedKey(Key key) { + Key linkedKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); + binder.bind(linkedKey) + .to(key); + return linkedKey; + } + @Override public void toContextSensitiveProvider(ContextSensitiveProvider provider) { error.cancel(); @@ -190,15 +198,19 @@ public class ContextSensitiveBinder { private static class ProviderKeyAdapter extends ProviderAdapter implements ContextSensitiveProviderKeyBinding { private final Key> providerKey; + private final Key> linkedKey; private Provider> provider; - ProviderKeyAdapter(Key> providerKey) { + ProviderKeyAdapter( + Key> providerKey, + Key> linkedKey) { this.providerKey = providerKey; + this.linkedKey = linkedKey; } @Inject void inject(Injector injector) { - provider = injector.getProvider(providerKey); + provider = injector.getProvider(linkedKey); } @Override diff --git a/sangria-contextual/src/test/java/com/tavianator/sangria/contextual/ContextSensitiveBinderTest.java b/sangria-contextual/src/test/java/com/tavianator/sangria/contextual/ContextSensitiveBinderTest.java index 1e2ab49..68250cb 100644 --- a/sangria-contextual/src/test/java/com/tavianator/sangria/contextual/ContextSensitiveBinderTest.java +++ b/sangria-contextual/src/test/java/com/tavianator/sangria/contextual/ContextSensitiveBinderTest.java @@ -28,6 +28,7 @@ import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.MembersInjector; +import com.google.inject.Module; import com.google.inject.Provider; import com.google.inject.ProvisionException; import com.google.inject.TypeLiteral; @@ -40,6 +41,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import static com.tavianator.sangria.test.SangriaMatchers.*; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; @@ -54,6 +56,10 @@ public class ContextSensitiveBinderTest { public @Rule ExpectedException thrown = ExpectedException.none(); private static class SelfProvider implements ContextSensitiveProvider { + @Inject + SelfProvider() { + } + // For testing getInjectionPoints() in the SPI below @SuppressWarnings("unused") @Inject Injector injector; @@ -191,6 +197,21 @@ public class ContextSensitiveBinderTest { assertThat(hasSelf.selfProvider.get(), equalTo("")); } + @Test + public void testBestPractices() { + Module module = new AbstractModule() { + @Override + protected void configure() { + ContextSensitiveBinder.create(binder()) + .bind(String.class) + .annotatedWith(Names.named("self")) + .toContextSensitiveProvider(SelfProvider.class); + } + }; + assertThat(module, is(atomic())); + assertThat(module, followsBestPractices()); + } + private static class RequiredContextProvider implements ContextSensitiveProvider { @Override public String getInContext(InjectionPoint injectionPoint) { -- cgit v1.2.3 From 181bf8b43411ff309517ca6ca804fc34dae1e13a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 4 Oct 2014 12:27:26 -0400 Subject: slf4j: Test best practices. --- sangria-slf4j/pom.xml | 6 ++++++ .../main/java/com/tavianator/sangria/slf4j/Slf4jLoggerProvider.java | 5 +++++ .../java/com/tavianator/sangria/slf4j/SangriaSlf4jModuleTest.java | 6 ++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/sangria-slf4j/pom.xml b/sangria-slf4j/pom.xml index c274a47..53cbfb7 100644 --- a/sangria-slf4j/pom.xml +++ b/sangria-slf4j/pom.xml @@ -38,6 +38,12 @@ true + + com.tavianator.sangria + sangria-test + test + + junit junit diff --git a/sangria-slf4j/src/main/java/com/tavianator/sangria/slf4j/Slf4jLoggerProvider.java b/sangria-slf4j/src/main/java/com/tavianator/sangria/slf4j/Slf4jLoggerProvider.java index 916f2b9..bcb7e8b 100644 --- a/sangria-slf4j/src/main/java/com/tavianator/sangria/slf4j/Slf4jLoggerProvider.java +++ b/sangria-slf4j/src/main/java/com/tavianator/sangria/slf4j/Slf4jLoggerProvider.java @@ -19,6 +19,7 @@ package com.tavianator.sangria.slf4j; import javax.inject.Singleton; +import com.google.inject.Inject; import com.google.inject.spi.InjectionPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,6 +35,10 @@ import com.tavianator.sangria.contextual.ContextSensitiveProvider; */ @Singleton class Slf4jLoggerProvider implements ContextSensitiveProvider { + @Inject + Slf4jLoggerProvider() { + } + @Override public Logger getInContext(InjectionPoint injectionPoint) { return LoggerFactory.getLogger(injectionPoint.getDeclaringType().getRawType()); diff --git a/sangria-slf4j/src/test/java/com/tavianator/sangria/slf4j/SangriaSlf4jModuleTest.java b/sangria-slf4j/src/test/java/com/tavianator/sangria/slf4j/SangriaSlf4jModuleTest.java index d142bde..aaf72b2 100644 --- a/sangria-slf4j/src/test/java/com/tavianator/sangria/slf4j/SangriaSlf4jModuleTest.java +++ b/sangria-slf4j/src/test/java/com/tavianator/sangria/slf4j/SangriaSlf4jModuleTest.java @@ -28,6 +28,7 @@ import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; +import static com.tavianator.sangria.test.SangriaMatchers.*; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; @@ -75,7 +76,8 @@ public class SangriaSlf4jModuleTest { } @Test - public void testDeDuplication() { - Guice.createInjector(new SangriaSlf4jModule(), new SangriaSlf4jModule()); + public void testBestPractices() { + assertThat(new SangriaSlf4jModule(), is(atomic())); + assertThat(new SangriaSlf4jModule(), followsBestPractices()); } } -- cgit v1.2.3 From 17a7490cabfd4428cfdb5d9106dc076b1dc4b905 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 4 Oct 2014 12:32:13 -0400 Subject: log4j: Test best practices. --- sangria-log4j/pom.xml | 6 ++++++ .../main/java/com/tavianator/sangria/log4j/Log4jLoggerProvider.java | 5 +++++ .../java/com/tavianator/sangria/log4j/SangriaLog4jModuleTest.java | 6 ++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/sangria-log4j/pom.xml b/sangria-log4j/pom.xml index 224081d..06e892a 100644 --- a/sangria-log4j/pom.xml +++ b/sangria-log4j/pom.xml @@ -38,6 +38,12 @@ true + + com.tavianator.sangria + sangria-test + test + + junit junit diff --git a/sangria-log4j/src/main/java/com/tavianator/sangria/log4j/Log4jLoggerProvider.java b/sangria-log4j/src/main/java/com/tavianator/sangria/log4j/Log4jLoggerProvider.java index dae2323..34262d7 100644 --- a/sangria-log4j/src/main/java/com/tavianator/sangria/log4j/Log4jLoggerProvider.java +++ b/sangria-log4j/src/main/java/com/tavianator/sangria/log4j/Log4jLoggerProvider.java @@ -19,6 +19,7 @@ package com.tavianator.sangria.log4j; import javax.inject.Singleton; +import com.google.inject.Inject; import com.google.inject.spi.InjectionPoint; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -34,6 +35,10 @@ import com.tavianator.sangria.contextual.ContextSensitiveProvider; */ @Singleton class Log4jLoggerProvider implements ContextSensitiveProvider { + @Inject + Log4jLoggerProvider() { + } + @Override public Logger getInContext(InjectionPoint injectionPoint) { return LogManager.getLogger(injectionPoint.getDeclaringType().getRawType()); diff --git a/sangria-log4j/src/test/java/com/tavianator/sangria/log4j/SangriaLog4jModuleTest.java b/sangria-log4j/src/test/java/com/tavianator/sangria/log4j/SangriaLog4jModuleTest.java index d91cf90..a3ecd66 100644 --- a/sangria-log4j/src/test/java/com/tavianator/sangria/log4j/SangriaLog4jModuleTest.java +++ b/sangria-log4j/src/test/java/com/tavianator/sangria/log4j/SangriaLog4jModuleTest.java @@ -29,6 +29,7 @@ import org.apache.logging.log4j.Logger; import org.junit.Before; import org.junit.Test; +import static com.tavianator.sangria.test.SangriaMatchers.*; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; @@ -74,7 +75,8 @@ public class SangriaLog4jModuleTest { } @Test - public void testDeDuplication() { - Guice.createInjector(new SangriaLog4jModule(), new SangriaLog4jModule()); + public void testBestPractices() { + assertThat(new SangriaLog4jModule(), is(atomic())); + assertThat(new SangriaLog4jModule(), followsBestPractices()); } } -- cgit v1.2.3 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