From 2737784763f9041067e9d07bb935c1c9c3952d11 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 3 Apr 2014 11:00:33 -0400 Subject: contextual: Add an extension SPI. --- .../contextual/ContextSensitiveBinderTest.java | 92 +++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) (limited to 'sangria-contextual/src/test/java/com/tavianator/sangria') 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 3463b0e..1e2ab49 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 @@ -17,10 +17,12 @@ package com.tavianator.sangria.contextual; +import java.util.*; import javax.inject.Inject; import javax.inject.Named; import com.google.inject.AbstractModule; +import com.google.inject.Binding; import com.google.inject.CreationException; import com.google.inject.Guice; import com.google.inject.Injector; @@ -30,6 +32,9 @@ import com.google.inject.Provider; import com.google.inject.ProvisionException; import com.google.inject.TypeLiteral; import com.google.inject.name.Names; +import com.google.inject.spi.DefaultBindingTargetVisitor; +import com.google.inject.spi.Element; +import com.google.inject.spi.Elements; import com.google.inject.spi.InjectionPoint; import org.junit.Rule; import org.junit.Test; @@ -49,6 +54,10 @@ public class ContextSensitiveBinderTest { public @Rule ExpectedException thrown = ExpectedException.none(); private static class SelfProvider implements ContextSensitiveProvider { + // For testing getInjectionPoints() in the SPI below + @SuppressWarnings("unused") + @Inject Injector injector; + @Override public String getInContext(InjectionPoint injectionPoint) { return injectionPoint.getDeclaringType().getRawType().getSimpleName(); @@ -138,7 +147,7 @@ public class ContextSensitiveBinderTest { } @Test - public void testDeDuplication() { + public void testKeyDeDuplication() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { @@ -153,6 +162,30 @@ public class ContextSensitiveBinderTest { .toContextSensitiveProvider(SelfProvider.class); } }); + + HasSelf hasSelf = injector.getInstance(HasSelf.class); + assertThat(hasSelf.self, equalTo("HasSelf")); + assertThat(hasSelf.selfProvider.get(), equalTo("")); + } + + @Test + public void testInstanceDeDuplication() { + final SelfProvider selfProvider = new SelfProvider(); + Injector injector = Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + ContextSensitiveBinder contextualBinder = ContextSensitiveBinder.create(binder()); + contextualBinder + .bind(String.class) + .annotatedWith(Names.named("self")) + .toContextSensitiveProvider(selfProvider); + contextualBinder + .bind(String.class) + .annotatedWith(Names.named("self")) + .toContextSensitiveProvider(selfProvider); + } + }); + HasSelf hasSelf = injector.getInstance(HasSelf.class); assertThat(hasSelf.self, equalTo("HasSelf")); assertThat(hasSelf.selfProvider.get(), equalTo("")); @@ -272,4 +305,61 @@ public class ContextSensitiveBinderTest { } }); } + + private static class TestVisitor extends DefaultBindingTargetVisitor implements ContextSensitiveBindingVisitor { + @Override + public Boolean visit(ContextSensitiveProviderKeyBinding binding) { + assertThat(binding.getContextSensitiveProviderKey().equals(new Key() { }), is(true)); + return true; + } + + @Override + public Boolean visit(ContextSensitiveProviderInstanceBinding binding) { + assertThat(binding.getContextSensitiveProviderInstance(), instanceOf(SelfProvider.class)); + assertThat(binding.getInjectionPoints(), hasSize(1)); + return true; + } + + @Override + protected Boolean visitOther(Binding binding) { + return false; + } + } + + private boolean visit(Binding binding) { + return binding.acceptTargetVisitor(new TestVisitor()); + } + + @Test + public void testExtensionSpi() { + List elements = Elements.getElements(new AbstractModule() { + @Override + protected void configure() { + ContextSensitiveBinder contextualBinder = ContextSensitiveBinder.create(binder()); + contextualBinder + .bind(String.class) + .annotatedWith(Names.named("key")) + .toContextSensitiveProvider(SelfProvider.class); + contextualBinder + .bind(String.class) + .annotatedWith(Names.named("instance")) + .toContextSensitiveProvider(new SelfProvider()); + } + }); + + int passed = 0; + for (Element element : elements) { + if (element instanceof Binding) { + if (visit(((Binding)element))) { + ++passed; + } + } + } + assertThat(passed, equalTo(2)); + + Injector injector = Guice.createInjector(Elements.getModule(elements)); + assertThat(visit(injector.getBinding(new Key(Names.named("key")) { })), is(true)); + assertThat(visit(injector.getBinding(new Key(Names.named("instance")) { })), is(true)); + assertThat(visit(injector.getBinding(SelfProvider.class)), is(false)); + } } -- cgit v1.2.3