From 096fb6e154cae2dda192360bf2b7bea17cfe9f4c Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 23 May 2021 20:06:45 +0200 Subject: [PATCH] Modernize netconf-util tests Use assertThrows() and fixup imports a bit. Change-Id: I2816141af08815afb0dc926e78ace962ba561ded Signed-off-by: Robert Varga --- .../netconf/util/CloseableUtilTest.java | 20 +++++++------------ .../netconf/util/NetconfUtilTest.java | 4 ++-- .../xml/HardcodedNamespaceResolverTest.java | 16 ++++++--------- .../netconf/util/xml/XMLNetconfUtilTest.java | 17 ++++++---------- 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/CloseableUtilTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/CloseableUtilTest.java index 43754e9ec6..9e517abbe3 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/CloseableUtilTest.java +++ b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/CloseableUtilTest.java @@ -5,38 +5,32 @@ * 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.netconf.util; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; -import com.google.common.collect.Lists; +import java.util.List; import org.junit.Test; public class CloseableUtilTest { - - @SuppressWarnings("checkstyle:IllegalCatch") @Test - public void testCloseAllFail() throws Exception { + public void testCloseAllFail() { final AutoCloseable failingCloseable = () -> { throw new RuntimeException("testing failing close"); }; - try { - CloseableUtil.closeAll(Lists.newArrayList(failingCloseable, failingCloseable)); - fail("Exception with suppressed should be thrown"); - } catch (final RuntimeException e) { - assertEquals(1, e.getSuppressed().length); - } + final RuntimeException ex = assertThrows(RuntimeException.class, + () -> CloseableUtil.closeAll(List.of(failingCloseable, failingCloseable))); + assertEquals(1, ex.getSuppressed().length); } @Test public void testCloseAll() throws Exception { final AutoCloseable failingCloseable = mock(AutoCloseable.class); doNothing().when(failingCloseable).close(); - CloseableUtil.closeAll(Lists.newArrayList(failingCloseable, failingCloseable)); + CloseableUtil.closeAll(List.of(failingCloseable, failingCloseable)); } } \ No newline at end of file diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfUtilTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfUtilTest.java index e05cb26e4f..fd291b22ad 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfUtilTest.java +++ b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfUtilTest.java @@ -10,12 +10,12 @@ package org.opendaylight.netconf.util; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import java.util.Collections; import javax.xml.transform.dom.DOMResult; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers; @@ -80,6 +80,6 @@ public class NetconfUtilTest { final Document actual = (Document) result.getNode(); final Document expected = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/sessions.xml")); final Diff diff = XMLUnit.compareXML(expected, actual); - Assert.assertTrue(diff.toString(), diff.similar()); + assertTrue(diff.toString(), diff.similar()); } } diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/HardcodedNamespaceResolverTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/HardcodedNamespaceResolverTest.java index c728862068..aae32b0e73 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/HardcodedNamespaceResolverTest.java +++ b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/HardcodedNamespaceResolverTest.java @@ -5,30 +5,26 @@ * 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.netconf.util.xml; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import org.junit.Test; public class HardcodedNamespaceResolverTest { - @Test public void testResolver() throws Exception { final HardcodedNamespaceResolver hardcodedNamespaceResolver = new HardcodedNamespaceResolver("prefix", "namespace"); assertEquals("namespace", hardcodedNamespaceResolver.getNamespaceURI("prefix")); - try { - hardcodedNamespaceResolver.getNamespaceURI("unknown"); - fail("Unknown namespace lookup should fail"); - } catch (IllegalStateException e) { - assertTrue(e.getMessage().startsWith("Prefix mapping not found for ")); - } + final IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> hardcodedNamespaceResolver.getNamespaceURI("unknown")); + assertThat(ex.getMessage(), startsWith("Prefix mapping not found for ")); assertNull(hardcodedNamespaceResolver.getPrefix("any")); assertNull(hardcodedNamespaceResolver.getPrefixes("any")); diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/XMLNetconfUtilTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/XMLNetconfUtilTest.java index bf3c573c1f..e9e572148e 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/XMLNetconfUtilTest.java +++ b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/XMLNetconfUtilTest.java @@ -5,12 +5,12 @@ * 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.netconf.util.xml; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; @@ -19,19 +19,14 @@ import org.opendaylight.netconf.api.xml.XmlUtil; import org.w3c.dom.Element; public class XMLNetconfUtilTest { - @Test public void testXPath() throws Exception { final XPathExpression correctXPath = XMLNetconfUtil.compileXPath("/top/innerText"); - try { - XMLNetconfUtil.compileXPath("!@(*&$!"); - fail("Incorrect xpath should fail"); - } catch (IllegalStateException e) { - assertTrue(e.getMessage().startsWith("Error while compiling xpath expression ")); - } + final IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> XMLNetconfUtil.compileXPath("!@(*&$!")); + assertThat(ex.getMessage(), startsWith("Error while compiling xpath expression ")); final Object value = XmlUtil.evaluateXPath(correctXPath, XmlUtil.readXmlToDocument("value"), XPathConstants.NODE); assertEquals("value", ((Element) value).getTextContent()); } - } -- 2.36.6