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());
}
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class ElementsTest method testBindKeysWithAnnotationInstance.
public void testBindKeysWithAnnotationInstance() {
FailingElementVisitor annotationChecker = new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertEquals(Key.get(String.class, Names.named("a")), command.getKey());
return null;
}
};
checkModule(new AbstractModule() {
protected void configure() {
bind(String.class).annotatedWith(Names.named("a")).toInstance("B");
bind(new TypeLiteral<String>() {
}).annotatedWith(Names.named("a")).toInstance("C");
}
}, annotationChecker, annotationChecker);
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class ElementsTest method testBindKeysWithAnnotationType.
public void testBindKeysWithAnnotationType() {
FailingElementVisitor annotationChecker = new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertEquals(Key.get(String.class, SampleAnnotation.class), command.getKey());
return null;
}
};
checkModule(new AbstractModule() {
protected void configure() {
bind(String.class).annotatedWith(SampleAnnotation.class).toInstance("A");
bind(new TypeLiteral<String>() {
}).annotatedWith(SampleAnnotation.class).toInstance("B");
}
}, annotationChecker, annotationChecker);
}
Aggregations