From 725c9f8e9daabebbb0bb85124f20fa83c6dc460f Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 5 Dec 2020 14:34:30 +0100 Subject: [PATCH] Refactor YT838Test 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 --- .../yangtools/yang/stmt/YT838Test.java | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT838Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT838Test.java index 386bde2ddc..f21fe3e769 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT838Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT838Test.java @@ -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 ")); } } -- 2.36.6