Skip to content

Commit b7cf79d

Browse files
committed
[test] refactor FunctionTypeInElementContentTest
- extend XQueryCompilationTest - assert dynamic and static errors with code, location and message - assert execution result - run all tests, none ignored - adapt tests to match actual behaviour
1 parent cfeab82 commit b7cf79d

File tree

1 file changed

+44
-68
lines changed

1 file changed

+44
-68
lines changed

exist-core/src/test/java/org/exist/xquery/FunctionTypeInElementContentTest.java

Lines changed: 44 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,99 +21,93 @@
2121
*/
2222
package org.exist.xquery;
2323

24-
import org.exist.test.ExistXmldbEmbeddedServer;
25-
import org.junit.ClassRule;
26-
import org.junit.Ignore;
24+
import org.exist.EXistException;
25+
import org.exist.security.PermissionDeniedException;
26+
import org.exist.test.XQueryCompilationTest;
2727
import org.junit.Test;
28-
import org.xmldb.api.base.XMLDBException;
29-
import org.xmldb.api.modules.XQueryService;
3028

31-
import static org.junit.Assert.*;
29+
import static org.exist.test.DiffMatcher.elemSource;
30+
import static org.exist.test.XQueryAssertions.*;
3231

3332
/**
3433
* Ensure function types returned in element content throws at compile time and has location information
3534
* https://github.com/eXist-db/exist/issues/3474
3635
*
3736
* @author <a href="mailto:[email protected]">Juri Leino</a>
3837
*/
39-
public class FunctionTypeInElementContentTest {
40-
@ClassRule
41-
public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(true, true, true);
38+
public class FunctionTypeInElementContentTest extends XQueryCompilationTest {
4239

4340
@Test
44-
public void arrayLiteral() throws XMLDBException {
41+
public void arrayLiteral() throws EXistException, PermissionDeniedException {
4542
final String query = "element test { [] }";
46-
assertCompilationSuccess(query);
43+
assertXQResultSimilar(elemSource("<test/>"), executeQuery(query));
4744
}
4845

4946
// TODO: array content could be removed after https://github.com/eXist-db/exist/issues/3472 is fixed
5047
@Test
51-
public void arrayConstructor() throws XMLDBException {
48+
public void arrayConstructor() throws EXistException, PermissionDeniedException {
5249
final String query = "element test { array { () } }";
53-
assertCompilationSuccess(query);
50+
assertXQResultSimilar(elemSource("<test/>"), executeQuery(query));
5451
}
5552

5653
@Test
57-
public void sequenceOfItems() throws XMLDBException {
54+
public void sequenceOfItems() throws EXistException, PermissionDeniedException {
5855
final String query = "element test { (1, map {})[1] }";
59-
assertCompilationSuccess(query);
56+
assertXQResultSimilar(elemSource("<test>1</test>"), executeQuery(query));
6057
}
6158

6259
@Test
63-
public void partialBuiltIn() throws XMLDBException {
60+
public void partialBuiltIn() throws EXistException, PermissionDeniedException {
6461
final String query = "element test { sum(?) }";
65-
final String error = "err:XQTY0105 Function types are not allowed in element content. Got function(*) [at line 1, column 16, source: element test { sum(?) }]";
66-
assertCompilationError(query, error);
62+
final String error = "Function types are not allowed in element content. Got function(*)";
63+
assertXQStaticError(ErrorCodes.XQTY0105, 1, 16, error, compileQuery(query));
6764
}
6865

69-
// TODO: Investigate does still throw without location info
70-
@Ignore
66+
// TODO: Does still throw without location info
7167
@Test
72-
public void functionReference() throws XMLDBException {
68+
public void functionReference() throws EXistException, PermissionDeniedException {
7369
final String query = "element test { sum#0 }";
74-
final String error = "err:XQTY0105 Function types are not allowed in element content. Got function(*) [at line 1, column 16, source: element test { sum#0 }]";
75-
assertCompilationError(query, error);
70+
final String error = "Function types are not allowed in element content. Got function(*)";
71+
assertXQStaticError(ErrorCodes.XQTY0105, 0, 0, error, compileQuery(query));
7672
}
7773

78-
// Does not throw at compile time
79-
@Ignore
74+
// TODO: Does not throw at compile time
8075
@Test
81-
public void functionVariable() throws XMLDBException {
76+
public void functionVariable() throws EXistException, PermissionDeniedException {
8277
final String query = "let $f := function () {} return element test { $f }";
83-
final String error = "err:XQTY0105 Function types are not allowed in element content. Got function(*) [at line 1, column 16, source: element test { sum(?) }]";
84-
assertCompilationError(query, error);
78+
final String error = "Enclosed expression contains function item";
79+
assertXQDynamicError(ErrorCodes.XQTY0105, 1, 49, error, executeQuery(query));
8580
}
8681

82+
// TODO: user defined function has its location offset to a weird location
8783
@Test
88-
public void userDefinedFunction() throws XMLDBException {
84+
public void userDefinedFunction() throws EXistException, PermissionDeniedException {
8985
final String query = "element test { function () {} }";
90-
// TODO: user defined function has its location offset to a weird location
91-
final String error = "err:XQTY0105 Function types are not allowed in element content. Got function(*) [at line 1, column 25, source: element test { function () { () } }]";
92-
assertCompilationError(query, error);
86+
final String error = "Function types are not allowed in element content. Got function(*)";
87+
assertXQStaticError(ErrorCodes.XQTY0105, 1, 25, error, compileQuery(query));
9388
}
9489

9590
@Test
96-
public void mapConstructor() throws XMLDBException {
91+
public void mapConstructor() throws EXistException, PermissionDeniedException {
9792
final String query = "element test { map {} }";
98-
final String error = "err:XQTY0105 Function types are not allowed in element content. Got map(*) [at line 1, column 16, source: element test { map {} }]";
99-
assertCompilationError(query, error);
93+
final String error = "Function types are not allowed in element content. Got map(*)";
94+
assertXQStaticError(ErrorCodes.XQTY0105, 1, 16, error, compileQuery(query));
10095
}
10196

10297
@Test
103-
public void mapConstructorLookup() throws XMLDBException {
98+
public void mapConstructorLookup() throws EXistException, PermissionDeniedException {
10499
final String query = "element test { map {1:1}?1 }";
105-
assertCompilationSuccess(query);
100+
assertXQResultSimilar(elemSource("<test>1</test>"), executeQuery(query));
106101
}
107102

108103
/**
109104
* sequence in enclosed expression with only a function type
110105
*/
111-
@Ignore
112106
@Test
113-
public void sequenceOfMaps() throws XMLDBException {
107+
public void sequenceOfMaps() throws EXistException, PermissionDeniedException {
114108
final String query = "element test { (map {}) }";
115-
final String error = "An exception occurred during query execution: err:XQTY0105 Function types are not allowed in element content. Got map(*) [source: element foo { (map{}) }]";
116-
assertCompilationError(query, error);
109+
final String error = "Function types are not allowed in element content. Got map(*)";
110+
assertXQDynamicError(ErrorCodes.XQTY0105, 0, 0, error, executeQuery(query));
117111
}
118112

119113
/**
@@ -122,45 +116,27 @@ public void sequenceOfMaps() throws XMLDBException {
122116
*/
123117
// TODO: add (sub-expression) location
124118
@Test
125-
public void sequenceOfMapsEdgeCase() throws XMLDBException {
119+
public void sequenceOfMapsEdgeCase() throws EXistException, PermissionDeniedException {
126120
final String query = "element test { (map {})[2] }";
127-
final String error = "err:XQTY0105 Function types are not allowed in element content. Got map(*) [source: element test { (map {})[2] }]";
128-
assertCompilationError(query, error);
121+
final String error = "Function types are not allowed in element content. Got map(*)";
122+
assertXQStaticError(ErrorCodes.XQTY0105, 0, 0, error, compileQuery(query));
129123
}
130124

131125
// TODO: add (sub-expression) location
132126
// TODO: this could throw at compile time
133127
@Test
134-
public void ArrayOfMaps() throws XMLDBException {
128+
public void arrayOfMaps() throws EXistException, PermissionDeniedException {
135129
final String query = "element test { [map {}] }";
136-
final String error = "An exception occurred during query execution: err:XQTY0105 Function types are not allowed in element content. Got map(*) [source: element test { [map{}] }]";
137-
assertCompilationError(query, error);
130+
final String error = "Enclosed expression contains function item";
131+
assertXQDynamicError(ErrorCodes.XQTY0105, 1, 16, error, executeQuery(query));
138132
};
139133

140134
// TODO: add (sub-expression) location
141135
// TODO: This should throw at compile time, but does not
142-
@Ignore
143136
@Test
144-
public void mapConstructorInSubExpression() throws XMLDBException {
137+
public void mapConstructorInSubExpression() throws EXistException, PermissionDeniedException {
145138
final String query = "element test { \"a\", map {} }";
146-
final String error = "err:XQTY0105 Function types are not allowed in element content. Got map(*) [at line 1, column 20, source: element test { map {} }]";
147-
assertCompilationError(query, error);
148-
}
149-
150-
private void assertCompilationError(final String query, final String error) throws XMLDBException {
151-
final XQueryService service = (XQueryService)existEmbeddedServer.getRoot().getService("XQueryService", "1.0");
152-
153-
try {
154-
service.compile(query);
155-
fail("no XMLDBException was thrown during compilation.");
156-
} catch (XMLDBException ex) {
157-
assertEquals( error, ex.getMessage() );
158-
}
159-
}
160-
private void assertCompilationSuccess(final String query) throws XMLDBException {
161-
final XQueryService service = (XQueryService)existEmbeddedServer.getRoot().getService("XQueryService", "1.0");
162-
163-
service.compile(query);
164-
assertTrue( true );
139+
final String error = "Enclosed expression contains function item";
140+
assertXQDynamicError(ErrorCodes.XQTY0105, 0, 0, error, executeQuery(query));
165141
}
166142
}

0 commit comments

Comments
 (0)