Search in sources :

Example 6 with MapBinder

use of com.google.inject.multibindings.MapBinder in project guice by google.

the class MapBinderTest method testMapBinderMultimapIsUnmodifiable.

public void testMapBinderMultimapIsUnmodifiable() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> mapBinder = MapBinder.newMapBinder(binder(), String.class, String.class);
            mapBinder.addBinding("a").toInstance("A");
            mapBinder.permitDuplicates();
        }
    });
    Map<String, Set<String>> map = injector.getInstance(Key.get(mapOfSetOfString));
    try {
        map.clear();
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    try {
        map.get("a").clear();
        fail();
    } catch (UnsupportedOperationException expected) {
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Injector(com.google.inject.Injector) MapBinder(com.google.inject.multibindings.MapBinder) AbstractModule(com.google.inject.AbstractModule)

Example 7 with MapBinder

use of com.google.inject.multibindings.MapBinder in project guice by google.

the class MapBinderTest method testWeakKeySet_integration_mapbinder.

// Tests for com.google.inject.internal.WeakKeySet not leaking memory.
public void testWeakKeySet_integration_mapbinder() {
    Key<Map<String, String>> mapKey = Key.get(new TypeLiteral<Map<String, String>>() {
    });
    Injector parentInjector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("hi");
        }
    });
    WeakKeySetUtils.assertNotBlacklisted(parentInjector, mapKey);
    Injector childInjector = parentInjector.createChildInjector(new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> binder = MapBinder.newMapBinder(binder(), String.class, String.class);
            binder.addBinding("bar").toInstance("foo");
        }
    });
    WeakReference<Injector> weakRef = new WeakReference<Injector>(childInjector);
    WeakKeySetUtils.assertBlacklisted(parentInjector, mapKey);
    // Clear the ref, GC, and ensure that we are no longer blacklisting.
    childInjector = null;
    Asserts.awaitClear(weakRef);
    WeakKeySetUtils.assertNotBlacklisted(parentInjector, mapKey);
}
Also used : Injector(com.google.inject.Injector) WeakReference(java.lang.ref.WeakReference) MapBinder(com.google.inject.multibindings.MapBinder) Map(java.util.Map) HashMap(java.util.HashMap) AbstractModule(com.google.inject.AbstractModule)

Example 8 with MapBinder

use of com.google.inject.multibindings.MapBinder in project guice by google.

the class MapBinderTest method testMultibinderDependencies.

/** Check that the dependencies are correct. */
public void testMultibinderDependencies() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class);
            mapBinder.addBinding(1).toInstance("A");
            mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b")));
            bindConstant().annotatedWith(Names.named("b")).to("B");
        }
    });
    Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {
    });
    HasDependencies withDependencies = (HasDependencies) binding;
    Set<Dependency<?>> actualDependencies = withDependencies.getDependencies();
    // We expect two dependencies, because the dependencies are annotated with
    // Element, which has a uniqueId, it's difficult to directly compare them.
    // Instead we will manually compare all the fields except the uniqueId
    assertEquals(2, actualDependencies.size());
    for (Dependency<?> dependency : actualDependencies) {
        Key<?> key = dependency.getKey();
        assertEquals(new TypeLiteral<String>() {
        }, key.getTypeLiteral());
        Annotation annotation = dependency.getKey().getAnnotation();
        assertTrue(annotation instanceof Element);
        Element element = (Element) annotation;
        assertEquals("", element.setName());
        assertEquals(Element.Type.MAPBINDER, element.type());
        assertEquals("java.lang.Integer", element.keyType());
    }
    Set<String> elements = Sets.newHashSet();
    elements.addAll(recurseForDependencies(injector, withDependencies));
    assertEquals(ImmutableSet.of("A", "B"), elements);
}
Also used : Dependency(com.google.inject.spi.Dependency) HasDependencies(com.google.inject.spi.HasDependencies) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) MapBinder(com.google.inject.multibindings.MapBinder) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

MapBinder (com.google.inject.multibindings.MapBinder)8 AbstractModule (com.google.inject.AbstractModule)7 Injector (com.google.inject.Injector)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 BindingAnnotation (com.google.inject.BindingAnnotation)3 Annotation (java.lang.annotation.Annotation)3 Dependency (com.google.inject.spi.Dependency)2 HasDependencies (com.google.inject.spi.HasDependencies)2 DatasetModule (co.cask.cdap.api.dataset.module.DatasetModule)1 ProgramRunnerRuntimeModule (co.cask.cdap.app.guice.ProgramRunnerRuntimeModule)1 PreviewRunnerModule (co.cask.cdap.app.preview.PreviewRunnerModule)1 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1 ConfigModule (co.cask.cdap.common.guice.ConfigModule)1 IOModule (co.cask.cdap.common.guice.IOModule)1 LocationRuntimeModule (co.cask.cdap.common.guice.LocationRuntimeModule)1 PreviewDiscoveryRuntimeModule (co.cask.cdap.common.guice.preview.PreviewDiscoveryRuntimeModule)1 ConfigStoreModule (co.cask.cdap.config.guice.ConfigStoreModule)1 DataSetServiceModules (co.cask.cdap.data.runtime.DataSetServiceModules)1 PreviewDataModules (co.cask.cdap.data.runtime.preview.PreviewDataModules)1