use of com.google.inject.Provider in project guice by google.
the class OptionalBinderTest method testSpreadAcrossModules.
public void testSpreadAcrossModules() throws Exception {
Module module1 = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class);
}
};
Module module2 = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("a");
}
};
Module module3 = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("b");
}
};
Injector injector = Guice.createInjector(module1, module2, module3);
assertEquals("b", injector.getInstance(String.class));
Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
assertTrue(optional.isPresent());
assertEquals("b", optional.get());
Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
assertTrue(optionalP.isPresent());
assertEquals("b", optionalP.get().get());
Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
assertTrue(optionalJxP.isPresent());
assertEquals("b", optionalJxP.get().get());
assertOptionalVisitor(stringKey, setOf(module1, module2, module3), VisitType.BOTH, 0, instance("a"), instance("b"), null);
if (HAS_JAVA_OPTIONAL) {
optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
assertTrue(optional.isPresent());
assertEquals("b", optional.get());
optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
assertTrue(optionalP.isPresent());
assertEquals("b", optionalP.get().get());
optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
assertTrue(optionalJxP.isPresent());
assertEquals("b", optionalJxP.get().get());
}
}
use of com.google.inject.Provider in project guice by google.
the class MapBinderTest method testProviderMultimapDependencyInjection.
/** Ensure there are no initialization race conditions in provider multimap injection. */
public void testProviderMultimapDependencyInjection() {
final AtomicReference<Map<String, Set<Provider<String>>>> injectedMultimap = new AtomicReference<Map<String, Set<Provider<String>>>>();
final Object anObject = new Object() {
@Inject
void initialize(Map<String, Set<Provider<String>>> multimap) {
injectedMultimap.set(multimap);
}
};
Module abc = new AbstractModule() {
@Override
protected void configure() {
requestInjection(anObject);
MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class);
multibinder.permitDuplicates();
multibinder.addBinding("a").toInstance("A");
multibinder.addBinding("b").toInstance("B");
multibinder.addBinding("c").toInstance("C");
}
};
Guice.createInjector(abc);
Map<String, String> map = Maps.transformValues(injectedMultimap.get(), new Function<Set<Provider<String>>, String>() {
@Override
public String apply(Set<Provider<String>> stringProvidersSet) {
return Iterables.getOnlyElement(stringProvidersSet).get();
}
});
assertEquals(mapOf("a", "A", "b", "B", "c", "C"), map);
}
use of com.google.inject.Provider in project guice by google.
the class MapBinderTest method testMapBinderProviderIsScoped.
public void testMapBinderProviderIsScoped() {
final Provider<Integer> counter = new Provider<Integer>() {
int next = 1;
@Override
public Integer get() {
return next++;
}
};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
MapBinder.newMapBinder(binder(), String.class, Integer.class).addBinding("one").toProvider(counter).asEagerSingleton();
}
});
assertEquals(1, (int) injector.getInstance(Key.get(mapOfInteger)).get("one"));
assertEquals(1, (int) injector.getInstance(Key.get(mapOfInteger)).get("one"));
}
use of com.google.inject.Provider in project guice by google.
the class MultibinderTest method testMultibinderCanInjectCollectionOfProviders.
public void testMultibinderCanInjectCollectionOfProviders() {
Module module = new AbstractModule() {
@Override
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");
}
};
Collection<String> expectedValues = ImmutableList.of("A", "B", "C");
Injector injector = Guice.createInjector(module);
Collection<Provider<String>> providers = injector.getInstance(Key.get(collectionOfProvidersOfStrings));
assertEquals(expectedValues, collectValues(providers));
Collection<javax.inject.Provider<String>> javaxProviders = injector.getInstance(Key.get(collectionOfJavaxProvidersOf(stringType)));
assertEquals(expectedValues, collectValues(javaxProviders));
}
use of com.google.inject.Provider in project guice by google.
the class OptionalBinderTest method testLinkedToNullProvidersMakeAbsentValuesAndPresentProviders_default.
public void testLinkedToNullProvidersMakeAbsentValuesAndPresentProviders_default() throws Exception {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toProvider(Providers.<String>of(null));
}
};
Injector injector = Guice.createInjector(module);
assertNull(injector.getInstance(String.class));
Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
assertFalse(optional.isPresent());
Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
assertTrue(optionalP.isPresent());
assertNull(optionalP.get().get());
Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
assertTrue(optionalJxP.isPresent());
assertNull(optionalJxP.get().get());
assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, SpiUtils.<String>providerInstance(null), null, null);
if (HAS_JAVA_OPTIONAL) {
optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
assertFalse(optional.isPresent());
optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
assertTrue(optionalP.isPresent());
assertNull(optionalP.get().get());
optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
assertTrue(optionalJxP.isPresent());
assertNull(optionalJxP.get().get());
}
}
Aggregations