use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class RecoverXmlTest method storeAndVerify.
@Override
protected void storeAndVerify(final DBBroker broker, final Txn transaction, final Collection collection, final InputSource data, final String dbFilename) throws EXistException, PermissionDeniedException, IOException, LockException {
final XmldbURI docUri = XmldbURI.create(dbFilename);
try {
broker.storeDocument(transaction, docUri, data, MimeType.XML_TYPE, collection);
} catch (final SAXException e) {
throw new IOException(e);
}
final DocumentImpl doc = broker.getResource(collection.getURI().append(docUri), Permission.READ);
assertNotNull(doc);
final Source expected;
if (data instanceof FileInputSource) {
expected = Input.fromFile(((FileInputSource) data).getFile().toFile()).build();
} else if (data instanceof StringInputSource) {
try (final Reader reader = data.getCharacterStream()) {
expected = Input.fromString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + readAll(reader)).build();
}
} else {
throw new IllegalStateException();
}
final Source actual = Input.fromDocument(doc).build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForIdentical().build();
assertFalse("XML identical: " + diff.toString(), diff.hasDifferences());
}
use of org.exist.xmldb.XmldbURI 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.xmldb.XmldbURI 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());
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class StoreResourceTest method replaceXmlDoc.
private void replaceXmlDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content), MimeType.XML_TYPE, col);
transaction.commit();
}
// check the replaced document is correct
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK)) {
final Serializer serializer = broker.borrowSerializer();
try {
final String docXml = serializer.serialize(lockedDoc.getDocument());
final Diff diff = DiffBuilder.compare(Input.fromString(content)).withTest(Input.fromString(docXml)).build();
assertFalse(diff.toString(), diff.hasDifferences());
} finally {
broker.returnSerializer(serializer);
}
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class StoreResourceTest method replaceBinDoc.
private void replaceBinDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content.getBytes(UTF_8)), MimeType.BINARY_TYPE, col);
transaction.commit();
}
// check the replaced document is correct
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK);
final InputStream is = broker.getBinaryResource((BinaryDocument) lockedDoc.getDocument());
final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
os.write(is);
assertArrayEquals(content.getBytes(UTF_8), os.toByteArray());
}
}
Aggregations