Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 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; |
| 18 | |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeMatcher; |
| 21 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 22 | |
| 23 | /** |
| 24 | * ExpectedException framework can use the Hamcrest matcher's to test |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 25 | * custom/extended exceptions. This class extends the type safe matcher to |
| 26 | * define the custom exception matcher. |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 27 | */ |
| 28 | public final class CustomExceptionMatcher extends TypeSafeMatcher<ParserException> { |
| 29 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 30 | private int actualLine; |
| 31 | private final int expectedLine; |
| 32 | private int actualCharPosition; |
| 33 | private final int expectedCharPosition; |
| 34 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 35 | /** |
| 36 | * Customized exception matcher to match error location. |
| 37 | * |
| 38 | * @param line error line |
| 39 | * @param charPosition error character position |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 40 | * @return customized exception matcher to match error location |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 41 | */ |
| 42 | public static CustomExceptionMatcher errorLocation(int line, int charPosition) { |
| 43 | return new CustomExceptionMatcher(line, charPosition); |
| 44 | } |
| 45 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 46 | private CustomExceptionMatcher(int expectedLine, int expectedCharPosition) { |
| 47 | this.expectedLine = expectedLine; |
| 48 | this.expectedCharPosition = expectedCharPosition; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | protected boolean matchesSafely(final ParserException exception) { |
| 53 | actualLine = exception.getLineNumber(); |
| 54 | actualCharPosition = exception.getCharPositionInLine(); |
| 55 | return ((actualLine == expectedLine) && (actualCharPosition == expectedCharPosition)); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void describeTo(Description description) { |
| 60 | description.appendText(" Error reported location ") |
| 61 | .appendText("Line " + actualLine + ", " + "CharPosition " + actualCharPosition) |
| 62 | .appendText(" instead of expected ") |
| 63 | .appendText("Line " + expectedLine + ", " + "CharPosition " + expectedCharPosition); |
| 64 | } |
| 65 | } |