Search in sources :

Example 6 with Binding

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

the class MultibinderTest method testSetAndMapValueAreDistinctInSpi.

// See issue 670
public void testSetAndMapValueAreDistinctInSpi() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder.newSetBinder(binder(), String.class).addBinding().toInstance("A");
            MapBinder.newMapBinder(binder(), String.class, String.class).addBinding("B").toInstance("b");
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("C");
        }
    });
    Collector collector = new Collector();
    Binding<Map<String, String>> mapbinding = injector.getBinding(Key.get(mapOfStringString));
    mapbinding.acceptTargetVisitor(collector);
    assertNotNull(collector.mapbinding);
    Binding<Set<String>> setbinding = injector.getBinding(Key.get(setOfString));
    setbinding.acceptTargetVisitor(collector);
    assertNotNull(collector.setbinding);
    Binding<Optional<String>> optionalbinding = injector.getBinding(Key.get(optionalOfString));
    optionalbinding.acceptTargetVisitor(collector);
    assertNotNull(collector.optionalbinding);
    // There should only be three instance bindings for string types
    // (but because of the OptionalBinder, there's 2 ProviderInstanceBindings also).
    // We also know the InstanceBindings will be in the order: A, b, C because that's
    // how we bound them, and binding order is preserved.
    List<Binding<String>> bindings = FluentIterable.from(injector.findBindingsByType(stringType)).filter(Predicates.instanceOf(InstanceBinding.class)).toList();
    assertEquals(bindings.toString(), 3, bindings.size());
    Binding<String> a = bindings.get(0);
    Binding<String> b = bindings.get(1);
    Binding<String> c = bindings.get(2);
    assertEquals("A", ((InstanceBinding<String>) a).getInstance());
    assertEquals("b", ((InstanceBinding<String>) b).getInstance());
    assertEquals("C", ((InstanceBinding<String>) c).getInstance());
    // Make sure the correct elements belong to their own sets.
    assertFalse(collector.mapbinding.containsElement(a));
    assertTrue(collector.mapbinding.containsElement(b));
    assertFalse(collector.mapbinding.containsElement(c));
    assertTrue(collector.setbinding.containsElement(a));
    assertFalse(collector.setbinding.containsElement(b));
    assertFalse(collector.setbinding.containsElement(c));
    assertFalse(collector.optionalbinding.containsElement(a));
    assertFalse(collector.optionalbinding.containsElement(b));
    assertTrue(collector.optionalbinding.containsElement(c));
}
Also used : Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Optional(com.google.common.base.Optional) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with Binding

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

the class MultibinderTest method testKeyHashCodesFixedAtInjectionTime.

/**
   * Ensure key hash codes are fixed at injection time, not binding time.
   */
public void testKeyHashCodesFixedAtInjectionTime() {
    Module ab = new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<List<String>> multibinder = Multibinder.newSetBinder(binder(), listOfStrings);
            List<String> list = Lists.newArrayList();
            multibinder.addBinding().toInstance(list);
            list.add("A");
            list.add("B");
        }
    };
    Injector injector = Guice.createInjector(ab);
    for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
        Key<?> bindingKey = entry.getKey();
        Key<?> clonedKey;
        if (bindingKey.getAnnotation() != null) {
            clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotation());
        } else if (bindingKey.getAnnotationType() != null) {
            clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotationType());
        } else {
            clonedKey = Key.get(bindingKey.getTypeLiteral());
        }
        assertEquals(bindingKey, clonedKey);
        assertEquals("Incorrect hashcode for " + bindingKey + " -> " + entry.getValue(), bindingKey.hashCode(), clonedKey.hashCode());
    }
}
Also used : Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding) Injector(com.google.inject.Injector) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 8 with Binding

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

the class OptionalBinderTest method testKeyHashCodesFixedAtInjectionTime.

/**
   * Ensure key hash codes are fixed at injection time, not binding time.
   */
public void testKeyHashCodesFixedAtInjectionTime() {
    Module m = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder<List<String>> b = OptionalBinder.newOptionalBinder(binder(), listOfStrings);
            List<String> list = Lists.newArrayList();
            b.setDefault().toInstance(list);
            b.setBinding().toInstance(list);
            list.add("A");
            list.add("B");
        }
    };
    Injector injector = Guice.createInjector(m);
    for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
        Key<?> bindingKey = entry.getKey();
        Key<?> clonedKey;
        if (bindingKey.getAnnotation() != null) {
            clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotation());
        } else if (bindingKey.getAnnotationType() != null) {
            clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotationType());
        } else {
            clonedKey = Key.get(bindingKey.getTypeLiteral());
        }
        assertEquals(bindingKey, clonedKey);
        assertEquals("Incorrect hashcode for " + bindingKey + " -> " + entry.getValue(), bindingKey.hashCode(), clonedKey.hashCode());
    }
}
Also used : Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) Injector(com.google.inject.Injector) List(java.util.List) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) SpiUtils.providerKey(com.google.inject.multibindings.SpiUtils.providerKey) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 9 with Binding

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

the class ElementsTest method testBindKeysWithAnnotationInstance.

public void testBindKeysWithAnnotationInstance() {
    FailingElementVisitor annotationChecker = new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertEquals(Key.get(String.class, Names.named("a")), command.getKey());
            return null;
        }
    };
    checkModule(new AbstractModule() {

        protected void configure() {
            bind(String.class).annotatedWith(Names.named("a")).toInstance("B");
            bind(new TypeLiteral<String>() {
            }).annotatedWith(Names.named("a")).toInstance("C");
        }
    }, annotationChecker, annotationChecker);
}
Also used : Binding(com.google.inject.Binding) TypeLiteral(com.google.inject.TypeLiteral) AbstractModule(com.google.inject.AbstractModule)

Example 10 with Binding

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

the class ElementsTest method testBindKeysWithAnnotationType.

public void testBindKeysWithAnnotationType() {
    FailingElementVisitor annotationChecker = new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertEquals(Key.get(String.class, SampleAnnotation.class), command.getKey());
            return null;
        }
    };
    checkModule(new AbstractModule() {

        protected void configure() {
            bind(String.class).annotatedWith(SampleAnnotation.class).toInstance("A");
            bind(new TypeLiteral<String>() {
            }).annotatedWith(SampleAnnotation.class).toInstance("B");
        }
    }, annotationChecker, annotationChecker);
}
Also used : Binding(com.google.inject.Binding) TypeLiteral(com.google.inject.TypeLiteral) 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