use of com.google.inject.Provider in project guice by google.
the class OptionalBinderTest method testOptionalBinderDependsOnJitBinding.
// A previous version of OptionalBinder would fail to find jit dependendencies that were created
// by other bindings
public void testOptionalBinderDependsOnJitBinding() {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), JitBinding.class);
}
};
// Everything should be absent since nothing triggered discovery of the jit binding
Injector injector = Guice.createInjector(module);
assertFalse(injector.getInstance(optionalKey(JitBinding.class)).isPresent());
assertNull(injector.getExistingBinding(Key.get(JitBinding.class)));
// in this case, because jit bindings are allowed in this injector, the DependsOnJitBinding
// binding will get initialized and create jit bindings for its dependency. The optionalbinder
// should then pick it up
module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), JitBinding.class);
bind(DependsOnJitBinding.class);
}
};
injector = Guice.createInjector(module);
assertTrue(injector.getInstance(optionalKey(JitBinding.class)).isPresent());
assertNotNull(injector.getExistingBinding(Key.get(JitBinding.class)));
// in this case, because the jit binding is discovered dynamically, the optionalbinder won't
// find it. In prior implementations of OptionalBinder this would depend on the exact
// sequencing of the installation of OptionalBinder vs. these injection points that trigger
// dynamic injection. In the current implementation it will consistently not find it.
module = new AbstractModule() {
@Override
protected void configure() {
bind(Object.class).toProvider(new Provider<Object>() {
@Inject
void setter(Injector injector) {
injector.getInstance(JitBinding.class);
}
@Override
public Object get() {
return null;
}
});
OptionalBinder.newOptionalBinder(binder(), JitBinding.class);
}
};
injector = Guice.createInjector(module);
assertFalse(injector.getInstance(optionalKey(JitBinding.class)).isPresent());
assertNotNull(injector.getExistingBinding(Key.get(JitBinding.class)));
}
use of com.google.inject.Provider in project guice by google.
the class OptionalBinderTest method testOptionalIsAbsentByDefault.
public void testOptionalIsAbsentByDefault() throws Exception {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class);
}
};
Injector injector = Guice.createInjector(module);
Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
assertFalse(optional.isPresent());
Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
assertFalse(optionalP.isPresent());
Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
assertFalse(optionalJxP.isPresent());
assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, null, null, null);
if (HAS_JAVA_OPTIONAL) {
optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
assertFalse(optional.isPresent());
optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
assertFalse(optionalP.isPresent());
optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
assertFalse(optionalJxP.isPresent());
}
}
use of com.google.inject.Provider in project guice by google.
the class OptionalBinderTest method testSetBinding.
public void testSetBinding() throws Exception {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("a");
}
};
Injector injector = Guice.createInjector(module);
assertEquals("a", injector.getInstance(String.class));
Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
assertTrue(optional.isPresent());
assertEquals("a", optional.get());
Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
assertTrue(optionalP.isPresent());
assertEquals("a", optionalP.get().get());
Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
assertTrue(optionalJxP.isPresent());
assertEquals("a", optionalJxP.get().get());
assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, null, instance("a"), null);
if (HAS_JAVA_OPTIONAL) {
optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
assertTrue(optional.isPresent());
assertEquals("a", optional.get());
optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
assertTrue(optionalP.isPresent());
assertEquals("a", optionalP.get().get());
optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
assertTrue(optionalJxP.isPresent());
assertEquals("a", optionalJxP.get().get());
}
}
use of com.google.inject.Provider in project guice by google.
the class OptionalBinderTest method testUsesUserBoundValueNullProvidersMakeAbsent.
public void testUsesUserBoundValueNullProvidersMakeAbsent() throws Exception {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class);
}
@Provides
String provideString() {
return null;
}
};
Injector injector = Guice.createInjector(module);
assertEquals(null, injector.getInstance(String.class));
Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
assertFalse(optional.isPresent());
Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
assertEquals(null, optionalP.get().get());
Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
assertEquals(null, optionalJxP.get().get());
assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, null, null, providerInstance(null));
if (HAS_JAVA_OPTIONAL) {
optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
assertFalse(optional.isPresent());
optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
assertEquals(null, optionalP.get().get());
optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
assertEquals(null, optionalJxP.get().get());
}
}
use of com.google.inject.Provider in project guice by google.
the class OptionalBinderTest method testLinkedToNullProvidersMakeAbsentValuesAndPresentProviders_actual.
public void testLinkedToNullProvidersMakeAbsentValuesAndPresentProviders_actual() throws Exception {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().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, null, SpiUtils.<String>providerInstance(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