diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2014-10-04 12:52:32 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2014-10-04 12:52:32 -0400 |
commit | 3fd5777a58f59218e3229c9ea11d816ecc6ae367 (patch) | |
tree | 53f937505806e010d2bc3134fa5507d0ff78d32c /sangria-contextual | |
parent | b0bb30831a334ff30e3e6a13790bbc324db89de5 (diff) | |
parent | 8e24a47b69f940c31ac95451b4584b95de7c8670 (diff) | |
download | sangria-3fd5777a58f59218e3229c9ea11d816ecc6ae367.tar.xz |
Merge branch 'test'
Diffstat (limited to 'sangria-contextual')
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 @@ -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-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<? extends ContextSensitiveProvider<? extends T>> 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 <U> Key<U> makeLinkedKey(Key<U> key) { + Key<U> linkedKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); + binder.bind(linkedKey) + .to(key); + return linkedKey; + } + @Override public void toContextSensitiveProvider(ContextSensitiveProvider<? extends T> provider) { error.cancel(); @@ -190,15 +198,19 @@ public class ContextSensitiveBinder { private static class ProviderKeyAdapter<T> extends ProviderAdapter<T> implements ContextSensitiveProviderKeyBinding<T> { private final Key<? extends ContextSensitiveProvider<? extends T>> providerKey; + private final Key<? extends ContextSensitiveProvider<? extends T>> linkedKey; private Provider<? extends ContextSensitiveProvider<? extends T>> provider; - ProviderKeyAdapter(Key<? extends ContextSensitiveProvider<? extends T>> providerKey) { + ProviderKeyAdapter( + Key<? extends ContextSensitiveProvider<? extends T>> providerKey, + Key<? extends ContextSensitiveProvider<? extends T>> 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<String> { + @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("<unknown>")); } + @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<String> { @Override public String getInContext(InjectionPoint injectionPoint) { |