22
33import static org .junit .Assert .*;
44
5+ import java .io .File ;
56import java .util .Calendar ;
67import java .util .HashMap ;
78
1314
1415import com .marklogic .client .DatabaseClient ;
1516import com .marklogic .client .DatabaseClientFactory ;
17+ import com .marklogic .client .FailedRequestException ;
1618import com .marklogic .client .DatabaseClientFactory .Authentication ;
1719import com .marklogic .client .Transaction ;
20+ import com .marklogic .client .document .BinaryDocumentManager ;
1821import com .marklogic .client .document .DocumentPage ;
1922import com .marklogic .client .document .DocumentRecord ;
2023import com .marklogic .client .document .DocumentWriteSet ;
2124import com .marklogic .client .document .XMLDocumentManager ;
2225import com .marklogic .client .io .DOMHandle ;
2326import com .marklogic .client .io .DocumentMetadataHandle ;
27+ import com .marklogic .client .io .FileHandle ;
2428import com .marklogic .client .io .Format ;
2529import com .marklogic .client .io .DocumentMetadataHandle .Capability ;
2630import com .marklogic .client .io .DocumentMetadataHandle .DocumentCollections ;
@@ -42,7 +46,7 @@ public static void setUpBeforeClass() throws Exception {
4246 setupJavaRESTServer (dbName , fNames [0 ], restServerName ,restPort );
4347 createRESTUser ("app-user" , "password" ,"rest-writer" ,"rest-reader" );
4448 createRESTUserWithPermissions ("usr1" , "password" ,getPermissionNode ("eval" ,Capability .READ ),getCollectionNode ("http://permission-collections/" ), "rest-writer" ,"rest-reader" );
45- setMaintainLastModified ( dbName , true );
49+
4650 }
4751 @ AfterClass
4852 public static void tearDownAfterClass () throws Exception {
@@ -87,14 +91,14 @@ public void validateMetadata(DocumentMetadataHandle mh){
8791 // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|";
8892 String actualProperties = getDocumentPropertiesString (properties );
8993 boolean result = actualProperties .contains ("size:5|" );
90- System .out .println (actualProperties );
94+ // System.out.println(actualProperties);
9195 assertTrue ("Document properties count" , result );
9296
9397 // Permissions
9498 String expectedPermissions1 = "size:4|rest-reader:[READ]|eval:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|" ;
9599 String expectedPermissions2 = "size:4|rest-reader:[READ]|eval:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|" ;
96100 String actualPermissions = getDocumentPermissionsString (permissions );
97- System .out .println (actualPermissions );
101+ // System.out.println(actualPermissions);
98102 if (actualPermissions .contains ("[UPDATE, READ]" ))
99103 assertEquals ("Document permissions difference" , expectedPermissions1 , actualPermissions );
100104 else if (actualPermissions .contains ("[READ, UPDATE]" ))
@@ -136,7 +140,8 @@ public void validateRecord(DocumentRecord record,Format type) {
136140 }
137141/*
138142This is a basic test with transaction, and doing commit
139- */ @ Test
143+ */
144+ @ Test
140145public void testBulkWritewithTransactionCommit () throws Exception {
141146
142147 int count =1 ;
@@ -172,7 +177,7 @@ public void testBulkWritewithTransactionCommit() throws Exception {
172177 count ++;
173178 }
174179 }catch (Exception e )
175- {System .out .println (e .getMessage ());}
180+ {System .out .println (e .getMessage ());throw e ; }
176181 assertEquals ("document count" , 102 ,count );
177182
178183 }
@@ -234,6 +239,7 @@ public void testBulkWritewithTransactionsNoCommit() throws Exception {
234239 }catch (Exception e ){
235240 System .out .println (e .getMessage ());
236241 tstatus =true ;
242+ throw e ;
237243 }
238244 finally {
239245 if (tstatus ) {t1 .rollback ();}
@@ -261,10 +267,12 @@ public void testBulkWritewithTransactionsNoCommit() throws Exception {
261267 if (count %BATCH_SIZE > 0 ){
262268 docMgr .write (writeset );
263269 }
264- count =0 ;
270+ count =1 ;
271+ HashMap <String ,String > map2 = new HashMap <String ,String >();
265272 DocumentMetadataHandle mh = setMetadata ();
266273 for (int i =0 ;i <102 ;i ++){
267- writeset .add (DIRECTORY +"sec" +i +".xml" ,mh , new DOMHandle (getDocumentContent ("This is so sec" +i )));
274+ writeset .add (DIRECTORY +"sec" +i +".xml" ,mh , new DOMHandle (getDocumentContent ("This is with metadata" +i )));
275+ map2 .put (DIRECTORY +"sec" +i +".xml" , convertXMLDocumentToString (getDocumentContent ("This is with metadata" +i )));
268276 if (count %BATCH_SIZE == 0 ){
269277 docMgr .write (writeset ,t1 );
270278 writeset = docMgr .newWriteSet ();
@@ -286,8 +294,8 @@ public void testBulkWritewithTransactionsNoCommit() throws Exception {
286294 DocumentRecord rec = page .next ();
287295 validateRecord (rec ,Format .XML );
288296 rec .getContent (dh );
289- assertEquals ("Comparing the content :" ,map .get (rec .getUri ()),convertXMLDocumentToString (dh .get ()));
290- docMgr .readMetadata (rec .getUri (), mh2 );
297+ assertEquals ("Comparing the content :" ,map2 .get (rec .getUri ()),convertXMLDocumentToString (dh .get ()));
298+ docMgr .readMetadata (rec .getUri (), mh2 , t1 );
291299 validateMetadata (mh2 );
292300 count ++;
293301 }
@@ -312,6 +320,7 @@ public void testBulkWritewithTransactionsNoCommit() throws Exception {
312320 }catch (Exception e ){
313321 System .out .println (e .getMessage ());
314322 tstatus =true ;
323+ throw e ;
315324 }finally {
316325 if (tstatus ){
317326 t1 .rollback ();
@@ -332,7 +341,8 @@ public void testBulkWritewithTransactionsNoCommit() throws Exception {
332341 DocumentWriteSet writeset =docMgr .newWriteSet ();
333342 DocumentMetadataHandle mh = setMetadata ();
334343 for (int i =0 ;i <102 ;i ++){
335- writeset .add (DIRECTORY +"sec" +i +".xml" ,mh , new DOMHandle (getDocumentContent ("This is so sec" +i )));
344+ writeset .add (DIRECTORY +"third" +i +".xml" ,mh , new DOMHandle (getDocumentContent ("This is third" +i )));
345+ map .put (DIRECTORY +"third" +i +".xml" , convertXMLDocumentToString (getDocumentContent ("This is third" +i )));
336346 if (count %BATCH_SIZE == 0 ){
337347 docMgr .write (writeset ,t1 );
338348 writeset = docMgr .newWriteSet ();
@@ -344,19 +354,19 @@ public void testBulkWritewithTransactionsNoCommit() throws Exception {
344354 }
345355 String uris [] = new String [102 ];
346356 for (int i =0 ;i <102 ;i ++){
347- uris [i ]=DIRECTORY +"sec " +i +".xml" ;
357+ uris [i ]=DIRECTORY +"third " +i +".xml" ;
348358 }
349359 count =0 ;
350- docMgr = c .newXMLDocumentManager ();
351- DocumentPage page = docMgr .read (t1 ,uris );
360+ XMLDocumentManager dMgr = c .newXMLDocumentManager ();
361+ DocumentPage page = dMgr .read (t1 ,uris );
352362 DOMHandle dh = new DOMHandle ();
353363 DocumentMetadataHandle mh2 = new DocumentMetadataHandle ();
354364 while (page .hasNext ()){
355365 DocumentRecord rec = page .next ();
356366 validateRecord (rec ,Format .XML );
357367 rec .getContent (dh );
358368 assertEquals ("Comparing the content :" ,map .get (rec .getUri ()),convertXMLDocumentToString (dh .get ()));
359- docMgr .readMetadata (rec .getUri (), mh2 );
369+ dMgr .readMetadata (rec .getUri (), mh2 , t1 );
360370 validateMetadata (mh2 );
361371 count ++;
362372 }
@@ -367,6 +377,7 @@ public void testBulkWritewithTransactionsNoCommit() throws Exception {
367377 }catch (Exception e ){
368378 System .out .println (e .getMessage ());
369379 tstatus =true ;
380+ throw e ;
370381 }finally {
371382 if (tstatus ){
372383 t1 .rollback ();
@@ -375,4 +386,52 @@ public void testBulkWritewithTransactionsNoCommit() throws Exception {
375386 }
376387
377388}
389+
390+ @ Test (expected = FailedRequestException .class )
391+ public void testBulkWritewithTransactionCommitTimeOut () throws Exception {
392+
393+ int count =1 ;
394+ String docId [] = {"Sega-4MB.jpg" };
395+ // boolean tstatus =false;
396+ Transaction t = client .openTransaction ("timeoutTrans" ,3 );
397+ BinaryDocumentManager docMgr = client .newBinaryDocumentManager ();
398+
399+ DocumentWriteSet writeset =docMgr .newWriteSet ();
400+ File file1 = null ;
401+ file1 = new File ("src/test/java/com/marklogic/javaclient/data/" + docId [0 ]);
402+ FileHandle h1 = new FileHandle (file1 );
403+ for (int i =0 ;i <102 ;i ++){
404+ writeset .add (DIRECTORY +"binary" +i +".jpg" , h1 );
405+ if (count %BATCH_SIZE == 0 ){
406+ docMgr .write (writeset ,t );
407+ writeset = docMgr .newWriteSet ();
408+ }
409+ count ++;
410+ }
411+ if (count %BATCH_SIZE > 0 ){
412+ docMgr .write (writeset ,t );
413+ }
414+ t .commit ();
415+ String uris [] = new String [102 ];
416+ for (int i =0 ;i <102 ;i ++){
417+ uris [i ]=DIRECTORY +"binary" +i +".jpg" ;
418+ }
419+ count =0 ;
420+ FileHandle rh = new FileHandle ();
421+ DocumentPage page = docMgr .read (uris );
422+ while (page .hasNext ()){
423+ DocumentRecord rec = page .next ();
424+ validateRecord (rec ,Format .BINARY );
425+ rec .getContent (rh );
426+ assertEquals ("Content length :" ,file1 .length (),rh .get ().length ());
427+ count ++;
428+ }
429+ assertEquals ("document count" , 102 ,count );
430+
431+
432+
433+ }
434+
435+
436+
378437}
0 commit comments