Search in sources :

Example 21 with AbstractModule

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

the class MapBinderTest method testDeduplicateMapBindings.

public void testDeduplicateMapBindings() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> mapbinder = MapBinder.newMapBinder(binder(), String.class, String.class);
            mapbinder.addBinding("a").toInstance("A");
            mapbinder.addBinding("a").toInstance("A");
            mapbinder.addBinding("b").toInstance("B");
            mapbinder.addBinding("b").toInstance("B");
        }
    };
    Injector injector = Guice.createInjector(module);
    assertEquals(mapOf("a", "A", "b", "B"), injector.getInstance(Key.get(mapOfString)));
    assertMapVisitor(Key.get(mapOfString), stringType, stringType, setOf(module), BOTH, false, 0, instance("a", "A"), instance("b", "B"));
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 22 with AbstractModule

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

the class MapBinderTest method testMapBinderWithMultipleAnnotationTypeSets.

public void testMapBinderWithMultipleAnnotationTypeSets() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> abcMapBinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
            abcMapBinder.addBinding("a").toInstance("A");
            abcMapBinder.addBinding("b").toInstance("B");
            abcMapBinder.addBinding("c").toInstance("C");
            MapBinder<String, String> deMapBinder = MapBinder.newMapBinder(binder(), String.class, String.class, De.class);
            deMapBinder.addBinding("d").toInstance("D");
            deMapBinder.addBinding("e").toInstance("E");
        }
    };
    Injector injector = Guice.createInjector(module);
    Key<Map<String, String>> abcKey = Key.get(mapOfString, Abc.class);
    Map<String, String> abc = injector.getInstance(abcKey);
    Key<Map<String, String>> deKey = Key.get(mapOfString, De.class);
    Map<String, String> de = injector.getInstance(deKey);
    assertEquals(mapOf("a", "A", "b", "B", "c", "C"), abc);
    assertEquals(mapOf("d", "D", "e", "E"), de);
    assertMapVisitor(abcKey, stringType, stringType, setOf(module), BOTH, false, 1, instance("a", "A"), instance("b", "B"), instance("c", "C"));
    assertMapVisitor(deKey, stringType, stringType, setOf(module), BOTH, false, 1, instance("d", "D"), instance("e", "E"));
    // just make sure these succeed
    injector.getInstance(Key.get(mapOfStringProvider, Abc.class));
    injector.getInstance(Key.get(mapOfStringJavaxProvider, Abc.class));
    injector.getInstance(Key.get(mapOfStringProvider, De.class));
    injector.getInstance(Key.get(mapOfStringJavaxProvider, De.class));
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) Map(java.util.Map) HashMap(java.util.HashMap) AbstractModule(com.google.inject.AbstractModule)

Example 23 with AbstractModule

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

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;
    WeakKeySetUtils.awaitClear(weakRef);
    WeakKeySetUtils.assertNotBlacklisted(parentInjector, mapKey);
}
Also used : Injector(com.google.inject.Injector) WeakReference(java.lang.ref.WeakReference) Map(java.util.Map) HashMap(java.util.HashMap) AbstractModule(com.google.inject.AbstractModule)

Example 24 with AbstractModule

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

the class MapBinderTest method testMapBinderMultimap.

public void testMapBinderMultimap() {
    AbstractModule ab1c = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class);
            multibinder.addBinding("a").toInstance("A");
            multibinder.addBinding("b").toInstance("B1");
            multibinder.addBinding("c").toInstance("C");
        }
    };
    AbstractModule b2c = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class);
            multibinder.addBinding("b").toInstance("B2");
            multibinder.addBinding("c").toInstance("C");
            multibinder.permitDuplicates();
        }
    };
    Injector injector = Guice.createInjector(ab1c, b2c);
    assertEquals(mapOf("a", setOf("A"), "b", setOf("B1", "B2"), "c", setOf("C")), injector.getInstance(Key.get(mapOfSetOfString)));
    assertMapVisitor(Key.get(mapOfString), stringType, stringType, setOf(ab1c, b2c), BOTH, true, 0, instance("a", "A"), instance("b", "B1"), instance("b", "B2"), instance("c", "C"));
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 25 with AbstractModule

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

the class MapBinderTest method testMapBinderAggregationForAnnotationType.

public void testMapBinderAggregationForAnnotationType() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
            multibinder.addBinding("a").toInstance("A");
            multibinder.addBinding("b").toInstance("B");
            multibinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
            multibinder.addBinding("c").toInstance("C");
        }
    };
    Injector injector = Guice.createInjector(module);
    Key<Map<String, String>> key = Key.get(mapOfString, Abc.class);
    Map<String, String> abc = injector.getInstance(key);
    assertEquals(mapOf("a", "A", "b", "B", "c", "C"), abc);
    assertMapVisitor(key, stringType, stringType, setOf(module), BOTH, false, 0, instance("a", "A"), instance("b", "B"), instance("c", "C"));
    // just make sure these succeed
    injector.getInstance(Key.get(mapOfStringProvider, Abc.class));
    injector.getInstance(Key.get(mapOfStringJavaxProvider, Abc.class));
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) Map(java.util.Map) HashMap(java.util.HashMap) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)840 Injector (com.google.inject.Injector)522 Module (com.google.inject.Module)232 CreationException (com.google.inject.CreationException)162 Provider (com.google.inject.Provider)64 Key (com.google.inject.Key)55 ConfigModule (co.cask.cdap.common.guice.ConfigModule)54 AuthorizationEnforcementModule (co.cask.cdap.security.authorization.AuthorizationEnforcementModule)49 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)46 PrivateModule (com.google.inject.PrivateModule)46 AuthenticationContextModules (co.cask.cdap.security.auth.context.AuthenticationContextModules)45 AuthorizationTestModule (co.cask.cdap.security.authorization.AuthorizationTestModule)43 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 TypeLiteral (com.google.inject.TypeLiteral)42 BeforeClass (org.junit.BeforeClass)38 Map (java.util.Map)37 CConfiguration (co.cask.cdap.common.conf.CConfiguration)36 UnsupportedUGIProvider (co.cask.cdap.security.impersonation.UnsupportedUGIProvider)36 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)35 DefaultOwnerAdmin (co.cask.cdap.security.impersonation.DefaultOwnerAdmin)35