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