use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProviderTest method testDuplicateAssistedFactoryBinding.
public void testDuplicateAssistedFactoryBinding() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Double.class).toInstance(5.0d);
bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
}
});
ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
Mustang blueMustang = (Mustang) carFactory.create(Color.BLUE);
assertEquals(Color.BLUE, blueMustang.color);
assertEquals(5.0d, blueMustang.engineSize, 0.0);
Mustang redMustang = (Mustang) carFactory.create(Color.RED);
assertEquals(Color.RED, redMustang.color);
assertEquals(5.0d, redMustang.engineSize, 0.0);
}
use of com.google.inject.AbstractModule in project guice by google.
the class ManyConstructorsTest method testDifferentOrderParameters.
public void testDifferentOrderParameters() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(OtherFactory.class));
}
});
OtherFactory factory = injector.getInstance(OtherFactory.class);
Foo noIndex = factory.create("no index");
assertEquals("no index", noIndex.name);
assertNull(noIndex.index);
Foo index = factory.create(1, "index");
assertEquals("index", index.name);
assertEquals(1, index.index.intValue());
Foo index2 = factory.create("index", 2);
assertEquals("index", index2.name);
assertEquals(2, index2.index.intValue());
}
use of com.google.inject.AbstractModule in project guice by google.
the class ManyConstructorsTest method testInterfaceToImpl.
public void testInterfaceToImpl() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(Bar.class, Foo.class).build(BarFactory.class));
}
});
BarFactory factory = injector.getInstance(BarFactory.class);
Bar noIndex = factory.create("no index");
assertEquals("no index", noIndex.getName());
assertNull(noIndex.getIndex());
Bar index = factory.create("index", 1);
assertEquals("index", index.getName());
assertEquals(1, index.getIndex().intValue());
}
use of com.google.inject.AbstractModule in project guice by google.
the class ManyConstructorsTest method testDependenciesAndOtherAnnotations.
public void testDependenciesAndOtherAnnotations() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(FamilyFarmFactory.class));
}
});
FamilyFarmFactory factory = injector.getInstance(FamilyFarmFactory.class);
Farm pops = factory.popsFarm("Pop");
assertEquals("Pop", pops.pop);
assertEquals(null, pops.mom);
Farm moms = factory.momsFarm("Mom");
assertEquals(null, moms.pop);
assertEquals("Mom", moms.mom);
Farm momAndPop = factory.momAndPopsFarm("Mom", "Pop");
assertEquals("Pop", momAndPop.pop);
assertEquals("Mom", momAndPop.mom);
}
use of com.google.inject.AbstractModule in project guice by google.
the class ManyConstructorsTest method testUsingOneConstructor.
public void testUsingOneConstructor() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(SimpleFactory.class));
}
});
SimpleFactory factory = injector.getInstance(SimpleFactory.class);
Foo noIndex = factory.create("no index");
assertEquals("no index", noIndex.name);
assertNull(noIndex.index);
injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(SimpleFactory2.class));
}
});
SimpleFactory2 factory2 = injector.getInstance(SimpleFactory2.class);
Foo index = factory2.create("index", 1);
assertEquals("index", index.name);
assertEquals(1, index.index.intValue());
}
Aggregations