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);
}
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);
}
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);
}
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());
}
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());
}
}
Aggregations