use of com.google.inject.Scope in project roboguice by roboguice.
the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.
public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
bindScope(BadScope.class, new Scope() {
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return new Provider<T>() {
@Override
public T get() {
throw new OutOfScopeException("failure");
}
};
}
});
}
});
try {
injector.getInstance(Key.get(remoteProviderOfFoo)).get();
fail();
} catch (ProvisionException pe) {
assertEquals(2, pe.getErrorMessages().size());
List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(0).getMessage());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(1).getMessage());
}
}
use of com.google.inject.Scope in project roboguice by roboguice.
the class OverrideModuleTest method testOverrideScopeAnnotation.
public void testOverrideScopeAnnotation() {
final Scope scope = new Scope() {
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.Scope in project roboguice by roboguice.
the class OverrideModuleTest method testFailsIfOverridenScopeInstanceHasBeenUsed.
public void testFailsIfOverridenScopeInstanceHasBeenUsed() {
final Scope scope = new Scope() {
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return unscoped;
}
@Override
public String toString() {
return "ORIGINAL SCOPE";
}
};
final Module original = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, scope);
bind(Date.class).in(scope);
bind(String.class).in(scope);
}
};
Module originalWrapper = new AbstractModule() {
@Override
protected void configure() {
install(original);
}
};
Module replacements = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, new SingleUseScope());
}
};
try {
createInjector(Modules.override(originalWrapper).with(replacements));
fail("Exception expected");
} catch (CreationException e) {
assertContains(e.getMessage(), "1) The scope for @TestScopeAnnotation is bound directly and cannot be overridden.", "original binding at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "at ", replacements.getClass().getName() + ".configure(", asModuleChain(Modules.OverrideModule.class, replacements.getClass()));
}
}
use of com.google.inject.Scope in project guice by google.
the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.
public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
bindScope(BadScope.class, new Scope() {
@Override
public <T> Provider<T> scope(final Key<T> key, Provider<T> unscoped) {
return new Provider<T>() {
@Override
public T get() {
throw new OutOfScopeException("failure: " + key.toString());
}
};
}
});
}
});
try {
injector.getInstance(Key.get(remoteProviderOfFoo)).get();
fail();
} catch (ProvisionException pe) {
assertEquals(2, pe.getErrorMessages().size());
List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped1.class), messages.get(0).getMessage());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped2.class), messages.get(1).getMessage());
}
}
use of com.google.inject.Scope in project guice by google.
the class ScopeBindingProcessor method visit.
@Override
public Boolean visit(ScopeBinding command) {
Scope scope = checkNotNull(command.getScope(), "scope");
Class<? extends Annotation> annotationType = checkNotNull(command.getAnnotationType(), "annotation type");
if (!Annotations.isScopeAnnotation(annotationType)) {
errors.missingScopeAnnotation(annotationType);
// Go ahead and bind anyway so we don't get collateral errors.
}
if (!Annotations.isRetainedAtRuntime(annotationType)) {
errors.missingRuntimeRetention(annotationType);
// Go ahead and bind anyway so we don't get collateral errors.
}
ScopeBinding existing = injector.state.getScopeBinding(annotationType);
if (existing != null) {
if (!scope.equals(existing.getScope())) {
errors.duplicateScopes(existing, annotationType, scope);
}
} else {
injector.state.putScopeBinding(annotationType, command);
}
return true;
}
Aggregations