Created
October 4, 2011 21:36
-
-
Save eishay/1262898 to your computer and use it in GitHub Desktop.
Asserting Guice Servlet Module Binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@VisibleForTesting | |
Injector createInjector() { | |
return Guice.createInjector(new ServletModule() { | |
@Override | |
protected void configureServlets() { | |
serve("/img/*").with(ImageFooServlet.class); | |
serve("/rd/*").with(RedirectFooServlet.class); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static com.netflix.beacon.server.ServletModuleAssert.assertBindingToPattern; | |
import org.junit.Test; | |
import com.google.inject.Injector; | |
public class GuiceServletConfigTest { | |
@Test | |
public void testBinding() { | |
Injector injector = new GuiceServletConfig().createInjector(); | |
assertBindingToPattern(injector, ImageFooServlet.class, "/img/*"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.String.format; | |
import static org.junit.Assert.fail; | |
import javax.servlet.http.HttpServlet; | |
import com.google.inject.Binding; | |
import com.google.inject.Injector; | |
import com.google.inject.servlet.InstanceFilterBinding; | |
import com.google.inject.servlet.InstanceServletBinding; | |
import com.google.inject.servlet.LinkedFilterBinding; | |
import com.google.inject.servlet.LinkedServletBinding; | |
import com.google.inject.servlet.ServletModuleTargetVisitor; | |
import com.google.inject.spi.DefaultBindingTargetVisitor; | |
public class ServletModuleAssert { | |
public static void assertBindingToPattern(Injector injector, | |
Class<? extends HttpServlet> clazz, String pattern) { | |
for (Binding<?> binding: injector.getAllBindings().values()) { | |
Visitor visitor = new Visitor(clazz); | |
if (binding.acceptTargetVisitor(visitor)) { | |
if (!pattern.equals(visitor.pattern)) { | |
fail(format("expected pattern for clazz %s is [%s], actual is [%s]", | |
clazz, pattern, visitor.pattern)); | |
} | |
return; | |
} | |
} | |
fail(format("no binding for clazz %s", pattern)); | |
} | |
private static class Visitor extends | |
DefaultBindingTargetVisitor<Object, Boolean> implements | |
ServletModuleTargetVisitor<Object, Boolean> { | |
private final Class<? extends HttpServlet> clazz; | |
private String pattern; | |
Visitor(Class<? extends HttpServlet> clazz) { | |
this.clazz = clazz; | |
} | |
@Override | |
public Boolean visit(LinkedFilterBinding binding) { | |
return false; | |
} | |
@Override | |
public Boolean visit(InstanceFilterBinding binding) { | |
return false; | |
} | |
@Override | |
public Boolean visit(LinkedServletBinding binding) { | |
if (clazz.equals(binding.getLinkedKey().getTypeLiteral().getRawType())) { | |
pattern = binding.getPattern(); | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public Boolean visit(InstanceServletBinding binding) { | |
if (clazz.equals(binding.getServletInstance().getClass())) { | |
pattern = binding.getPattern(); | |
return true; | |
} | |
return false; | |
} | |
@Override | |
protected Boolean visitOther(Binding<? extends Object> binding) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice. thx!