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());
}
}
}
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);
}
}
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);
}
}
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);
}
}
}
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());
}
}
Aggregations