下面列出了怎么用com.google.inject.CreationException的API类实例代码及写法,或者点击链接到github查看源代码。
@VisibleForTesting
static void handleUncaughtException(final Thread t, final Throwable e) {
if (e instanceof UnrecoverableException) {
if (((UnrecoverableException) e).isShowException()) {
log.error("An unrecoverable Exception occurred. Exiting HiveMQ", t, e);
}
System.exit(1);
} else if (e instanceof CreationException) {
if (e.getCause() instanceof UnrecoverableException) {
log.error("An unrecoverable Exception occurred. Exiting HiveMQ");
System.exit(1);
}
final CreationException creationException = (CreationException) e;
checkGuiceErrorsForUnrecoverable(creationException.getErrorMessages());
} else if (e instanceof ProvisionException) {
if (e.getCause() instanceof UnrecoverableException) {
log.error("An unrecoverable Exception occurred. Exiting HiveMQ");
System.exit(1);
}
final ProvisionException provisionException = (ProvisionException) e;
checkGuiceErrorsForUnrecoverable(provisionException.getErrorMessages());
}
final Throwable rootCause = Throwables.getRootCause(e);
if (rootCause instanceof UnrecoverableException) {
final boolean showException = ((UnrecoverableException) rootCause).isShowException();
if (showException) {
log.error("Cause: ", e);
}
} else {
log.error("Uncaught Error:", e);
}
}
@Test
public void test_creationException() {
exit.expectSystemExitWithStatus(1);
final CreationException creationException = new CreationException(Collections.singletonList(new Message("test",
new UnrecoverableException(false))));
HiveMQExceptionHandlerBootstrap.handleUncaughtException(Thread.currentThread(), creationException);
}
@Test
public void originalBindingRequired() throws Exception {
assertThrows(CreationException.class, () -> {
Guice.createInjector(binder -> {
new TransformableBinder<>(binder, String.class);
});
});
}
@Test
public void regularBindingConflictsWithTransformableBinding() throws Exception {
assertThrows(CreationException.class, () -> {
Guice.createInjector(binder -> {
binder.bind(String.class).toInstance("hi");
new TransformableBinder<>(binder, String.class);
});
});
}
@Test
public void voidReturnType() throws Exception {
class Woot {
void foo() {}
}
InjectableMethod<?> method = InjectableMethod.forDeclaredMethod(new Woot(), "foo");
assertThrows(CreationException.class, () ->
Guice.createInjector(method.bindingModule())
);
}
public static void main(String... args) {
try {
Guice.createInjector(
new ConfigModule(args, System.out, System.err),
new TestabilityModule()).
getInstance(Testability.class).
run();
} catch (CreationException e) {
// ignore, the error is printed in the ConfigModule
}
}
@Test(expected = CreationException.class)
public void loggerInjectionThrowsErrorOnUnexpectedType() {
try {
injector.createChildInjector((Module) binder -> binder.bind(BadLoggerHolder.class));
} catch (CreationException e) {
Throwable cause = e.getCause();
assertThat(cause).isInstanceOf(SeedException.class);
assertThat(((SeedException) cause).getErrorCode()).isEqualTo(CoreErrorCode.BAD_LOGGER_TYPE);
throw e;
}
fail("should have failed");
}
@Test(expected = CreationException.class)
public void testNoTrappingNonVoidMethods() {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
GuiceUtils.bindExceptionTrap(binder(), NonVoid.class);
fail("Bind should have failed.");
}
});
}
@Test(expected = CreationException.class)
public void testIncompleteLegacySettings() throws IOException {
LegacyCapabilitiesProvisioning.LegacyProvisioningPropertiesHolder properties = createLegacyProvisioningPropertiesHolder();
properties.capabilitiesDirectoryParticipantId = "";
Set<DiscoveryEntry> discoveryEntries = createDiscoveryEntries("io.joynr",
GlobalCapabilitiesDirectory.INTERFACE_NAME,
GlobalDomainAccessController.INTERFACE_NAME);
final String serializedDiscoveryEntries = objectMapper.writeValueAsString(discoveryEntries);
createInjectorForJsonValue(serializedDiscoveryEntries, properties);
fail("Expecting legacy capabilities provisioning to fail fast.");
}
@Test
public void applicationCreationPropertiesCannotOverrideJoynFactoryProperties() {
creationException.expect(CreationException.class);
creationException.expectMessage(StringContains.containsString("A binding to java.lang.String annotated with @com.google.inject.name.Named(value=joynr.messaging.bounceproxyurl) was already configured"));
applicationCreationProperties.setProperty(MessagingPropertyKeys.BOUNCE_PROXY_URL,
"http://test-bounce-proxy-url");
injectorfactory.createApplication(new TestApplicationModule(applicationCreationProperties));
}
@Test
public void applicationLevelDefaultPropertiesCannotOverrideJoynDefaultProperties() {
creationException.expect(CreationException.class);
creationException.expectMessage(StringContains.containsString("A binding to java.lang.String annotated with @com.google.inject.name.Named(value=joynr.messaging.bounceproxyurl) was already configured"));
injectorfactory.createApplication(new TestApplicationModule(applicationCreationProperties, true));
}
public static Injector create(Iterable<Module> modules) throws MetaborgException {
try {
return Guice.createInjector(modules);
} catch(CreationException e) {
throw new MetaborgException("Could not create injector because of dependency injection errors", e);
}
}
public static Injector create(Module... modules) throws MetaborgException {
try {
return Guice.createInjector(modules);
} catch(CreationException e) {
throw new MetaborgException("Could not create injector because of dependency injection errors", e);
}
}
public static Injector createChild(Injector parent, Iterable<Module> modules) throws MetaborgException {
try {
return parent.createChildInjector(modules);
} catch(CreationException e) {
throw new MetaborgException("Could not create child injector because of dependency injection errors", e);
}
}
public static Injector createChild(Injector parent, Module... modules) throws MetaborgException {
try {
return parent.createChildInjector(modules);
} catch(CreationException e) {
throw new MetaborgException("Could not create child injector because of dependency injection errors", e);
}
}
@Test
void startShouldFailWhenSslUsedAndNotSupportedByServer(GuiceJamesServer jamesServer) {
assertThatThrownBy(jamesServer::start)
.isInstanceOf(CreationException.class)
.hasStackTraceContaining("Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed");
}
@Test
void startShouldFailOnBadPassword(GuiceJamesServer jamesServer) {
assertThatThrownBy(jamesServer::start)
.isInstanceOf(CreationException.class)
.hasStackTraceContaining("Caused by: com.datastax.driver.core.exceptions.AuthenticationException: Authentication error");
}
@Test
@Expected(CreationException.class)
public void injectionShouldNotWork() {
assertThat(object).isNull();
}
@Test(expected = CreationException.class)
public void testInvalidJson() throws IOException {
Injector injector = createInjectorForJsonValue("this is not json");
injector.getInstance(CapabilitiesProvisioning.class);
}