Search in sources :

Example 86 with AbstractModule

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

the class ManyConstructorsTest method testNoMatchingConstructorsBecauseTooManyParams.

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

            @Override
            protected void configure() {
                install(new FactoryModuleBuilder().build(ComplexFactory.class));
            }
        });
        fail("should have failed");
    } catch (CreationException expected) {
        Asserts.assertContains(expected.getMessage(), "1) " + Foo.class.getName() + " has @AssistedInject constructors, but none of them match the parameters in method " + ComplexFactory.class.getName());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 87 with AbstractModule

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

the class ManyConstructorsTest method testTwoConstructors.

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

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().build(Factory.class));
        }
    });
    Factory factory = injector.getInstance(Factory.class);
    Foo noIndex = factory.create("no index");
    assertEquals("no index", noIndex.name);
    assertNull(noIndex.index);
    Foo index = factory.create("index", 1);
    assertEquals("index", index.name);
    assertEquals(1, index.index.intValue());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 88 with AbstractModule

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

the class ManyConstructorsTest method testTooManyMatchingConstructors.

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

            @Override
            protected void configure() {
                install(new FactoryModuleBuilder().implement(Foo.class, TooManyMatches.class).build(SimpleFactory2.class));
            }
        });
        fail("should have failed");
    } catch (CreationException expected) {
        Asserts.assertContains(expected.getMessage(), "1) " + TooManyMatches.class.getName() + " has more than one constructor annotated with @AssistedInject that " + "matches the parameters in method " + SimpleFactory2.class.getName());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 89 with AbstractModule

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

the class FactoryProvider2Test method testConstructorExceptionsAreThrownByFactory.

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

        @Override
        protected void configure() {
            bind(CorrectDefectiveCarFactory.class).toProvider(FactoryProvider.newFactory(CorrectDefectiveCarFactory.class, DefectiveCar.class));
        }
    });
    try {
        injector.getInstance(CorrectDefectiveCarFactory.class).createCar();
        fail();
    } catch (FireException e) {
        fail();
    } catch (ExplosionException expected) {
    }
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 90 with AbstractModule

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

the class FactoryProvider2Test method testFactoryWithImplicitBindings.

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

        @Override
        protected void configure() {
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Fiat.class));
        }
    });
    ColoredCarFactory coloredCarFactory = injector.getInstance(ColoredCarFactory.class);
    Fiat fiat = (Fiat) coloredCarFactory.create(Color.GREEN);
    assertEquals(Color.GREEN, fiat.color);
    assertNotNull(fiat.steeringWheel);
}
Also used : Injector(com.google.inject.Injector) 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