use of com.google.inject.AbstractModule in project roboguice by roboguice.
the class BinderTest method testBindingToProvider.
public void testBindingToProvider() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<Provider<String>>() {
}).toInstance(Providers.of("A"));
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) Binding to Provider is not allowed.", "at " + BinderTest.class.getName(), getDeclaringSourcePart(getClass()));
}
}
use of com.google.inject.AbstractModule in project roboguice by roboguice.
the class BinderTest method testToStringOnBinderApi.
public void testToStringOnBinderApi() {
try {
Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
assertEquals("Binder", binder().toString());
assertEquals("Provider<java.lang.Integer>", getProvider(Integer.class).toString());
assertEquals("Provider<java.util.List<java.lang.String>>", getProvider(Key.get(new TypeLiteral<List<String>>() {
})).toString());
assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).toString());
assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).annotatedWith(Names.named("a")).toString());
assertEquals("ConstantBindingBuilder", bindConstant().toString());
assertEquals("ConstantBindingBuilder", bindConstant().annotatedWith(Names.named("b")).toString());
assertEquals("AnnotatedElementBuilder", binder().newPrivateBinder().expose(Integer.class).toString());
}
});
fail();
} catch (CreationException ignored) {
}
}
use of com.google.inject.AbstractModule in project roboguice by roboguice.
the class BinderTest method testUntargettedBinding.
/**
* Untargetted bindings should follow @ImplementedBy and @ProvidedBy
* annotations if they exist. Otherwise the class should be constructed
* directly.
*/
public void testUntargettedBinding() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(HasProvidedBy1.class);
bind(HasImplementedBy1.class);
bind(HasProvidedBy2.class);
bind(HasImplementedBy2.class);
bind(JustAClass.class);
}
});
assertNotNull(injector.getInstance(HasProvidedBy1.class));
assertNotNull(injector.getInstance(HasImplementedBy1.class));
assertNotSame(HasProvidedBy2.class, injector.getInstance(HasProvidedBy2.class).getClass());
assertSame(ExtendsHasImplementedBy2.class, injector.getInstance(HasImplementedBy2.class).getClass());
assertSame(JustAClass.class, injector.getInstance(JustAClass.class).getClass());
}
use of com.google.inject.AbstractModule in project roboguice by roboguice.
the class NamesTest method testBindPropertiesUsingMap.
public void testBindPropertiesUsingMap() {
final Map<String, String> properties = ImmutableMap.of("SanJose", "Sharks", "Edmonton", "Oilers");
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
Names.bindProperties(binder(), properties);
}
});
assertEquals("Sharks", injector.getInstance(Key.get(String.class, Names.named("SanJose"))));
assertEquals("Oilers", injector.getInstance(Key.get(String.class, Names.named("Edmonton"))));
}
use of com.google.inject.AbstractModule in project roboguice by roboguice.
the class NamesTest method testBindPropertiesUsingProperties.
public void testBindPropertiesUsingProperties() {
final Properties teams = new Properties();
teams.setProperty("SanJose", "Sharks");
teams.setProperty("Edmonton", "Oilers");
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
Names.bindProperties(binder(), teams);
}
});
assertEquals("Sharks", injector.getInstance(Key.get(String.class, Names.named("SanJose"))));
assertEquals("Oilers", injector.getInstance(Key.get(String.class, Names.named("Edmonton"))));
}
Aggregations