use of org.exist.util.StringInputSource in project exist by eXist-db.
the class MatchListenerTest method configureAndStore.
private void configureAndStore(String config, String xml) throws PermissionDeniedException, IOException, SAXException, EXistException, LockException, CollectionConfigurationException {
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);
assertNotNull(root);
final CollectionConfigurationManager mgr = pool.getConfigurationManager();
mgr.addConfiguration(transaction, broker, root, config);
broker.storeDocument(transaction, XmldbURI.create("test_matches.xml"), new StringInputSource(xml), MimeType.XML_TYPE, root);
transact.commit(transaction);
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class IndexerTest3 method store_suppress_type.
private void store_suppress_type(final String propValue, final String xml) throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
pool.getConfiguration().setProperty(Indexer.PROPERTY_SUPPRESS_WHITESPACE, propValue);
// Make sure to keep preserve whitespace mixed content stable even if default changes. fixme! - should test both. /ljo
boolean propWSMValue = false;
pool.getConfiguration().setProperty(Indexer.PROPERTY_PRESERVE_WS_MIXED_CONTENT, propWSMValue);
final TransactionManager txnMgr = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().authenticate("admin", "")));
final Txn txn = txnMgr.beginTransaction()) {
try (final Collection collection = broker.getOrCreateCollection(txn, TestConstants.TEST_COLLECTION_URI)) {
broker.storeDocument(txn, TestConstants.TEST_XML_URI, new StringInputSource(xml), MimeType.XML_TYPE, collection);
broker.flush();
broker.saveCollection(txn, collection);
}
txnMgr.commit(txn);
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class DeepEmbeddedBackupRestoreTest method createHierarchy.
private CollectionsAndDocuments createHierarchy(final XmldbURI baseCollectionUri, final int depth, final int maxWidth, final int xmlDocsPerCollection, final int binDocsPerCollection) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
final List<XmldbURI> collectionUris = new ArrayList<>();
final List<ResourceInfo> documentInfos = new ArrayList<>();
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
try (final Txn transaction = brokerPool.getTransactionManager().beginTransaction();
final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
try (final Collection baseCollection = broker.getOrCreateCollection(transaction, baseCollectionUri)) {
collectionUris.add(baseCollectionUri);
final Random random = new Random();
XmldbURI subCollectionUri = baseCollectionUri;
for (int d = 0; d < depth; d++) {
subCollectionUri = subCollectionUri.append("sub_" + d);
try (final Collection subCollection = broker.getOrCreateCollection(transaction, subCollectionUri)) {
collectionUris.add(subCollectionUri);
final int width = random.nextInt(maxWidth - 1) + 1;
for (int w = 0; w < width; w++) {
final XmldbURI sibCollectionUri = subCollectionUri.append("sib_" + w);
try (final Collection sibCollection = broker.getOrCreateCollection(transaction, sibCollectionUri)) {
collectionUris.add(sibCollectionUri);
for (int x = 0; x < xmlDocsPerCollection; x++) {
// store XML document
final XmldbURI xmlName = XmldbURI.create("doc_" + x + ".xml");
final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + EOL + "<position id=\"" + x + "\" d=\"" + d + "\" w=\"" + w + "\"/>";
broker.storeDocument(transaction, xmlName, new StringInputSource(xml), MimeType.XML_TYPE, sibCollection);
final byte[] xmlData = xml.getBytes(UTF_8);
final long xmlHash = hash64.hash(xmlData, 0, xmlData.length, XXHASH64_SEED);
documentInfos.add(new ResourceInfo(sibCollectionUri.append(xmlName), xmlHash));
// store Binary document
final XmldbURI binName = XmldbURI.create("doc_" + x + ".bin");
final String bin = x + ":" + d + ":" + w;
broker.storeDocument(transaction, binName, new StringInputSource(bin.getBytes(UTF_8)), MimeType.BINARY_TYPE, sibCollection);
final byte[] binData = bin.getBytes(UTF_8);
final long binHash = hash64.hash(binData, 0, binData.length, XXHASH64_SEED);
documentInfos.add(new ResourceInfo(sibCollectionUri.append(binName), binHash));
}
}
}
}
}
}
transaction.commit();
}
return new CollectionsAndDocuments(collectionUris, documentInfos);
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class JournalXmlTest method largeJournalEntry_nonCorrupt.
@Test
public void largeJournalEntry_nonCorrupt() throws IllegalAccessException, EXistException, NoSuchFieldException, LockException, SAXException, PermissionDeniedException, IOException, InterruptedException {
checkpointJournalAndSwitchFile();
// generate a string filled with random a-z characters which is larger than the journal buffer
// 3 * the journal buffer size
final byte[] buf = new byte[Journal.BUFFER_SIZE * 3];
final Random random = new Random();
for (int i = 0; i < buf.length; i++) {
final byte singleByteChar = (byte) ('a' + random.nextInt('z' - 'a' - 1));
buf[i] = singleByteChar;
}
final String largeText = new String(buf, UTF_8);
final String xml = "<large-text>" + largeText + "</large-text>";
final InputSource source = new StringInputSource(xml);
source.setEncoding("UTF-8");
BrokerPool.FORCE_CORRUPTION = false;
final TxnDoc<String> stored = store(COMMIT, source, "large-non-corrupt.xml");
flushJournal();
// shutdown the broker pool (without destroying the data dir)
existEmbeddedServer.getBrokerPool().shutdown();
// check journal entries written for store
assertPartialOrdered(store_expected(stored, 0, largeText), readLatestJournalEntries());
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class ConnectionIT method getConnectionFromModuleIsAutomaticallyClosed.
@Test
public void getConnectionFromModuleIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException, LockException, SAXException {
final String moduleQuery = "module namespace mymodule = \"http://mymodule.com\";\n" + "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + "declare function mymodule:get-handle() {\n" + " sql:get-connection(\"" + h2Database.getDriverClass().getName() + "\", \"" + h2Database.getUrl() + "\", \"" + h2Database.getUser() + "\", \"" + h2Database.getPassword() + "\")\n" + "};\n";
final String mainQuery = "import module namespace mymodule = \"http://mymodule.com\" at \"xmldb:exist:///db/mymodule.xqm\";\n" + "mymodule:get-handle()";
final Source mainQuerySource = new StringSource(mainQuery);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store module
try (final Collection collection = broker.openCollection(XmldbURI.create("/db"), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, XmldbURI.create("mymodule.xqm"), new StringInputSource(moduleQuery.getBytes(UTF_8)), MimeType.XQUERY_TYPE, collection);
}
final Tuple2<XQueryContext, ModuleContext> escapedContexts = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
final XQueryContext mainQueryContext = mainCompiledQuery.getContext();
// get the context of the library module
final Module[] libraryModules = mainQueryContext.getModules("http://mymodule.com");
assertEquals(1, libraryModules.length);
assertTrue(libraryModules[0] instanceof ExternalModule);
final ExternalModule libraryModule = (ExternalModule) libraryModules[0];
final XQueryContext libraryQueryContext = libraryModule.getContext();
assertTrue(libraryQueryContext instanceof ModuleContext);
// execute the query
final Sequence result = executeQuery(broker, mainCompiledQuery);
// check that the handle for the sql connection that was created was valid
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0) instanceof IntegerValue);
assertEquals(Type.LONG, result.itemAt(0).getType());
final long connectionHandle = result.itemAt(0).toJavaObject(long.class);
assertFalse(connectionHandle == 0);
// intentionally escape the contexts from the lambda
return Tuple(mainQueryContext, (ModuleContext) libraryQueryContext);
});
final XQueryContext escapedMainQueryContext = escapedContexts._1;
final ModuleContext escapedLibraryQueryContext = escapedContexts._2;
assertTrue(escapedMainQueryContext != escapedLibraryQueryContext);
// check the connections were closed in the main module
final int mainConnectionsCount = ModuleUtils.readContextMap(escapedMainQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
assertEquals(0, mainConnectionsCount);
// check the connections were closed in the library module
final int libraryConnectionsCount = ModuleUtils.readContextMap(escapedLibraryQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
assertEquals(0, libraryConnectionsCount);
transaction.commit();
}
}
Aggregations