Bug 8300: Caches v1.0 incl. CLI commands (see demo video)
authorMichael Vorburger <vorburger@redhat.com>
Fri, 2 Dec 2016 11:59:30 +0000 (12:59 +0100)
committerMichael Vorburger <vorburger@redhat.com>
Mon, 21 Aug 2017 11:00:13 +0000 (13:00 +0200)
see https://www.youtube.com/watch?v=h4HOSRN2aFc

The main interface APIs are CacheProvider, Cache and CacheFunction.

The SampleServiceWithCachingImpl class illustrates the intended usage.

Actual cache implementations are pluggable; Guava for now, more later.

Change-Id: Ie46f481077283524789212ee48b059fdb8988a62
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AbstractCheckedModule.java [new file with mode: 0644]
inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AbstractGuiceJsr250Module.java
inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AnnotationsModule.java [new file with mode: 0644]
inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/GuiceRule.java
inject-guice-testutils/src/test/java/org/opendaylight/infrautils/inject/guice/testutils/tests/GuiceRuleForgotAnnotationsModuleTest.java [new file with mode: 0644]

diff --git a/inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AbstractCheckedModule.java b/inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AbstractCheckedModule.java
new file mode 100644 (file)
index 0000000..d710c76
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+}
index 5f1d2da49fa59551f59c7187afb37f656884f086..9674211ce81a25561ef86d6cb26b3452541ebf3b 100644 (file)
@@ -7,29 +7,20 @@
  */
 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 &amp;
- * Closeable support for {@literal @}PreDestroy &amp; {@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;
diff --git a/inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AnnotationsModule.java b/inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AnnotationsModule.java
new file mode 100644 (file)
index 0000000..8c24ccd
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * 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 &amp;
+ * Closeable support for {@literal @}PreDestroy &amp; {@literal @}PostConstruct.
+ *
+ * @author Michael Vorburger.ch
+ */
+public class AnnotationsModule extends AbstractModule {
+
+    @Override
+    protected void configure() {
+        install(new CloseableModule());
+        install(new Jsr250Module());
+    }
+
+}
index 4f7fea921cb3a99ef47a20b17b0a372d355d1fd3..012b3292eb1b6f65c93b91b9dd726c336ae7eecd 100644 (file)
@@ -7,6 +7,7 @@
  */
 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;
@@ -96,7 +97,13 @@ public class GuiceRule implements MethodRule {
     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);
+            }
         }
     }
 
diff --git a/inject-guice-testutils/src/test/java/org/opendaylight/infrautils/inject/guice/testutils/tests/GuiceRuleForgotAnnotationsModuleTest.java b/inject-guice-testutils/src/test/java/org/opendaylight/infrautils/inject/guice/testutils/tests/GuiceRuleForgotAnnotationsModuleTest.java
new file mode 100644 (file)
index 0000000..481d524
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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() {
+        }
+    }
+
+}