use of com.google.inject.multibindings.MultibinderBinding in project guice by google.
the class SpiUtils method setInjectorTest.
@SuppressWarnings("unchecked")
private static <T> void setInjectorTest(Key<Set<T>> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
Key<?> collectionOfProvidersKey = setKey.ofType(collectionOfProvidersOf(elementType));
Key<?> collectionOfJavaxProvidersKey = setKey.ofType(collectionOfJavaxProvidersOf(elementType));
Injector injector = Guice.createInjector(modules);
Visitor<Set<T>> visitor = new Visitor<Set<T>>();
Binding<Set<T>> binding = injector.getBinding(setKey);
MultibinderBinding<Set<T>> multibinder = (MultibinderBinding<Set<T>>) binding.acceptTargetVisitor(visitor);
assertNotNull(multibinder);
assertEquals(elementType, multibinder.getElementTypeLiteral());
assertEquals(allowDuplicates, multibinder.permitsDuplicates());
List<Binding<?>> elements = Lists.newArrayList(multibinder.getElements());
List<BindResult> bindResults = Lists.newArrayList(results);
assertEquals("wrong bind elements, expected: " + bindResults + ", but was: " + multibinder.getElements(), bindResults.size(), elements.size());
for (BindResult result : bindResults) {
Binding found = null;
for (Binding item : elements) {
if (matches(item, result)) {
found = item;
break;
}
}
if (found == null) {
fail("Could not find element: " + result + " in remaining elements: " + elements);
} else {
elements.remove(found);
}
}
if (!elements.isEmpty()) {
fail("Found all elements of: " + bindResults + ", but more were left over: " + elements);
}
Set<Binding> setOfElements = new HashSet<Binding>(multibinder.getElements());
Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
Indexer indexer = new Indexer(injector);
for (Binding<?> oneBinding : setOfElements) {
setOfIndexed.add(oneBinding.acceptTargetVisitor(indexer));
}
List<Object> otherMultibinders = Lists.newArrayList();
List<Binding> otherContains = Lists.newArrayList();
boolean collectionOfProvidersMatch = false;
boolean collectionOfJavaxProvidersMatch = false;
for (Binding b : injector.getAllBindings().values()) {
boolean contains = multibinder.containsElement(b);
Key key = b.getKey();
Object visited = b.acceptTargetVisitor(visitor);
if (visited != null) {
if (visited.equals(multibinder)) {
assertTrue(contains);
} else {
otherMultibinders.add(visited);
}
} else if (setOfElements.contains(b)) {
assertTrue(contains);
} else if (key.equals(collectionOfProvidersKey)) {
assertTrue(contains);
collectionOfProvidersMatch = true;
} else if (key.equals(collectionOfJavaxProvidersKey)) {
assertTrue(contains);
collectionOfJavaxProvidersMatch = true;
} else if (contains) {
if (!indexer.isIndexable(b) || !setOfIndexed.contains(b.acceptTargetVisitor(indexer))) {
otherContains.add(b);
}
}
}
assertTrue(collectionOfProvidersMatch);
assertTrue(collectionOfJavaxProvidersMatch);
if (allowDuplicates) {
assertEquals("contained more than it should: " + otherContains, 1, otherContains.size());
} else {
assertTrue("contained more than it should: " + otherContains, otherContains.isEmpty());
}
assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
}
use of com.google.inject.multibindings.MultibinderBinding in project guice by google.
the class SpiUtils method setModuleTest.
@SuppressWarnings("unchecked")
private static <T> void setModuleTest(Key<Set<T>> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
Key<?> collectionOfProvidersKey = setKey.ofType(collectionOfProvidersOf(elementType));
Key<?> collectionOfJavaxProvidersKey = setKey.ofType(collectionOfJavaxProvidersOf(elementType));
List<BindResult> bindResults = Lists.newArrayList(results);
List<Element> elements = Elements.getElements(modules);
Visitor<T> visitor = new Visitor<T>();
MultibinderBinding<Set<T>> multibinder = null;
for (Element element : elements) {
if (element instanceof Binding && ((Binding) element).getKey().equals(setKey)) {
multibinder = (MultibinderBinding<Set<T>>) ((Binding) element).acceptTargetVisitor(visitor);
break;
}
}
assertNotNull(multibinder);
assertEquals(elementType, multibinder.getElementTypeLiteral());
List<Object> otherMultibinders = Lists.newArrayList();
Set<Element> otherContains = new HashSet<Element>();
List<Element> otherElements = Lists.newArrayList();
int duplicates = 0;
Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
Indexer indexer = new Indexer(null);
boolean collectionOfProvidersMatch = false;
boolean collectionOfJavaxProvidersMatch = false;
for (Element element : elements) {
boolean contains = multibinder.containsElement(element);
if (!contains) {
otherElements.add(element);
}
boolean matched = false;
Key key = null;
if (element instanceof Binding) {
Binding binding = (Binding) element;
if (indexer.isIndexable(binding) && !setOfIndexed.add((IndexedBinding) binding.acceptTargetVisitor(indexer))) {
duplicates++;
}
key = binding.getKey();
Object visited = binding.acceptTargetVisitor(visitor);
if (visited != null) {
matched = true;
if (visited.equals(multibinder)) {
assertTrue(contains);
} else {
otherMultibinders.add(visited);
}
}
}
if (collectionOfProvidersKey.equals(key)) {
assertTrue(contains);
assertFalse(matched);
collectionOfProvidersMatch = true;
} else if (collectionOfJavaxProvidersKey.equals(key)) {
assertTrue(contains);
assertFalse(matched);
collectionOfJavaxProvidersMatch = true;
} else if (!matched && contains) {
otherContains.add(element);
}
}
if (allowDuplicates) {
assertEquals("wrong contained elements: " + otherContains, bindResults.size() + 1 + duplicates, otherContains.size());
} else {
assertEquals("wrong contained elements: " + otherContains, bindResults.size() + duplicates, otherContains.size());
}
assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
assertTrue(collectionOfProvidersMatch);
assertTrue(collectionOfJavaxProvidersMatch);
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(otherElements));
}
use of com.google.inject.multibindings.MultibinderBinding 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.MultibinderBinding 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());
}
use of com.google.inject.multibindings.MultibinderBinding in project guice by google.
the class SpiUtils method setInjectorTest.
@SuppressWarnings("unchecked")
private static <T> void setInjectorTest(Key<Set<T>> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult<T>... results) {
Key<?> collectionOfProvidersKey = setKey.ofType(collectionOfProvidersOf(elementType));
Key<?> collectionOfJavaxProvidersKey = setKey.ofType(collectionOfJavaxProvidersOf(elementType));
Key<?> setOfExtendsKey = setKey.ofType(setOfExtendsOf(elementType));
Injector injector = Guice.createInjector(modules);
Visitor<Set<T>> visitor = new Visitor<>();
Binding<Set<T>> binding = injector.getBinding(setKey);
MultibinderBinding<Set<T>> multibinder = (MultibinderBinding<Set<T>>) binding.acceptTargetVisitor(visitor);
assertNotNull(multibinder);
assertEquals(setKey, multibinder.getSetKey());
assertEquals(elementType, multibinder.getElementTypeLiteral());
assertEquals(allowDuplicates, multibinder.permitsDuplicates());
assertEquals(ImmutableSet.of(collectionOfProvidersKey, collectionOfJavaxProvidersKey, setOfExtendsKey), multibinder.getAlternateSetKeys());
List<Binding<?>> elements = Lists.newArrayList(multibinder.getElements());
List<BindResult<?>> bindResults = Lists.newArrayList(results);
assertEquals("wrong bind elements, expected: " + bindResults + ", but was: " + multibinder.getElements(), bindResults.size(), elements.size());
for (BindResult<?> result : bindResults) {
Binding<?> found = null;
for (Binding<?> item : elements) {
if (matches(item, result)) {
found = item;
break;
}
}
if (found == null) {
fail("Could not find element: " + result + " in remaining elements: " + elements);
} else {
elements.remove(found);
}
}
if (!elements.isEmpty()) {
fail("Found all elements of: " + bindResults + ", but more were left over: " + elements);
}
Set<Binding<?>> setOfElements = new HashSet<>(multibinder.getElements());
Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
Indexer indexer = new Indexer(injector);
for (Binding<?> oneBinding : setOfElements) {
setOfIndexed.add(oneBinding.acceptTargetVisitor(indexer));
}
List<Object> otherMultibinders = Lists.newArrayList();
List<Binding<?>> otherContains = Lists.newArrayList();
boolean collectionOfProvidersMatch = false;
boolean collectionOfJavaxProvidersMatch = false;
boolean setOfExtendsKeyMatch = false;
for (Binding<?> b : injector.getAllBindings().values()) {
boolean contains = multibinder.containsElement(b);
Key<?> key = b.getKey();
Object visited = ((Binding<Set<T>>) b).acceptTargetVisitor(visitor);
if (visited != null) {
if (visited.equals(multibinder)) {
assertTrue(contains);
} else {
otherMultibinders.add(visited);
}
} else if (setOfElements.contains(b)) {
assertTrue(contains);
} else if (key.equals(collectionOfProvidersKey)) {
assertTrue(contains);
collectionOfProvidersMatch = true;
} else if (key.equals(collectionOfJavaxProvidersKey)) {
assertTrue(contains);
collectionOfJavaxProvidersMatch = true;
} else if (key.equals(setOfExtendsKey)) {
assertTrue(contains);
setOfExtendsKeyMatch = true;
} else if (contains) {
if (!indexer.isIndexable(b) || !setOfIndexed.contains(b.acceptTargetVisitor(indexer))) {
otherContains.add(b);
}
}
}
assertTrue(collectionOfProvidersMatch);
assertTrue(collectionOfJavaxProvidersMatch);
assertTrue(setOfExtendsKeyMatch);
if (allowDuplicates) {
assertEquals("contained more than it should: " + otherContains, 1, otherContains.size());
} else {
assertTrue("contained more than it should: " + otherContains, otherContains.isEmpty());
}
assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
}
Aggregations