Search in sources :

Example 6 with AnnotatedBindingBuilder

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;
}
Also used : ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) AbstractModule(com.google.inject.AbstractModule) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) TestSuite(junit.framework.TestSuite)

Example 7 with AnnotatedBindingBuilder

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);
}
Also used : AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) DefaultEventBus(org.apache.shiro.event.support.DefaultEventBus) EventBus(org.apache.shiro.event.EventBus) Test(org.junit.Test)

Aggregations

AnnotatedBindingBuilder (com.google.inject.binder.AnnotatedBindingBuilder)7 AbstractModule (com.google.inject.AbstractModule)2 ScopedBindingBuilder (com.google.inject.binder.ScopedBindingBuilder)2 TestSuite (junit.framework.TestSuite)2 HttpConfiguration (org.platformlayer.extensions.HttpConfiguration)2 CORSFilter (org.platformlayer.web.CORSFilter)2 OrchidModule (com.eden.orchid.api.registration.OrchidModule)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 Module (com.google.inject.Module)1 LinkedList (java.util.LinkedList)1 EventBus (org.apache.shiro.event.EventBus)1 DefaultEventBus (org.apache.shiro.event.support.DefaultEventBus)1 Test (org.junit.Test)1 Scope (org.platformlayer.Scope)1 ScopeFilter (org.platformlayer.ScopeFilter)1 ProjectAuthorization (org.platformlayer.model.ProjectAuthorization)1 ObjectMapperProvider (org.platformlayer.xaas.web.jaxrs.ObjectMapperProvider)1