use of com.google.inject.multibindings.MapBinderBinding in project guice by google.
the class MapBinderTest method testTwoMapBindersAreDistinct.
public void testTwoMapBindersAreDistinct() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
MapBinder.newMapBinder(binder(), String.class, String.class).addBinding("A").toInstance("a");
MapBinder.newMapBinder(binder(), Integer.class, String.class).addBinding(1).toInstance("b");
}
});
Collector collector = new Collector();
Binding<Map<String, String>> map1 = injector.getBinding(Key.get(mapOfString));
map1.acceptTargetVisitor(collector);
assertNotNull(collector.mapbinding);
MapBinderBinding<?> map1Binding = collector.mapbinding;
Binding<Map<Integer, String>> map2 = injector.getBinding(Key.get(mapOfIntString));
map2.acceptTargetVisitor(collector);
assertNotNull(collector.mapbinding);
MapBinderBinding<?> map2Binding = collector.mapbinding;
List<Binding<String>> bindings = injector.findBindingsByType(stringType);
assertEquals("should have two elements: " + bindings, 2, bindings.size());
Binding<String> a = bindings.get(0);
Binding<String> b = bindings.get(1);
assertEquals("a", ((InstanceBinding<String>) a).getInstance());
assertEquals("b", ((InstanceBinding<String>) b).getInstance());
// Make sure the correct elements belong to their own sets.
assertTrue(map1Binding.containsElement(a));
assertFalse(map1Binding.containsElement(b));
assertFalse(map2Binding.containsElement(a));
assertTrue(map2Binding.containsElement(b));
}
use of com.google.inject.multibindings.MapBinderBinding in project guice by google.
the class MapBinderTest method testGetEntriesWithDuplicateValues.
@SuppressWarnings("rawtypes")
public void testGetEntriesWithDuplicateValues() {
// Set up the module
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");
}
};
// Get the MapBinderBinding
List<com.google.inject.spi.Element> elements = Elements.getElements(module);
MapBinderBinding<?> mapBinderBinding = getMapBinderBinding(elements);
// Execute the call to getEntries
List<Map.Entry<?, Binding<?>>> mapEntries = mapBinderBinding.getEntries(elements);
// Assert on the results
Map.Entry<?, Binding<?>> firstEntry = mapEntries.get(0);
assertEquals("A", firstEntry.getKey());
Binding<?> firstBinding = firstEntry.getValue();
assertEquals("a", ((InstanceBinding) firstBinding).getInstance());
Map.Entry<?, Binding<?>> secondEntry = mapEntries.get(1);
assertEquals("A", secondEntry.getKey());
Binding<?> secondBinding = secondEntry.getValue();
assertEquals("a", ((InstanceBinding) secondBinding).getInstance());
}
use of com.google.inject.multibindings.MapBinderBinding in project guice by google.
the class MapBinderTest method testGetEntriesWithDuplicateKeys.
@SuppressWarnings("rawtypes")
public void testGetEntriesWithDuplicateKeys() {
// Set up the module
Module module = new AbstractModule() {
@Override
protected void configure() {
MapBinder<String, String> mapBinder = MapBinder.newMapBinder(binder(), String.class, String.class);
mapBinder.addBinding("A").toInstance("a1");
mapBinder.addBinding("A").toInstance("a2");
mapBinder.permitDuplicates();
}
};
// Get the MapBinderBinding
List<com.google.inject.spi.Element> elements = Elements.getElements(module);
MapBinderBinding<?> mapBinderBinding = getMapBinderBinding(elements);
// Execute the call to getEntries
List<Map.Entry<?, Binding<?>>> mapEntries = mapBinderBinding.getEntries(elements);
// Assert on the results
Map.Entry<?, Binding<?>> firstEntry = mapEntries.get(0);
assertEquals("A", firstEntry.getKey());
Binding<?> firstBinding = firstEntry.getValue();
assertEquals("a1", ((InstanceBinding) firstBinding).getInstance());
Map.Entry<?, Binding<?>> secondEntry = mapEntries.get(1);
assertEquals("A", secondEntry.getKey());
Binding<?> secondBinding = secondEntry.getValue();
assertEquals("a2", ((InstanceBinding) secondBinding).getInstance());
}
use of com.google.inject.multibindings.MapBinderBinding in project guice by google.
the class SpiUtils method mapInjectorTest.
@SuppressWarnings("unchecked")
private static <T> void mapInjectorTest(Key<T> mapKey, TypeLiteral<?> keyType, TypeLiteral<?> valueType, Iterable<? extends Module> modules, boolean allowDuplicates, int expectedMapBindings, MapResult<?, ?>... results) {
Injector injector = Guice.createInjector(modules);
Visitor<T> visitor = new Visitor<>();
Binding<T> mapBinding = injector.getBinding(mapKey);
MapBinderBinding<T> mapbinder = (MapBinderBinding<T>) mapBinding.acceptTargetVisitor(visitor);
assertNotNull(mapbinder);
assertEquals(mapKey, mapbinder.getMapKey());
assertEquals(keyType, mapbinder.getKeyTypeLiteral());
assertEquals(valueType, mapbinder.getValueTypeLiteral());
assertEquals(allowDuplicates, mapbinder.permitsDuplicates());
List<Map.Entry<?, Binding<?>>> entries = Lists.newArrayList(mapbinder.getEntries());
List<MapResult<?, ?>> mapResults = Lists.newArrayList(results);
assertEquals("wrong entries, expected: " + mapResults + ", but was: " + entries, mapResults.size(), entries.size());
for (MapResult<?, ?> result : mapResults) {
Map.Entry<?, Binding<?>> found = null;
for (Map.Entry<?, Binding<?>> entry : entries) {
Object key = entry.getKey();
Binding<?> value = entry.getValue();
if (key.equals(result.k) && matches(value, result.v)) {
found = entry;
break;
}
}
if (found == null) {
fail("Could not find entry: " + result + " in remaining entries: " + entries);
} else {
assertTrue("mapBinder doesn't contain: " + found.getValue(), mapbinder.containsElement(found.getValue()));
entries.remove(found);
}
}
if (!entries.isEmpty()) {
fail("Found all entries of: " + mapResults + ", but more were left over: " + entries);
}
Key<?> mapOfJavaxProvider = mapKey.ofType(mapOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfProvider = mapKey.ofType(mapOfProviderOf(keyType, valueType));
Key<?> mapOfSetOfProvider = mapKey.ofType(mapOfSetOfProviderOf(keyType, valueType));
Key<?> mapOfSetOfJavaxProvider = mapKey.ofType(mapOfSetOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfCollectionOfProvider = mapKey.ofType(mapOfCollectionOfProviderOf(keyType, valueType));
Key<?> mapOfCollectionOfJavaxProvider = mapKey.ofType(mapOfCollectionOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfSet = mapKey.ofType(mapOf(keyType, setOf(valueType)));
Key<?> setOfEntry = mapKey.ofType(setOf(entryOfProviderOf(keyType, valueType)));
Key<?> setOfJavaxEntry = mapKey.ofType(setOf(entryOfJavaxProviderOf(keyType, valueType)));
Key<?> collectionOfProvidersOfEntryOfProvider = mapKey.ofType(collectionOfProvidersOf(entryOfProviderOf(keyType, valueType)));
Key<?> collectionOfJavaxProvidersOfEntryOfProvider = mapKey.ofType(collectionOfJavaxProvidersOf(entryOfProviderOf(keyType, valueType)));
Key<?> setOfExtendsOfEntryOfProvider = mapKey.ofType(setOfExtendsOf(entryOfProviderOf(keyType, valueType)));
Key<?> mapOfKeyExtendsValueKey = mapKey.ofType(mapOf(keyType, TypeLiteral.get(Types.subtypeOf(valueType.getType()))));
assertEquals(ImmutableSet.of(mapOfJavaxProvider, mapOfProvider, mapOfSetOfProvider, mapOfSetOfJavaxProvider, mapOfCollectionOfProvider, mapOfCollectionOfJavaxProvider, mapOfSet, mapOfKeyExtendsValueKey), mapbinder.getAlternateMapKeys());
boolean entrySetMatch = false;
boolean javaxEntrySetMatch = false;
boolean mapJavaxProviderMatch = false;
boolean mapProviderMatch = false;
boolean mapSetMatch = false;
boolean mapSetProviderMatch = false;
boolean mapSetJavaxProviderMatch = false;
boolean mapCollectionProviderMatch = false;
boolean mapCollectionJavaxProviderMatch = false;
boolean collectionOfProvidersOfEntryOfProviderMatch = false;
boolean collectionOfJavaxProvidersOfEntryOfProviderMatch = false;
boolean setOfExtendsOfEntryOfProviderMatch = false;
boolean mapOfKeyExtendsValueKeyMatch = false;
List<Object> otherMapBindings = Lists.newArrayList();
List<Binding<?>> otherMatches = Lists.newArrayList();
Multimap<Object, IndexedBinding> indexedEntries = MultimapBuilder.hashKeys().hashSetValues().build();
Indexer indexer = new Indexer(injector);
int duplicates = 0;
for (Binding<?> b : injector.getAllBindings().values()) {
boolean contains = mapbinder.containsElement(b);
Object visited = ((Binding<T>) b).acceptTargetVisitor(visitor);
if (visited instanceof MapBinderBinding) {
if (visited.equals(mapbinder)) {
assertTrue(contains);
} else {
otherMapBindings.add(visited);
}
} else if (b.getKey().equals(mapOfProvider)) {
assertTrue(contains);
mapProviderMatch = true;
} else if (b.getKey().equals(mapOfJavaxProvider)) {
assertTrue(contains);
mapJavaxProviderMatch = true;
} else if (b.getKey().equals(mapOfSet)) {
assertTrue(contains);
mapSetMatch = true;
} else if (b.getKey().equals(mapOfSetOfProvider)) {
assertTrue(contains);
mapSetProviderMatch = true;
} else if (b.getKey().equals(mapOfSetOfJavaxProvider)) {
assertTrue(contains);
mapSetJavaxProviderMatch = true;
} else if (b.getKey().equals(mapOfCollectionOfProvider)) {
assertTrue(contains);
mapCollectionProviderMatch = true;
} else if (b.getKey().equals(mapOfCollectionOfJavaxProvider)) {
assertTrue(contains);
mapCollectionJavaxProviderMatch = true;
} else if (b.getKey().equals(setOfEntry)) {
assertTrue(contains);
entrySetMatch = true;
// Validate that this binding is also a MultibinderBinding.
assertThat(((Binding<T>) b).acceptTargetVisitor(visitor)).isInstanceOf(MultibinderBinding.class);
} else if (b.getKey().equals(setOfJavaxEntry)) {
assertTrue(contains);
javaxEntrySetMatch = true;
} else if (b.getKey().equals(collectionOfProvidersOfEntryOfProvider)) {
assertTrue(contains);
collectionOfProvidersOfEntryOfProviderMatch = true;
} else if (b.getKey().equals(collectionOfJavaxProvidersOfEntryOfProvider)) {
assertTrue(contains);
collectionOfJavaxProvidersOfEntryOfProviderMatch = true;
} else if (b.getKey().equals(setOfExtendsOfEntryOfProvider)) {
assertTrue(contains);
setOfExtendsOfEntryOfProviderMatch = true;
} else if (b.getKey().equals(mapOfKeyExtendsValueKey)) {
assertTrue(contains);
mapOfKeyExtendsValueKeyMatch = true;
} else if (contains) {
if (b instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> pib = (ProviderInstanceBinding<?>) b;
if (pib.getUserSuppliedProvider() instanceof ProviderMapEntry) {
// weird casting required to workaround compilation issues with jdk6
ProviderMapEntry<?, ?> pme = (ProviderMapEntry<?, ?>) (Provider) pib.getUserSuppliedProvider();
Binding<?> valueBinding = injector.getBinding(pme.getValueKey());
if (indexer.isIndexable(valueBinding) && !indexedEntries.put(pme.getKey(), valueBinding.acceptTargetVisitor(indexer))) {
duplicates++;
}
}
}
otherMatches.add(b);
}
}
int sizeOfOther = otherMatches.size();
if (allowDuplicates) {
// account for 1 duplicate binding
sizeOfOther--;
}
// Multiply by two because each has a value and Map.Entry.
int expectedSize = 2 * (mapResults.size() + duplicates);
assertEquals("Incorrect other matches:\n\t" + Joiner.on("\n\t").join(otherMatches), expectedSize, sizeOfOther);
assertTrue(entrySetMatch);
assertTrue(javaxEntrySetMatch);
assertTrue(mapProviderMatch);
assertTrue(mapJavaxProviderMatch);
assertTrue(collectionOfProvidersOfEntryOfProviderMatch);
assertTrue(collectionOfJavaxProvidersOfEntryOfProviderMatch);
assertTrue(setOfExtendsOfEntryOfProviderMatch);
assertTrue(mapOfKeyExtendsValueKeyMatch);
assertEquals(allowDuplicates, mapSetMatch);
assertEquals(allowDuplicates, mapSetProviderMatch);
assertEquals(allowDuplicates, mapSetJavaxProviderMatch);
assertEquals(allowDuplicates, mapCollectionJavaxProviderMatch);
assertEquals(allowDuplicates, mapCollectionProviderMatch);
assertEquals("other MapBindings found: " + otherMapBindings, expectedMapBindings, otherMapBindings.size());
}
use of com.google.inject.multibindings.MapBinderBinding in project guice by google.
the class SpiUtils method mapInjectorTest.
@SuppressWarnings("unchecked")
private static <T> void mapInjectorTest(Key<T> mapKey, TypeLiteral<?> keyType, TypeLiteral<?> valueType, Iterable<? extends Module> modules, boolean allowDuplicates, int expectedMapBindings, MapResult... results) {
Injector injector = Guice.createInjector(modules);
Visitor<T> visitor = new Visitor<T>();
Binding<T> mapBinding = injector.getBinding(mapKey);
MapBinderBinding<T> mapbinder = (MapBinderBinding<T>) mapBinding.acceptTargetVisitor(visitor);
assertNotNull(mapbinder);
assertEquals(keyType, mapbinder.getKeyTypeLiteral());
assertEquals(valueType, mapbinder.getValueTypeLiteral());
assertEquals(allowDuplicates, mapbinder.permitsDuplicates());
List<Map.Entry<?, Binding<?>>> entries = Lists.newArrayList(mapbinder.getEntries());
List<MapResult> mapResults = Lists.newArrayList(results);
assertEquals("wrong entries, expected: " + mapResults + ", but was: " + entries, mapResults.size(), entries.size());
for (MapResult result : mapResults) {
Map.Entry<?, Binding<?>> found = null;
for (Map.Entry<?, Binding<?>> entry : entries) {
Object key = entry.getKey();
Binding<?> value = entry.getValue();
if (key.equals(result.k) && matches(value, result.v)) {
found = entry;
break;
}
}
if (found == null) {
fail("Could not find entry: " + result + " in remaining entries: " + entries);
} else {
assertTrue("mapBinder doesn't contain: " + found.getValue(), mapbinder.containsElement(found.getValue()));
entries.remove(found);
}
}
if (!entries.isEmpty()) {
fail("Found all entries of: " + mapResults + ", but more were left over: " + entries);
}
Key<?> mapOfJavaxProvider = mapKey.ofType(mapOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfProvider = mapKey.ofType(mapOfProviderOf(keyType, valueType));
Key<?> mapOfSetOfProvider = mapKey.ofType(mapOfSetOfProviderOf(keyType, valueType));
Key<?> mapOfSetOfJavaxProvider = mapKey.ofType(mapOfSetOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfCollectionOfProvider = mapKey.ofType(mapOfCollectionOfProviderOf(keyType, valueType));
Key<?> mapOfCollectionOfJavaxProvider = mapKey.ofType(mapOfCollectionOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfSet = mapKey.ofType(mapOf(keyType, setOf(valueType)));
Key<?> setOfEntry = mapKey.ofType(setOf(entryOfProviderOf(keyType, valueType)));
Key<?> setOfJavaxEntry = mapKey.ofType(setOf(entryOfJavaxProviderOf(keyType, valueType)));
Key<?> collectionOfProvidersOfEntryOfProvider = mapKey.ofType(collectionOfProvidersOf(entryOfProviderOf(keyType, valueType)));
Key<?> collectionOfJavaxProvidersOfEntryOfProvider = mapKey.ofType(collectionOfJavaxProvidersOf(entryOfProviderOf(keyType, valueType)));
boolean entrySetMatch = false;
boolean javaxEntrySetMatch = false;
boolean mapJavaxProviderMatch = false;
boolean mapProviderMatch = false;
boolean mapSetMatch = false;
boolean mapSetProviderMatch = false;
boolean mapSetJavaxProviderMatch = false;
boolean mapCollectionProviderMatch = false;
boolean mapCollectionJavaxProviderMatch = false;
boolean collectionOfProvidersOfEntryOfProviderMatch = false;
boolean collectionOfJavaxProvidersOfEntryOfProviderMatch = false;
List<Object> otherMapBindings = Lists.newArrayList();
List<Binding> otherMatches = Lists.newArrayList();
Multimap<Object, IndexedBinding> indexedEntries = MultimapBuilder.hashKeys().hashSetValues().build();
Indexer indexer = new Indexer(injector);
int duplicates = 0;
for (Binding b : injector.getAllBindings().values()) {
boolean contains = mapbinder.containsElement(b);
Object visited = b.acceptTargetVisitor(visitor);
if (visited instanceof MapBinderBinding) {
if (visited.equals(mapbinder)) {
assertTrue(contains);
} else {
otherMapBindings.add(visited);
}
} else if (b.getKey().equals(mapOfProvider)) {
assertTrue(contains);
mapProviderMatch = true;
} else if (b.getKey().equals(mapOfJavaxProvider)) {
assertTrue(contains);
mapJavaxProviderMatch = true;
} else if (b.getKey().equals(mapOfSet)) {
assertTrue(contains);
mapSetMatch = true;
} else if (b.getKey().equals(mapOfSetOfProvider)) {
assertTrue(contains);
mapSetProviderMatch = true;
} else if (b.getKey().equals(mapOfSetOfJavaxProvider)) {
assertTrue(contains);
mapSetJavaxProviderMatch = true;
} else if (b.getKey().equals(mapOfCollectionOfProvider)) {
assertTrue(contains);
mapCollectionProviderMatch = true;
} else if (b.getKey().equals(mapOfCollectionOfJavaxProvider)) {
assertTrue(contains);
mapCollectionJavaxProviderMatch = true;
} else if (b.getKey().equals(setOfEntry)) {
assertTrue(contains);
entrySetMatch = true;
// Validate that this binding is also a MultibinderBinding.
assertTrue(b.acceptTargetVisitor(visitor) instanceof MultibinderBinding);
} else if (b.getKey().equals(setOfJavaxEntry)) {
assertTrue(contains);
javaxEntrySetMatch = true;
} else if (b.getKey().equals(collectionOfProvidersOfEntryOfProvider)) {
assertTrue(contains);
collectionOfProvidersOfEntryOfProviderMatch = true;
} else if (b.getKey().equals(collectionOfJavaxProvidersOfEntryOfProvider)) {
assertTrue(contains);
collectionOfJavaxProvidersOfEntryOfProviderMatch = true;
} else if (contains) {
if (b instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> pib = (ProviderInstanceBinding<?>) b;
if (pib.getUserSuppliedProvider() instanceof ProviderMapEntry) {
// weird casting required to workaround compilation issues with jdk6
ProviderMapEntry<?, ?> pme = (ProviderMapEntry<?, ?>) (Provider) pib.getUserSuppliedProvider();
Binding<?> valueBinding = injector.getBinding(pme.getValueKey());
if (indexer.isIndexable(valueBinding) && !indexedEntries.put(pme.getKey(), valueBinding.acceptTargetVisitor(indexer))) {
duplicates++;
}
}
}
otherMatches.add(b);
}
}
int sizeOfOther = otherMatches.size();
if (allowDuplicates) {
// account for 1 duplicate binding
sizeOfOther--;
}
// Multiply by two because each has a value and Map.Entry.
int expectedSize = 2 * (mapResults.size() + duplicates);
assertEquals("Incorrect other matches:\n\t" + Joiner.on("\n\t").join(otherMatches), expectedSize, sizeOfOther);
assertTrue(entrySetMatch);
assertTrue(javaxEntrySetMatch);
assertTrue(mapProviderMatch);
assertTrue(mapJavaxProviderMatch);
assertTrue(collectionOfProvidersOfEntryOfProviderMatch);
assertTrue(collectionOfJavaxProvidersOfEntryOfProviderMatch);
assertEquals(allowDuplicates, mapSetMatch);
assertEquals(allowDuplicates, mapSetProviderMatch);
assertEquals(allowDuplicates, mapSetJavaxProviderMatch);
assertEquals(allowDuplicates, mapCollectionJavaxProviderMatch);
assertEquals(allowDuplicates, mapCollectionProviderMatch);
assertEquals("other MapBindings found: " + otherMapBindings, expectedMapBindings, otherMapBindings.size());
}
Aggregations