Skip to content

Commit d351838

Browse files
line-oadamretter
authored andcommitted
[test] add utility classes for testing XQuery compilation
- XQueryAsssertions provide methods for verifying compilation and execution results and errors. - XQueryCompilationTest is a base class to build compilation and execution tests by extending it.
1 parent 9c62152 commit d351838

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.EXistException;
26+
import org.exist.security.PermissionDeniedException;
27+
import org.exist.storage.BrokerPool;
28+
import org.exist.storage.DBBroker;
29+
import org.exist.xquery.CompiledXQuery;
30+
import org.exist.xquery.XPathException;
31+
import org.exist.xquery.XQuery;
32+
import org.exist.xquery.XQueryContext;
33+
import org.exist.xquery.value.Sequence;
34+
import org.junit.ClassRule;
35+
36+
import static com.evolvedbinary.j8fu.Either.Left;
37+
import static com.evolvedbinary.j8fu.Either.Right;
38+
39+
/**
40+
* Base class for test suites testing XQuery compilation
41+
* @author <a href="mailto:[email protected]">Juri Leino</a>
42+
*/
43+
public abstract class XQueryCompilationTest {
44+
@ClassRule
45+
public static final ExistEmbeddedServer server = new ExistEmbeddedServer(true, true);
46+
47+
protected static Either<XPathException, CompiledXQuery> compileQuery(final String string) throws EXistException, PermissionDeniedException {
48+
final BrokerPool pool = server.getBrokerPool();
49+
final XQuery xqueryService = pool.getXQueryService();
50+
try (final DBBroker broker = pool.getBroker()) {
51+
try {
52+
return Right(xqueryService.compile(broker, new XQueryContext(broker.getDatabase()), string));
53+
} catch (final XPathException e) {
54+
return Left(e);
55+
}
56+
}
57+
}
58+
59+
protected static Either<XPathException, Sequence> executeQuery(final String string) throws EXistException, PermissionDeniedException {
60+
final BrokerPool pool = server.getBrokerPool();
61+
final XQuery xqueryService = pool.getXQueryService();
62+
try (final DBBroker broker = pool.getBroker()) {
63+
try {
64+
return Right(xqueryService.execute(broker, string, null));
65+
} catch (final XPathException e) {
66+
return Left(e);
67+
}
68+
}
69+
}
70+
71+
}

0 commit comments

Comments
 (0)