Beispiel #1
0
        public static Document CreateDocumentWithProperties(Database db, IDictionary <string
                                                                                      , object> properties)
        {
            Document doc = db.CreateDocument();

            NUnit.Framework.Assert.IsNotNull(doc);
            NUnit.Framework.Assert.IsNull(doc.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNull(doc.GetCurrentRevision());
            NUnit.Framework.Assert.IsNotNull("Document has no ID", doc.GetId());
            // 'untitled' docs are no longer untitled (8/10/12)
            try
            {
                doc.PutProperties(properties);
            }
            catch (Exception e)
            {
                Log.E(Tag, "Error creating document", e);
                NUnit.Framework.Assert.IsTrue("can't create new document in db:" + db.GetName() +
                                              " with properties:" + properties.ToString(), false);
            }
            NUnit.Framework.Assert.IsNotNull(doc.GetId());
            NUnit.Framework.Assert.IsNotNull(doc.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNotNull(doc.GetUserProperties());
            // should be same doc instance, since there should only ever be a single Document instance for a given document
            NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()), doc);
            NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()).GetId(), doc.GetId());
            return(doc);
        }
        public static Document CreateDocumentWithProperties(Database db, IDictionary <string
                                                                                      , object> properties)
        {
            Document doc = db.CreateDocument();

            NUnit.Framework.Assert.IsNotNull(doc);
            NUnit.Framework.Assert.IsNull(doc.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNull(doc.GetCurrentRevision());
            NUnit.Framework.Assert.IsNotNull("Document has no ID", doc.GetId());
            // 'untitled' docs are no longer untitled (8/10/12)
            try
            {
                doc.PutProperties(properties);
            }
            catch (Exception e)
            {
                Log.E(Tag, "Error creating document", e);
                NUnit.Framework.Assert.IsTrue("can't create new document in db:" + db.GetName() +
                                              " with properties:" + properties.ToString(), false);
            }
            NUnit.Framework.Assert.IsNotNull(doc.GetId());
            NUnit.Framework.Assert.IsNotNull(doc.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNotNull(doc.GetUserProperties());
            // this won't work until the weakref hashmap is implemented which stores all docs
            // Assert.assertEquals(db.getDocument(doc.getId()), doc);
            NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()).GetId(), doc.GetId());
            return(doc);
        }
Beispiel #3
0
        public override bool Equals(object o)
        {
            bool result = false;

            if (o is SavedRevision)
            {
                SavedRevision other = (SavedRevision)o;
                if (document.GetId().Equals(other.GetDocument().GetId()) && GetId().Equals(other.
                                                                                           GetId()))
                {
                    result = true;
                }
            }
            return(result);
        }
        /// <exception cref="System.Exception"></exception>
        public virtual void TestCorrectWinningRevisionTiebreaker()
        {
            // Create a conflict on purpose
            Document      doc   = database.CreateDocument();
            SavedRevision rev1  = doc.CreateRevision().Save();
            SavedRevision rev2a = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision rev2b = CreateRevisionWithRandomProps(rev1, true);
            // the tiebreaker will happen based on which rev hash has lexicographically higher sort order
            SavedRevision expectedWinner = null;

            if (Sharpen.Runtime.CompareOrdinal(rev2a.GetId(), rev2b.GetId()) > 0)
            {
                expectedWinner = rev2a;
            }
            else
            {
                if (Sharpen.Runtime.CompareOrdinal(rev2a.GetId(), rev2b.GetId()) < 0)
                {
                    expectedWinner = rev2b;
                }
            }
            RevisionInternal revFound = database.GetDocumentWithIDAndRev(doc.GetId(), null, EnumSet
                                                                         .NoneOf <Database.TDContentOptions>());

            NUnit.Framework.Assert.AreEqual(expectedWinner.GetId(), revFound.GetRevId());
        }
        // Reproduces issue #167
        // https://github.com/couchbase/couchbase-lite-android/issues/167
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestLoadRevisionBody()
        {
            Document document = database.CreateDocument();
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("foo", "foo");
            properties.Put("bar", false);
            document.PutProperties(properties);
            NUnit.Framework.Assert.IsNotNull(document.GetCurrentRevision());
            bool             deleted          = false;
            RevisionInternal revisionInternal = new RevisionInternal(document.GetId(), document
                                                                     .GetCurrentRevisionId(), deleted, database);
            EnumSet <Database.TDContentOptions> contentOptions = EnumSet.Of(Database.TDContentOptions
                                                                            .TDIncludeAttachments, Database.TDContentOptions.TDBigAttachmentsFollow);

            database.LoadRevisionBody(revisionInternal, contentOptions);
            // now lets purge the document, and then try to load the revision body again
            NUnit.Framework.Assert.IsTrue(document.Purge());
            bool gotExpectedException = false;

            try
            {
                database.LoadRevisionBody(revisionInternal, contentOptions);
            }
            catch (CouchbaseLiteException e)
            {
                if (e.GetCBLStatus().GetCode() == Status.NotFound)
                {
                    gotExpectedException = true;
                }
            }
            NUnit.Framework.Assert.IsTrue(gotExpectedException);
        }
 protected internal UnsavedRevision(Document document, SavedRevision parentRevision
     ) : base(document)
 {
     if (parentRevision == null)
     {
         parentRevID = null;
     }
     else
     {
         parentRevID = parentRevision.GetId();
     }
     IDictionary<string, object> parentRevisionProperties;
     if (parentRevision == null)
     {
         parentRevisionProperties = null;
     }
     else
     {
         parentRevisionProperties = parentRevision.GetProperties();
     }
     if (parentRevisionProperties == null)
     {
         properties = new Dictionary<string, object>();
         properties.Put("_id", document.GetId());
         if (parentRevID != null)
         {
             properties.Put("_rev", parentRevID);
         }
     }
     else
     {
         properties = new Dictionary<string, object>(parentRevisionProperties);
     }
 }
Beispiel #7
0
        /// <exception cref="System.Exception"></exception>
        public virtual void TestPruneRevsToMaxDepth()
        {
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("testName", "testDatabaseCompaction");
            properties.Put("tag", 1337);
            Document      doc = CreateDocumentWithProperties(database, properties);
            SavedRevision rev = doc.GetCurrentRevision();

            database.SetMaxRevTreeDepth(1);
            for (int i = 0; i < 10; i++)
            {
                IDictionary <string, object> properties2 = new Dictionary <string, object>(properties
                                                                                           );
                properties2.Put("tag", i);
                rev = rev.CreateRevision(properties2);
            }
            int numPruned = database.PruneRevsToMaxDepth(1);

            NUnit.Framework.Assert.AreEqual(9, numPruned);
            Document fetchedDoc             = database.GetDocument(doc.GetId());
            IList <SavedRevision> revisions = fetchedDoc.GetRevisionHistory();

            NUnit.Framework.Assert.AreEqual(1, revisions.Count);
            numPruned = database.PruneRevsToMaxDepth(1);
            NUnit.Framework.Assert.AreEqual(0, numPruned);
        }
            private bool IsMemberOfTheCurrentList(Couchbase.Lite.Document user)
            {
                IList <string> members = (IList <string>) this._enclosing.mCurrentList.GetProperty("members"
                                                                                                   );

                return(members != null?members.Contains(user.GetId()) : false);
            }
        protected internal UnsavedRevision(Document document, SavedRevision parentRevision
                                           ) : base(document)
        {
            if (parentRevision == null)
            {
                parentRevID = null;
            }
            else
            {
                parentRevID = parentRevision.GetId();
            }
            IDictionary <string, object> parentRevisionProperties;

            if (parentRevision == null)
            {
                parentRevisionProperties = null;
            }
            else
            {
                parentRevisionProperties = parentRevision.GetProperties();
            }
            if (parentRevisionProperties == null)
            {
                properties = new Dictionary <string, object>();
                properties.Put("_id", document.GetId());
                if (parentRevID != null)
                {
                    properties.Put("_rev", parentRevID);
                }
            }
            else
            {
                properties = new Dictionary <string, object>(parentRevisionProperties);
            }
        }
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestDeleteDocument()
        {
            Document document = database.CreateDocument();
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("foo", "foo");
            properties.Put("bar", false);
            document.PutProperties(properties);
            NUnit.Framework.Assert.IsNotNull(document.GetCurrentRevision());
            string docId = document.GetId();

            document.Delete();
            NUnit.Framework.Assert.IsTrue(document.IsDeleted());
            Document fetchedDoc = database.GetExistingDocument(docId);

            NUnit.Framework.Assert.IsNull(fetchedDoc);
            // query all docs and make sure we don't see that document
            database.GetAllDocs(new QueryOptions());
            Query           queryAllDocs    = database.CreateAllDocumentsQuery();
            QueryEnumerator queryEnumerator = queryAllDocs.Run();

            for (IEnumerator <QueryRow> it = queryEnumerator; it.HasNext();)
            {
                QueryRow row = it.Next();
                NUnit.Framework.Assert.IsFalse(row.GetDocument().GetId().Equals(docId));
            }
        }
Beispiel #11
0
        /// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/106</summary>
        /// <exception cref="System.Exception"></exception>
        public virtual void TestResolveConflict()
        {
            IDictionary <string, object> result;
            // Create a conflict on purpose
            Document      doc        = database.CreateDocument();
            SavedRevision rev1       = doc.CreateRevision().Save();
            SavedRevision rev2a      = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision rev2b      = CreateRevisionWithRandomProps(rev1, true);
            SavedRevision winningRev = null;
            SavedRevision losingRev  = null;

            if (doc.GetCurrentRevisionId().Equals(rev2a.GetId()))
            {
                winningRev = rev2a;
                losingRev  = rev2b;
            }
            else
            {
                winningRev = rev2b;
                losingRev  = rev2a;
            }
            NUnit.Framework.Assert.AreEqual(2, doc.GetConflictingRevisions().Count);
            NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count);
            result = (IDictionary <string, object>)Send("GET", string.Format("/%s/%s?conflicts=true"
                                                                             , DefaultTestDb, doc.GetId()), Status.Ok, null);
            IList <string> conflicts = (IList)result.Get("_conflicts");

            NUnit.Framework.Assert.AreEqual(1, conflicts.Count);
            string conflictingRevId = conflicts[0];

            NUnit.Framework.Assert.AreEqual(losingRev.GetId(), conflictingRevId);
            long docNumericID = database.GetDocNumericID(doc.GetId());

            NUnit.Framework.Assert.IsTrue(docNumericID != 0);
            NUnit.Framework.Assert.IsNotNull(database.GetDocument(doc.GetId()));
            Log.D(Tag, "docNumericID for " + doc.GetId() + " is: " + docNumericID);
            result = (IDictionary <string, object>)Send("DELETE", string.Format("/%s/%s?rev=%s"
                                                                                , DefaultTestDb, doc.GetId(), conflictingRevId), Status.Ok, null);
            result = (IDictionary <string, object>)Send("GET", string.Format("/%s/%s?conflicts=true"
                                                                             , DefaultTestDb, doc.GetId()), Status.Ok, null);
            conflicts = (IList)result.Get("_conflicts");
            NUnit.Framework.Assert.AreEqual(0, conflicts.Count);
        }
Beispiel #12
0
        /// <exception cref="System.Exception"></exception>
        public virtual void TestCache()
        {
            int   retainCount = 1;
            Cache cache       = new Cache <string, Document>(retainCount);
            IDictionary <string, object> props = new Dictionary <string, object>();

            props.Put("foo", "bar");
            Document doc1 = CreateDocumentWithProperties(database, props);

            cache.Put(doc1.GetId(), doc1);
            IDictionary <string, object> props2 = new Dictionary <string, object>();

            props2.Put("foo2", "bar2");
            Document doc2 = CreateDocumentWithProperties(database, props2);

            cache.Put(doc2.GetId(), doc2);
            NUnit.Framework.Assert.IsNotNull(cache.Get(doc1.GetId()));
            NUnit.Framework.Assert.IsNotNull(cache.Get(doc2.GetId()));
            cache.Remove(doc1.GetId());
            NUnit.Framework.Assert.IsNull(cache.Get(doc1.GetId()));
            cache.Clear();
            NUnit.Framework.Assert.IsNull(cache.Get(doc2.GetId()));
        }
        /// <exception cref="System.Exception"></exception>
        public virtual void TestCorrectWinningRevisionLongerBranch()
        {
            // Create a conflict on purpose
            Document      doc   = database.CreateDocument();
            SavedRevision rev1  = doc.CreateRevision().Save();
            SavedRevision rev2a = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision rev2b = CreateRevisionWithRandomProps(rev1, true);
            SavedRevision rev3b = CreateRevisionWithRandomProps(rev2b, true);
            // rev3b should be picked as the winner since it has a longer branch
            SavedRevision    expectedWinner = rev3b;
            RevisionInternal revFound       = database.GetDocumentWithIDAndRev(doc.GetId(), null, EnumSet
                                                                               .NoneOf <Database.TDContentOptions>());

            NUnit.Framework.Assert.AreEqual(expectedWinner.GetId(), revFound.GetRevId());
        }
        /// <exception cref="System.Exception"></exception>
        public virtual void TestWinningRevIDOfDoc()
        {
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("testName", "testCreateRevisions");
            properties.Put("tag", 1337);
            IDictionary <string, object> properties2a = new Dictionary <string, object>();

            properties2a.Put("testName", "testCreateRevisions");
            properties2a.Put("tag", 1338);
            IDictionary <string, object> properties2b = new Dictionary <string, object>();

            properties2b.Put("testName", "testCreateRevisions");
            properties2b.Put("tag", 1339);
            IList <bool> outIsDeleted  = new AList <bool>();
            IList <bool> outIsConflict = new AList <bool>();
            // Create a conflict on purpose
            Document        doc     = database.CreateDocument();
            UnsavedRevision newRev1 = doc.CreateRevision();

            newRev1.SetUserProperties(properties);
            SavedRevision rev1         = newRev1.Save();
            long          docNumericId = database.GetDocNumericID(doc.GetId());

            NUnit.Framework.Assert.IsTrue(docNumericId != 0);
            NUnit.Framework.Assert.AreEqual(rev1.GetId(), database.WinningRevIDOfDoc(docNumericId
                                                                                     , outIsDeleted, outIsConflict));
            NUnit.Framework.Assert.IsTrue(outIsConflict.Count == 0);
            outIsDeleted  = new AList <bool>();
            outIsConflict = new AList <bool>();
            UnsavedRevision newRev2a = rev1.CreateRevision();

            newRev2a.SetUserProperties(properties2a);
            SavedRevision rev2a = newRev2a.Save();

            NUnit.Framework.Assert.AreEqual(rev2a.GetId(), database.WinningRevIDOfDoc(docNumericId
                                                                                      , outIsDeleted, outIsConflict));
            NUnit.Framework.Assert.IsTrue(outIsConflict.Count == 0);
            outIsDeleted  = new AList <bool>();
            outIsConflict = new AList <bool>();
            UnsavedRevision newRev2b = rev1.CreateRevision();

            newRev2b.SetUserProperties(properties2b);
            SavedRevision rev2b = newRev2b.Save(true);

            database.WinningRevIDOfDoc(docNumericId, outIsDeleted, outIsConflict);
            NUnit.Framework.Assert.IsTrue(outIsConflict.Count > 0);
        }
Beispiel #15
0
        /// <summary>https://github.com/couchbase/couchbase-lite-android/issues/281</summary>
        public virtual void TestDocumentWithRemovedProperty()
        {
            IDictionary <string, object> props = new Dictionary <string, object>();

            props.Put("_id", "fakeid");
            props.Put("_removed", true);
            props.Put("foo", "bar");
            Document doc = CreateDocumentWithProperties(database, props);

            NUnit.Framework.Assert.IsNotNull(doc);
            Document docFetched = database.GetDocument(doc.GetId());
            IDictionary <string, object> fetchedProps = docFetched.GetCurrentRevision().GetProperties
                                                            ();

            NUnit.Framework.Assert.IsNotNull(fetchedProps.Get("_removed"));
            NUnit.Framework.Assert.IsTrue(docFetched.GetCurrentRevision().IsGone());
        }
        /// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/135</summary>
        /// <exception cref="System.Exception"></exception>
        public virtual void TestCorrectWinningRevisionHighRevisionNumber()
        {
            // Create a conflict on purpose
            Document         doc      = database.CreateDocument();
            SavedRevision    rev1     = doc.CreateRevision().Save();
            SavedRevision    rev2a    = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision    rev2b    = CreateRevisionWithRandomProps(rev1, true);
            SavedRevision    rev3b    = CreateRevisionWithRandomProps(rev2b, true);
            SavedRevision    rev4b    = CreateRevisionWithRandomProps(rev3b, true);
            SavedRevision    rev5b    = CreateRevisionWithRandomProps(rev4b, true);
            SavedRevision    rev6b    = CreateRevisionWithRandomProps(rev5b, true);
            SavedRevision    rev7b    = CreateRevisionWithRandomProps(rev6b, true);
            SavedRevision    rev8b    = CreateRevisionWithRandomProps(rev7b, true);
            SavedRevision    rev9b    = CreateRevisionWithRandomProps(rev8b, true);
            SavedRevision    rev10b   = CreateRevisionWithRandomProps(rev9b, true);
            RevisionInternal revFound = database.GetDocumentWithIDAndRev(doc.GetId(), null, EnumSet
                                                                         .NoneOf <Database.TDContentOptions>());

            NUnit.Framework.Assert.AreEqual(rev10b.GetId(), revFound.GetRevId());
        }
Beispiel #17
0
            public void OnClick(IDialogInterface dialog, int whichButton)
            {
                string title = ((IEditable)input).Text.ToString();

                if (title.Length == 0)
                {
                    return;
                }
                try
                {
                    string currentUserId = ((Application)this._enclosing.GetApplication()).GetCurrentUserId
                                               ();
                    Couchbase.Lite.Document document = List.CreateNewList(this._enclosing.GetDatabase
                                                                              (), title, currentUserId);
                    this._enclosing.DisplayListContent(document.GetId());
                    this._enclosing.InvalidateOptionsMenu();
                }
                catch (CouchbaseLiteException e)
                {
                    Log.E(Application.Tag, "Cannot create a new list", e);
                }
            }
Beispiel #18
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestReplaceDatabaseNamedWithAttachments()
        {
            InputStream dbStream = GetAsset("withattachments.cblite");

            string[] attachmentlist = null;
            IDictionary <string, InputStream> attachments = new Dictionary <string, InputStream
                                                                            >();
            InputStream blobStream = GetAsset("attachments/356a192b7913b04c54574d18c28d46e6395428ab.blob"
                                              );

            attachments.Put("356a192b7913b04c54574d18c28d46e6395428ab.blob", blobStream);
            manager.ReplaceDatabase("replaced2", dbStream, attachments);
            //Validate the number of files in the DB
            NUnit.Framework.Assert.AreEqual(1, manager.GetDatabase("replaced2").GetDocumentCount
                                                ());
            //get the attachment from the document
            Document doc = manager.GetDatabase("replaced2").GetExistingDocument("168e0c56-4588-4df4-8700-4d5115fa9c74"
                                                                                );

            NUnit.Framework.Assert.IsNotNull(doc);
            RevisionInternal gotRev1 = database.GetDocumentWithIDAndRev(doc.GetId(), doc.GetCurrentRevisionId
                                                                            (), EnumSet.NoneOf <Database.TDContentOptions>());
        }
Beispiel #19
0
		protected internal void RemoveDocumentFromCache(Document document)
		{
			docCache.Remove(document.GetId());
		}