BUG-7161: Do not tolerate source-level exceptions 73/48273/4
authorRobert Varga <rovarga@cisco.com>
Fri, 11 Nov 2016 17:58:33 +0000 (18:58 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 16 Nov 2016 16:07:58 +0000 (16:07 +0000)
We should not be suppressing malformed exceptions,
as that can result in us not providing correct
interpretation of models.

Change-Id: I535f066b329f02489f492e9a2b60f86bd5e21019
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangStatementParserListenerImpl.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6410Test.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/SubstatementValidatorTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java

index b699369c1821fe1dde4175660acd6265ca274e23..c9bd2c61a499ad76d73b6fb23967ea197785be46 100644 (file)
@@ -25,13 +25,9 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 @Immutable
 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
-    private static final Logger LOG = LoggerFactory.getLogger(YangStatementParserListenerImpl.class);
-
     private final List<String> toBeSkipped = new ArrayList<>();
     private final String sourceName;
     private QNameToStatementDefinition stmtDef;
@@ -78,19 +74,15 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList
         final StatementSourceReference ref = DeclarationInTextSource.atPosition(
             sourceName, ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
 
-        try {
-            KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
-            String statementName = keyword.getText();
-            QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
-            if (stmtDef != null && Utils.getValidStatementDefinition(prefixes, stmtDef, identifier) != null
-                    && toBeSkipped.isEmpty()) {
-                writer.endStatement(ref);
-            }
-
-            // No-op if the statement is not on the list
-            toBeSkipped.remove(statementName);
-        } catch (SourceException e) {
-            LOG.warn(e.getMessage(), e);
+        KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
+        String statementName = keyword.getText();
+        QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
+        if (stmtDef != null && Utils.getValidStatementDefinition(prefixes, stmtDef, identifier) != null
+                && toBeSkipped.isEmpty()) {
+            writer.endStatement(ref);
         }
+
+        // No-op if the statement is not on the list
+        toBeSkipped.remove(statementName);
     }
 }
index 0ab758bbe0252da3c4bffc04eb06d5d03591f22c..c6fa2c33fbbcdd46d60c2e367ae61c42ae2944e5 100644 (file)
@@ -44,12 +44,12 @@ public class Bug6410Test {
     @Test
     public void shouldFailOnDuplicateTypedefs() {
         try {
-            final SchemaContext schemaContext = StmtTestUtils.parseYangSources(
-                    new YangStatementSourceImpl("/bugs/bug6410/bar.yang", false));
+            StmtTestUtils.parseYangSources(new YangStatementSourceImpl("/bugs/bug6410/bar.yang", false));
             fail("A ReactorException should have been thrown.");
         } catch (ReactorException ex) {
-            assertTrue(ex.getCause() instanceof SourceException);
-            assertTrue(ex.getCause().getMessage().contains("Node name collision"));
+            final Throwable cause = ex.getCause();
+            assertTrue(cause instanceof SourceException);
+            assertTrue(cause.getMessage().contains("Duplicate name for typedef"));
         }
     }
 }
index f1a3c292d69cfbf51c2def8d0758be9793ce8ed3..f57a2a6a426b181b1ee7185b1d4c71f229fd5aa2 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.yangtools.yang.stmt;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import com.google.common.base.VerifyException;
 import java.io.ByteArrayOutputStream;
@@ -25,6 +26,7 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
 
 public class SubstatementValidatorTest {
 
@@ -53,27 +55,32 @@ public class SubstatementValidatorTest {
     }
 
     @Test
-    public void undesirableElementException() throws URISyntaxException, ReactorException {
-        final Set<Module> modules = TestUtils.loadModules(getClass().getResource(
-                "/substatement-validator/undesirable-element").toURI());
-        testLog = output.toString();
-        assertTrue(testLog.contains("TYPE is not valid for REVISION"));
+    public void undesirableElementException() throws URISyntaxException {
+        try {
+            TestUtils.loadModules(getClass().getResource("/substatement-validator/undesirable-element").toURI());
+            fail("Unexpected success");
+        } catch (ReactorException ex) {
+            assertNotNull(ex.getCause());
+            assertTrue(ex.getCause().getMessage().contains("TYPE is not valid for REVISION"));
+        }
     }
 
     @Test
     public void maximalElementCountException() throws URISyntaxException, ReactorException {
-        final Set<Module> modules = TestUtils.loadModules(getClass().getResource(
-                "/substatement-validator/maximal-element").toURI());
-        testLog = output.toString();
-        assertTrue(testLog.contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
+        try {
+            TestUtils.loadModules(getClass().getResource("/substatement-validator/maximal-element").toURI());
+            fail("Unexpected success");
+        } catch (ReactorException ex) {
+            assertNotNull(ex.getCause());
+            assertTrue(ex.getCause().getMessage().contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
+        }
     }
 
     @Test
     public void missingElementException() throws URISyntaxException, ReactorException {
         expectedEx.expect(VerifyException.class);
 
-        final Set<Module> modules = TestUtils.loadModules(getClass().getResource(
-                "/substatement-validator/missing-element").toURI());
+        TestUtils.loadModules(getClass().getResource("/substatement-validator/missing-element").toURI());
     }
 
     @Test
@@ -85,9 +92,7 @@ public class SubstatementValidatorTest {
 
     @Test
     public void bug4310test() throws URISyntaxException, ReactorException {
-        expectedEx.expect(IllegalArgumentException.class);
-
-        final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/substatement-validator/bug-4310")
-                .toURI());
+        expectedEx.expect(SomeModifiersUnresolvedException.class);
+        TestUtils.loadModules(getClass().getResource("/substatement-validator/bug-4310").toURI());
     }
 }
\ No newline at end of file
index 6330790c51010c3ef6d29e8b710f5034f316f2cc..2b1143a0b74c0ca38d252702b82db1d0f4835a4a 100644 (file)
@@ -197,10 +197,8 @@ public class YangParserNegativeTest {
                 fail("SourceException should be thrown");
             }
         } catch (final ReactorException e) {
-            final String expected = "Error in module 'typedef': cannot add '(urn:simple.container" +
-                    ".demo?revision=1970-01-01)int-ext'. Node name collision: '(urn:simple.container" +
-                    ".demo?revision=1970-01-01)int-ext' already declared.";
-            assertTrue(e.getCause().getMessage().startsWith(expected));
+            assertTrue(e.getCause().getMessage().startsWith(
+                "Duplicate name for typedef (urn:simple.container.demo?revision=1970-01-01)int-ext [at"));
         }
     }