Do not catch instantiation exceptions during augment
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug5335Test.java
index ac9912c4cc537af7e813f9f70fe5d2edd3f08563..a580748dcfe3a907b5f3ea5a3387ee04baa44c30 100644 (file)
@@ -7,16 +7,13 @@
  */
 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.assertNotNull;
-import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.StandardCharsets;
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
@@ -24,6 +21,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 
 public class Bug5335Test {
     private static final String FOO = "foo";
@@ -31,66 +30,39 @@ public class Bug5335Test {
     private static final String REV = "2016-03-04";
 
     private static final QName ROOT = QName.create(FOO, REV, "root");
-    private static final QName PRESENCE_CONTAINER_F = QName.create(FOO, REV, "presence-container");
     private static final QName NON_PRESENCE_CONTAINER_F = QName.create(FOO, REV, "non-presence-container");
     private static final QName MANDATORY_LEAF_F = QName.create(FOO, REV, "mandatory-leaf");
     private static final QName PRESENCE_CONTAINER_B = QName.create(BAR, REV, "presence-container");
     private static final QName NON_PRESENCE_CONTAINER_B = QName.create(BAR, REV, "non-presence-container");
     private static final QName MANDATORY_LEAF_B = QName.create(BAR, REV, "mandatory-leaf");
 
-    private final ByteArrayOutputStream output = new ByteArrayOutputStream();
-
-    @Before
-    public void setUp() throws UnsupportedEncodingException {
-        System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8));
-    }
-
-    @After
-    @SuppressWarnings("checkstyle:regexpSinglelineJava")
-    public void cleanUp() {
-        System.setOut(System.out);
-    }
-
     @Test
-    public void incorrectTest1() throws Exception {
-        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1");
-        assertNotNull(context);
-
-        final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
-        final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
-        assertNull(mandatoryLeaf);
-
-        final String testLog = output.toString();
-        assertTrue(testLog.contains(
+    public void incorrectTest1() {
+        final ReactorException ex = assertThrows(ReactorException.class,
+            () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1"));
+        final Throwable cause = ex.getCause();
+        assertThat(cause, instanceOf(InferenceException.class));
+        assertThat(cause.getMessage(), startsWith(
             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
     }
 
     @Test
-    public void incorrectTest2() throws Exception {
-        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
-        assertNotNull(context);
-
-        final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, MANDATORY_LEAF_B);
-        final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
-        assertNull(mandatoryLeaf);
-
-        final String testLog = output.toString();
-        assertTrue(testLog.contains(
+    public void incorrectTest2() {
+        final ReactorException ex = assertThrows(ReactorException.class,
+            () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2"));
+        final Throwable cause = ex.getCause();
+        assertThat(cause, instanceOf(InferenceException.class));
+        assertThat(cause.getMessage(), startsWith(
             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
     }
 
     @Test
     public void incorrectTest3() throws Exception {
-        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
-        assertNotNull(context);
-
-        final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, NON_PRESENCE_CONTAINER_B,
-                MANDATORY_LEAF_B);
-        final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
-        assertNull(mandatoryLeaf);
-
-        final String testLog = output.toString();
-        assertTrue(testLog.contains(
+        final ReactorException ex = assertThrows(ReactorException.class,
+            () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2"));
+        final Throwable cause = ex.getCause();
+        assertThat(cause, instanceOf(InferenceException.class));
+        assertThat(cause.getMessage(), startsWith(
             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
     }