--- /dev/null
+/*
+ * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.infrautils.inject.guice.testutils;
+
+import com.google.inject.AbstractModule;
+import org.opendaylight.infrautils.inject.ModuleSetupRuntimeException;
+
+/**
+ * Convenience Guice module support class with configure method that allows
+ * throwing checked exceptions, which are caught and re-thrown as unchecked
+ * {@link ModuleSetupRuntimeException}.
+ *
+ * @author Michael Vorburger.ch
+ */
+public abstract class AbstractCheckedModule extends AbstractModule {
+
+ @Override
+ @SuppressWarnings("checkstyle:IllegalCatch")
+ protected final void configure() throws ModuleSetupRuntimeException {
+ try {
+ checkedConfigure();
+ } catch (Exception e) {
+ throw new ModuleSetupRuntimeException(e);
+ }
+ }
+
+ protected abstract void checkedConfigure() throws Exception;
+
+}
*/
package org.opendaylight.infrautils.inject.guice.testutils;
-import com.google.inject.AbstractModule;
-import com.mycila.guice.ext.closeable.CloseableModule;
-import com.mycila.guice.ext.jsr250.Jsr250Module;
-import org.opendaylight.infrautils.inject.ModuleSetupRuntimeException;
-
/**
- * Guice module with built-in Mycila Guice Extensions for JSR-250 &
- * Closeable support for {@literal @}PreDestroy & {@literal @}PostConstruct.
+ * Convenience Guice module support class, which installs the
+ * {@link AnnotationsModule}, and handles exceptions as the
+ * {@link AbstractCheckedModule} does.
*
- * @author Michael Vorburger
+ * @author Michael Vorburger.ch
*/
-public abstract class AbstractGuiceJsr250Module extends AbstractModule {
+public abstract class AbstractGuiceJsr250Module extends AbstractCheckedModule {
@Override
@SuppressWarnings("checkstyle:IllegalCatch")
- protected final void configure() throws ModuleSetupRuntimeException {
- install(new CloseableModule());
- install(new Jsr250Module());
- try {
- configureBindings();
- } catch (Exception e) {
- throw new ModuleSetupRuntimeException(e);
- }
+ protected final void checkedConfigure() throws Exception {
+ install(new AnnotationsModule());
+ configureBindings();
}
protected abstract void configureBindings() throws Exception;
--- /dev/null
+/*
+ * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.infrautils.inject.guice.testutils;
+
+import com.google.inject.AbstractModule;
+import com.mycila.guice.ext.closeable.CloseableModule;
+import com.mycila.guice.ext.jsr250.Jsr250Module;
+
+/**
+ * Guice module with built-in Mycila Guice Extensions for JSR-250 &
+ * Closeable support for {@literal @}PreDestroy & {@literal @}PostConstruct.
+ *
+ * @author Michael Vorburger.ch
+ */
+public class AnnotationsModule extends AbstractModule {
+
+ @Override
+ protected void configure() {
+ install(new CloseableModule());
+ install(new Jsr250Module());
+ }
+
+}
*/
package org.opendaylight.infrautils.inject.guice.testutils;
+import com.google.inject.ConfigurationException;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
protected void tearDownGuice() {
if (injector != null) {
// http://code.mycila.com/guice/#3-jsr-250
- injector.getInstance(CloseableInjector.class).close();
+ try {
+ injector.getInstance(CloseableInjector.class).close();
+ } catch (ConfigurationException e) {
+ throw new IllegalStateException("You forgot to either add GuiceRule(..., AnnotationsModule.class), "
+ + "or in your Module use an install(new AnnotationsModule()) with "
+ + AnnotationsModule.class.getName(), e);
+ }
}
}
--- /dev/null
+/*
+ * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.infrautils.inject.guice.testutils.tests;
+
+import com.google.inject.AbstractModule;
+import org.junit.Test;
+import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
+
+/**
+ * Tests friendly and more helpful than default error message in case
+ * AnnotationsModule was forgotten to be found.
+ *
+ * @author Michael Vorburger.ch
+ */
+@SuppressWarnings("checkstyle:IllegalThrows")
+public class GuiceRuleForgotAnnotationsModuleTest {
+
+ // This is intentional, with this it fails expectedly; remove to see how
+ // public @Rule GuiceRule guice = new GuiceRule(TestModule.class);
+
+ @Test(expected = IllegalStateException.class)
+ public void testGuiceWithRule() throws Throwable {
+ new GuiceRule(TestModule.class).apply(null, null, null).evaluate();
+ }
+
+ public static class TestModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ }
+ }
+
+}