use of com.google.inject.binder.AnnotatedBindingBuilder in project roboguice by roboguice.
the class BinderTestSuite method suite.
public static Test suite() {
TestSuite suite = new TestSuite();
new Builder().name("bind A").module(new AbstractModule() {
protected void configure() {
bind(A.class);
}
}).creationException("No implementation for %s was bound", A.class.getName()).addToSuite(suite);
new Builder().name("bind PlainA named apple").module(new AbstractModule() {
protected void configure() {
bind(PlainA.class).annotatedWith(named("apple"));
}
}).creationException("No implementation for %s annotated with %s was bound", PlainA.class.getName(), named("apple")).addToSuite(suite);
new Builder().name("bind A to new PlainA(1)").module(new AbstractModule() {
protected void configure() {
bind(A.class).toInstance(new PlainA(1));
}
}).creationTime(CreationTime.NONE).expectedValues(new PlainA(1), new PlainA(1), new PlainA(1)).addToSuite(suite);
new Builder().name("no binding, AWithProvidedBy").key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class).addToSuite(suite);
new Builder().name("no binding, AWithImplementedBy").key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class).addToSuite(suite);
new Builder().name("no binding, ScopedA").key(Key.get(ScopedA.class), InjectsScopedA.class).expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202)).addToSuite(suite);
new Builder().name("no binding, AWithProvidedBy named apple").key(Key.get(AWithProvidedBy.class, named("apple")), InjectsAWithProvidedByNamedApple.class).configurationException("No implementation for %s annotated with %s was bound", AWithProvidedBy.class.getName(), named("apple")).addToSuite(suite);
new Builder().name("no binding, AWithImplementedBy named apple").key(Key.get(AWithImplementedBy.class, named("apple")), InjectsAWithImplementedByNamedApple.class).configurationException("No implementation for %s annotated with %s was bound", AWithImplementedBy.class.getName(), named("apple")).addToSuite(suite);
new Builder().name("no binding, ScopedA named apple").key(Key.get(ScopedA.class, named("apple")), InjectsScopedANamedApple.class).configurationException("No implementation for %s annotated with %s was bound", ScopedA.class.getName(), named("apple")).addToSuite(suite);
for (final Scoper scoper : Scoper.values()) {
new Builder().name("bind PlainA").key(Key.get(PlainA.class), InjectsPlainA.class).module(new AbstractModule() {
protected void configure() {
AnnotatedBindingBuilder<PlainA> abb = bind(PlainA.class);
scoper.configure(abb);
}
}).scoper(scoper).addToSuite(suite);
new Builder().name("bind A to PlainA").module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(A.class).to(PlainA.class);
scoper.configure(sbb);
}
}).scoper(scoper).addToSuite(suite);
new Builder().name("bind A to PlainAProvider.class").module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(A.class).toProvider(PlainAProvider.class);
scoper.configure(sbb);
}
}).scoper(scoper).addToSuite(suite);
new Builder().name("bind A to new PlainAProvider()").module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(A.class).toProvider(new PlainAProvider());
scoper.configure(sbb);
}
}).scoper(scoper).addToSuite(suite);
new Builder().name("bind AWithProvidedBy").key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class).module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(AWithProvidedBy.class);
scoper.configure(sbb);
}
}).scoper(scoper).addToSuite(suite);
new Builder().name("bind AWithImplementedBy").key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class).module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(AWithImplementedBy.class);
scoper.configure(sbb);
}
}).scoper(scoper).addToSuite(suite);
new Builder().name("bind ScopedA").key(Key.get(ScopedA.class), InjectsScopedA.class).module(new AbstractModule() {
protected void configure() {
ScopedBindingBuilder sbb = bind(ScopedA.class);
scoper.configure(sbb);
}
}).expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202)).scoper(scoper).addToSuite(suite);
new Builder().name("bind AWithProvidedBy named apple").module(new AbstractModule() {
protected void configure() {
scoper.configure(bind(AWithProvidedBy.class).annotatedWith(named("apple")));
}
}).creationException("No implementation for %s annotated with %s was bound", AWithProvidedBy.class.getName(), named("apple")).addToSuite(suite);
new Builder().name("bind AWithImplementedBy named apple").module(new AbstractModule() {
protected void configure() {
scoper.configure(bind(AWithImplementedBy.class).annotatedWith(named("apple")));
}
}).creationException("No implementation for %s annotated with %s was bound", AWithImplementedBy.class.getName(), named("apple")).addToSuite(suite);
new Builder().name("bind ScopedA named apple").module(new AbstractModule() {
protected void configure() {
scoper.configure(bind(ScopedA.class).annotatedWith(named("apple")));
}
}).creationException("No implementation for %s annotated with %s was bound", ScopedA.class.getName(), named("apple")).addToSuite(suite);
}
return suite;
}
use of com.google.inject.binder.AnnotatedBindingBuilder in project shiro by apache.
the class ShiroModuleTest method testEventListener.
/**
* @since 1.4
* @throws Exception
*/
@Test
public void testEventListener() throws Exception {
final MockRealm mockRealm = createMock(MockRealm.class);
final EventBus eventBus = createMock(EventBus.class);
// expect both objects to be registered
eventBus.register(anyObject(MockEventListener1.class));
eventBus.register(anyObject(MockEventListener2.class));
replay(eventBus);
final ShiroModule shiroModule = new ShiroModule() {
@Override
protected void configureShiro() {
bindRealm().to(MockRealm.class);
// bind our event listeners
binder().bind(MockEventListener1.class).asEagerSingleton();
binder().bind(MockEventListener2.class).asEagerSingleton();
}
@Override
protected void bindEventBus(AnnotatedBindingBuilder<EventBus> bind) {
bind.toInstance(eventBus);
}
@Provides
public MockRealm createRealm() {
return mockRealm;
}
};
Guice.createInjector(shiroModule);
verify(eventBus);
}
Aggregations