Search in sources :

Example 61 with Collection

use of org.exist.collections.Collection in project exist by eXist-db.

the class BinaryDocumentTest method overwriteCollection.

@Test
public void overwriteCollection() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final XmldbURI testCollectionUri = XmldbURI.create("/db/overwrite-collection-test");
    final XmldbURI thingUri = testCollectionUri.append("thing");
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        // create a collection
        final Collection thingCollection = broker.getOrCreateCollection(transaction, thingUri);
        broker.saveCollection(transaction, thingCollection);
        // attempt to create a binary document with the same uri as the thingCollection (should fail)
        final Collection testCollection = broker.getCollection(testCollectionUri);
        try {
            broker.storeDocument(transaction, thingUri.lastSegment(), new StringInputSource("binary-file".getBytes(UTF_8)), MimeType.BINARY_TYPE, testCollection);
            fail("Should not have been able to overwrite Collection with Binary Document");
        } catch (final EXistException e) {
            assertEquals("The collection '" + testCollectionUri.getRawCollectionPath() + "' already has a sub-collection named '" + thingUri.lastSegment().toString() + "', you cannot create a Document with the same name as an existing collection.", e.getMessage());
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 62 with Collection

use of org.exist.collections.Collection in project exist by eXist-db.

the class CopyCollectionRecoveryTest method store.

private void store() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        broker.saveCollection(transaction, root);
        final Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI.append("test2"));
        broker.saveCollection(transaction, test);
        final String sample = getSampleData();
        broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test);
        final Collection dest = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("destination"));
        broker.saveCollection(transaction, dest);
        broker.copyCollection(transaction, test, dest, XmldbURI.create("test3"));
        transact.commit(transaction);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn)

Example 63 with Collection

use of org.exist.collections.Collection in project exist by eXist-db.

the class CopyCollectionRecoveryTest method storeAborted.

private void storeAborted() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        Collection test2;
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI.append("test2"));
            assertNotNull(test2);
            broker.saveCollection(transaction, test2);
            final String sample = getSampleData();
            broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test2);
            transact.commit(transaction);
        }
        final Txn transaction = transact.beginTransaction();
        final Collection dest = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("destination"));
        assertNotNull(dest);
        broker.saveCollection(transaction, dest);
        broker.copyCollection(transaction, test2, dest, XmldbURI.create("test3"));
        // DO NOT COMMIT TRANSACTION
        pool.getJournalManager().get().flush(true, false);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn)

Example 64 with Collection

use of org.exist.collections.Collection in project exist by eXist-db.

the class CopyResourceRecoveryTest method store.

private void store(final String testCollectionName, final String subCollection) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, URISyntaxException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        Collection testCollection;
        DocumentImpl doc;
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection root = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test"));
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            testCollection = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test").append(testCollectionName));
            assertNotNull(testCollection);
            broker.saveCollection(transaction, testCollection);
            final Collection subTestCollection = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test").append(testCollectionName).append(subCollection));
            assertNotNull(subTestCollection);
            broker.saveCollection(transaction, subTestCollection);
            final String sample;
            try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
                assertNotNull(is);
                sample = InputStreamUtil.readString(is, UTF_8);
            }
            broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, subTestCollection);
            doc = subTestCollection.getDocument(broker, XmldbURI.create("test.xml"));
            transact.commit(transaction);
        }
        try (final Txn transaction = transact.beginTransaction()) {
            broker.copyResource(transaction, doc, testCollection, XmldbURI.create("new_test.xml"));
            broker.saveCollection(transaction, testCollection);
            transact.commit(transaction);
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) InputStream(java.io.InputStream) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 65 with Collection

use of org.exist.collections.Collection in project exist by eXist-db.

the class CopyResourceTest method copyDoc.

private void copyDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI srcDocName, final XmldbURI destDocName) throws EXistException, PermissionDeniedException, LockException, IOException, TriggerException {
    final XmldbURI src = TEST_COLLECTION_URI.append(srcDocName);
    final XmldbURI dest = TEST_COLLECTION_URI.append(destDocName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final LockedDocument lockedSrcDoc = broker.getXMLResource(src, LockMode.READ_LOCK);
        final Collection destCol = broker.openCollection(dest.removeLastSegment(), LockMode.WRITE_LOCK)) {
        broker.copyResource(transaction, lockedSrcDoc.getDocument(), destCol, dest.lastSegment(), preserve);
        transaction.commit();
    }
    // check the copy of the document is the same as the original
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedOriginal = broker.getXMLResource(src, LockMode.READ_LOCK);
        final LockedDocument lockedCopy = broker.getXMLResource(dest, LockMode.READ_LOCK)) {
        final Diff diff = DiffBuilder.compare(Input.fromDocument(lockedOriginal.getDocument())).withTest(Input.fromDocument(lockedCopy.getDocument())).build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : Diff(org.xmlunit.diff.Diff) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

Collection (org.exist.collections.Collection)297 Txn (org.exist.storage.txn.Txn)160 XmldbURI (org.exist.xmldb.XmldbURI)99 DBBroker (org.exist.storage.DBBroker)89 TransactionManager (org.exist.storage.txn.TransactionManager)86 BrokerPool (org.exist.storage.BrokerPool)69 StringInputSource (org.exist.util.StringInputSource)57 Test (org.junit.Test)57 EXistException (org.exist.EXistException)43 PermissionDeniedException (org.exist.security.PermissionDeniedException)43 DocumentImpl (org.exist.dom.persistent.DocumentImpl)42 IOException (java.io.IOException)33 LockedDocument (org.exist.dom.persistent.LockedDocument)31 SAXException (org.xml.sax.SAXException)26 InputStream (java.io.InputStream)19 Path (java.nio.file.Path)19 Permission (org.exist.security.Permission)19 LockException (org.exist.util.LockException)16 TriggerException (org.exist.collections.triggers.TriggerException)15 BinaryDocument (org.exist.dom.persistent.BinaryDocument)15