* 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
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;
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());
}
}
* 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"));
* 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;
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("<top><innerText>value</innerText></top>"), XPathConstants.NODE);
assertEquals("value", ((Element) value).getTextContent());
}
-
}