Search in sources :

Example 26 with AbstractModule

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

the class MapBinderTest method testMapBinderMapDoesNotDedupeDuplicateValues.

public void testMapBinderMapDoesNotDedupeDuplicateValues() {
    class ValueType {

        int keyPart;

        int dataPart;

        private ValueType(int keyPart, int dataPart) {
            this.keyPart = keyPart;
            this.dataPart = dataPart;
        }

        @Override
        public boolean equals(Object obj) {
            return (obj instanceof ValueType) && (keyPart == ((ValueType) obj).keyPart);
        }

        @Override
        public int hashCode() {
            return keyPart;
        }
    }
    Module m1 = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, ValueType> multibinder = MapBinder.newMapBinder(binder(), String.class, ValueType.class);
            multibinder.addBinding("a").toInstance(new ValueType(1, 2));
        }
    };
    Module m2 = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, ValueType> multibinder = MapBinder.newMapBinder(binder(), String.class, ValueType.class);
            multibinder.addBinding("b").toInstance(new ValueType(1, 3));
        }
    };
    Injector injector = Guice.createInjector(m1, m2);
    Map<String, ValueType> map = injector.getInstance(new Key<Map<String, ValueType>>() {
    });
    assertEquals(2, map.get("a").dataPart);
    assertEquals(3, map.get("b").dataPart);
}
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 27 with AbstractModule

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

the class MapBinderTest method testMapBinderMatching.

@Marker
public void testMapBinderMatching() throws Exception {
    Method m = MapBinderTest.class.getDeclaredMethod("testMapBinderMatching");
    assertNotNull(m);
    final Annotation marker = m.getAnnotation(Marker.class);
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        public void configure() {
            MapBinder<Integer, Integer> mb1 = MapBinder.newMapBinder(binder(), Integer.class, Integer.class, Marker.class);
            MapBinder<Integer, Integer> mb2 = MapBinder.newMapBinder(binder(), Integer.class, Integer.class, marker);
            mb1.addBinding(1).toInstance(1);
            mb2.addBinding(2).toInstance(2);
            // This assures us that the two binders are equivalent, so we expect the instance added to
            // each to have been added to one set.
            assertEquals(mb1, mb2);
        }
    });
    TypeLiteral<Map<Integer, Integer>> t = new TypeLiteral<Map<Integer, Integer>>() {
    };
    Map<Integer, Integer> s1 = injector.getInstance(Key.get(t, Marker.class));
    Map<Integer, Integer> s2 = injector.getInstance(Key.get(t, marker));
    // This assures us that the two sets are in fact equal.  They may not be same set (as in Java
    // object identical), but we shouldn't expect that, since probably Guice creates the set each
    // time in case the elements are dependent on scope.
    assertEquals(s1, s2);
    // This ensures that MultiBinder is internally using the correct set name --
    // making sure that instances of marker annotations have the same set name as
    // MarkerAnnotation.class.
    Map<Integer, Integer> expected = new HashMap<Integer, Integer>();
    expected.put(1, 1);
    expected.put(2, 2);
    assertEquals(expected, s1);
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Map(java.util.Map) HashMap(java.util.HashMap)

Example 28 with AbstractModule

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

the class MultibinderTest method testModuleOverrideRepeatedInstallsAndMultibindings_toConstructor.

public void testModuleOverrideRepeatedInstallsAndMultibindings_toConstructor() {
    TypeLiteral<Set<StringGrabber>> setOfStringGrabber = new TypeLiteral<Set<StringGrabber>>() {
    };
    Module ab = new AbstractModule() {

        @Override
        protected void configure() {
            Key<String> aKey = Key.get(String.class, Names.named("A_string"));
            Key<String> bKey = Key.get(String.class, Names.named("B_string"));
            bind(aKey).toInstance("A");
            bind(bKey).toInstance("B");
            // used to disambiguate constructors
            bind(Integer.class).toInstance(0);
            Multibinder<StringGrabber> multibinder = Multibinder.newSetBinder(binder(), StringGrabber.class);
            try {
                multibinder.addBinding().toConstructor(StringGrabber.class.getConstructor(String.class));
                multibinder.addBinding().toConstructor(StringGrabber.class.getConstructor(String.class, int.class));
            } catch (NoSuchMethodException e) {
                fail("No such method: " + e.getMessage());
            }
        }
    };
    // Guice guarantees this assertion, as the same module cannot be installed twice.
    assertEquals(ImmutableSet.of("A", "B"), StringGrabber.values(Guice.createInjector(ab, ab).getInstance(Key.get(setOfStringGrabber))));
    // Guice will only guarantee this assertion if Multibinder ensures the bindings match.
    Injector injector = Guice.createInjector(ab, Modules.override(ab).with(ab));
    assertEquals(ImmutableSet.of("A", "B"), StringGrabber.values(injector.getInstance(Key.get(setOfStringGrabber))));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 29 with AbstractModule

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

the class MultibinderTest method testModuleOverrideRepeatedInstallsAndMultibindings_toInstance.

/**
   * Doubly-installed modules should not conflict, even when one is overridden.
   */
public void testModuleOverrideRepeatedInstallsAndMultibindings_toInstance() {
    Module ab = new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
            multibinder.addBinding().toInstance("A");
            multibinder.addBinding().toInstance("B");
        }
    };
    // Guice guarantees this assertion, as the same module cannot be installed twice.
    assertEquals(ImmutableSet.of("A", "B"), Guice.createInjector(ab, ab).getInstance(Key.get(setOfString)));
    // Guice will only guarantee this assertion if Multibinder ensures the bindings match.
    Injector injector = Guice.createInjector(ab, Modules.override(ab).with(ab));
    assertEquals(ImmutableSet.of("A", "B"), injector.getInstance(Key.get(setOfString)));
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 30 with AbstractModule

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

the class MultibinderTest method testMultibinderCanInjectCollectionOfProviders.

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

        protected void configure() {
            final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
            multibinder.addBinding().toProvider(Providers.of("A"));
            multibinder.addBinding().toProvider(Providers.of("B"));
            multibinder.addBinding().toInstance("C");
        }
    };
    Injector injector = Guice.createInjector(module);
    injector.getInstance(Key.get(collectionOfProvidersOfStrings));
    Key<Collection<Provider<String>>> collectionKey = Key.get(collectionOfProvidersOfStrings);
    Collection<Provider<String>> providers = injector.getInstance(collectionKey);
    Collection<String> values = Lists.newArrayList();
    for (Provider<String> provider : providers) {
        values.add(provider.get());
    }
    Collection<String> expectedValues = ImmutableList.of("A", "B", "C");
    assertEquals(expectedValues, values);
}
Also used : Injector(com.google.inject.Injector) Collection(java.util.Collection) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

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