use of com.google.inject.multibindings.MapBinder.RealMapBinder.ProviderMapEntry in project roboguice by roboguice.
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<?> mapOfSet = mapKey.ofType(mapOf(keyType, setOf(valueType)));
Key<?> setOfEntry = mapKey.ofType(setOf(entryOfProviderOf(keyType, valueType)));
boolean entrySetMatch = false;
boolean mapJavaxProviderMatch = false;
boolean mapProviderMatch = false;
boolean mapSetMatch = false;
boolean mapSetProviderMatch = 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(setOfEntry)) {
assertTrue(contains);
entrySetMatch = true;
// Validate that this binding is also a MultibinderBinding.
assertTrue(b.acceptTargetVisitor(visitor) instanceof MultibinderBinding);
} 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--;
}
// account for 1 value & 1 Map.Entry of each expected binding.
sizeOfOther = sizeOfOther / 2;
assertEquals("Incorrect other matches: " + otherMatches, mapResults.size() + duplicates, sizeOfOther);
assertTrue(entrySetMatch);
assertTrue(mapProviderMatch);
assertTrue(mapJavaxProviderMatch);
assertEquals(allowDuplicates, mapSetMatch);
assertEquals(allowDuplicates, mapSetProviderMatch);
assertEquals("other MapBindings found: " + otherMapBindings, expectedMapBindings, otherMapBindings.size());
}
use of com.google.inject.multibindings.MapBinder.RealMapBinder.ProviderMapEntry in project roboguice by roboguice.
the class SpiUtils method mapModuleTest.
@SuppressWarnings("unchecked")
private static <T> void mapModuleTest(Key<T> mapKey, TypeLiteral<?> keyType, TypeLiteral<?> valueType, Iterable<? extends Module> modules, boolean allowDuplicates, int expectedMapBindings, MapResult... results) {
Set<Element> elements = ImmutableSet.copyOf(Elements.getElements(modules));
Visitor<T> visitor = new Visitor<T>();
MapBinderBinding<T> mapbinder = null;
Map<Key<?>, Binding<?>> keyMap = Maps.newHashMap();
for (Element element : elements) {
if (element instanceof Binding) {
Binding<?> binding = (Binding<?>) element;
keyMap.put(binding.getKey(), binding);
if (binding.getKey().equals(mapKey)) {
mapbinder = (MapBinderBinding<T>) ((Binding<T>) binding).acceptTargetVisitor(visitor);
}
}
}
assertNotNull(mapbinder);
assertEquals(keyType, mapbinder.getKeyTypeLiteral());
assertEquals(valueType, mapbinder.getValueTypeLiteral());
List<MapResult> mapResults = Lists.newArrayList(results);
Key<?> mapOfProvider = mapKey.ofType(mapOfProviderOf(keyType, valueType));
Key<?> mapOfSetOfProvider = mapKey.ofType(mapOfSetOfProviderOf(keyType, valueType));
Key<?> mapOfSet = mapKey.ofType(mapOf(keyType, setOf(valueType)));
Key<?> setOfEntry = mapKey.ofType(setOf(entryOfProviderOf(keyType, valueType)));
Key<?> collectionOfProvidersOfEntry = mapKey.ofType(collectionOfProvidersOf(entryOfProviderOf(keyType, valueType)));
boolean entrySetMatch = false;
boolean entryProviderCollectionMatch = false;
boolean mapProviderMatch = false;
boolean mapSetMatch = false;
boolean mapSetProviderMatch = false;
List<Object> otherMapBindings = Lists.newArrayList();
List<Element> otherMatches = Lists.newArrayList();
List<Element> otherElements = Lists.newArrayList();
Indexer indexer = new Indexer(null);
Multimap<Object, IndexedBinding> indexedEntries = MultimapBuilder.hashKeys().hashSetValues().build();
int duplicates = 0;
for (Element element : elements) {
boolean contains = mapbinder.containsElement(element);
if (!contains) {
otherElements.add(element);
}
boolean matched = false;
Key key = null;
Binding b = null;
if (element instanceof Binding) {
b = (Binding) element;
if (b instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> pb = (ProviderInstanceBinding<?>) b;
if (pb.getUserSuppliedProvider() instanceof ProviderMapEntry) {
// weird casting required to workaround jdk6 compilation problems
ProviderMapEntry<?, ?> pme = (ProviderMapEntry<?, ?>) (Provider) pb.getUserSuppliedProvider();
Binding<?> valueBinding = keyMap.get(pme.getValueKey());
if (indexer.isIndexable(valueBinding) && !indexedEntries.put(pme.getKey(), valueBinding.acceptTargetVisitor(indexer))) {
duplicates++;
}
}
}
key = b.getKey();
Object visited = b.acceptTargetVisitor(visitor);
if (visited instanceof MapBinderBinding) {
matched = true;
if (visited.equals(mapbinder)) {
assertTrue(contains);
} else {
otherMapBindings.add(visited);
}
}
} else if (element instanceof ProviderLookup) {
key = ((ProviderLookup) element).getKey();
}
if (!matched && key != null) {
if (key.equals(mapOfProvider)) {
matched = true;
assertTrue(contains);
mapProviderMatch = true;
} else if (key.equals(mapOfSet)) {
matched = true;
assertTrue(contains);
mapSetMatch = true;
} else if (key.equals(mapOfSetOfProvider)) {
matched = true;
assertTrue(contains);
mapSetProviderMatch = true;
} else if (key.equals(setOfEntry)) {
matched = true;
assertTrue(contains);
entrySetMatch = true;
// Validate that this binding is also a MultibinderBinding.
if (b != null) {
assertTrue(b.acceptTargetVisitor(visitor) instanceof MultibinderBinding);
}
} else if (key.equals(collectionOfProvidersOfEntry)) {
matched = true;
assertTrue(contains);
entryProviderCollectionMatch = true;
}
}
if (!matched && contains) {
otherMatches.add(element);
}
}
int otherMatchesSize = otherMatches.size();
if (allowDuplicates) {
// allow for 1 duplicate binding
otherMatchesSize--;
}
// value, ProviderLookup per value, Map.Entry per value
otherMatchesSize = otherMatchesSize / 3;
assertEquals("incorrect number of contains, leftover matches: " + otherMatches, mapResults.size() + duplicates, otherMatchesSize);
assertTrue(entrySetMatch);
assertTrue(entryProviderCollectionMatch);
assertTrue(mapProviderMatch);
assertEquals(allowDuplicates, mapSetMatch);
assertEquals(allowDuplicates, mapSetProviderMatch);
assertEquals("other MapBindings found: " + otherMapBindings, expectedMapBindings, otherMapBindings.size());
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(otherElements));
}
Aggregations