Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.parser.impl.parseutils; |
| 18 | |
| 19 | import org.antlr.v4.runtime.ANTLRFileStream; |
| 20 | import org.antlr.v4.runtime.ANTLRInputStream; |
| 21 | import org.antlr.v4.runtime.CommonTokenStream; |
| 22 | import org.antlr.v4.runtime.tree.ParseTree; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer; |
| 27 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 28 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 29 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 30 | import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener; |
| 31 | |
| 32 | import java.io.BufferedWriter; |
| 33 | import java.io.File; |
| 34 | import java.io.FileWriter; |
| 35 | import java.io.IOException; |
| 36 | |
| 37 | import static org.hamcrest.core.Is.is; |
| 38 | import static org.junit.Assert.assertThat; |
| 39 | |
| 40 | /** |
| 41 | * Test case for testing parse tree error listener. |
| 42 | */ |
| 43 | public class ParseTreeErrorListenerTest { |
| 44 | |
| 45 | YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 46 | File file; |
| 47 | BufferedWriter out; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() throws Exception { |
| 51 | file = new File("demo.yang"); |
| 52 | out = new BufferedWriter(new FileWriter(file)); |
| 53 | } |
| 54 | @After |
| 55 | public void tearDown() throws Exception { |
| 56 | file.delete(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * This test case checks whether the error received from parser is correctly |
| 61 | * handled. |
| 62 | */ |
| 63 | @Test |
| 64 | public void syntaxErrorValidationTest() throws IOException { |
| 65 | |
| 66 | out.write("module ONOS {\n"); |
| 67 | out.write("yang-version 1\n"); |
| 68 | out.write("namespace urn:ietf:params:xml:ns:yang:ietf-ospf;\n"); |
| 69 | out.write("prefix On;\n"); |
| 70 | out.write("}\n"); |
| 71 | out.close(); |
| 72 | |
| 73 | ANTLRInputStream input = new ANTLRFileStream("demo.yang"); |
| 74 | |
| 75 | // Create a lexer that feeds off of input char stream. |
| 76 | GeneratedYangLexer lexer = new GeneratedYangLexer(input); |
| 77 | // Create a buffer of tokens pulled from the lexer. |
| 78 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 79 | // Create a parser that feeds off the tokens buffer. |
| 80 | GeneratedYangParser parser = new GeneratedYangParser(tokens); |
| 81 | // Remove console error listener. |
| 82 | parser.removeErrorListeners(); |
| 83 | // Create instance of customized error listener. |
| 84 | ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); |
| 85 | // Add customized error listener to catch errors during parsing. |
| 86 | parser.addErrorListener(parseTreeErrorListener); |
| 87 | // Begin parsing YANG file and generate parse tree. |
| 88 | ParseTree tree = parser.yangfile(); |
| 89 | // Get the exception occurred during parsing. |
| 90 | ParserException parserException = parseTreeErrorListener.getParserException(); |
| 91 | |
| 92 | /** |
| 93 | * Check for the values set in syntax error function. If not set properly |
| 94 | * report an assert. |
| 95 | */ |
| 96 | assertThat(parseTreeErrorListener.isExceptionFlag(), is(true)); |
| 97 | assertThat(parserException.getLineNumber(), is(3)); |
| 98 | assertThat(parserException.getCharPositionInLine(), is(0)); |
| 99 | } |
| 100 | } |