use of com.google.inject.Provides in project roboguice by roboguice.
the class ProviderMethodsTest method testProviderMethodDependenciesAreExposed.
public void testProviderMethodDependenciesAreExposed() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(50);
bindConstant().annotatedWith(Names.named("units")).to("Kg");
}
@Provides
@Named("weight")
String provideWeight(Integer count, @Named("units") String units) {
return count + units;
}
});
ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) injector.getBinding(Key.get(String.class, Names.named("weight")));
assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Integer.class)), Dependency.get(Key.get(String.class, Names.named("units")))), binding.getDependencies());
}
use of com.google.inject.Provides in project roboguice by roboguice.
the class ProviderMethodsTest method testMultipleBindingAnnotations.
// We'll have to make getProvider() support circular dependencies before this
// will work.
//
// public void testCircularDependency() {
// Injector injector = Guice.createInjector(new Module() {
// public void configure(Binder binder) {
// binder.install(ProviderMethods.from(ProviderMethodsTest.this));
// }
// });
//
// Foo foo = injector.getInstance(Foo.class);
// assertEquals(5, foo.getI());
// assertEquals(10, foo.getBar().getI());
// assertEquals(5, foo.getBar().getFoo().getI());
// }
//
// interface Foo {
// Bar getBar();
// int getI();
// }
//
// interface Bar {
// Foo getFoo();
// int getI();
// }
//
// @Provides Foo newFoo(final Bar bar) {
// return new Foo() {
//
// public Bar getBar() {
// return bar;
// }
//
// public int getI() {
// return 5;
// }
// };
// }
//
// @Provides Bar newBar(final Foo foo) {
// return new Bar() {
//
// public Foo getFoo() {
// return foo;
// }
//
// public int getI() {
// return 10;
// }
// };
// }
public void testMultipleBindingAnnotations() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
}
@Provides
@Named("A")
@Blue
public String provideString() {
return "a";
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "more than one annotation annotated with @BindingAnnotation:", "Named", "Blue", "at " + getClass().getName(), ".provideString(ProviderMethodsTest.java:");
}
}
use of com.google.inject.Provides in project roboguice by roboguice.
the class FactoryModuleBuilderTest method testImplicitForwardingAssistedBindingCreatesNewObjects.
public void testImplicitForwardingAssistedBindingCreatesNewObjects() {
final Mustang providedMustang = new Mustang(Color.BLUE);
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(MustangFactory.class));
}
@Provides
Mustang provide() {
return providedMustang;
}
});
assertSame(providedMustang, injector.getInstance(Mustang.class));
MustangFactory factory = injector.getInstance(MustangFactory.class);
Mustang created = factory.create(Color.GREEN);
assertNotSame(providedMustang, created);
assertEquals(Color.BLUE, providedMustang.color);
assertEquals(Color.GREEN, created.color);
}
use of com.google.inject.Provides in project roboguice by roboguice.
the class TransferRequestIntegrationTest method testTransferNonHttpRequest.
public void testTransferNonHttpRequest() throws Exception {
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindScope(RequestScoped.class, ServletScopes.REQUEST);
}
@Provides
@RequestScoped
Object provideObject() {
return new Object();
}
});
Callable<Callable<Boolean>> callable = new Callable<Callable<Boolean>>() {
@Override
public Callable<Boolean> call() {
final Object original = injector.getInstance(Object.class);
return ServletScopes.transferRequest(new Callable<Boolean>() {
@Override
public Boolean call() {
return original == injector.getInstance(Object.class);
}
});
}
};
ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
Callable<Boolean> transfer = ServletScopes.scopeRequest(callable, seedMap).call();
ExecutorService executor = Executors.newSingleThreadExecutor();
assertTrue(executor.submit(transfer).get());
executor.shutdownNow();
}
use of com.google.inject.Provides in project airpal by airbnb.
the class AirpalModule method provideDBI.
@Singleton
@Provides
public DBI provideDBI(ObjectMapper objectMapper) throws ClassNotFoundException {
final DBIFactory factory = new DBIFactory();
final DBI dbi = factory.build(environment, config.getDataSourceFactory(), provideDbType().name());
dbi.registerMapper(new TableRow.TableRowMapper(objectMapper));
dbi.registerMapper(new QueryStoreMapper(objectMapper));
dbi.registerArgumentFactory(new UUIDArgumentFactory());
dbi.registerArgumentFactory(new URIArgumentFactory());
return dbi;
}
Aggregations