use of com.google.inject.AbstractModule in project guice by google.
the class OverrideModuleTest method testStandardScopeAnnotation.
public void testStandardScopeAnnotation() {
final SingleUseScope scope = new SingleUseScope();
Module module = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, scope);
bind(String.class).in(TestScopeAnnotation.class);
}
};
assertFalse(scope.used);
Guice.createInjector(module);
assertTrue(scope.used);
}
use of com.google.inject.AbstractModule in project guice by google.
the class Jsr330Test method testProviderInject.
public void testProviderInject() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(B.class).annotatedWith(Names.named("jodie")).toInstance(b);
bind(C.class).toInstance(c);
bind(D.class).annotatedWith(RED).toInstance(d);
bind(E.class).toInstance(e);
bind(G.class);
}
});
G g = injector.getInstance(G.class);
assertSame(b, g.bProvider.get());
assertSame(c, g.cProvider.get());
assertSame(d, g.dProvider.get());
assertSame(e, g.eProvider.get());
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testSingletonScopeOnAssistedClassIsIgnored.
public void testSingletonScopeOnAssistedClassIsIgnored() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(SingletonFactory.class));
}
});
fail();
} catch (CreationException ce) {
assertEquals(1, ce.getErrorMessages().size());
assertEquals("Found scope annotation [" + Singleton.class.getName() + "]" + " on implementation class [" + AssistedSingleton.class.getName() + "]" + " of AssistedInject factory [" + SingletonFactory.class.getName() + "]." + "\nThis is not allowed, please remove the scope annotation.", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
}
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testMultipleReturnTypes.
public void testMultipleReturnTypes() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Double.class).toInstance(5.0d);
install(new FactoryModuleBuilder().build(VersatileCarFactory.class));
}
});
VersatileCarFactory factory = injector.getInstance(VersatileCarFactory.class);
Mustang mustang = factory.getMustang(Color.RED);
assertEquals(Color.RED, mustang.color);
Beetle beetle = factory.getBeetle(Color.GREEN);
assertEquals(Color.GREEN, beetle.color);
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testFactoryPublicAndReturnTypeNotPublic.
public void testFactoryPublicAndReturnTypeNotPublic() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(Hidden.class, HiddenImpl.class).build(NotHidden.class));
}
});
fail("Expected CreationException");
} catch (CreationException ce) {
assertEquals(NotHidden.class.getName() + " is public, but has a method that returns a non-public type: " + Hidden.class.getName() + ". Due to limitations with java.lang.reflect.Proxy, this is not allowed. " + "Please either make the factory non-public or the return type public.", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
}
}
Aggregations