Search in sources :

Example 66 with AbstractModule

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

the class FactoryProviderTest method testFactoryWithMultipleMethods.

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

        @Override
        protected void configure() {
            bind(float.class).toInstance(140f);
            bind(SummerCarFactory.class).toProvider(FactoryProvider.newFactory(SummerCarFactory.class, Corvette.class));
        }
    });
    SummerCarFactory carFactory = injector.getInstance(SummerCarFactory.class);
    Corvette blueCorvette = (Corvette) carFactory.createConvertible(Color.BLUE);
    assertEquals(Color.BLUE, blueCorvette.color);
    assertEquals(100f, blueCorvette.maxMph, 0.0f);
    assertTrue(blueCorvette.isConvertable);
    Corvette redCorvette = (Corvette) carFactory.create(Color.RED, false);
    assertEquals(Color.RED, redCorvette.color);
    assertEquals(140f, redCorvette.maxMph, 0.0f);
    assertFalse(redCorvette.isConvertable);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 67 with AbstractModule

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

the class FactoryProviderTest method testTypeTokenProviderInjection.

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

        @Override
        protected void configure() {
            bind(new TypeLiteral<Set<String>>() {
            }).toInstance(Collections.singleton("Datsun"));
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Z.class));
        }
    });
    ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
    Z orangeZ = (Z) carFactory.create(Color.ORANGE);
    assertEquals(Color.ORANGE, orangeZ.color);
    assertEquals("Datsun", orangeZ.manufacturersProvider.get().iterator().next());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 68 with AbstractModule

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

the class FactoryProviderTest method testAssistedFactoryForConcreteType.

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

        @Override
        protected void configure() {
            bind(Double.class).annotatedWith(Names.named("lowLimit")).toInstance(50000.0d);
            bind(Double.class).annotatedWith(Names.named("highLimit")).toInstance(100000.0d);
            bind(MustangInsuranceFactory.class).toProvider(FactoryProvider.newFactory(MustangInsuranceFactory.class, MustangInsurance.class));
            bind(CamaroInsuranceFactory.class).toProvider(FactoryProvider.newFactory(CamaroInsuranceFactory.class, CamaroInsurance.class));
        }
    });
    MustangInsuranceFactory mustangInsuranceFactory = injector.getInstance(MustangInsuranceFactory.class);
    CamaroInsuranceFactory camaroInsuranceFactory = injector.getInstance(CamaroInsuranceFactory.class);
    Mustang mustang = new Mustang(5000d, Color.BLACK);
    MustangInsurance mustangPolicy = (MustangInsurance) mustangInsuranceFactory.create(mustang, 800.0d);
    assertEquals(800.0d, mustangPolicy.premium, 0.0);
    assertEquals(50000.0d, mustangPolicy.limit, 0.0);
    Camaro camaro = new Camaro(3000, 1967, Color.BLUE);
    CamaroInsurance camaroPolicy = (CamaroInsurance) camaroInsuranceFactory.create(camaro, 800.0d);
    assertEquals(800.0d, camaroPolicy.premium, 0.0);
    assertEquals(100000.0d, camaroPolicy.limit, 0.0);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 69 with AbstractModule

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

the class FactoryProviderTest 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 70 with AbstractModule

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

the class FactoryModuleBuilderTest method testExplicitForwardingAssistedBindingFailsWithAbstractClass.

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

            @Override
            protected void configure() {
                bind(AbstractCar.class).to(ArtCar.class);
                install(new FactoryModuleBuilder().implement(Car.class, AbstractCar.class).build(ColoredCarFactory.class));
            }
        });
        fail();
    } catch (CreationException ce) {
        assertContains(ce.getMessage(), "1) " + AbstractCar.class.getName() + " is abstract, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + AbstractCar.class.getName(), "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create(");
        assertEquals(1, ce.getErrorMessages().size());
    }
}
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