@@ -48,7 +48,7 @@ public void testPutDeletedDocument() throws CouchbaseLiteException {
4848 properties .put ("mykey" , "myval" );
4949 SavedRevision newRev = document .putProperties (properties );
5050 newRev .loadProperties ();
51- assertTrue ( newRev .getProperties ().containsKey ("mykey" ) );
51+ assertTrue (newRev .getProperties ().containsKey ("mykey" ));
5252
5353 Assert .assertTrue (document .isDeleted ());
5454 Document fetchedDoc = database .getExistingDocument (docId );
@@ -527,4 +527,85 @@ public void changed(Document.ChangeEvent event) {
527527 boolean success = documentChanged .await (30 , TimeUnit .SECONDS );
528528 assertTrue (success );
529529 }
530+
531+ /**
532+ * https://github.com/couchbase/couchbase-lite-android/issues/563
533+ * Updating a document in a transaction block twice using Document.DocumentUpdater results in an infinite loop
534+ *
535+ * NOTE: Use Document.update()
536+ */
537+ public void testMultipleUpdatesInTransactionWithUpdate () throws CouchbaseLiteException {
538+ Log .e (TAG , "testMultipleUpdatesInTransactionWithUpdate() START" );
539+ final Document doc = database .createDocument ();
540+ HashMap <String , Object > properties = new HashMap <String , Object >();
541+ properties .put ("key" , "value1" );
542+ doc .putProperties (properties );
543+ database .runInTransaction (
544+ new TransactionalTask () {
545+ @ Override
546+ public boolean run () {
547+ try {
548+ doc .update (new Document .DocumentUpdater () {
549+ @ Override
550+ public boolean update (UnsavedRevision newRevision ) {
551+ Log .e ("updateDocumentInTransactionTest" , "Trying to update key to value 2" );
552+ Map <String , Object > properties = newRevision .getUserProperties ();
553+ properties .put ("key" , "value2" );
554+ newRevision .setUserProperties (properties );
555+ return true ;
556+ }
557+ });
558+ doc .update (new Document .DocumentUpdater () {
559+ @ Override
560+ public boolean update (UnsavedRevision newRevision ) {
561+ Log .e ("updateDocumentInTransactionTest" , "Trying to update key to value 3" );
562+ Map <String , Object > properties = newRevision .getUserProperties ();
563+ properties .put ("key" , "value3" );
564+ newRevision .setUserProperties (properties );
565+ return true ;
566+ }
567+ });
568+ } catch (CouchbaseLiteException e ) {
569+ Log .e ("updateDocumentInTransactionTest" , "Trying to update key to value 2" );
570+ return false ;
571+ }
572+ return true ;
573+ }
574+ });
575+ Map <String , Object > properties4 = doc .getProperties ();
576+ Log .e (TAG , "properties4 = " + properties4 );
577+ Log .e (TAG , "testMultipleUpdatesInTransactionWithUpdate() END" );
578+ }
579+
580+ /**
581+ * Note: Document.putProperties()
582+ */
583+ public void testMultipleUpdatesInTransactionWithPutProperties () throws CouchbaseLiteException {
584+ Log .e (TAG , "testMultipleUpdatesInTransactionWithPutProperties() START" );
585+ final Document doc = database .createDocument ();
586+ HashMap <String , Object > properties1 = new HashMap <String , Object >();
587+ properties1 .put ("key" , "value1" );
588+ doc .putProperties (properties1 );
589+ database .runInTransaction (
590+ new TransactionalTask () {
591+ @ Override
592+ public boolean run () {
593+ try {
594+ Map <String , Object > properties2 = new HashMap <String , Object >(doc .getProperties ());
595+ properties2 .put ("key" , "value2" );
596+ doc .putProperties (properties2 );
597+ Map <String , Object > properties3 = new HashMap <String , Object >(doc .getProperties ());
598+ properties3 .put ("key" , "value3" );
599+ doc .putProperties (properties3 );
600+ } catch (CouchbaseLiteException e ) {
601+ Log .e ("updateDocumentInTransactionTest" , "Trying to update key to value 2" );
602+ return false ;
603+ }
604+ return true ;
605+ }
606+ });
607+ Map <String , Object > properties4 = doc .getProperties ();
608+ Log .e (TAG , "properties4 = " +properties4 );
609+ Log .e (TAG , "testMultipleUpdatesInTransactionWithPutProperties() END" );
610+ }
530611}
0 commit comments