blob: 4489e77f60a3848607ff8156a0a2fe73f4a00faa [file] [log] [blame]
Gaurav Agrawal88897632016-02-12 18:37:50 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal88897632016-02-12 18:37:50 +05303 *
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
Bharat saraswalef2e6392016-04-19 19:50:32 +053017package org.onosproject.yangutils.parser.impl.parseutils;
Gaurav Agrawal88897632016-02-12 18:37:50 +053018
19import org.antlr.v4.runtime.ANTLRFileStream;
20import org.antlr.v4.runtime.ANTLRInputStream;
21import org.antlr.v4.runtime.CommonTokenStream;
22import org.antlr.v4.runtime.tree.ParseTree;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.junit.Rule;
Gaurav Agrawal88897632016-02-12 18:37:50 +053024import org.junit.Test;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053025import org.junit.rules.ExpectedException;
Gaurav Agrawal88897632016-02-12 18:37:50 +053026import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer;
27import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
28import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053029import org.onosproject.yangutils.parser.impl.CustomExceptionMatcher;
Gaurav Agrawal88897632016-02-12 18:37:50 +053030import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
31import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener;
32
33import java.io.BufferedWriter;
34import java.io.File;
Gaurav Agrawal88897632016-02-12 18:37:50 +053035import java.io.IOException;
36
Gaurav Agrawal88897632016-02-12 18:37:50 +053037/**
38 * Test case for testing parse tree error listener.
39 */
40public class ParseTreeErrorListenerTest {
41
42 YangUtilsParserManager manager = new YangUtilsParserManager();
43 File file;
44 BufferedWriter out;
45
Gaurav Agrawala04483c2016-02-13 14:23:40 +053046 @Rule
47 public ExpectedException thrown = ExpectedException.none();
Gaurav Agrawal88897632016-02-12 18:37:50 +053048
49 /**
Gaurav Agrawala04483c2016-02-13 14:23:40 +053050 * Checks that no exception is generated for YANG file with valid syntax.
Gaurav Agrawal88897632016-02-12 18:37:50 +053051 */
52 @Test
Gaurav Agrawala04483c2016-02-13 14:23:40 +053053 public void checkValidYangFileForNoSyntaxError() throws IOException {
Gaurav Agrawal88897632016-02-12 18:37:50 +053054
Gaurav Agrawala04483c2016-02-13 14:23:40 +053055 ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithoutSyntaxError.yang");
Gaurav Agrawal88897632016-02-12 18:37:50 +053056
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();
Gaurav Agrawala04483c2016-02-13 14:23:40 +053071 }
Gaurav Agrawal88897632016-02-12 18:37:50 +053072
Gaurav Agrawala04483c2016-02-13 14:23:40 +053073 /**
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();
Gaurav Agrawal88897632016-02-12 18:37:50 +0530100 }
101}