Search in sources :

Example 16 with Binding

use of com.google.inject.Binding in project roboguice by roboguice.

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)));
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) PrivateModule(com.google.inject.PrivateModule) Key(com.google.inject.Key)

Example 17 with Binding

use of com.google.inject.Binding in project roboguice by roboguice.

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();
}
Also used : Binding(com.google.inject.Binding) PrivateElements(com.google.inject.spi.PrivateElements) Element(com.google.inject.spi.Element) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) Key(com.google.inject.Key)

Example 18 with Binding

use of com.google.inject.Binding in project roboguice by roboguice.

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);
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element)

Example 19 with Binding

use of com.google.inject.Binding in project roboguice by roboguice.

the class FilterDefinitionTest method testFilterInitAndConfig.

public final void testFilterInitAndConfig() throws ServletException {
    Injector injector = createMock(Injector.class);
    Binding binding = createMock(Binding.class);
    final MockFilter mockFilter = new MockFilter();
    expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
    expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
    expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
    replay(binding, injector);
    //some init params
    //noinspection SSBasedInspection
    final Map<String, String> initParams = new ImmutableMap.Builder<String, String>().put("ahsd", "asdas24dok").put("ahssd", "asdasd124ok").build();
    ServletContext servletContext = createMock(ServletContext.class);
    final String contextName = "thing__!@@44";
    expect(servletContext.getServletContextName()).andReturn(contextName);
    replay(servletContext);
    String pattern = "/*";
    final FilterDefinition filterDef = new FilterDefinition(pattern, Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), initParams, null);
    filterDef.init(servletContext, injector, Sets.<Filter>newIdentityHashSet());
    assertTrue(filterDef.getFilter() instanceof MockFilter);
    final FilterConfig filterConfig = mockFilter.getConfig();
    assertTrue(null != filterConfig);
    assertEquals(filterConfig.getServletContext().getServletContextName(), contextName);
    assertEquals(filterConfig.getFilterName(), Key.get(Filter.class).toString());
    final Enumeration names = filterConfig.getInitParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        assertTrue(initParams.containsKey(name));
        assertEquals(filterConfig.getInitParameter(name), initParams.get(name));
    }
    verify(binding, injector, servletContext);
}
Also used : Binding(com.google.inject.Binding) Enumeration(java.util.Enumeration) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 20 with Binding

use of com.google.inject.Binding in project roboguice by roboguice.

the class ExtensionSpiTest method testSpiOnElements.

public final void testSpiOnElements() throws Exception {
    AssistedInjectSpiVisitor visitor = new AssistedInjectSpiVisitor();
    Integer count = 0;
    for (Element element : Elements.getElements(new Module())) {
        if (element instanceof Binding) {
            assertEquals(count++, ((Binding<?>) element).acceptTargetVisitor(visitor));
        }
    }
    validateVisitor(visitor);
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element) AbstractModule(com.google.inject.AbstractModule)

Aggregations

Binding (com.google.inject.Binding)91 Injector (com.google.inject.Injector)57 Key (com.google.inject.Key)35 AbstractModule (com.google.inject.AbstractModule)33 InstanceBinding (com.google.inject.spi.InstanceBinding)23 Map (java.util.Map)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Element (com.google.inject.spi.Element)18 Module (com.google.inject.Module)17 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)17 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)16 HttpServlet (javax.servlet.http.HttpServlet)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)12 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)12 ServletContext (javax.servlet.ServletContext)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 HashMap (java.util.HashMap)11 TypeLiteral (com.google.inject.TypeLiteral)10 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)10