use of com.google.inject.Provider in project roboguice by roboguice.
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 roboguice by roboguice.
the class OptionalBinderTest method testQualifiedAggregatesTogether.
public void testQualifiedAggregatesTogether() throws Exception {
Module module1 = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), Key.get(String.class, Names.named("foo")));
}
};
Module module2 = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), Key.get(String.class, Names.named("foo"))).setDefault().toInstance("a");
}
};
Module module3 = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), Key.get(String.class, Names.named("foo"))).setBinding().toInstance("b");
}
};
Injector injector = Guice.createInjector(module1, module2, module3);
assertEquals("b", injector.getInstance(Key.get(String.class, Names.named("foo"))));
Optional<String> optional = injector.getInstance(Key.get(optionalOfString, Names.named("foo")));
assertTrue(optional.isPresent());
assertEquals("b", optional.get());
Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString, Names.named("foo")));
assertTrue(optionalP.isPresent());
assertEquals("b", optionalP.get().get());
Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString, Names.named("foo")));
assertTrue(optionalJxP.isPresent());
assertEquals("b", optionalJxP.get().get());
assertOptionalVisitor(Key.get(String.class, Names.named("foo")), setOf(module1, module2, module3), VisitType.BOTH, 0, instance("a"), instance("b"), null);
if (HAS_JAVA_OPTIONAL) {
optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString, Names.named("foo"))));
assertTrue(optional.isPresent());
assertEquals("b", optional.get());
optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString, Names.named("foo"))));
assertTrue(optionalP.isPresent());
assertEquals("b", optionalP.get().get());
optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString, Names.named("foo"))));
assertTrue(optionalJxP.isPresent());
assertEquals("b", optionalJxP.get().get());
}
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class OptionalBinderTest method testSetBindingOverridesDefault.
public void testSetBindingOverridesDefault() throws Exception {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder<String> optionalBinder = OptionalBinder.newOptionalBinder(binder(), String.class);
optionalBinder.setDefault().toInstance("a");
optionalBinder.setBinding().toInstance("b");
}
};
Injector injector = Guice.createInjector(module);
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(module), 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 roboguice by roboguice.
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());
}
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class OptionalBinderTest method testLinkedToNullActualDoesntFallbackToDefault.
// TODO(sameb): Maybe change this?
public void testLinkedToNullActualDoesntFallbackToDefault() throws Exception {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("a");
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(optionalP.get().get());
assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, instance("a"), 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