Search in sources :

Example 56 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class Jsr330Test method testSingleton.

public void testSingleton() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(B.class).in(Singleton.class);
        }
    });
    B b = injector.getInstance(B.class);
    assertSame(b, injector.getInstance(B.class));
    assertSame(b, injector.getInstance(B.class));
    J j = injector.getInstance(J.class);
    assertSame(j, injector.getInstance(J.class));
    assertSame(j, injector.getInstance(J.class));
}
Also used : Injector(com.google.inject.Injector) Singleton(javax.inject.Singleton) AbstractModule(com.google.inject.AbstractModule)

Example 57 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class Jsr330Test method testInjectingFinalFieldsIsForbidden.

public void testInjectingFinalFieldsIsForbidden() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(L.class);
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Injected field " + L.class.getName() + ".b cannot be final.");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 58 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class Jsr330Test method testInject.

public void testInject() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(B.class).toInstance(b);
            bind(C.class).toInstance(c);
            bind(D.class).toInstance(d);
            bind(E.class).toInstance(e);
            bind(A.class);
        }
    });
    A a = injector.getInstance(A.class);
    assertSame(b, a.b);
    assertSame(c, a.c);
    assertSame(d, a.d);
    assertSame(e, a.e);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 59 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class FactoryProvider2Test method testMethodsDeclaredInObject.

public void testMethodsDeclaredInObject() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Double.class).toInstance(5.0d);
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
        }
    });
    ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
    assertEqualsBothWays(carFactory, carFactory);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 60 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class FactoryProvider2Test method testAssistedProviderIsDisallowed.

public void testAssistedProviderIsDisallowed() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(ProviderBasedColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ProviderBasedColoredCarFactory.class, Subaru.class));
            }
        });
        fail();
    } catch (CreationException expected) {
        assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size());
        // Assert each method individually, because JDK7 doesn't guarantee method ordering.
        assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n  Offending instance is parameter [1] with key" + " [com.google.inject.Provider<" + Color.class.getName() + ">] on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
        assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n  Offending instance is parameter [2] with key" + " [com.google.inject.Provider<java.lang.String>] on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
        assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n  Offending instance is parameter [1] with key" + " [com.google.inject.Provider<" + Color.class.getName() + ">" + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]" + " on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createMustang()]");
        assertContains(expected.getMessage(), ") No implementation for com.google.inject.assistedinject." + "FactoryProvider2Test$ProviderBasedColoredCarFactory was bound.");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)845 Injector (com.google.inject.Injector)525 Module (com.google.inject.Module)232 CreationException (com.google.inject.CreationException)162 Provider (com.google.inject.Provider)64 Key (com.google.inject.Key)55 ConfigModule (co.cask.cdap.common.guice.ConfigModule)54 AuthorizationEnforcementModule (co.cask.cdap.security.authorization.AuthorizationEnforcementModule)49 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)46 PrivateModule (com.google.inject.PrivateModule)46 AuthenticationContextModules (co.cask.cdap.security.auth.context.AuthenticationContextModules)45 AuthorizationTestModule (co.cask.cdap.security.authorization.AuthorizationTestModule)43 TypeLiteral (com.google.inject.TypeLiteral)43 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 BeforeClass (org.junit.BeforeClass)38 Map (java.util.Map)37 CConfiguration (co.cask.cdap.common.conf.CConfiguration)36 UnsupportedUGIProvider (co.cask.cdap.security.impersonation.UnsupportedUGIProvider)36 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)35 DefaultOwnerAdmin (co.cask.cdap.security.impersonation.DefaultOwnerAdmin)35