use of com.google.inject.multibindings.OptionalBinder in project guice by google.
the class OptionalBinderTest method testDependencies_actual.
public void testDependencies_actual() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
optionalbinder.setBinding().to(Key.get(String.class, Names.named("b")));
bindConstant().annotatedWith(Names.named("b")).to("B");
}
});
Binding<String> binding = injector.getBinding(Key.get(String.class));
HasDependencies withDependencies = (HasDependencies) binding;
Set<String> elements = Sets.newHashSet();
elements.addAll(recurseForDependencies(injector, withDependencies));
assertEquals(ImmutableSet.of("B"), elements);
}
use of com.google.inject.multibindings.OptionalBinder in project guice by google.
the class OptionalBinderTest method testDependencies_both.
public void testDependencies_both() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
optionalbinder.setDefault().toInstance("A");
optionalbinder.setBinding().to(Key.get(String.class, Names.named("b")));
bindConstant().annotatedWith(Names.named("b")).to("B");
}
});
Binding<String> binding = injector.getBinding(Key.get(String.class));
HasDependencies withDependencies = (HasDependencies) binding;
Set<String> elements = Sets.newHashSet();
elements.addAll(recurseForDependencies(injector, withDependencies));
assertEquals(ImmutableSet.of("B"), elements);
}
use of com.google.inject.multibindings.OptionalBinder in project guice by google.
the class OptionalBinderTest method testDependencies_default.
public void testDependencies_default() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
optionalbinder.setDefault().toInstance("A");
}
});
Binding<String> binding = injector.getBinding(Key.get(String.class));
HasDependencies withDependencies = (HasDependencies) binding;
Set<String> elements = Sets.newHashSet();
elements.addAll(recurseForDependencies(injector, withDependencies));
assertEquals(ImmutableSet.of("A"), elements);
}
use of com.google.inject.multibindings.OptionalBinder in project guice by google.
the class OptionalBinderTest method testMatchingMarkerAnnotations.
@Marker
public void testMatchingMarkerAnnotations() throws Exception {
Method m = OptionalBinderTest.class.getDeclaredMethod("testMatchingMarkerAnnotations");
assertNotNull(m);
final Annotation marker = m.getAnnotation(Marker.class);
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
OptionalBinder<Integer> mb1 = OptionalBinder.newOptionalBinder(binder(), Key.get(Integer.class, Marker.class));
OptionalBinder<Integer> mb2 = OptionalBinder.newOptionalBinder(binder(), Key.get(Integer.class, marker));
mb1.setDefault().toInstance(1);
mb2.setBinding().toInstance(2);
// This assures us that the two binders are equivalent, so we expect the instance added to
// each to have been added to one set.
assertEquals(mb1, mb2);
}
});
Integer i1 = injector.getInstance(Key.get(Integer.class, Marker.class));
Integer i2 = injector.getInstance(Key.get(Integer.class, marker));
// These must be identical, because the marker annotations collapsed to the same thing.
assertSame(i1, i2);
assertEquals(2, i2.intValue());
}
Aggregations