|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.test; |
| 23 | + |
| 24 | +import com.evolvedbinary.j8fu.Either; |
| 25 | +import org.exist.xquery.CompiledXQuery; |
| 26 | +import org.exist.xquery.ErrorCodes; |
| 27 | +import org.exist.xquery.XPathException; |
| 28 | +import org.exist.xquery.value.Sequence; |
| 29 | + |
| 30 | +import static org.junit.Assert.*; |
| 31 | + |
| 32 | +/** |
| 33 | + * This set of assertions are meant to help when testing XQuery compilation, execution and errors |
| 34 | + * Especially useful, if the testsuite inherits from/ extends XQueryCompilationTest class |
| 35 | + * @author <a href="mailto:[email protected]">Juri Leino</a> |
| 36 | + */ |
| 37 | +public class XQueryAssertions { |
| 38 | + public static void assertXQStaticError(final ErrorCodes.ErrorCode expectedCode, final int line, final int column, final String expectedMessage, final Either<XPathException, CompiledXQuery> actual) { |
| 39 | + assertXQStaticError(expectedCode, line, column, actual); |
| 40 | + assertXQErrorMessage(expectedMessage, actual.left().get()); |
| 41 | + } |
| 42 | + |
| 43 | + public static void assertXQStaticError(final ErrorCodes.ErrorCode expectedCode, final int line, final int column, final Either<XPathException, CompiledXQuery> actual) { |
| 44 | + assertTrue("Expected static error: " + expectedCode.getErrorQName() + ", but no error was thrown.", actual.isLeft()); |
| 45 | + final XPathException xpe = actual.left().get(); |
| 46 | + assertXQErrorCode(expectedCode, xpe); |
| 47 | + assertXQErrorLine(line, xpe); |
| 48 | + assertXQErrorColumn(column, xpe); |
| 49 | + } |
| 50 | + |
| 51 | + public static void assertXQDynamicError(final ErrorCodes.ErrorCode expectedCode, final int line, final int column, final String expectedMessage, final Either<XPathException, Sequence> actual) { |
| 52 | + assertXQDynamicError(expectedCode, line, column, actual); |
| 53 | + assertXQErrorMessage(expectedMessage, actual.left().get()); |
| 54 | + } |
| 55 | + |
| 56 | + public static void assertXQDynamicError(final ErrorCodes.ErrorCode expectedCode, final int line, final int column, final Either<XPathException, Sequence> actual) { |
| 57 | + assertTrue("Expected dynamic error: " + expectedCode.getErrorQName() + ", but no error was thrown.", actual.isLeft()); |
| 58 | + final XPathException xpe = actual.left().get(); |
| 59 | + assertXQErrorLine(line, xpe); |
| 60 | + assertXQErrorColumn(column, xpe); |
| 61 | + assertXQErrorCode(expectedCode, xpe); |
| 62 | + } |
| 63 | + |
| 64 | + public static void assertXQResultToStringEquals(final String expectedSerializedString, final Either<XPathException, Sequence> actual) { |
| 65 | + assertTrue("Expected: " + expectedSerializedString + ", but got result: " + actual.toString(), actual.isRight()); |
| 66 | + final Sequence sequence = actual.right().get(); |
| 67 | + assertEquals(expectedSerializedString, sequence.toString()); |
| 68 | + } |
| 69 | + |
| 70 | + public static void assertXQErrorCode(final ErrorCodes.ErrorCode expectedCode, final XPathException exception) { |
| 71 | + assertEquals("Expected: " + expectedCode.getErrorQName() + ", but got: " + exception.getErrorCode().getErrorQName(), |
| 72 | + expectedCode, exception.getErrorCode()); |
| 73 | + } |
| 74 | + |
| 75 | + public static void assertXQErrorLine(final int expectedLine, final XPathException exception) { |
| 76 | + assertEquals("Expected line to be " + expectedLine + ", but got " + exception.getLine(), |
| 77 | + expectedLine, exception.getLine()); |
| 78 | + } |
| 79 | + |
| 80 | + public static void assertXQErrorColumn(final int expectedColumn, final XPathException exception) { |
| 81 | + assertEquals("Expected column to be " + expectedColumn + ", but got " + exception.getColumn(), |
| 82 | + expectedColumn, exception.getColumn()); |
| 83 | + } |
| 84 | + |
| 85 | + public static void assertXQErrorMessage(final String expectedMessage, final XPathException exception) { |
| 86 | + assertEquals("Expected message to be " + expectedMessage + ", but got " + exception.getMessage(), |
| 87 | + expectedMessage, exception.getDetailMessage()); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments