use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method ancestorAxis.
@Test
public void ancestorAxis() throws XMLDBException {
final XQueryService service = storeXMLStringAndGetQueryService("nested3.xml", nested3);
// test ancestor axis with positional predicate
queryResource(service, "nested3.xml", "//a[ancestor::a[2]/t = '1']", 1);
queryResource(service, "nested3.xml", "//a[ancestor::*[2]/t = '1']", 1);
queryResource(service, "nested3.xml", "//a[ancestor::a[1]/t = '2']", 1);
queryResource(service, "nested3.xml", "//a[ancestor::*[1]/t = '2']", 1);
queryResource(service, "nested3.xml", "//a[ancestor-or-self::*[3]/t = '1']", 1);
queryResource(service, "nested3.xml", "//a[ancestor-or-self::a[3]/t = '1']", 1);
// Following test fails
// queryResource(service, "nested3.xml", "//a[ancestor-or-self::*[2]/t = '2']", 1);
queryResource(service, "nested3.xml", "//a[ancestor-or-self::a[2]/t = '2']", 1);
queryResource(service, "nested3.xml", "//a[t = '3'][ancestor-or-self::a[3]/t = '1']", 1);
queryResource(service, "nested3.xml", "//a[t = '3'][ancestor-or-self::*[3]/t = '1']", 1);
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method booleans.
@Test
public void booleans() throws XMLDBException {
final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
ResourceSet result = queryResource(service, "numbers.xml", "boolean(1.0)", 1);
assertEquals("boolean value of 1.0 should be true", "true", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(0.0)", 1);
assertEquals("boolean value of 0.0 should be false", "false", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(xs:double(0.0))", 1);
assertEquals("boolean value of double 0.0 should be false", "false", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(xs:double(1.0))", 1);
assertEquals("boolean value of double 1.0 should be true", "true", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(xs:float(1.0))", 1);
assertEquals("boolean value of float 1.0 should be true", "true", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(xs:float(0.0))", 1);
assertEquals("boolean value of float 0.0 should be false", "false", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(xs:integer(0))", 1);
assertEquals("boolean value of integer 0 should be false", "false", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(xs:integer(1))", 1);
assertEquals("boolean value of integer 1 should be true", "true", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "'true' cast as xs:boolean", 1);
assertEquals("boolean value of 'true' cast to xs:boolean should be true", "true", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "'false' cast as xs:boolean", 1);
assertEquals("boolean value of 'false' cast to xs:boolean should be false", "false", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean('Hello')", 1);
assertEquals("boolean value of string 'Hello' should be true", "true", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean('')", 1);
assertEquals("boolean value of empty string should be false", "false", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(())", 1);
assertEquals("boolean value of empty sequence should be false", "false", result.getResource(0).getContent().toString());
result = queryResource(service, "numbers.xml", "boolean(('Hello'))", 1);
assertEquals("boolean value of sequence with non-empty string should be true", "true", result.getResource(0).getContent().toString());
// result = queryResource(service, "numbers.xml", "boolean((0.0, 0.0))", 1);
// assertEquals("boolean value of sequence with two elements should be true", "true",
// result.getResource(0).getContent());
result = queryResource(service, "numbers.xml", "boolean(//item[@id = '1']/price)", 1);
assertEquals("boolean value of 5.6 should be true", "true", result.getResource(0).getContent().toString());
String message = "";
try {
queryResource(service, "numbers.xml", "boolean(current-time())", 1);
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("FORG0006") > -1);
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method externalVars.
@Test
public void externalVars() throws XMLDBException {
XQueryService service = storeXMLStringAndGetQueryService("strings.xml", strings);
String query = "declare variable $x external;" + "$x";
CompiledExpression expr = service.compile(query);
// Do not declare the variable...
boolean exceptionThrown = false;
try {
service.execute(expr);
} catch (XMLDBException e) {
exceptionThrown = true;
}
assertTrue("Expected XPTY0002", exceptionThrown);
query = "declare variable $local:string external;" + "/test/string[. = $local:string]";
expr = service.compile(query);
service.declareVariable("local:string", "Hello");
ResourceSet result = service.execute(expr);
final XMLResource r = (XMLResource) result.getResource(0);
Node node = r.getContentAsDOM();
if (node.getNodeType() == Node.DOCUMENT_NODE) {
node = node.getFirstChild();
}
assertEquals("string", node.getNodeName());
// Instanciate a new service to prevent variable reuse
// TODO : consider auto-reset ?
service = storeXMLStringAndGetQueryService("strings.xml", strings);
query = "declare variable $local:string as xs:string external;" + "$local:string";
expr = service.compile(query);
// TODO : we should virtually pass any kind of value
service.declareVariable("local:string", new Integer(1));
String message = "";
try {
service.execute(expr);
} catch (XMLDBException e) {
// e.printStackTrace();
message = e.getMessage();
}
assertTrue(message.indexOf("XPTY0004") > -1);
service = storeXMLStringAndGetQueryService("strings.xml", strings);
query = "declare variable $x as xs:integer external; " + "$x";
expr = service.compile(query);
// TODO : we should virtually pass any kind of value
service.declareVariable("x", "1");
message = "";
try {
service.execute(expr);
} catch (XMLDBException e) {
// e.printStackTrace();
message = e.getMessage();
}
assertTrue(message.indexOf("XPTY0004") > -1);
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method predicate_bug1488303.
/**
* @see http://sourceforge.net/tracker/index.php?func=detail&aid=1488303&group_id=17691&atid=117691
*/
@Test
public void predicate_bug1488303() throws XMLDBException {
XQueryService service = getQueryService();
ResourceSet rs = null;
// test one
final String xQuery1 = "let $q := <q><t>eXist</t></q> return $q//t";
rs = service.query(xQuery1);
assertEquals("nr of results", 1, rs.getSize());
assertEquals("result", "<t>eXist</t>", rs.getResource(0).getContent().toString());
// test two
final String xQuery2 = "let $q := <q><t>eXist</t></q> return ($q//t)[1]";
rs = service.query(xQuery2);
assertEquals("nr of results", 1, rs.getSize());
assertEquals("result", "<t>eXist</t>", rs.getResource(0).getContent().toString());
// This one fails http://sourceforge.net/tracker/index.php?func=detail&aid=1488303&group_id=17691&atid=117691
final String xQuery3 = "let $q := <q><t>eXist</t></q> return $q//t[1]";
rs = service.query(xQuery3);
assertEquals("SFBUG 1488303 nr of results", 1, rs.getSize());
assertEquals("SFBUG 1488303 result", "<t>eXist</t>", rs.getResource(0).getContent().toString());
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method not.
@Test
public void not() throws XMLDBException {
final XQueryService service = storeXMLStringAndGetQueryService("strings.xml", strings);
queryResource(service, "strings.xml", "/test/string[not(@value)]", 2);
ResourceSet result = queryResource(service, "strings.xml", "not(/test/abcd)", 1);
Resource r = result.getResource(0);
assertEquals("true", r.getContent().toString());
result = queryResource(service, "strings.xml", "not(/test)", 1);
r = result.getResource(0);
assertEquals("false", r.getContent().toString());
result = queryResource(service, "strings.xml", "/test/string[not(@id)]", 3);
r = result.getResource(0);
assertEquals("<string>Hello World!</string>", r.getContent().toString());
// test with non-existing items
queryResource(service, "strings.xml", "/blah[not(blah)]", 0);
queryResource(service, "strings.xml", "//*[string][not(@value)]", 1);
queryResource(service, "strings.xml", "//*[string][not(@blah)]", 1);
queryResource(service, "strings.xml", "//*[blah][not(@blah)]", 0);
}
Aggregations