From: Michael Vorburger Date: Wed, 10 Aug 2016 15:22:30 +0000 (+0200) Subject: DataBrokerTestModule: use AbstractDataBrokerTest without inheritance X-Git-Tag: v4.0.15~12 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=mdsal.git;a=commitdiff_plain;h=0e5a91c81e8fbd2a2118acba69aa3ba6159806ba DataBrokerTestModule: use AbstractDataBrokerTest without inheritance While starting to write JUnit tests (for functional components) with the AbstractDataBrokerTest, I found myself not liking its design which forces one to use a class SomeTest extends AbstractDataBrokerTest... Modern JUnit tests should be written by composition, not enforced inheritance. Thus this new DataBrokerTestModule allows one to obtain a DataBroker suitable for Tests from anywhere. The *(Test)Module naming convention is obviously inspired by a DI point of view (think Spring Framework's @Configuration classes, or Google Guice or Dagger (yay!) *Module classes - which is exactly what this really is - a particular way to obtain an instance of an implementationof some service (here a DataBroker) in a certain environment (here for tests). The internal implementation of DataBrokerTestModule could be changed later; I guess ideally what's in AbstractDataBrokerTest could go into DataBrokerTestModule and AbstractDataBrokerTest could use it, but for now I'm too lazy to change that, as this does the trick nicely. Later implementation changes would be transparent to users of DataBrokerTestModule. JIRA: MDSAL-556 Change-Id: I9641f527bbc0cb92732f2e513cdd64cc6a837200 Signed-off-by: Michael Vorburger Signed-off-by: Robert Varga (cherry picked from commit e94d340da0d1b19e506fe2ad7e4e19c79959e241) --- diff --git a/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/tests/AbstractDataBrokerTestTest.java b/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/tests/AbstractDataBrokerTestTest.java new file mode 100644 index 0000000000..e777bd3d5f --- /dev/null +++ b/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/tests/AbstractDataBrokerTestTest.java @@ -0,0 +1,22 @@ +/* + * 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.mdsal.binding.dom.adapter.test.tests; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest; + +public class AbstractDataBrokerTestTest extends AbstractDataBrokerTest { + + @Test + public void ensureDataBrokerTestModuleWorksWithoutException() { + assertNotNull(getDataBroker()); + } + +} diff --git a/binding/mdsal-binding-test-utils/src/main/java/org/opendaylight/mdsal/binding/testutils/DataBrokerTestModule.java b/binding/mdsal-binding-test-utils/src/main/java/org/opendaylight/mdsal/binding/testutils/DataBrokerTestModule.java new file mode 100644 index 0000000000..311f0e795b --- /dev/null +++ b/binding/mdsal-binding-test-utils/src/main/java/org/opendaylight/mdsal/binding/testutils/DataBrokerTestModule.java @@ -0,0 +1,31 @@ +/* + * 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.mdsal.binding.testutils; + +import org.opendaylight.mdsal.binding.api.DataBroker; + +// @Module +public class DataBrokerTestModule { + + // Suppress IllegalCatch because of AbstractDataBrokerTest (change later) + @SuppressWarnings({ "checkstyle:IllegalCatch", "checkstyle:IllegalThrows" }) + public static /* @Provides @Singleton */ DataBroker dataBroker() throws RuntimeException { + try { + // This is a little bit "upside down" - in the future, + // we should probably put what is in AbstractDataBrokerTest + // into this DataBrokerTestModule, and make AbstractDataBrokerTest + // use it, instead of the way around it currently is (the opposite); + // this is just for historical reasons... and works for now. + AbstractDataBrokerTest dataBrokerTest = new AbstractDataBrokerTest(); + dataBrokerTest.setup(); + return dataBrokerTest.getDataBroker(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/binding/mdsal-binding-test-utils/src/test/java/org/opendaylight/mdsal/binding/testutils/DataBrokerTestModuleTest.java b/binding/mdsal-binding-test-utils/src/test/java/org/opendaylight/mdsal/binding/testutils/DataBrokerTestModuleTest.java new file mode 100644 index 0000000000..44c5dc9c0d --- /dev/null +++ b/binding/mdsal-binding-test-utils/src/test/java/org/opendaylight/mdsal/binding/testutils/DataBrokerTestModuleTest.java @@ -0,0 +1,20 @@ +/* + * 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.mdsal.binding.testutils; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; + +public class DataBrokerTestModuleTest { + + @Test + public void ensureDataBrokerTestModuleWorksWithoutException() { + assertThat(DataBrokerTestModule.dataBroker()).isNotNull(); + } +}