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"));
}
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));
}
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);
}
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"));
}
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));
}
Aggregations