Refactor YT838Test 72/94072/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 5 Dec 2020 13:34:30 +0000 (14:34 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 5 Dec 2020 13:35:14 +0000 (14:35 +0100)
We are checking for thrown exceptions here, make use of assertThrows()
and hamcrest to make the reports more understandable.

Change-Id: Ia44be0835897300b86e7896cd7b441f7ccb8fb5c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT838Test.java

index 386bde2ddc6c1b6039a38b97caf6b89f4f04bf00..f21fe3e769e3ffcac7014a470119030b8b5bdff8 100644 (file)
@@ -7,8 +7,11 @@
  */
 package org.opendaylight.yangtools.yang.stmt;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
@@ -16,46 +19,41 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 public class YT838Test {
     @Test
-    public void testGroupingShadowing() throws Exception {
+    public void testGroupingShadowing() {
         testGrouping("grouping.yang");
     }
 
     @Test
-    public void testGroupingPostShadowing() throws Exception {
+    public void testGroupingPostShadowing() {
         testGrouping("grouping-post.yang");
     }
 
     @Test
-    public void testTypedefShadowing() throws Exception {
+    public void testTypedefShadowing() {
         testTypedef("typedef.yang");
     }
 
     @Test
-    public void testTypedefPostShadowing() throws Exception {
+    public void testTypedefPostShadowing() {
         testTypedef("typedef-post.yang");
     }
 
-    private static void testGrouping(final String model) throws Exception {
-        try {
-            StmtTestUtils.parseYangSource("/bugs/YT838/" + model);
-            fail("Expected failure due to grouping identifier shadowing");
-        } catch (ReactorException e) {
-            final Throwable cause = e.getCause();
-            assertTrue(cause instanceof SourceException);
-            assertTrue(cause.getMessage().startsWith(
-                "Duplicate name for grouping (grouping?revision=2017-12-20)foo [at "));
-        }
+    private static void testGrouping(final String model) {
+        final ReactorException ex = assertThrows(ReactorException.class,
+            () -> StmtTestUtils.parseYangSource("/bugs/YT838/" + model));
+
+        final Throwable cause = ex.getCause();
+        assertThat(cause, instanceOf(SourceException.class));
+        assertThat(cause.getMessage(),
+            startsWith("Duplicate name for grouping (grouping?revision=2017-12-20)foo [at "));
     }
 
-    private static void testTypedef(final String model) throws Exception {
-        try {
-            StmtTestUtils.parseYangSource("/bugs/YT838/" + model);
-            fail("Expected failure due to type identifier shadowing");
-        } catch (ReactorException e) {
-            final Throwable cause = e.getCause();
-            assertTrue(cause instanceof SourceException);
-            assertTrue(cause.getMessage().startsWith(
-                "Duplicate name for typedef (typedef?revision=2017-12-20)foo [at "));
-        }
+    private static void testTypedef(final String model) {
+        final ReactorException ex = assertThrows(ReactorException.class,
+            () -> StmtTestUtils.parseYangSource("/bugs/YT838/" + model));
+        final Throwable cause = ex.getCause();
+        assertTrue(cause instanceof SourceException);
+        assertThat(cause, instanceOf(SourceException.class));
+        assertThat(cause.getMessage(), startsWith("Duplicate name for typedef (typedef?revision=2017-12-20)foo [at "));
     }
 }