use of com.google.inject.spi.HasDependencies 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.spi.HasDependencies in project roboguice by roboguice.
the class MapBinderTest method recurseForDependencies.
private Set<String> recurseForDependencies(Injector injector, HasDependencies hasDependencies) {
Set<String> elements = Sets.newHashSet();
for (Dependency<?> dependency : hasDependencies.getDependencies()) {
Binding<?> binding = injector.getBinding(dependency.getKey());
HasDependencies deps = (HasDependencies) binding;
if (binding instanceof InstanceBinding) {
elements.add((String) ((InstanceBinding<?>) binding).getInstance());
} else {
elements.addAll(recurseForDependencies(injector, deps));
}
}
return elements;
}
use of com.google.inject.spi.HasDependencies 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.spi.HasDependencies in project roboguice by roboguice.
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.spi.HasDependencies in project roboguice by roboguice.
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);
}
Aggregations