use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method strings.
@Test
public void strings() throws XMLDBException {
final XQueryService service = storeXMLStringAndGetQueryService("strings.xml", strings);
ResourceSet result = queryResource(service, "strings.xml", "substring(/test/string[1], 1, 5)", 1);
assertEquals("Hello", result.getResource(0).getContent().toString());
queryResource(service, "strings.xml", "/test/string[starts-with(string(.), 'Hello')]", 2);
result = queryResource(service, "strings.xml", "count(/test/item/price)", 1, "Query should return an empty set (wrong document)");
assertEquals("0", result.getResource(0).getContent().toString());
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method namespaces.
@Test
public void namespaces() throws XMLDBException {
final XQueryService service = storeXMLStringAndGetQueryService("namespaces.xml", namespaces);
service.setNamespace("t", "http://www.foo.com");
service.setNamespace("c", "http://www.other.com");
ResourceSet result = service.queryResource("namespaces.xml", "//t:section");
assertEquals(1, result.getSize());
result = service.queryResource("namespaces.xml", "/t:test//c:comment");
assertEquals(1, result.getSize());
result = service.queryResource("namespaces.xml", "//c:*");
assertEquals(1, result.getSize());
result = service.queryResource("namespaces.xml", "//*:comment");
assertEquals(1, result.getSize());
result = service.queryResource("namespaces.xml", "namespace-uri(//t:test)");
assertEquals(1, result.getSize());
assertEquals("http://www.foo.com", result.getResource(0).getContent().toString());
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method satisfies.
@Test
public void satisfies() throws XMLDBException {
final XQueryService service = getQueryService();
ResourceSet result = queryAndAssert(service, "every $foo in (1,2,3) satisfies" + " let $bar := 'baz'" + " return false() ", 1, "");
assertEquals("satisfies + FLWR expression allways false 1", "false", result.getResource(0).getContent().toString());
result = queryAndAssert(service, "declare function local:foo() { false() };" + " every $bar in (1,2,3) satisfies" + " local:foo()", 1, "");
assertEquals("satisfies + FLWR expression allways false 2", "false", result.getResource(0).getContent().toString());
String query = "every $x in () satisfies false()";
result = queryAndAssert(service, query, 1, "");
assertEquals(query, "true", result.getResource(0).getContent().toString());
query = "some $x in () satisfies true()";
result = queryAndAssert(service, query, 1, "");
assertEquals(query, "false", result.getResource(0).getContent().toString());
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method convertToBoolean.
@Test
public void convertToBoolean() throws XMLDBException {
final XQueryService service = getQueryService();
ResourceSet result = queryAndAssert(service, "let $doc := <element attribute=''/>" + "return (" + " <true>{boolean(($doc,2,3))}</true> ," + " <true>{boolean(($doc/@*,2,3))}</true> ," + " <true>{boolean(true())}</true> ," + " <true>{boolean('test')}</true> ," + " <true>{boolean(number(1))}</true> ," + " <false>{boolean((0))}</false> ," + " <false>{boolean(false())}</false> ," + " <false>{boolean('')}</false> ," + " <false>{boolean(number(0))}</false> ," + " <false>{boolean(number('NaN'))}</false>" + ")", 10, "");
for (int i = 0; i < 10; i++) {
if (i < 5) {
assertEquals("true " + (i + 1), "<true>true</true>", result.getResource(i).getContent().toString());
} else {
assertEquals("false " + (i + 1), "<false>false</false>", result.getResource(i).getContent().toString());
}
}
boolean exceptionThrown = false;
String message = "";
try {
queryAndAssert(service, "let $doc := <element attribute=''/>" + " return boolean( (1,2,$doc) )", 1, "");
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue("Exception wanted: " + message, exceptionThrown);
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method virtualNodeset_bug_wiki_3.
/**
* Problem in VirtualNodeSet which return 2 attributes because it
* computes every level
* @see http://wiki.exist-db.org/space/XQueryBugs
*/
@Test
public void virtualNodeset_bug_wiki_3() throws XMLDBException {
final String xQuery = "declare option exist:serialize \"method=xml indent=no\"; " + "let $node := (<c id=\"OK\"><b id=\"cool\"/></c>)" + "/descendant::*/attribute::id return <a>{$node}</a>";
final XQueryService service = getQueryService();
final ResourceSet rs = service.query(xQuery);
assertEquals("VirtualNodesetBUG_wiki_3", 1, rs.getSize());
assertEquals("VirtualNodesetBUG_wiki_3", "<a id=\"cool\"/>", rs.getResource(0).getContent().toString());
}
Aggregations