use of com.google.inject.AbstractModule in project guice by google.
the class OverrideModuleTest method testHashcodeNeverCalledOnProviderInstance.
public void testHashcodeNeverCalledOnProviderInstance() {
final HashEqualsTester a = new HashEqualsTester();
a.throwOnHashcode = true;
a.equality = "test";
final HashEqualsTester b = new HashEqualsTester();
b.throwOnHashcode = true;
b.equality = "test";
Guice.createInjector(Modules.override(new AbstractModule() {
@Override
protected void configure() {
bind(String.class);
bind(Object.class).toProvider(a);
bind(Object.class).toProvider(b);
}
}).with());
}
use of com.google.inject.AbstractModule in project guice by google.
the class OverrideModuleTest method testOverrideScopeAnnotation.
public void testOverrideScopeAnnotation() {
final Scope scope = new Scope() {
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
throw new AssertionError("Should not be called");
}
};
final SingleUseScope replacementScope = new SingleUseScope();
Module original = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, scope);
bind(Date.class).in(TestScopeAnnotation.class);
}
};
Module replacements = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, replacementScope);
}
};
Injector injector = createInjector(Modules.override(original).with(replacements));
injector.getInstance(Date.class);
assertTrue(replacementScope.used);
}
use of com.google.inject.AbstractModule in project guice by google.
the class OverrideModuleTest method testOverridesApplyOriginalScanners.
public void testOverridesApplyOriginalScanners() {
Injector injector = Guice.createInjector(Modules.override(NamedMunger.module()).with(new AbstractModule() {
@Override
protected void configure() {
}
@TestProvides
@Named("test")
String provideString() {
return "foo";
}
}));
assertNull(injector.getExistingBinding(Key.get(String.class, named("test"))));
Binding<String> binding = injector.getBinding(Key.get(String.class, named("test-munged")));
assertEquals("foo", binding.getProvider().get());
}
use of com.google.inject.AbstractModule in project guice by google.
the class ProviderMethodsTest method testWildcardProviderMethods.
public void testWildcardProviderMethods() {
final List<String> strings = ImmutableList.of("A", "B", "C");
final List<Number> numbers = ImmutableList.<Number>of(1, 2, 3);
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
@SuppressWarnings("unchecked") Key<List<? super Integer>> listOfSupertypesOfInteger = (Key<List<? super Integer>>) Key.get(Types.listOf(Types.supertypeOf(Integer.class)));
bind(listOfSupertypesOfInteger).toInstance(numbers);
}
@Provides
public List<? extends CharSequence> provideCharSequences() {
return strings;
}
@Provides
public Class<?> provideType() {
return Float.class;
}
});
assertSame(strings, injector.getInstance(HasWildcardInjection.class).charSequences);
assertSame(numbers, injector.getInstance(HasWildcardInjection.class).numbers);
assertSame(Float.class, injector.getInstance(HasWildcardInjection.class).type);
}
use of com.google.inject.AbstractModule in project guice by google.
the class ProviderMethodsTest method testAutomaticProviderMethodsDoNotCauseDoubleBinding.
/**
* If the user installs provider methods for the module manually, that shouldn't cause a double
* binding of the provider methods' types.
*/
public void testAutomaticProviderMethodsDoNotCauseDoubleBinding() {
Module installsSelf = new AbstractModule() {
@Override
protected void configure() {
install(this);
bind(Integer.class).toInstance(5);
}
@Provides
public String provideString(Integer count) {
return "A" + count;
}
};
Injector injector = Guice.createInjector(installsSelf);
assertEquals("A5", injector.getInstance(String.class));
}
Aggregations