Search in sources :

Example 76 with AbstractModule

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

the class FactoryModuleBuilderTest method testBindingAssistedInject.

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

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().implement(Car.class, Mustang.class).build(ColoredCarFactory.class));
        }
    });
    ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class);
    Mustang mustang = (Mustang) factory.create(Color.BLUE);
    assertEquals(Color.BLUE, mustang.color);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 77 with AbstractModule

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

the class FactoryProvider2Test method testAssistedFactoryForTypeVariableParameters.

public void testAssistedFactoryForTypeVariableParameters() {
    final TypeLiteral<InsuranceFactory<Camaro>> camaroInsuranceFactoryType = new TypeLiteral<InsuranceFactory<Camaro>>() {
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Double.class).toInstance(50000.0d);
            bind(camaroInsuranceFactoryType).toProvider(FactoryProvider.newFactory(camaroInsuranceFactoryType, new TypeLiteral<AutoInsurance<Camaro>>() {
            }));
        }
    });
    InsuranceFactory<Camaro> camaroInsuranceFactory = injector.getInstance(Key.get(camaroInsuranceFactoryType));
    Camaro camaro = new Camaro(3000, 1967, Color.BLUE);
    AutoInsurance<?> camaroPolicy = (AutoInsurance<?>) camaroInsuranceFactory.create(camaro, 800.0d);
    assertEquals(800.0d, camaroPolicy.premium, 0.0);
    assertEquals(50000.0d, camaroPolicy.limit, 0.0);
    assertEquals(camaro, camaroPolicy.car);
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 78 with AbstractModule

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

the class FactoryProvider2Test method testConstructorDoesntNeedAllFactoryMethodArguments.

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

        @Override
        protected void configure() {
            bind(SummerCarFactory.class).toProvider(FactoryProvider.newFactory(SummerCarFactory.class, Beetle.class));
        }
    });
    SummerCarFactory factory = injector.getInstance(SummerCarFactory.class);
    Beetle beetle = (Beetle) factory.create(Color.RED, true);
    assertSame(Color.RED, beetle.color);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 79 with AbstractModule

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

the class FactoryProviderTest method testProviderInjection.

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

        @Override
        protected void configure() {
            bind(String.class).toInstance("trans am");
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Firebird.class));
        }
    });
    ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
    Firebird blackFirebird = (Firebird) carFactory.create(Color.BLACK);
    assertEquals(Color.BLACK, blackFirebird.color);
    assertEquals("trans am", blackFirebird.modifiersProvider.get());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 80 with AbstractModule

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

the class FactoryProviderTest method testExceptionDuringConstruction.

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

        @Override
        protected void configure() {
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, ExplodingCar.class));
        }
    });
    try {
        injector.getInstance(ColoredCarFactory.class).create(Color.ORANGE);
        fail();
    } catch (IllegalStateException e) {
        assertEquals("kaboom!", e.getMessage());
    }
}
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