Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.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.Rule; |
| 24 | import org.junit.Test; |
| 25 | import org.junit.rules.ExpectedException; |
| 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.CustomExceptionMatcher; |
| 30 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 31 | import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener; |
| 32 | |
| 33 | import java.io.BufferedWriter; |
| 34 | import java.io.File; |
| 35 | import java.io.IOException; |
| 36 | |
| 37 | /** |
| 38 | * Test case for testing parse tree error listener. |
| 39 | */ |
| 40 | public class ParseTreeErrorListenerTest { |
| 41 | |
| 42 | YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 43 | File file; |
| 44 | BufferedWriter out; |
| 45 | |
| 46 | @Rule |
| 47 | public ExpectedException thrown = ExpectedException.none(); |
| 48 | |
| 49 | /** |
| 50 | * Checks that no exception is generated for YANG file with valid syntax. |
| 51 | */ |
| 52 | @Test |
| 53 | public void checkValidYangFileForNoSyntaxError() throws IOException { |
| 54 | |
| 55 | ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithoutSyntaxError.yang"); |
| 56 | |
| 57 | // Create a lexer that feeds off of input char stream. |
| 58 | GeneratedYangLexer lexer = new GeneratedYangLexer(input); |
| 59 | // Create a buffer of tokens pulled from the lexer. |
| 60 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 61 | // Create a parser that feeds off the tokens buffer. |
| 62 | GeneratedYangParser parser = new GeneratedYangParser(tokens); |
| 63 | // Remove console error listener. |
| 64 | parser.removeErrorListeners(); |
| 65 | // Create instance of customized error listener. |
| 66 | ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); |
| 67 | // Add customized error listener to catch errors during parsing. |
| 68 | parser.addErrorListener(parseTreeErrorListener); |
| 69 | // Begin parsing YANG file and generate parse tree. |
| 70 | ParseTree tree = parser.yangfile(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Checks that exception is generated for YANG file with invalid syntax. |
| 75 | */ |
| 76 | @Test |
| 77 | public void checkInvalidYangFileForSyntaxError() throws IOException { |
| 78 | |
| 79 | // Get the exception occurred during parsing. |
| 80 | thrown.expect(ParserException.class); |
| 81 | thrown.expect(CustomExceptionMatcher.errorLocation(3, 0)); |
| 82 | thrown.expectMessage("no viable alternative at input 'yang-version 1\\nnamespace'"); |
| 83 | |
| 84 | ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithSyntaxError.yang"); |
| 85 | |
| 86 | // Create a lexer that feeds off of input char stream. |
| 87 | GeneratedYangLexer lexer = new GeneratedYangLexer(input); |
| 88 | // Create a buffer of tokens pulled from the lexer. |
| 89 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 90 | // Create a parser that feeds off the tokens buffer. |
| 91 | GeneratedYangParser parser = new GeneratedYangParser(tokens); |
| 92 | // Remove console error listener. |
| 93 | parser.removeErrorListeners(); |
| 94 | // Create instance of customized error listener. |
| 95 | ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); |
| 96 | // Add customized error listener to catch errors during parsing. |
| 97 | parser.addErrorListener(parseTreeErrorListener); |
| 98 | // Begin parsing YANG file and generate parse tree. |
| 99 | ParseTree tree = parser.yangfile(); |
| 100 | } |
| 101 | } |