use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProvider2Test method testDuplicateKeys.
public void testDuplicateKeys() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(DoubleToneCarFactory.class).toProvider(FactoryProvider.newFactory(DoubleToneCarFactory.class, Maxima.class));
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "A binding to " + Color.class.getName() + " annotated with @" + Assisted.class.getName() + "(value=paint) was already configured at");
}
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProvider2Test method testFactoryUsesInjectedConstructor.
public void testFactoryUsesInjectedConstructor() {
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 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 FactoryProvider2Test method testAssistedFactoryForParameterizedType.
public void testAssistedFactoryForParameterizedType() {
final TypeLiteral<InsuranceFactory<Mustang>> mustangInsuranceFactoryType = new TypeLiteral<InsuranceFactory<Mustang>>() {
};
final TypeLiteral<InsuranceFactory<Camaro>> camaroInsuranceFactoryType = new TypeLiteral<InsuranceFactory<Camaro>>() {
};
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(mustangInsuranceFactoryType).toProvider(FactoryProvider.newFactory(mustangInsuranceFactoryType, TypeLiteral.get(MustangInsurance.class)));
bind(camaroInsuranceFactoryType).toProvider(FactoryProvider.newFactory(camaroInsuranceFactoryType, TypeLiteral.get(CamaroInsurance.class)));
}
});
InsuranceFactory<Mustang> mustangInsuranceFactory = injector.getInstance(Key.get(mustangInsuranceFactoryType));
InsuranceFactory<Camaro> camaroInsuranceFactory = injector.getInstance(Key.get(camaroInsuranceFactoryType));
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 ServletScopesTest method testIsRequestScopedNegative.
public void testIsRequestScopedNegative() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<String> e = Key.get(String.class, named("E"));
final Key<String> f = Key.get(String.class, named("F"));
final Key<String> g = Key.get(String.class, named("G"));
final Key<String> h = Key.get(String.class, named("H"));
final Key<String> i = Key.get(String.class, named("I"));
final Key<String> j = Key.get(String.class, named("J"));
Module requestScopedBindings = new AbstractModule() {
@Override
protected void configure() {
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(Scopes.NO_SCOPE);
bind(d).toInstance("d");
bind(e).toProvider(Providers.of("e")).asEagerSingleton();
bind(f).toProvider(Providers.of("f")).in(Scopes.SINGLETON);
bind(g).toProvider(Providers.of("g")).in(Singleton.class);
bind(h).toProvider(Providers.of("h")).in(CustomScoped.class);
bindScope(CustomScoped.class, Scopes.NO_SCOPE);
install(new PrivateModule() {
@Override
protected void configure() {
bind(i).toProvider(Providers.of("i")).in(CustomScoped.class);
expose(i);
}
});
}
@Provides
@Named("J")
@CustomScoped
String provideJ() {
return "j";
}
};
// we know the module contains only bindings
@SuppressWarnings("unchecked") List<Element> moduleBindings = Elements.getElements(requestScopedBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
assertFalse(ServletScopes.isRequestScoped(map.get(a)));
assertFalse(ServletScopes.isRequestScoped(map.get(b)));
assertFalse(ServletScopes.isRequestScoped(map.get(c)));
assertFalse(ServletScopes.isRequestScoped(map.get(d)));
assertFalse(ServletScopes.isRequestScoped(map.get(e)));
assertFalse(ServletScopes.isRequestScoped(map.get(f)));
assertFalse(ServletScopes.isRequestScoped(map.get(g)));
assertFalse(ServletScopes.isRequestScoped(map.get(h)));
assertFalse(ServletScopes.isRequestScoped(map.get(i)));
assertFalse(ServletScopes.isRequestScoped(map.get(j)));
Injector injector = Guice.createInjector(requestScopedBindings);
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(a)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(b)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(c)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(d)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(e)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(f)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(g)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(h)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(i)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(j)));
}
use of com.google.inject.AbstractModule in project guice by google.
the class EdslTest method testExplicitBindingsWorksWithGuiceServlet.
public final void testExplicitBindingsWorksWithGuiceServlet() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
binder().requireExplicitBindings();
}
}, new ServletModule() {
@Override
protected void configureServlets() {
bind(DummyServlet.class).in(Singleton.class);
serve("/*").with(DummyServlet.class);
}
});
assertNotNull(injector.getInstance(DummyServlet.class));
}
Aggregations