use of com.google.inject.Key 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.Key in project roboguice by roboguice.
the class MultibinderTest method testMultibindingProviderDependencies.
public void testMultibindingProviderDependencies() {
final Annotation setAnn = Names.named("foo");
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, setAnn);
multibinder.addBinding().toInstance("a");
multibinder.addBinding().toInstance("b");
}
});
HasDependencies providerBinding = (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {
});
HasDependencies setBinding = (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {
});
// sanity check the size
assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size());
Set<Dependency<?>> expected = Sets.newHashSet();
for (Dependency<?> dep : setBinding.getDependencies()) {
Key key = dep.getKey();
Dependency<?> providerDependency = Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType())));
expected.add(providerDependency);
}
assertEquals(expected, providerBinding.getDependencies());
}
use of com.google.inject.Key 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.Key in project roboguice by roboguice.
the class MapBinderTest method testMultibinderDependenciesInToolStage.
/** We just want to make sure that mapbinder's binding depends on the underlying multibinder. */
public void testMultibinderDependenciesInToolStage() {
Injector injector = Guice.createInjector(Stage.TOOL, new AbstractModule() {
@Override
protected void configure() {
MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class);
mapBinder.addBinding(1).toInstance("A");
mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b")));
bindConstant().annotatedWith(Names.named("b")).to("B");
}
});
Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {
});
HasDependencies withDependencies = (HasDependencies) binding;
Key<?> setKey = new Key<Set<Map.Entry<Integer, Provider<String>>>>() {
};
assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(setKey)), withDependencies.getDependencies());
}
use of com.google.inject.Key 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