use of com.google.inject.Binding in project guice by google.
the class ServletPipelineRequestDispatcherTest method testForwardToManagedServlet.
public final void testForwardToManagedServlet() throws IOException, ServletException {
String pattern = "blah.html";
final ServletDefinition servletDefinition = new ServletDefinition(Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
final Injector injector = createMock(Injector.class);
final Binding binding = createMock(Binding.class);
final HttpServletRequest requestMock = createMock(HttpServletRequest.class);
final HttpServletResponse mockResponse = createMock(HttpServletResponse.class);
expect(requestMock.getAttribute(A_KEY)).andReturn(A_VALUE);
requestMock.setAttribute(REQUEST_DISPATCHER_REQUEST, true);
requestMock.removeAttribute(REQUEST_DISPATCHER_REQUEST);
expect(mockResponse.isCommitted()).andReturn(false);
mockResponse.resetBuffer();
expectLastCall().once();
final List<String> paths = new ArrayList<String>();
final HttpServlet mockServlet = new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
paths.add(request.getRequestURI());
final Object o = request.getAttribute(A_KEY);
assertEquals("Wrong attrib returned - " + o, A_VALUE, o);
}
};
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding);
expect(injector.getInstance(HTTP_SERLVET_KEY)).andReturn(mockServlet);
final Key<ServletDefinition> servetDefsKey = Key.get(TypeLiteral.get(ServletDefinition.class));
Binding<ServletDefinition> mockBinding = createMock(Binding.class);
expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))).andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding));
Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition);
expect(mockBinding.getProvider()).andReturn(bindingProvider);
replay(injector, binding, requestMock, mockResponse, mockBinding);
// Have to init the Servlet before we can dispatch to it.
servletDefinition.init(null, injector, Sets.<HttpServlet>newIdentityHashSet());
final RequestDispatcher dispatcher = new ManagedServletPipeline(injector).getRequestDispatcher(pattern);
assertNotNull(dispatcher);
dispatcher.forward(requestMock, mockResponse);
assertTrue("Include did not dispatch to our servlet!", paths.contains(pattern));
verify(injector, requestMock, mockResponse, mockBinding);
}
use of com.google.inject.Binding in project guice by google.
the class ServletScopesTest method testIsRequestScopedNegative.
public void testIsRequestScopedNegative() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<String> e = Key.get(String.class, named("E"));
final Key<String> f = Key.get(String.class, named("F"));
final Key<String> g = Key.get(String.class, named("G"));
final Key<String> h = Key.get(String.class, named("H"));
final Key<String> i = Key.get(String.class, named("I"));
final Key<String> j = Key.get(String.class, named("J"));
Module requestScopedBindings = new AbstractModule() {
@Override
protected void configure() {
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(Scopes.NO_SCOPE);
bind(d).toInstance("d");
bind(e).toProvider(Providers.of("e")).asEagerSingleton();
bind(f).toProvider(Providers.of("f")).in(Scopes.SINGLETON);
bind(g).toProvider(Providers.of("g")).in(Singleton.class);
bind(h).toProvider(Providers.of("h")).in(CustomScoped.class);
bindScope(CustomScoped.class, Scopes.NO_SCOPE);
install(new PrivateModule() {
@Override
protected void configure() {
bind(i).toProvider(Providers.of("i")).in(CustomScoped.class);
expose(i);
}
});
}
@Provides
@Named("J")
@CustomScoped
String provideJ() {
return "j";
}
};
// we know the module contains only bindings
@SuppressWarnings("unchecked") List<Element> moduleBindings = Elements.getElements(requestScopedBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
assertFalse(ServletScopes.isRequestScoped(map.get(a)));
assertFalse(ServletScopes.isRequestScoped(map.get(b)));
assertFalse(ServletScopes.isRequestScoped(map.get(c)));
assertFalse(ServletScopes.isRequestScoped(map.get(d)));
assertFalse(ServletScopes.isRequestScoped(map.get(e)));
assertFalse(ServletScopes.isRequestScoped(map.get(f)));
assertFalse(ServletScopes.isRequestScoped(map.get(g)));
assertFalse(ServletScopes.isRequestScoped(map.get(h)));
assertFalse(ServletScopes.isRequestScoped(map.get(i)));
assertFalse(ServletScopes.isRequestScoped(map.get(j)));
Injector injector = Guice.createInjector(requestScopedBindings);
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(a)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(b)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(c)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(d)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(e)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(f)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(g)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(h)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(i)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(j)));
}
use of com.google.inject.Binding in project guice by google.
the class ServletScopesTest method indexBindings.
private ImmutableMap<Key<?>, Binding<?>> indexBindings(Iterable<Element> elements) {
ImmutableMap.Builder<Key<?>, Binding<?>> builder = ImmutableMap.builder();
for (Element element : elements) {
if (element instanceof Binding) {
Binding<?> binding = (Binding<?>) element;
builder.put(binding.getKey(), binding);
} else if (element instanceof PrivateElements) {
PrivateElements privateElements = (PrivateElements) element;
Map<Key<?>, Binding<?>> privateBindings = indexBindings(privateElements.getElements());
for (Key<?> exposed : privateElements.getExposedKeys()) {
builder.put(exposed, privateBindings.get(exposed));
}
}
}
return builder.build();
}
use of com.google.inject.Binding in project guice by google.
the class ExtensionSpiTest method testSpiOnElements.
public final void testSpiOnElements() {
ServletSpiVisitor visitor = new ServletSpiVisitor(false);
int count = 0;
for (Element element : Elements.getElements(new Module())) {
if (element instanceof Binding) {
assertEquals(count++, ((Binding) element).acceptTargetVisitor(visitor));
}
}
validateVisitor(visitor);
}
use of com.google.inject.Binding in project guice by google.
the class ServletDefinitionPathsTest method pathInfoWithServletStyleMatching.
private void pathInfoWithServletStyleMatching(final String requestUri, final String contextPath, String mapping, final String expectedPathInfo, final String servletPath) throws IOException, ServletException {
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
HttpServletResponse response = createMock(HttpServletResponse.class);
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding);
final boolean[] run = new boolean[1];
//get an instance of this servlet
expect(injector.getInstance(Key.get(HttpServlet.class))).andReturn(new HttpServlet() {
@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
final String path = servletRequest.getPathInfo();
if (null == expectedPathInfo) {
assertNull(String.format("expected [%s] but was [%s]", expectedPathInfo, path), path);
} else {
assertEquals(String.format("expected [%s] but was [%s]", expectedPathInfo, path), expectedPathInfo, path);
}
//assert memoizer
//noinspection StringEquality
assertSame("memo field did not work", path, servletRequest.getPathInfo());
run[0] = true;
}
});
expect(request.getRequestURI()).andReturn(requestUri);
expect(request.getServletPath()).andReturn(servletPath).anyTimes();
expect(request.getContextPath()).andReturn(contextPath);
expect(request.getAttribute(REQUEST_DISPATCHER_REQUEST)).andReturn(null);
replay(injector, binding, request);
ServletDefinition servletDefinition = new ServletDefinition(Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, mapping), new HashMap<String, String>(), null);
servletDefinition.init(null, injector, Sets.<HttpServlet>newIdentityHashSet());
servletDefinition.doService(request, response);
assertTrue("Servlet did not run!", run[0]);
verify(injector, binding, request);
}
Aggregations