public Transaction openTransaction() { return this.client.openTransaction(); }
private void testConnection(DatabaseClient client) { try { // Since creating client is not enough for verifying established connection, need to make fake call. client.openTransaction().commit(); } catch (Exception e) { MarkLogicErrorCode errorCode = new MarkLogicErrorCode( e instanceof FailedRequestException ? MESSAGES.getMessage("error.invalid.credentials") : MESSAGES.getMessage("error.server.notReachable")); throw new MarkLogicException(errorCode, e); } }
private void testConnection(DatabaseClient client) { try { // Since creating client is not enough for verifying established connection, need to make fake call. client.openTransaction().commit(); } catch (Exception e) { MarkLogicErrorCode errorCode = new MarkLogicErrorCode( e instanceof FailedRequestException ? MESSAGES.getMessage("error.invalid.credentials") : MESSAGES.getMessage("error.server.notReachable")); throw new MarkLogicException(errorCode, e); } }
@Override public void run() { Transaction t2 = writeclient.openTransaction(); try { System.out.println("In ExecuteAskSecondThreadFalse run method"); SPARQLQueryManager sparqlQmgrTh = writeclient.newSPARQLQueryManager(); StringBuffer sparqlQueryTh = new StringBuffer(); sparqlQueryTh.append("ASK FROM <rdfxml> where { <http://example.org/kennedy/person1> <http://purl.org/dc/elements/1.1/title> \"Person\'s title\"@en }"); SPARQLQueryDefinition qdefTh = sparqlQmgrTh.newQueryDefinition(sparqlQueryTh.toString()); // Verify result in t2 transaction. boolean bAskInTransT2 = sparqlQmgrTh.executeAsk(qdefTh, t2); System.out.println("Method ExecuteAskSecondThreadFalse Run result is " + bAskInTransT2); assertFalse("Class ExecuteAskSecondThreadFalse Run result is incorrect. No Records should be returned.", bAskInTransT2); setbCompleted(true); } catch (ForbiddenUserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FailedRequestException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (t2 != null) { t2.rollback(); t2 = null; } } } }
@Test public void testStack20170725() throws IOException { XMLDocumentManager documentManager = Common.client.newXMLDocumentManager(); Transaction transaction = Common.client.openTransaction(); DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); documentManager.writeAs("all_well.xml", new FileHandle( new File("../marklogic-client-api-functionaltests/src/test/java/com" + "/marklogic/client/functionaltest/data/all_well.xml"))); InputStreamHandle handle = documentManager.read("all_well.xml", metadataHandle, new InputStreamHandle(), transaction); try { InputStream stream = handle.get(); try { while ( stream.read() > -1 ) {} } finally { stream.close(); } } finally { handle.close(); } // if we ran without throwing any exceptions, then this test passes } }
@Test public void testMergeAs_jsonxml_Trx_FileHandle() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileInputStream fis = new FileInputStream(file); Object graphData = convertInputStreamToString(fis); fis.close(); gmWriter.writeAs("htp://test.sem.graph/jsonMergeAsTrx", graphData, trx); File file2 = new File(datasource + "relative6.json"); Object graphData2 = convertFileToString(file2); gmWriter.mergeAs("htp://test.sem.graph/jsonMergeAsTrx", graphData2, trx); File readFile = gmWriter.readAs("htp://test.sem.graph/jsonMergeAsTrx", File.class, trx); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://example.com/ns/person#firstName") && expectedContent.contains("Anna's Homepage")); try { trx.commit(); trx = null; } catch (Exception e) { System.out.println(e); } finally { if (trx != null) { trx.rollback(); trx = null; } } }
@Test public void testWriteAs_ReadAs_FileHandle() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); File file = new File(datasource + "semantics.ttl"); trx = writerClient.openTransaction(); String readContent = gmWriter.readAs("htp://test.sem.graph/ttlas", String.class, trx); trx.commit();
@Test public void testRequestLogger() throws KeyManagementException, NoSuchAlgorithmException, IOException { System.out.println("Running testRequestLogger"); String filename = "bbq1.xml"; String uri = "/request-logger/"; File file = new File("src/test/java/com/marklogic/client/functionaltest/data/" + filename); // create transaction Transaction transaction = client.openTransaction(); // create a manager for XML documents XMLDocumentManager docMgr = client.newXMLDocumentManager(); // create an identifier for the document String docId = uri + filename; // create a handle on the content FileHandle handle = new FileHandle(file); handle.set(file); // create logger RequestLogger logger = client.newLogger(System.out); logger.setContentMax(RequestLogger.ALL_CONTENT); // start logging docMgr.startLogging(logger); // write the document content docMgr.write(docId, handle, transaction); // commit transaction transaction.commit(); // stop logging docMgr.stopLogging(); String expectedContentMax = "9223372036854775807"; assertEquals("Content log is not equal", expectedContentMax, Long.toString(logger.getContentMax())); }
@Test public void testRequestLogger() throws KeyManagementException, NoSuchAlgorithmException, IOException { System.out.println("Running testRequestLogger"); String filename = "bbq1.xml"; String uri = "/request-logger/"; File file = new File("src/test/java/com/marklogic/client/functionaltest/data/" + filename); // create transaction Transaction transaction = client.openTransaction(); // create a manager for XML documents XMLDocumentManager docMgr = client.newXMLDocumentManager(); // create an identifier for the document String docId = uri + filename; // create a handle on the content FileHandle handle = new FileHandle(file); handle.set(file); // create logger RequestLogger logger = client.newLogger(System.out); logger.setContentMax(RequestLogger.ALL_CONTENT); // start logging docMgr.startLogging(logger); // write the document content docMgr.write(docId, handle, transaction); // commit transaction transaction.commit(); // stop logging docMgr.stopLogging(); String expectedContentMax = "9223372036854775807"; assertEquals("Content log is not equal", expectedContentMax, Long.toString(logger.getContentMax())); }
@Test public void testWriteMergeDelete_Trx() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); File file = new File(datasource + "triplexml1.xml"); gmWriter.write("htp://test.sem.graph/mergetrx", new FileHandle(file).withMimetype(RDFMimeTypes.TRIPLEXML), trx); file = new File(datasource + "bug25348.json"); gmWriter.merge("htp://test.sem.graph/mergetrx", new FileHandle(file).withMimetype(RDFMimeTypes.RDFJSON), trx); FileHandle handle = gmWriter.read("htp://test.sem.graph/mergetrx", new FileHandle(), trx); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); try { assertTrue("Did not Merge document or inserted empty doc", expectedContent.contains("Michelle") && expectedContent.contains("Anna's Homepage")); gmWriter.delete("htp://test.sem.graph/mergetrx", trx); trx.commit(); trx = null; StringHandle readContent = gmWriter.read("htp://test.sem.graph/mergetrx", new StringHandle(), trx); assertTrue("Unexpected Content from read, expecting null", readContent == null); } catch (Exception e) { assertTrue("Unexpected Exception Thrown", e.toString().contains("ResourceNotFoundException")); } finally { if (trx != null) trx.commit(); trx = null; } }
@Test public void issue_357() { // open transaction Transaction transaction = Common.client.openTransaction(); try { // insert some data under transaction String insertString="BASE <http://example.org/addressbook>\n " + "INSERT DATA { <http://example.org/id#3333> <#email> \"jonny@ramone.com\"}"; SPARQLQueryDefinition insertDef = smgr.newQueryDefinition(insertString); smgr.executeUpdate(insertDef, transaction); // ask for it w/ transaction String queryString = "ASK WHERE { <http://example.org/id#3333> <http://example.org/addressbook#email> ?o }"; SPARQLQueryDefinition booleanDef = smgr.newQueryDefinition(queryString); assertTrue(smgr.executeAsk(booleanDef,transaction)); } finally { transaction.rollback(); } }
@Test public void testWrite_triplexml_FileHandle() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); File file = new File(datasource + "relative5.xml"); FileHandle filehandle = new FileHandle(); filehandle.set(file); // Write Graph using transaction gmWriter.write("htp://test.sem.graph/tripxml", filehandle.withMimetype(RDFMimeTypes.TRIPLEXML), trx); // Validate graph written to DB by reading within the same transaction StringHandle handle = gmWriter.read("htp://test.sem.graph/tripxml", new StringHandle().withMimetype(RDFMimeTypes.TRIPLEXML), trx); assertTrue("Did not insert document or inserted empty doc", handle.toString().contains("Anna's Homepage")); // Delete Graph in the same transaction gmWriter.delete("htp://test.sem.graph/tripxml", trx); // Validate Graph is deleted try { gmWriter.read("htp://test.sem.graph/tripxml", new StringHandle(), trx); trx.commit(); trx = null; } catch (ResourceNotFoundException e) { System.out.println(e); } finally { if (trx != null) { trx.rollback(); trx = null; } } }
SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("eval-user", "x"); DatabaseClient client = DatabaseClientFactory.newClient(appServerHostname, uberPort, dbName, secContext, getConnType()); Transaction t = client.openTransaction("Transac");
@Test public void testReadUsingMultipleUriAndMetadataHandleInTransaction() throws KeyManagementException, NoSuchAlgorithmException, Exception { String docId[] = { "/foo/test/multipleURIFoo1.txt", "/foo/test/multipleURIFoo2.txt", "/foo/test/multipleURIFoo3.txt" }; Transaction transaction = client.openTransaction(); try { TextDocumentManager docMgr = client.newTextDocumentManager(); docMgr.write(writeset, transaction); transaction.commit(); transaction = client.openTransaction();
@Test public void testPartialUpdateXMLDscriptorTranc() throws IOException { System.out.println("Running testPartialUpdateXMLDescriptorTranc"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("eval-user", "x"); DatabaseClient client = DatabaseClientFactory.newClient(appServerHostname, uberPort, dbName, secContext, getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); } String docId = "/partial-update/constraint1.xml"; // create doc manager XMLDocumentManager docMgr = client.newXMLDocumentManager(); // create template DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); template.withDirectory(docId); DocumentDescriptor desc = docMgr.newDescriptor(template.getDirectory()); DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); patchBldr.insertFragment("/root", Position.LAST_CHILD, "<modified>2013-03-21</modified>"); DocumentPatchHandle patchHandle = patchBldr.build(); Transaction t = client.openTransaction("Tranc"); docMgr.patch(desc, patchHandle, t); t.commit(); waitForPropertyPropagate(); String content = docMgr.read(docId, new StringHandle()).get(); System.out.println("After" + content); assertTrue("fragment is not inserted", content.contains("<modified>2013-03-21</modified></root>")); // release client client.release(); }
@Test public void testBulkReadUsingMultipleUri() throws KeyManagementException, NoSuchAlgorithmException, Exception { String docId[] = { "/foo/test/transactionURIFoo1.txt", "/foo/test/transactionURIFoo2.txt", "/foo/test/transactionURIFoo3.txt" }; Transaction transaction = client.openTransaction(); try { TextDocumentManager docMgr = client.newTextDocumentManager(); docMgr.write(writeset, transaction); transaction.commit(); transaction = client.openTransaction();
@Test public void testCommit() throws XpathException { String docId1 = "/test/testExists1.txt"; String docId2 = "/test/testExists2.txt"; TextDocumentManager docMgr = Common.client.newTextDocumentManager(); docMgr.write(docId1,new StringHandle().with("A simple text document")); String transactionName = "java-client-" + seed.nextLong(); Transaction transaction = Common.client.openTransaction(transactionName); StringHandle docHandle = docMgr.read(docId1, new StringHandle(), transaction); docMgr.write(docId2, docHandle, transaction); docMgr.delete(docId1, transaction); Document status = transaction.readStatus(new DOMHandle()).get(); assertXpathExists("//*[local-name() = 'transaction-name' and "+ "string(.) = '"+transactionName+"']", status); transaction.commit(); assertTrue("Document 1 exists", docMgr.exists(docId1)==null); assertTrue("Document 2 doesn't exist", docMgr.exists(docId2)!=null); docMgr.delete(docId2); }
@Test public void testRollback() { String docId1 = "/test/testExists1.txt"; String docId2 = "/test/testExists2.txt"; TextDocumentManager docMgr = Common.client.newTextDocumentManager(); docMgr.write(docId1,new StringHandle().with("A simple text document")); Transaction transaction = Common.client.openTransaction(); StringHandle docHandle = docMgr.read(docId1, new StringHandle(), transaction); docMgr.write(docId2, docHandle, transaction); docMgr.delete(docId1, transaction); transaction.rollback(); assertTrue("Document 1 doesn't exist", docMgr.exists(docId1)!=null); assertTrue("Document 2 exists", docMgr.exists(docId2)==null); docMgr.delete(docId1); }
@Test public void test0POJOWriteWithTransaction() throws KeyManagementException, NoSuchAlgorithmException, Exception { PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class); Transaction t = client.openTransaction(); // Load more than 100 objects try { for (int i = 1; i < 112; i++) { products.write(this.getArtifact(i), t); } assertEquals("Total number of object recods", 111, products.count(t)); for (long i = 1; i < 112; i++) { assertTrue("Product id " + i + " does not exist", products.exists(i, t)); this.validateArtifact(products.read(i, t)); } } catch (Exception e) { throw e; } finally { t.rollback(); } for (long i = 1; i < 112; i++) { assertFalse("Product id exists ?", products.exists(i)); } }
metaWriteHandle.getCollections().addAll(collections); Transaction transaction = Common.client.openTransaction();