Search in sources :

Example 16 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class XQueryTest method module.

@Test
public void module() throws XMLDBException {
    Resource doc;
    ResourceSet result;
    String query;
    String message;
    Collection testCollection = getTestCollection();
    doc = testCollection.createResource(MODULE1_NAME, "BinaryResource");
    doc.setContent(module1);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(MODULE3_NAME, "BinaryResource");
    doc.setContent(module3);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(MODULE4_NAME, "BinaryResource");
    doc.setContent(module4);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(FATHER_MODULE_NAME, "BinaryResource");
    doc.setContent(fatherModule);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(CHILD1_MODULE_NAME, "BinaryResource");
    doc.setContent(child1Module);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(CHILD2_MODULE_NAME, "BinaryResource");
    doc.setContent(child2Module);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "$blah:param";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "value-1", result.getResource(0).getContent());
    // query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + getUri + "/test/" + MODULE1_NAME + "\";\n" + "(:: redefine variable ::)\n" + "declare variable $blah:param  {\"value-2\"};\n" + "$blah:param";
    // try {
    // message = "";
    // result = service.query(query);
    // } catch (XMLDBException e) {
    // message = e.getMessage();
    // }
    // assertTrue(message.indexOf("XQST0049") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "declare namespace blah2=\"blah\";\n" + "$blah2:param";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "value-1", result.getResource(0).getContent());
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"bla\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "$blah:param";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("does not match namespace URI") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + FATHER_MODULE_NAME + "\";\n" + "$foo:bar, $foo:bar1, $foo:bar2";
    result = service.query(query);
    assertEquals("XQuery: " + query, 3, result.getSize());
    assertEquals("XQuery: " + query, "bar", result.getResource(0).getContent());
    assertEquals("XQuery: " + query, "bar1", result.getResource(1).getContent());
    assertEquals("XQuery: " + query, "bar2", result.getResource(2).getContent());
    // Non-heritance check
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + FATHER_MODULE_NAME + "\";\n" + "declare namespace foo1=\"foo1\"; \n" + "$foo1:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XPDY0002") > -1);
    // Non-heritance check
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + FATHER_MODULE_NAME + "\";\n" + "declare namespace foo2=\"foo2\"; \n" + "$foo2:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XPDY0002") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo1=\"foo\" at \"" + URI + "/test/" + CHILD1_MODULE_NAME + "\";\n" + "import module namespace foo2=\"foo\" at \"" + URI + "/test/" + CHILD1_MODULE_NAME + "\";\n" + "$foo1:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    // Should be a XQST0047 error
    assertTrue(message.indexOf("does not match namespace URI") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE3_NAME + "\";\n" + "$bar:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("No namespace defined for prefix") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE4_NAME + "\";\n" + "foo:bar()";
    try {
        message = "";
        result = service.query(query);
        // WARNING !
        // This result is false ! The external vairable has not been resolved
        // Furthermore it is not in the module's namespace !
        assertEquals("XQuery: " + query, 0, result.getSize());
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
// This is the good result !
// assertTrue(message.indexOf("XQST0048") > -1);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) 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 17 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class XQueryTest method doubleDocNode_2078755.

@Test
public void doubleDocNode_2078755() throws XMLDBException {
    Collection testCollection = getTestCollection();
    Resource doc = testCollection.createResource(MODULE8_NAME, "BinaryResource");
    doc.setContent(module8);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    service.setProperty(OutputKeys.INDENT, "no");
    String query = "import module namespace dr = \"double-root2\" at \"" + URI + "/test/" + MODULE8_NAME + "\";\n" + "let $doc1 := dr:documentIn() \n" + "let $count1 := count($doc1/element()) \n" + "let $doc2 := dr:documentIn() \n" + "let $count2 := count($doc2/element()) \n" + "return ($count1, $count2) \n";
    ResourceSet result = service.query(query);
    assertEquals(2, result.getSize());
    assertEquals("1", result.getResource(0).getContent().toString());
    assertEquals("1", result.getResource(1).getContent().toString());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 18 with EXistResource

use of org.exist.xmldb.EXistResource 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 19 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class XQueryTest method modulesAndNS.

@Test
public void modulesAndNS() throws XMLDBException, IOException, SAXException {
    Collection testCollection = getTestCollection();
    Resource doc = testCollection.createResource(MODULE7_NAME, "BinaryResource");
    doc.setContent(module7);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    service.setProperty(OutputKeys.INDENT, "no");
    String query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE7_NAME + "\";\n" + "<div xmlns='http://www.w3.org/1999/xhtml'>" + "{ foo:link() }" + "</div>";
    ResourceSet result = service.query(query);
    assertEquals(1, result.getSize());
    result.getResource(0).getContent();
    assertXMLEqual("<div xmlns='http://www.w3.org/1999/xhtml'><a xmlns=\"\" href='#'>Link</a></div>", result.getResource(0).getContent().toString());
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE7_NAME + "\";\n" + "<div xmlns='http://www.w3.org/1999/xhtml'>" + "{ foo:copy(<a>Link</a>) }" + "</div>";
    result = service.query(query);
    assertEquals(1, result.getSize());
    result.getResource(0).getContent();
    assertXMLEqual("<div xmlns='http://www.w3.org/1999/xhtml'><a>Link</a></div>", result.getResource(0).getContent().toString());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 20 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class XQueryTest method importExternalClasspathLibraryModule.

@Test
public void importExternalClasspathLibraryModule() throws XMLDBException {
    final long timestamp = System.currentTimeMillis();
    final Collection testCollection = getTestCollection();
    final Resource doc = testCollection.createResource("import-external-classpath.xq", "BinaryResource");
    doc.setContent("import module namespace ext1 = \"http://import-external-classpath-library-module-test.com\" at \"resource:org/exist/xquery/external-classpath-library-module.xqm\";\n" + "ext1:echo(" + timestamp + ")");
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    final EXistXPathQueryService service = (EXistXPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    final ResourceSet resourceSet = service.executeStoredQuery("/db/test/import-external-classpath.xq");
    assertEquals(1, resourceSet.getSize());
    final Resource resource = resourceSet.getResource(0);
    final Source expected = Input.fromString("<echo>" + timestamp + "</echo>").build();
    final Source actual = Input.fromString(resource.getContent().toString()).build();
    final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForIdentical().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) Diff(org.xmlunit.diff.Diff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet) Source(javax.xml.transform.Source)

Aggregations

EXistResource (org.exist.xmldb.EXistResource)42 Collection (org.xmldb.api.base.Collection)20 BinaryResource (org.xmldb.api.modules.BinaryResource)20 Resource (org.xmldb.api.base.Resource)17 XMLResource (org.xmldb.api.modules.XMLResource)17 ResourceSet (org.xmldb.api.base.ResourceSet)12 Path (java.nio.file.Path)11 XMLDBException (org.xmldb.api.base.XMLDBException)10 ExtendedResource (org.exist.xmldb.ExtendedResource)9 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)8 URISyntaxException (java.net.URISyntaxException)6 XPathQueryService (org.xmldb.api.modules.XPathQueryService)6 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)5 UserManagementService (org.exist.xmldb.UserManagementService)5 BeforeClass (org.junit.BeforeClass)5 Test (org.junit.Test)5 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)4 MimeType (org.exist.util.MimeType)4 XPathException (org.exist.xquery.XPathException)4 LocalBinaryResource (org.exist.xmldb.LocalBinaryResource)3