use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProvider2Test method testAssistedJavaxProviderIsDisallowed.
public void testAssistedJavaxProviderIsDisallowed() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(JavaxProviderBasedColoredCarFactory.class).toProvider(FactoryProvider.newFactory(JavaxProviderBasedColoredCarFactory.class, Subaru.class));
}
});
fail();
} catch (CreationException expected) {
assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size());
assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [1] with key" + " [com.google.inject.Provider<" + Color.class.getName() + ">] on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [2] with key" + " [com.google.inject.Provider<java.lang.String>] on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [1] with key" + " [com.google.inject.Provider<" + Color.class.getName() + ">" + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]" + " on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createMustang()]");
assertContains(expected.getMessage(), ") No implementation for com.google.inject.assistedinject." + "FactoryProvider2Test$JavaxProviderBasedColoredCarFactory was bound.");
}
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProvider2Test method testInjectingNullParameter.
public void testInjectingNullParameter() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Subaru.class));
}
});
ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
Subaru subaru = (Subaru) carFactory.create(null);
assertNull(subaru.colorProvider.get());
assertNull(subaru.colorProvider.get());
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProviderTest method testMethodsDeclaredInObject.
@SuppressWarnings("SelfEquals")
public void testMethodsDeclaredInObject() {
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));
}
});
ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
carFactory.equals(carFactory);
carFactory.hashCode();
carFactory.toString();
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProviderTest method testTypeTokenInjection.
public void testTypeTokenInjection() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<Set<String>>() {
}).toInstance(Collections.singleton("Flux Capacitor"));
bind(new TypeLiteral<Set<Integer>>() {
}).toInstance(Collections.singleton(88));
bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, DeLorean.class));
}
});
ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
DeLorean deLorean = (DeLorean) carFactory.create(Color.GRAY);
assertEquals(Color.GRAY, deLorean.color);
assertEquals("Flux Capacitor", deLorean.features.iterator().next());
assertEquals(Integer.valueOf(88), deLorean.featureActivationSpeeds.iterator().next());
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProviderTest method testAssistedFactory.
public void testAssistedFactory() {
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));
}
});
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);
}
Aggregations