Search in sources :

Example 86 with XQueryService

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);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 87 with XQueryService

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);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 88 with XQueryService

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);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) Node(org.w3c.dom.Node) XMLResource(org.xmldb.api.modules.XMLResource)

Example 89 with XQueryService

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());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 90 with XQueryService

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);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource)

Aggregations

XQueryService (org.xmldb.api.modules.XQueryService)129 EXistXQueryService (org.exist.xmldb.EXistXQueryService)71 XMLResource (org.xmldb.api.modules.XMLResource)33 ResourceSet (org.xmldb.api.base.ResourceSet)29 Test (org.junit.Test)28 Resource (org.xmldb.api.base.Resource)8 Node (org.w3c.dom.Node)6 EXistResource (org.exist.xmldb.EXistResource)5 Document (org.w3c.dom.Document)5 Collection (org.xmldb.api.base.Collection)5 Source (javax.xml.transform.Source)4 IndexQueryService (org.exist.xmldb.IndexQueryService)4 CompiledExpression (org.xmldb.api.base.CompiledExpression)4 XMLDBException (org.xmldb.api.base.XMLDBException)4 Diff (org.xmlunit.diff.Diff)4 Path (java.nio.file.Path)2 XmlRpcTest (org.exist.xmlrpc.XmlRpcTest)2 StringWriter (java.io.StringWriter)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1