Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +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.listeners; |
| 18 | |
| 19 | import org.junit.Rule; |
| 20 | import org.junit.Test; |
| 21 | import org.junit.rules.ExpectedException; |
| 22 | import org.onosproject.yangutils.datamodel.YangModule; |
| 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 26 | |
| 27 | /** |
| 28 | * Test cases for testing base rule listener functionality. |
| 29 | */ |
| 30 | public class BaseFileListenerTest { |
| 31 | |
| 32 | @Rule |
| 33 | public ExpectedException thrown = ExpectedException.none(); |
| 34 | |
| 35 | /** |
| 36 | * Checks for exception if stack of parsable data is not empty at the entry |
| 37 | * of yang base rule. |
| 38 | */ |
| 39 | @Test |
| 40 | public void processYangFileEntryNonEmptyStack() { |
| 41 | thrown.expect(ParserException.class); |
| 42 | thrown.expectMessage("Internal parser error detected: Invalid holder for yangbase before processing."); |
| 43 | |
| 44 | YangModule tmpModule = new YangModule(); |
| 45 | TreeWalkListener listener = new TreeWalkListener(); |
| 46 | listener.getParsedDataStack().push(tmpModule); |
| 47 | GeneratedYangParser.YangfileContext ctx = null; |
| 48 | BaseFileListener.processYangFileEntry(listener, ctx); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Checks that exception shouldn't be generated if stack of parsable data is |
| 53 | * empty at the entry of yang base rule. |
| 54 | */ |
| 55 | @Test |
| 56 | public void processYangFileEntryEmptyStack() { |
| 57 | |
| 58 | TreeWalkListener listener = new TreeWalkListener(); |
| 59 | GeneratedYangParser.YangfileContext ctx = null; |
| 60 | BaseFileListener.processYangFileEntry(listener, ctx); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Checks that exception should be generated if stack of parsable data is |
| 65 | * not empty at the exit of yang base rule. |
| 66 | */ |
| 67 | @Test |
| 68 | public void processYangFileExitEmptyStack() { |
| 69 | thrown.expect(ParserException.class); |
| 70 | thrown.expectMessage("Internal parser error detected: Missing holder at yangbase after processing."); |
| 71 | |
| 72 | TreeWalkListener listener = new TreeWalkListener(); |
| 73 | GeneratedYangParser.YangfileContext ctx = null; |
| 74 | BaseFileListener.processYangFileExit(listener, ctx); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks that exception shouldn't be generated if stack of parsable data is |
| 79 | * empty at the exit of yang base rule. |
| 80 | */ |
| 81 | @Test |
| 82 | public void processYangFileExitNonEmptyStack() { |
| 83 | |
| 84 | TreeWalkListener listener = new TreeWalkListener(); |
| 85 | GeneratedYangParser.YangfileContext ctx = null; |
| 86 | BaseFileListener.processYangFileEntry(listener, ctx); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Checks that after popping out the parsable node from stack it should be |
| 91 | * empty. |
| 92 | */ |
| 93 | @Test |
| 94 | public void processYangFileExitStackErrorExtraEntryTest() { |
| 95 | thrown.expect(ParserException.class); |
| 96 | thrown.expectMessage("Internal parser error detected: Invalid holder for yangbase after processing."); |
| 97 | |
| 98 | YangModule tmpModule = new YangModule(); |
| 99 | YangModule tmpModule2 = new YangModule(); |
| 100 | TreeWalkListener listener = new TreeWalkListener(); |
| 101 | listener.getParsedDataStack().push(tmpModule); |
| 102 | listener.getParsedDataStack().push(tmpModule2); |
| 103 | GeneratedYangParser.YangfileContext ctx = null; |
| 104 | BaseFileListener.processYangFileExit(listener, ctx); |
| 105 | } |
| 106 | } |