use of com.google.inject.Provider in project roboguice by roboguice.
the class MultibinderTest method testMultibinderCanInjectCollectionOfProviders.
public void testMultibinderCanInjectCollectionOfProviders() {
Module module = new AbstractModule() {
protected void configure() {
final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
multibinder.addBinding().toProvider(Providers.of("A"));
multibinder.addBinding().toProvider(Providers.of("B"));
multibinder.addBinding().toInstance("C");
}
};
Injector injector = Guice.createInjector(module);
injector.getInstance(Key.get(collectionOfProvidersOfStrings));
Key<Collection<Provider<String>>> collectionKey = Key.get(collectionOfProvidersOfStrings);
Collection<Provider<String>> providers = injector.getInstance(collectionKey);
Collection<String> values = Lists.newArrayList();
for (Provider<String> provider : providers) {
values.add(provider.get());
}
Collection<String> expectedValues = ImmutableList.of("A", "B", "C");
assertEquals(expectedValues, values);
}
use of com.google.inject.Provider 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.Provider in project roboguice by roboguice.
the class MultibinderTest method testMultibinderSetShowsBothElementsIfToStringDifferent.
public void testMultibinderSetShowsBothElementsIfToStringDifferent() {
// considers.
class ValueType {
int a;
int b;
ValueType(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
return (obj instanceof ValueType) && (((ValueType) obj).a == a);
}
@Override
public int hashCode() {
return a;
}
@Override
public String toString() {
return String.format("ValueType(%d,%d)", a, b);
}
}
Module module1 = new AbstractModule() {
protected void configure() {
final Multibinder<ValueType> multibinder = Multibinder.newSetBinder(binder(), ValueType.class);
multibinder.addBinding().toProvider(Providers.of(new ValueType(1, 2)));
}
};
Module module2 = new AbstractModule() {
protected void configure() {
final Multibinder<ValueType> multibinder = Multibinder.newSetBinder(binder(), ValueType.class);
multibinder.addBinding().toInstance(new ValueType(1, 3));
}
};
Injector injector = Guice.createInjector(module1, module2);
TypeLiteral<ValueType> valueType = TypeLiteral.get(ValueType.class);
TypeLiteral<Set<ValueType>> setOfValueType = new TypeLiteral<Set<ValueType>>() {
};
TypeLiteral<Collection<Provider<ValueType>>> collectionOfProvidersOfValueType = new TypeLiteral<Collection<Provider<ValueType>>>() {
};
try {
injector.getInstance(Key.get(setOfValueType));
fail();
} catch (ProvisionException expected) {
assertContains(expected.getMessage(), "1) Set injection failed due to multiple elements comparing equal:", "\"ValueType(1,2)\"", "bound at " + module1.getClass().getName(), "\"ValueType(1,3)\"", "bound at " + module2.getClass().getName());
}
// But we can still visit the module!
assertSetVisitor(Key.get(setOfValueType), Key.get(collectionOfProvidersOfValueType), valueType, setOf(module1, module2), MODULE, false, 0, instance(new ValueType(1, 2)), instance(new ValueType(1, 3)));
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class MultibinderTest method testMultibinderCanInjectCollectionOfProvidersWithAnnotation.
public void testMultibinderCanInjectCollectionOfProvidersWithAnnotation() {
final Annotation ann = Names.named("foo");
Module module = new AbstractModule() {
protected void configure() {
final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, ann);
multibinder.addBinding().toProvider(Providers.of("A"));
multibinder.addBinding().toProvider(Providers.of("B"));
multibinder.addBinding().toInstance("C");
}
};
Injector injector = Guice.createInjector(module);
Key<Collection<Provider<String>>> collectionKey = Key.get(collectionOfProvidersOfStrings, ann);
Collection<Provider<String>> providers = injector.getInstance(collectionKey);
Collection<String> values = Lists.newArrayList();
for (Provider<String> provider : providers) {
values.add(provider.get());
}
Collection<String> expectedValues = ImmutableList.of("A", "B", "C");
assertEquals(expectedValues, values);
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class OptionalBinderTest method testOptionalIsAppropriatelyLazy.
public void testOptionalIsAppropriatelyLazy() throws Exception {
Module module = new AbstractModule() {
int nextValue = 1;
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), Integer.class).setDefault().to(Key.get(Integer.class, Names.named("foo")));
}
@Provides
@Named("foo")
int provideInt() {
return nextValue++;
}
};
Injector injector = Guice.createInjector(module);
Optional<Provider<Integer>> optionalP = injector.getInstance(Key.get(optionalOfProviderInteger));
Optional<javax.inject.Provider<Integer>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderInteger));
assertEquals(1, injector.getInstance(Integer.class).intValue());
assertEquals(2, injector.getInstance(Integer.class).intValue());
// Calling .get() on an Optional<Integer> multiple times will keep giving the same thing
Optional<Integer> optional = injector.getInstance(Key.get(optionalOfInteger));
assertEquals(3, optional.get().intValue());
assertEquals(3, optional.get().intValue());
// But getting another Optional<Integer> will give a new one.
assertEquals(4, injector.getInstance(Key.get(optionalOfInteger)).get().intValue());
// And the Optional<Provider> will return a provider that gives a new value each time.
assertEquals(5, optionalP.get().get().intValue());
assertEquals(6, optionalP.get().get().intValue());
assertEquals(7, optionalJxP.get().get().intValue());
assertEquals(8, optionalJxP.get().get().intValue());
// and same rules with java.util.Optional
if (HAS_JAVA_OPTIONAL) {
optional = toOptional(injector.getInstance(Key.get(javaOptionalOfInteger)));
assertEquals(9, optional.get().intValue());
assertEquals(9, optional.get().intValue());
optional = toOptional(injector.getInstance(Key.get(javaOptionalOfInteger)));
assertEquals(10, optional.get().intValue());
optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderInteger)));
assertEquals(11, optionalP.get().get().intValue());
assertEquals(12, optionalP.get().get().intValue());
optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderInteger)));
assertEquals(13, optionalJxP.get().get().intValue());
assertEquals(14, optionalJxP.get().get().intValue());
}
}
Aggregations