use of com.google.inject.Binding in project roboguice by roboguice.
the class SpiUtils method setModuleTest.
@SuppressWarnings("unchecked")
private static <T> void setModuleTest(Key<Set<T>> setKey, Key<Collection<Provider<T>>> collectionOfProvidersKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
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;
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 (!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);
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(otherElements));
}
use of com.google.inject.Binding 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.Binding in project roboguice by roboguice.
the class MultibinderTest method testSetAndMapValueAreDistinctInSpi.
// See issue 670
public void testSetAndMapValueAreDistinctInSpi() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
Multibinder.newSetBinder(binder(), String.class).addBinding().toInstance("A");
MapBinder.newMapBinder(binder(), String.class, String.class).addBinding("B").toInstance("b");
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("C");
}
});
Collector collector = new Collector();
Binding<Map<String, String>> mapbinding = injector.getBinding(Key.get(mapOfStringString));
mapbinding.acceptTargetVisitor(collector);
assertNotNull(collector.mapbinding);
Binding<Set<String>> setbinding = injector.getBinding(Key.get(setOfString));
setbinding.acceptTargetVisitor(collector);
assertNotNull(collector.setbinding);
Binding<Optional<String>> optionalbinding = injector.getBinding(Key.get(optionalOfString));
optionalbinding.acceptTargetVisitor(collector);
assertNotNull(collector.optionalbinding);
// There should only be three instance bindings for string types
// (but because of the OptionalBinder, there's 2 ProviderInstanceBindings also).
// We also know the InstanceBindings will be in the order: A, b, C because that's
// how we bound them, and binding order is preserved.
List<Binding<String>> bindings = FluentIterable.from(injector.findBindingsByType(stringType)).filter(Predicates.instanceOf(InstanceBinding.class)).toList();
assertEquals(bindings.toString(), 3, bindings.size());
Binding<String> a = bindings.get(0);
Binding<String> b = bindings.get(1);
Binding<String> c = bindings.get(2);
assertEquals("A", ((InstanceBinding<String>) a).getInstance());
assertEquals("b", ((InstanceBinding<String>) b).getInstance());
assertEquals("C", ((InstanceBinding<String>) c).getInstance());
// Make sure the correct elements belong to their own sets.
assertFalse(collector.mapbinding.containsElement(a));
assertTrue(collector.mapbinding.containsElement(b));
assertFalse(collector.mapbinding.containsElement(c));
assertTrue(collector.setbinding.containsElement(a));
assertFalse(collector.setbinding.containsElement(b));
assertFalse(collector.setbinding.containsElement(c));
assertFalse(collector.optionalbinding.containsElement(a));
assertFalse(collector.optionalbinding.containsElement(b));
assertTrue(collector.optionalbinding.containsElement(c));
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class MultibinderTest method testKeyHashCodesFixedAtInjectionTime.
/**
* Ensure key hash codes are fixed at injection time, not binding time.
*/
public void testKeyHashCodesFixedAtInjectionTime() {
Module ab = new AbstractModule() {
@Override
protected void configure() {
Multibinder<List<String>> multibinder = Multibinder.newSetBinder(binder(), listOfStrings);
List<String> list = Lists.newArrayList();
multibinder.addBinding().toInstance(list);
list.add("A");
list.add("B");
}
};
Injector injector = Guice.createInjector(ab);
for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
Key<?> bindingKey = entry.getKey();
Key<?> clonedKey;
if (bindingKey.getAnnotation() != null) {
clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotation());
} else if (bindingKey.getAnnotationType() != null) {
clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotationType());
} else {
clonedKey = Key.get(bindingKey.getTypeLiteral());
}
assertEquals(bindingKey, clonedKey);
assertEquals("Incorrect hashcode for " + bindingKey + " -> " + entry.getValue(), bindingKey.hashCode(), clonedKey.hashCode());
}
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class OptionalBinderTest method testKeyHashCodesFixedAtInjectionTime.
/**
* Ensure key hash codes are fixed at injection time, not binding time.
*/
public void testKeyHashCodesFixedAtInjectionTime() {
Module m = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder<List<String>> b = OptionalBinder.newOptionalBinder(binder(), listOfStrings);
List<String> list = Lists.newArrayList();
b.setDefault().toInstance(list);
b.setBinding().toInstance(list);
list.add("A");
list.add("B");
}
};
Injector injector = Guice.createInjector(m);
for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
Key<?> bindingKey = entry.getKey();
Key<?> clonedKey;
if (bindingKey.getAnnotation() != null) {
clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotation());
} else if (bindingKey.getAnnotationType() != null) {
clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotationType());
} else {
clonedKey = Key.get(bindingKey.getTypeLiteral());
}
assertEquals(bindingKey, clonedKey);
assertEquals("Incorrect hashcode for " + bindingKey + " -> " + entry.getValue(), bindingKey.hashCode(), clonedKey.hashCode());
}
}
Aggregations