Fix broken GuiceModule, and rename it to AbstractGuiceJsr250Module
authorMichael Vorburger <vorburger@redhat.com>
Mon, 10 Oct 2016 11:40:01 +0000 (13:40 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Mon, 10 Oct 2016 11:40:01 +0000 (13:40 +0200)
Bug: final configure() forgot to call the abstract configureBindings()

The self test has also been improved to actually detect this problem.
(Before it was too simplistic; without an interface, there was actually
no need at all for a binding, so the problem went undetected.)

Change-Id: I25ff3ccfb84d4179e50befd898e4e911d854f795
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AbstractGuiceJsr250Module.java [moved from inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/GuiceModule.java with 89% similarity]
inject-guice-testutils/src/test/java/org/opendaylight/infrautils/inject/guice/testutils/tests/ExampleGuiceRuleTest.java
inject-guice-testutils/src/test/java/org/opendaylight/infrautils/inject/guice/testutils/tests/SomeClassWithPostConstruct.java
inject-guice-testutils/src/test/java/org/opendaylight/infrautils/inject/guice/testutils/tests/SomeInterfaceWithPostConstruct.java [new file with mode: 0644]

similarity index 89%
rename from inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/GuiceModule.java
rename to inject-guice-testutils/src/main/java/org/opendaylight/infrautils/inject/guice/testutils/AbstractGuiceJsr250Module.java
index 7406720229b710d31be4b9958631d9b9081dc977..179ca604cb21253d79c782e827a1b8b7c3c27421 100644 (file)
@@ -17,12 +17,13 @@ import com.mycila.guice.ext.jsr250.Jsr250Module;
  *
  * @author Michael Vorburger
  */
-public abstract class GuiceModule extends AbstractModule {
+public abstract class AbstractGuiceJsr250Module extends AbstractModule {
 
     @Override
     protected final void configure() {
         install(new CloseableModule());
         install(new Jsr250Module());
+        configureBindings();
     }
 
     protected abstract void configureBindings();
index 11eb573879fe8cf9304ce1f8e8c94d63b814efd6..77f7eb00443e08c3c94e8983d6ed9124e83bb19e 100644 (file)
@@ -12,11 +12,11 @@ import static com.google.common.truth.Truth.assertThat;
 import javax.inject.Inject;
 import org.junit.Rule;
 import org.junit.Test;
-import org.opendaylight.infrautils.inject.guice.testutils.GuiceModule;
+import org.opendaylight.infrautils.inject.guice.testutils.AbstractGuiceJsr250Module;
 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
 
 /**
- * Example Guice Test using the {@link GuiceRule} & {@link GuiceModule}.
+ * Example Guice Test using the {@link GuiceRule} & {@link AbstractGuiceJsr250Module}.
  *
  * @author Michael Vorburger
  */
@@ -24,16 +24,16 @@ public class ExampleGuiceRuleTest {
 
     public @Rule GuiceRule guice = new GuiceRule(TestModule.class);
 
-    @Inject SomeClassWithPostConstruct someClass;
+    @Inject SomeInterfaceWithPostConstruct someService;
 
     @Test public void testGuiceWithRule() {
-        assertThat(someClass.isInit).named("isInit").isTrue();
+        assertThat(someService.isInit()).named("isInit()").isTrue();
     }
 
-    public static class TestModule extends GuiceModule {
+    public static class TestModule extends AbstractGuiceJsr250Module {
         @Override
         protected void configureBindings() {
-            bind(SomeClassWithPostConstruct.class);
+            bind(SomeInterfaceWithPostConstruct.class).to(SomeClassWithPostConstruct.class);
         }
     }
 }
index 59ef7a9da8b2981f884d899cc57f5b8e3df5136e..5febd6488ca9389a63cd7f614fa6041864d77719 100644 (file)
@@ -9,13 +9,18 @@ package org.opendaylight.infrautils.inject.guice.testutils.tests;
 
 import javax.annotation.PostConstruct;
 
-class SomeClassWithPostConstruct {
+class SomeClassWithPostConstruct implements SomeInterfaceWithPostConstruct {
 
     boolean isInit = false;
 
+    @Override
     @PostConstruct
     public void init() {
         isInit = true;
     }
 
+    @Override
+    public boolean isInit() {
+        return isInit;
+    }
 }
diff --git a/inject-guice-testutils/src/test/java/org/opendaylight/infrautils/inject/guice/testutils/tests/SomeInterfaceWithPostConstruct.java b/inject-guice-testutils/src/test/java/org/opendaylight/infrautils/inject/guice/testutils/tests/SomeInterfaceWithPostConstruct.java
new file mode 100644 (file)
index 0000000..0b491f1
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2016 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;
+
+interface SomeInterfaceWithPostConstruct {
+
+    void init();
+
+    boolean isInit();
+
+}