diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2014-10-04 12:24:51 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2014-10-04 12:24:51 -0400 |
commit | b8c1ca531321dbcee3ae237bad20fa8691b31f83 (patch) | |
tree | 2432ad47f835af34ebdddb2306598f6d7d34c9ff /sangria-contextual/src/test | |
parent | 5a08861fcb8c5bc5831a6b99c192aba0a7933952 (diff) | |
download | sangria-b8c1ca531321dbcee3ae237bad20fa8691b31f83.tar.xz |
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.
Diffstat (limited to 'sangria-contextual/src/test')
-rw-r--r-- | sangria-contextual/src/test/java/com/tavianator/sangria/contextual/ContextSensitiveBinderTest.java | 21 |
1 files changed, 21 insertions, 0 deletions
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) { |