use of com.google.inject.Provider in project roboguice by roboguice.
the class BoundFieldModuleTest method testRawProviderCannotBeBound.
public void testRawProviderCannotBeBound() {
final Integer testValue = 1024;
Object instance = new Object() {
@Bind
private Provider anIntProvider = new Provider() {
@Override
public Object get() {
return testValue;
}
};
};
BoundFieldModule module = BoundFieldModule.of(instance);
try {
Guice.createInjector(module);
fail();
} catch (CreationException e) {
assertContains(e.getMessage(), "Non parameterized Provider fields must have an " + "explicit binding class via @Bind(to = Foo.class)");
}
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class BoundFieldModuleTest method testIncrementingProvider.
public void testIncrementingProvider() {
final Integer testBaseValue = 1024;
Object instance = new Object() {
@Bind
private Provider<Integer> anIntProvider = new Provider<Integer>() {
private int value = testBaseValue;
@Override
public Integer get() {
return value++;
}
};
};
BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);
assertEquals(testBaseValue, injector.getInstance(Integer.class));
assertEquals((Integer) (testBaseValue + 1), injector.getInstance(Integer.class));
assertEquals((Integer) (testBaseValue + 2), injector.getInstance(Integer.class));
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class BoundFieldModuleTest method testBindingProviderWithProviderSubclassValue.
public void testBindingProviderWithProviderSubclassValue() {
final Integer testValue = 1024;
Object instance = new Object() {
@Bind
private Provider<Integer> anIntProvider = new IntegerProvider(testValue);
};
BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);
assertEquals(testValue, injector.getInstance(Integer.class));
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class BoundFieldModuleTest method testRawProviderCanBindToIncorrectType.
public void testRawProviderCanBindToIncorrectType() {
final Integer testValue = 1024;
Object instance = new Object() {
@Bind(to = String.class)
private Provider anIntProvider = new Provider() {
@Override
public Object get() {
return testValue;
}
};
};
BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);
assertEquals(testValue, injector.getInstance(String.class));
}
use of com.google.inject.Provider in project guice by google.
the class OptionalBinderTest method testExactSameBindingCollapses_defaults.
public void testExactSameBindingCollapses_defaults() throws Exception {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance(// using new String to ensure .equals is checked.
new String("a"));
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance(new String("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, instance("a"), null, 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());
}
}
Aggregations