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