Search in sources :

Example 11 with EXistResource

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

the class FilterInputStreamCacheMonitorTest method binaryResult.

@Test
public void binaryResult() throws XMLDBException {
    final FilterInputStreamCacheMonitor monitor = FilterInputStreamCacheMonitor.getInstance();
    // assert no binaries in use yet
    int activeCount = monitor.getActive().size();
    if (activeCount != 0) {
        fail("FilterInputStreamCacheMonitor should have no active binaries, but found: " + activeCount + "." + System.getProperty("line.separator") + monitor.dump());
    }
    ResourceSet resourceSet = null;
    try {
        resourceSet = existXmldbEmbeddedServer.executeQuery("util:binary-doc('/db/" + TEST_COLLECTION_NAME + "/icon.png')");
        assertEquals(1, resourceSet.getSize());
        try (final EXistResource resource = (EXistResource) resourceSet.getResource(0)) {
            assertTrue(resource instanceof LocalBinaryResource);
            assertTrue(((ExtendedResource) resource).getExtendedContent() instanceof BinaryValue);
            // one active binary (as it is in the result set)
            assertEquals(1, monitor.getActive().size());
        }
        // assert no active binaries as we just closed the resource in the try-with-resources
        activeCount = monitor.getActive().size();
        if (activeCount != 0) {
            fail("FilterInputStreamCacheMonitor should again have no active binaries, but found: " + activeCount + "." + System.getProperty("line.separator") + monitor.dump());
        }
    } finally {
        resourceSet.clear();
    }
}
Also used : EXistResource(org.exist.xmldb.EXistResource) LocalBinaryResource(org.exist.xmldb.LocalBinaryResource) ExtendedResource(org.exist.xmldb.ExtendedResource) BinaryValue(org.exist.xquery.value.BinaryValue) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 12 with EXistResource

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

the class FilterInputStreamCacheMonitorTest method setup.

@BeforeClass
public static void setup() throws XMLDBException, URISyntaxException {
    final FilterInputStreamCacheMonitor monitor = FilterInputStreamCacheMonitor.getInstance();
    int activeCount = monitor.getActive().size();
    if (activeCount != 0) {
        LOG.warn("FilterInputStreamCacheMonitor should have no active binaries, but found: {}.{}{}It is likely that a previous test or process within the same JVM is leaking file handles! This should be investigated...", activeCount, System.getProperty("line.separator"), monitor.dump());
    }
    monitor.clear();
    final Path icon = Paths.get(FilterInputStreamCacheMonitorTest.class.getResource("icon.png").toURI());
    final Collection testCollection = existXmldbEmbeddedServer.createCollection(existXmldbEmbeddedServer.getRoot(), TEST_COLLECTION_NAME);
    try (final EXistResource resource = (EXistResource) testCollection.createResource("icon.png", BinaryResource.RESOURCE_TYPE)) {
        resource.setContent(icon);
        testCollection.storeResource(resource);
    }
    testCollection.close();
}
Also used : Path(java.nio.file.Path) EXistResource(org.exist.xmldb.EXistResource) Collection(org.xmldb.api.base.Collection) BeforeClass(org.junit.BeforeClass)

Example 13 with EXistResource

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

the class StoredModuleTest method writeModule.

private void writeModule(Collection collection, String modulename, String module) throws XMLDBException {
    BinaryResource res = (BinaryResource) collection.createResource(modulename, "BinaryResource");
    ((EXistResource) res).setMimeType("application/xquery");
    res.setContent(module.getBytes());
    collection.storeResource(res);
    collection.close();
}
Also used : EXistResource(org.exist.xmldb.EXistResource) BinaryResource(org.xmldb.api.modules.BinaryResource)

Example 14 with EXistResource

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

the class XQueryTest method currentDateTimeInModules_1894009.

@Test
public void currentDateTimeInModules_1894009() throws XMLDBException {
    String module = "module namespace dt = \"dt\";\n" + "\n" + "declare function dt:fib($n) {\n" + "  if ($n < 2) then $n else dt:fib($n - 1) + dt:fib($n - 2) \n" + "};\n" + "\n" + "declare function dt:dateTime() {\n" + "  (: Do something time consuming first. :)  \n" + "  let $a := dt:fib(25)" + "  return current-dateTime()\n" + "};";
    String module_name = "dt.xqm";
    Resource doc;
    // Store module
    Collection testCollection = getTestCollection();
    doc = testCollection.createResource(module_name, "BinaryResource");
    doc.setContent(module);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    String query = "import module namespace dt = \"dt\" at" + "  \"xmldb:exist:///db/test/dt.xqm\"; " + "(<this>{current-dateTime()}</this>, <this>{dt:dateTime()}</this>)";
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals(2, result.getSize());
    assertEquals("First", result.getResource(0).getContent().toString(), 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 15 with EXistResource

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

the class XQueryTest method namespace.

@Test
public void namespace() throws XMLDBException, IOException, SAXException {
    Resource doc;
    ResourceSet result;
    String query;
    @SuppressWarnings("unused") XMLResource resu;
    @SuppressWarnings("unused") boolean exceptionThrown;
    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(MODULE2_NAME, "BinaryResource");
    doc.setContent(module2);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(NAMESPACED_NAME, "XMLResource");
    doc.setContent(namespacedDocument);
    ((EXistResource) doc).setMimeType("application/xml");
    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" + "(:: redefine existing prefix ::)\n" + "declare namespace blah=\"bla\";\n" + "$blah:param";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XQST0033") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "(:: redefine existing prefix with same getUri ::)\n" + "declare namespace blah=\"blah\";\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("XQST0033") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"ho\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "$foo:bar";
    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=\"ho\" at \"" + URI + "/test/" + MODULE2_NAME + "\";\n" + "$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=\"blah\" at \"" + URI + "/test/" + MODULE2_NAME + "\";\n" + "$bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("No namespace defined for prefix") > -1);
    query = "declare namespace x = \"http://www.foo.com\"; \n" + "let $a := doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NAMESPACED_NAME + "') \n" + "return $a//x:edition";
    result = service.query(query);
    assertEquals("XQuery: " + query, 0, result.getSize());
    query = "declare namespace x = \"http://www.foo.com\"; \n" + "declare namespace y = \"http://exist.sourceforge.net/dc-ext\"; \n" + "let $a := doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NAMESPACED_NAME + "') \n" + "return $a//y:edition";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "<x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>", ((XMLResource) result.getResource(0)).getContent());
    query = "<result xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>{//rdf:Description}</result>";
    result = service.query(query);
    assertEquals(query, "<result xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" + "    <rdf:Description id=\"3\">\n" + "        <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title</dc:title>\n" + "        <dc:creator xmlns:dc=\"http://purl.org/dc/elements/1.1/\">creator</dc:creator>\n" + "        <x:place xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:place>\n" + "        <x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>\n" + "    </rdf:Description>\n" + "</result>", ((XMLResource) result.getResource(0)).getContent());
    query = "<result xmlns='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>{//Description}</result>";
    result = service.query(query);
    assertEquals("XQuery: " + query, "<result xmlns=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" + "    <rdf:Description xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" id=\"3\">\n" + "        <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title</dc:title>\n" + "        <dc:creator xmlns:dc=\"http://purl.org/dc/elements/1.1/\">creator</dc:creator>\n" + "        <x:place xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:place>\n" + "        <x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>\n" + "    </rdf:Description>\n" + "</result>", ((XMLResource) result.getResource(0)).getContent());
    // Interesting one : let's see with XQuery gurus :-)
    // declare namespace fn="";
    // fn:current-time()
    /*
        If the URILiteral part of a namespace declaration is a zero-length string,
        any existing namespace binding for the given prefix is removed from
        the statically known namespaces. This feature provides a way
        to remove predeclared namespace prefixes such as local.
         */
    query = "declare option exist:serialize 'indent=no';" + "for $x in <parent4 xmlns=\"http://www.example.com/parent4\"><child4/></parent4> " + "return <new>{$x//*:child4}</new>";
    result = service.query(query);
    assertXMLEqual("<new><child4 xmlns='http://www.example.com/parent4'/></new>", ((XMLResource) 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) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Aggregations

EXistResource (org.exist.xmldb.EXistResource)43 Collection (org.xmldb.api.base.Collection)20 BinaryResource (org.xmldb.api.modules.BinaryResource)20 Resource (org.xmldb.api.base.Resource)18 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)11 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