|
| 1 | +package com.marklogic.javaclient; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.util.HashMap; |
| 9 | + |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.AfterClass; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.BeforeClass; |
| 14 | +import org.junit.FixMethodOrder; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runners.MethodSorters; |
| 17 | + |
| 18 | +import com.marklogic.client.DatabaseClient; |
| 19 | +import com.marklogic.client.DatabaseClientFactory; |
| 20 | +import com.marklogic.client.Transaction; |
| 21 | +import com.marklogic.client.DatabaseClientFactory.Authentication; |
| 22 | +import com.marklogic.client.document.DocumentManager; |
| 23 | +import com.marklogic.client.document.DocumentWriteSet; |
| 24 | +import com.marklogic.client.document.GenericDocumentManager; |
| 25 | +import com.marklogic.client.document.XMLDocumentManager; |
| 26 | +import com.marklogic.client.eval.EvalResult; |
| 27 | +import com.marklogic.client.eval.EvalResultIterator; |
| 28 | +import com.marklogic.client.eval.ServerEvaluationCall; |
| 29 | +import com.marklogic.client.eval.EvalResult.Type; |
| 30 | +import com.marklogic.client.io.DOMHandle; |
| 31 | +import com.marklogic.client.io.FileHandle; |
| 32 | +import com.marklogic.client.io.Format; |
| 33 | +import com.marklogic.client.io.InputStreamHandle; |
| 34 | +/* |
| 35 | + * This test is intended for |
| 36 | + * looping eval query for more than 100 times |
| 37 | + * Eval with transactions |
| 38 | + */ |
| 39 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 40 | +public class TestEvalwithRunTimeDBnTransactions extends BasicJavaClientREST { |
| 41 | + private static String dbName = "TestEvalXqueryWithTransDB"; |
| 42 | + private static String [] fNames = {"TestEvalXqueryWithTransDB-1"}; |
| 43 | + private static String restServerName = "REST-Java-Client-API-Server"; |
| 44 | + private static int restPort = 8011; |
| 45 | + private DatabaseClient client ; |
| 46 | + @BeforeClass |
| 47 | + public static void setUpBeforeClass() throws Exception { |
| 48 | + System.out.println("In setup"); |
| 49 | + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort,false); |
| 50 | + createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke"); |
| 51 | + createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader"); |
| 52 | +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); |
| 53 | + } |
| 54 | + |
| 55 | + @AfterClass |
| 56 | + public static void tearDownAfterClass() throws Exception { |
| 57 | + System.out.println("In tear down" ); |
| 58 | + tearDownJavaRESTServer(dbName, fNames, restServerName); |
| 59 | + deleteRESTUser("eval-user"); |
| 60 | + deleteUserRole("test-eval"); |
| 61 | + } |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() throws Exception { |
| 65 | + client = DatabaseClientFactory.newClient("localhost", restPort,dbName,"eval-user", "x", Authentication.DIGEST); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void tearDown() throws Exception { |
| 70 | + client.release(); |
| 71 | + } |
| 72 | +//issue 169, loop the eval query more than 150 times and should not stuck |
| 73 | + @Test |
| 74 | + public void test1MultipleEvalQueries() throws Exception { |
| 75 | + |
| 76 | + GenericDocumentManager docMgr = client.newDocumentManager(); |
| 77 | + File file1= null; |
| 78 | + FileInputStream fis=null; |
| 79 | + try{ |
| 80 | + file1 = new File("src/test/java/com/marklogic/javaclient/data/Sega-4MB.jpg"); |
| 81 | + fis = new FileInputStream(file1); |
| 82 | + InputStreamHandle handle1 = new InputStreamHandle(fis); |
| 83 | + handle1.setFormat(Format.BINARY); |
| 84 | + docMgr.write("/binary4mbdoc",handle1); |
| 85 | + String query = "declare variable $myInteger as xs:integer external;" |
| 86 | + + "(fn:doc()/binary(),$myInteger,xdmp:database-name(xdmp:database()))"; |
| 87 | + for(int i=0 ; i<=20;i++){ |
| 88 | + ServerEvaluationCall evl= client.newServerEval().xquery(query); |
| 89 | + evl.addVariable("myInteger", (int)i); |
| 90 | + EvalResultIterator evr = evl.eval(); |
| 91 | + while(evr.hasNext()){ |
| 92 | + EvalResult er =evr.next(); |
| 93 | + if(er.getType().equals(Type.INTEGER)){ |
| 94 | + assertEquals("itration number",i,er.getNumber().intValue()); |
| 95 | + }else if(er.getType().equals(Type.BINARY)){ |
| 96 | + FileHandle readHandle1 = new FileHandle(); |
| 97 | + assertEquals("size of the binary ",er.get(readHandle1).get().length(),docMgr.read("/binary4mbdoc",new InputStreamHandle()).getByteLength()); |
| 98 | + |
| 99 | + }else if(er.getType().equals(Type.STRING)){ |
| 100 | + assertEquals("database name ","TestEvalXqueryWithTransDB",er.getString()); |
| 101 | + }else{ |
| 102 | + fail("Getting incorrect type"); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | + }catch(Exception e){throw e;} |
| 108 | + finally{ |
| 109 | + fis.close(); |
| 110 | + } |
| 111 | + } |
| 112 | + //issue 170,171 are blocking the test progress in here |
| 113 | + @Test |
| 114 | + public void test2XqueryEvalTransactions() throws Exception{ |
| 115 | + int count=1; |
| 116 | + boolean tstatus =true; |
| 117 | + Transaction t1 = client.openTransaction(); |
| 118 | + try{ |
| 119 | + XMLDocumentManager docMgr = client.newXMLDocumentManager(); |
| 120 | + HashMap<String,String> map= new HashMap<String,String>(); |
| 121 | + DocumentWriteSet writeset =docMgr.newWriteSet(); |
| 122 | + for(int i =0;i<102;i++){ |
| 123 | + writeset.add("/sec"+i+".xml", new DOMHandle(getDocumentContent("This is so sec"+i))); |
| 124 | + map.put("/sec"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is so sec"+i))); |
| 125 | + if(count%100 == 0){ |
| 126 | + docMgr.write(writeset,t1); |
| 127 | + writeset = docMgr.newWriteSet(); |
| 128 | + } |
| 129 | + count++; |
| 130 | + } |
| 131 | + if(count%100 > 0){ |
| 132 | + docMgr.write(writeset,t1); |
| 133 | + } |
| 134 | + String query = "declare variable $myInteger as xs:integer external;" |
| 135 | + + "(fn:count(fn:doc()))"; |
| 136 | + ServerEvaluationCall evl= client.newServerEval().xquery(query); |
| 137 | + EvalResultIterator evr = evl.eval(); |
| 138 | + assertEquals("Count of documents outside of the transaction",1,evr.next().getNumber().intValue()); |
| 139 | + evl= client.newServerEval().xquery(query).transaction(t1); |
| 140 | + evr = evl.eval(); |
| 141 | + assertEquals("Count of documents outside of the transaction",103,evr.next().getNumber().intValue()); |
| 142 | + }catch(Exception e){ |
| 143 | + System.out.println(e.getMessage()); |
| 144 | + tstatus=true; |
| 145 | + throw e; |
| 146 | + }finally{ |
| 147 | + if(tstatus){ |
| 148 | + t1.rollback(); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +} |
0 commit comments