blob: 44d0c67c3ad493361592c08e61b58518f05a12ee [file] [log] [blame]
Gaurav Agrawala04483c2016-02-13 14:23:40 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawala04483c2016-02-13 14:23:40 +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
17package org.onosproject.yangutils.parser.impl;
18
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeMatcher;
21import org.onosproject.yangutils.parser.exceptions.ParserException;
22
23/**
24 * ExpectedException framework can use the Hamcrest matcher's to test
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025 * custom/extended exceptions. This class extends the type safe matcher to
26 * define the custom exception matcher.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053027 */
28public final class CustomExceptionMatcher extends TypeSafeMatcher<ParserException> {
29
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053030 private int actualLine;
31 private final int expectedLine;
32 private int actualCharPosition;
33 private final int expectedCharPosition;
34
Gaurav Agrawala04483c2016-02-13 14:23:40 +053035 /**
36 * Customized exception matcher to match error location.
37 *
38 * @param line error line
39 * @param charPosition error character position
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053040 * @return customized exception matcher to match error location
Gaurav Agrawala04483c2016-02-13 14:23:40 +053041 */
42 public static CustomExceptionMatcher errorLocation(int line, int charPosition) {
43 return new CustomExceptionMatcher(line, charPosition);
44 }
45
Gaurav Agrawala04483c2016-02-13 14:23:40 +053046 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}