Search in sources :

Example 26 with XMLResource

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

the class XQueryTriggerTest method documentDelete.

/**
 * test a trigger fired by a Document Delete
 */
@Test
public void documentDelete() throws XMLDBException {
    final IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
    idxConf.configureCollection(COLLECTION_CONFIG);
    final XMLResource doc = (XMLResource) testCollection.createResource(DOCUMENT_NAME, "XMLResource");
    doc.setContent(DOCUMENT_CONTENT);
    testCollection.storeResource(doc);
    testCollection.removeResource(testCollection.getResource(DOCUMENT_NAME));
    idxConf.configureCollection(EMPTY_COLLECTION_CONFIG);
    final XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    service.setProperty(OutputKeys.INDENT, "no");
    ResourceSet result = service.query(BEFORE + CREATE + DOCUMENT + documentURI);
    assertEquals(1, result.getSize());
    result = service.query(AFTER + CREATE + DOCUMENT + documentURI);
    assertEquals(1, result.getSize());
    result = service.query(BEFORE + DELETE + DOCUMENT + documentURI);
    assertEquals(1, result.getSize());
    result = service.query(AFTER + DELETE + DOCUMENT + documentURI);
    assertEquals(1, result.getSize());
    result = service.query(EVENTS);
    assertEquals(4, result.getSize());
// TODO: document itself
// result = service.query("/events/event[@id = 'trigger3']/document/test");
// assertEquals(1, result.getSize());
// assertXMLEqual(MODIFIED_DOCUMENT_CONTENT, result.getResource(0).getContent().toString());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) IndexQueryService(org.exist.xmldb.IndexQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 27 with XMLResource

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

the class XQueryTriggerTest method setup.

/**
 * create "log" document that will be updated by the trigger,
 * and store the XQuery module implementing the trigger under test
 */
@Before
public void setup() throws XMLDBException {
    final CollectionManagementService service = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    testCollection = service.createCollection(TEST_COLLECTION);
    assertNotNull(testCollection);
    final XMLResource doc = (XMLResource) testCollection.createResource(LOG_NAME, "XMLResource");
    doc.setContent(EMPTY_LOG);
    testCollection.storeResource(doc);
    final BinaryResource module = (BinaryResource) testCollection.createResource(MODULE_NAME, "BinaryResource");
    ((EXistResource) module).setMimeType("application/xquery");
    module.setContent(MODULE.getBytes());
    testCollection.storeResource(module);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource)

Example 28 with XMLResource

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

the class XQueryTriggerTest method documentCreate.

/**
 * test a trigger fired by storing a new Document
 */
@Test
public void documentCreate() throws XMLDBException {
    // configure the Collection with the trigger under test
    final IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
    idxConf.configureCollection(COLLECTION_CONFIG);
    // this will fire the trigger
    final XMLResource doc = (XMLResource) testCollection.createResource(DOCUMENT_NAME, "XMLResource");
    doc.setContent(DOCUMENT_CONTENT);
    testCollection.storeResource(doc);
    // remove the trigger for the Collection under test
    idxConf.configureCollection(EMPTY_COLLECTION_CONFIG);
    final XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(BEFORE + CREATE + DOCUMENT + documentURI);
    assertEquals(1, result.getSize());
    result = service.query(AFTER + CREATE + DOCUMENT + documentURI);
    assertEquals(1, result.getSize());
    result = service.query(EVENTS);
    assertEquals(2, result.getSize());
// TODO: document itself
// result = service.query(afterCreate+objDocument+documentURI+"/document/test");
// assertEquals(1, result.getSize());
// assertXMLEqual(DOCUMENT_CONTENT, ((XMLResource)result.getResource(0)).getContent().toString());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) IndexQueryService(org.exist.xmldb.IndexQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 29 with XMLResource

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

the class ProtectedModeTest method setupDb.

@BeforeClass
public static void setupDb() throws XMLDBException, SAXException {
    CollectionManagementService mgmt = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    final Collection collection = mgmt.createCollection("protected");
    mgmt = (CollectionManagementService) collection.getService("CollectionManagementService", "1.0");
    final TestDataGenerator generator = new TestDataGenerator("xdb", DOCUMENT_COUNT);
    for (int i = 0; i < COLLECTION_COUNT; i++) {
        Collection currentColl = mgmt.createCollection("test" + i);
        final Path[] files = generator.generate(currentColl, generateXQ);
        for (int j = 0; j < files.length; j++) {
            final XMLResource resource = (XMLResource) currentColl.createResource("xdb" + j + ".xml", "XMLResource");
            resource.setContent(files[j].toFile());
            currentColl.storeResource(resource);
        }
    }
}
Also used : Path(java.nio.file.Path) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Collection(org.xmldb.api.base.Collection) XMLResource(org.xmldb.api.modules.XMLResource) TestDataGenerator(org.exist.TestDataGenerator) BeforeClass(org.junit.BeforeClass)

Example 30 with XMLResource

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

the class NodeTypeTest method load.

/**
 * Loads the xml document identified by <code>document</code> from the database.
 *
 * @param document the document to load
 */
@SuppressWarnings("unused")
private Node load(final String document) throws XMLDBException {
    final StringBuilder query = new StringBuilder();
    query.append("let $result := doc(string-join(('" + XmldbURI.ROOT_COLLECTION + "', $document), '/'))");
    query.append("return ($result)");
    final XQueryService service = (XQueryService) server.getRoot().getService("XQueryService", "1.0");
    service.declareVariable("document", document);
    final CompiledExpression cQuery = service.compile(query.toString());
    final ResourceSet set = service.execute(cQuery);
    if (set != null && set.getSize() > 0) {
        return ((XMLResource) set.getIterator().nextResource()).getContentAsDOM();
    }
    return null;
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource)

Aggregations

XMLResource (org.xmldb.api.modules.XMLResource)142 Collection (org.xmldb.api.base.Collection)56 ResourceSet (org.xmldb.api.base.ResourceSet)56 XPathQueryService (org.xmldb.api.modules.XPathQueryService)49 Test (org.junit.Test)33 Resource (org.xmldb.api.base.Resource)24 XQueryService (org.xmldb.api.modules.XQueryService)23 Node (org.w3c.dom.Node)22 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)20 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)18 Document (org.w3c.dom.Document)16 XMLDBException (org.xmldb.api.base.XMLDBException)15 EXistResource (org.exist.xmldb.EXistResource)9 EXistXQueryService (org.exist.xmldb.EXistXQueryService)9 BinaryResource (org.xmldb.api.modules.BinaryResource)9 Path (java.nio.file.Path)8 Before (org.junit.Before)8 Element (org.w3c.dom.Element)8 Source (javax.xml.transform.Source)7 SAXSerializer (org.exist.util.serializer.SAXSerializer)7