Search in sources :

Example 56 with XQueryService

use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.

the class Base64BinaryTest method castToBase64ThenBackToString.

@Test
public void castToBase64ThenBackToString() throws XMLDBException {
    final String base64String = "QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
    final String query = "let $data := '" + base64String + "' cast as xs:base64Binary return $data cast as xs:string";
    final XQueryService service = (XQueryService) server.getRoot().getService("XQueryService", "1.0");
    final ResourceSet result = service.query(query);
    final String queryResult = (String) result.getResource(0).getContent();
    assertEquals(base64String, queryResult);
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 57 with XQueryService

use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.

the class XQueryTest method globalVars.

@Test
public void globalVars() throws XMLDBException {
    Collection testCollection = getTestCollection();
    Resource doc = testCollection.createResource(MODULE5_NAME, "BinaryResource");
    doc.setContent(module5);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(MODULE6_NAME, "BinaryResource");
    doc.setContent(module6);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    XQueryService service = (XQueryService) testCollection.getService("XPathQueryService", "1.0");
    String query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE5_NAME + "\";\n" + "$foo:bar";
    ResourceSet result = service.query(query);
    assertEquals(result.getSize(), 1);
    assertEquals(result.getResource(0).getContent(), "bar");
    query = "xquery version \"1.0\";\n" + "declare variable $local:a := 'abc';" + "$local:a";
    result = service.query(query);
    assertEquals(result.getSize(), 1);
    assertEquals(result.getResource(0).getContent(), "abc");
    boolean gotException = false;
    try {
        query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE6_NAME + "\";\n" + "$foo:bar";
        result = service.query(query);
    } catch (XMLDBException e) {
        assertTrue("Test should generate err:XQST0049, got: " + e.getMessage(), e.getMessage().indexOf("err:XQST0049") > -1);
        gotException = true;
    }
    assertTrue("Duplicate global variable should generate error", gotException);
    gotException = false;
    try {
        query = "xquery version \"1.0\";\n" + "declare variable $local:a := 'abc';" + "declare variable $local:a := 'abc';" + "$local:a";
        result = service.query(query);
    } catch (XMLDBException e) {
        assertTrue("Test should generate err:XQST0049, got: " + e.getMessage(), e.getMessage().indexOf("err:XQST0049") > -1);
        gotException = true;
    }
    assertTrue("Duplicate global variable should generate error", gotException);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 58 with XQueryService

use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.

the class OptimizerTest method execute.

private void execute(String query, boolean optimize, String message, long expected) throws XMLDBException {
    XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
    if (optimize) {
        query = NAMESPACES + OPTIMIZE + query;
    } else {
        query = NAMESPACES + NO_OPTIMIZE + query;
    }
    ResourceSet result = service.query(query);
    assertEquals(message, expected, result.getSize());
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 59 with XQueryService

use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.

the class OptimizerTest method execute.

private long execute(String query, boolean optimize) throws XMLDBException {
    XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
    if (optimize) {
        query = OPTIMIZE + query;
    } else {
        query = NO_OPTIMIZE + query;
    }
    query = NAMESPACES + query;
    ResourceSet result = service.query(query);
    return result.getSize();
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 60 with XQueryService

use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.

the class UpdateInsertTest method insertNamespacedAttribute.

@Test
public void insertNamespacedAttribute() throws XMLDBException {
    final String docName = "pathNs2.xml";
    final XQueryService service = storeXMLStringAndGetQueryService(docName, "<test/>");
    queryResource(service, docName, "//t[@xml:id]", 0);
    String update = "update insert <t xml:id=\"id1\"/> into /test";
    queryResource(service, docName, update, 0);
    queryResource(service, docName, "//t[@xml:id eq 'id1']", 1);
    queryResource(service, docName, "/test/id('id1')", 1);
    update = "update value //t/@xml:id with 'id2'";
    queryResource(service, docName, update, 0);
    queryResource(service, docName, "//t[@xml:id eq 'id2']", 1);
    queryResource(service, docName, "id('id2', /test)", 1);
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) Test(org.junit.Test)

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