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