use of com.google.inject.OutOfScopeException 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.OutOfScopeException 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.OutOfScopeException in project gerrit by GerritCodeReview.
the class IdentifiedUser method guessHost.
private String guessHost() {
String host = null;
SocketAddress remotePeer = null;
try {
remotePeer = remotePeerProvider.get();
} catch (OutOfScopeException | ProvisionException e) {
// Leave null.
}
if (remotePeer instanceof InetSocketAddress) {
InetSocketAddress sa = (InetSocketAddress) remotePeer;
InetAddress in = sa.getAddress();
host = in != null ? getHost(in) : sa.getHostName();
}
if (Strings.isNullOrEmpty(host)) {
return "unknown";
}
return host;
}
use of com.google.inject.OutOfScopeException in project gerrit by GerritCodeReview.
the class InProcessProtocol method module.
static Module module() {
return new AbstractModule() {
@Override
public void configure() {
install(new GerritRequestModule());
bind(RequestScopePropagator.class).to(Propagator.class);
bindScope(RequestScoped.class, InProcessProtocol.REQUEST);
}
@Provides
@RemotePeer
SocketAddress getSocketAddress() {
// something.
throw new OutOfScopeException("No remote peer in acceptance tests");
}
};
}
use of com.google.inject.OutOfScopeException in project guice by google.
the class ScopeRequestIntegrationTest method testNullReplacement.
public final void testNullReplacement() throws Exception {
Injector injector = Guice.createInjector(new ServletModule() {
@Override
protected void configureServlets() {
bindConstant().annotatedWith(Names.named(SomeObject.INVALID)).to(SHOULDNEVERBESEEN);
bind(SomeObject.class).in(RequestScoped.class);
}
});
Callable<SomeObject> callable = injector.getInstance(Caller.class);
try {
assertNotNull(callable.call());
fail();
} catch (ProvisionException pe) {
assertTrue(pe.getCause() instanceof OutOfScopeException);
}
// Validate that an actual null entry in the map results in a null injected object.
Map<Key<?>, Object> map = Maps.newHashMap();
map.put(Key.get(SomeObject.class), null);
callable = ServletScopes.scopeRequest(injector.getInstance(Caller.class), map);
assertNull(callable.call());
}
Aggregations