예제 #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());
            // 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);
        }
예제 #2
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);
        }
예제 #3
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);
        }
예제 #4
0
        // 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);
        }
예제 #5
0
        /// <summary>Regression test for https://github.com/couchbase/couchbase-lite-android-core/issues/70
        ///     </summary>
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void TestAttachmentDisappearsAfterSave()
        {
            // create a doc with an attachment
            Document             doc     = database.CreateDocument();
            string               content = "This is a test attachment!";
            ByteArrayInputStream body    = new ByteArrayInputStream(Sharpen.Runtime.GetBytesForString
                                                                        (content));
            UnsavedRevision rev = doc.CreateRevision();

            rev.SetAttachment("index.html", "text/plain; charset=utf-8", body);
            rev.Save();
            // make sure the doc's latest revision has the attachment
            IDictionary <string, object> attachments = (IDictionary)doc.GetCurrentRevision().GetProperty
                                                           ("_attachments");

            NUnit.Framework.Assert.IsNotNull(attachments);
            NUnit.Framework.Assert.AreEqual(1, attachments.Count);
            // make sure the rev has the attachment
            attachments = (IDictionary)rev.GetProperty("_attachments");
            NUnit.Framework.Assert.IsNotNull(attachments);
            NUnit.Framework.Assert.AreEqual(1, attachments.Count);
            // create new properties to add
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("foo", "bar");
            // make sure the new rev still has the attachment
            UnsavedRevision rev2 = doc.CreateRevision();

            rev2.GetProperties().PutAll(properties);
            rev2.Save();
            attachments = (IDictionary)rev2.GetProperty("_attachments");
            NUnit.Framework.Assert.IsNotNull(attachments);
            NUnit.Framework.Assert.AreEqual(1, attachments.Count);
        }
예제 #6
0
        /// <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));
            }
        }
예제 #7
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());
        }
예제 #8
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestNewDocumentHasCurrentRevision()
        {
            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.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNotNull(document.GetCurrentRevision());
        }
예제 #9
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> properties = new Dictionary <string, object>();

            properties.Put("testName", "testCreateRevisions");
            properties.Put("tag", 1337);
            // Create a conflict on purpose
            Document        doc     = database.CreateDocument();
            UnsavedRevision newRev1 = doc.CreateRevision();

            newRev1.SetUserProperties(properties);
            SavedRevision rev1       = newRev1.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);
            // let's manually choose the losing rev as the winner.  First, delete winner, which will
            // cause losing rev to be the current revision.
            SavedRevision         deleteRevision       = winningRev.DeleteDocument();
            IList <SavedRevision> conflictingRevisions = doc.GetConflictingRevisions();

            NUnit.Framework.Assert.AreEqual(1, conflictingRevisions.Count);
            NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count);
            NUnit.Framework.Assert.AreEqual(3, deleteRevision.GetGeneration());
            NUnit.Framework.Assert.AreEqual(losingRev.GetId(), doc.GetCurrentRevision().GetId
                                                ());
            // Finally create a new revision rev3 based on losing rev
            SavedRevision rev3 = CreateRevisionWithRandomProps(losingRev, true);

            NUnit.Framework.Assert.AreEqual(rev3.GetId(), doc.GetCurrentRevisionId());
            IList <SavedRevision> conflictingRevisions1 = doc.GetConflictingRevisions();

            NUnit.Framework.Assert.AreEqual(1, conflictingRevisions1.Count);
            NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count);
        }
예제 #10
0
        /// <exception cref="System.Exception"></exception>
        public static Document CreateDocWithAttachment(Database database, string attachmentName
                                                       , string content)
        {
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("foo", "bar");
            Document      doc = CreateDocumentWithProperties(database, properties);
            SavedRevision rev = doc.GetCurrentRevision();

            NUnit.Framework.Assert.AreEqual(rev.GetAttachments().Count, 0);
            NUnit.Framework.Assert.AreEqual(rev.GetAttachmentNames().Count, 0);
            NUnit.Framework.Assert.IsNull(rev.GetAttachment(attachmentName));
            ByteArrayInputStream body = new ByteArrayInputStream(Sharpen.Runtime.GetBytesForString
                                                                     (content));
            UnsavedRevision rev2 = doc.CreateRevision();

            rev2.SetAttachment(attachmentName, "text/plain; charset=utf-8", body);
            SavedRevision rev3 = rev2.Save();

            NUnit.Framework.Assert.IsNotNull(rev3);
            NUnit.Framework.Assert.AreEqual(rev3.GetAttachments().Count, 1);
            NUnit.Framework.Assert.AreEqual(rev3.GetAttachmentNames().Count, 1);
            Attachment attach = rev3.GetAttachment(attachmentName);

            NUnit.Framework.Assert.IsNotNull(attach);
            NUnit.Framework.Assert.AreEqual(doc, attach.GetDocument());
            NUnit.Framework.Assert.AreEqual(attachmentName, attach.GetName());
            IList <string> attNames = new AList <string>();

            attNames.AddItem(attachmentName);
            NUnit.Framework.Assert.AreEqual(rev3.GetAttachmentNames(), attNames);
            NUnit.Framework.Assert.AreEqual("text/plain; charset=utf-8", attach.GetContentType
                                                ());
            NUnit.Framework.Assert.AreEqual(IOUtils.ToString(attach.GetContent(), "UTF-8"), content
                                            );
            NUnit.Framework.Assert.AreEqual(Sharpen.Runtime.GetBytesForString(content).Length
                                            , attach.GetLength());
            return(doc);
        }
예제 #11
0
                public override global::Android.Views.View GetView(int position, global::Android.Views.View convertView
                                                                   , ViewGroup parent)
                {
                    if (convertView == null)
                    {
                        LayoutInflater inflater = (LayoutInflater)parent.GetContext().GetSystemService(Context
                                                                                                       .LayoutInflaterService);
                        convertView = inflater.Inflate(Resource.Layout.view_task, null);
                    }
                    Couchbase.Lite.Document task   = (Couchbase.Lite.Document) this.GetItem(position);
                    Bitmap             image       = null;
                    Bitmap             thumbnail   = null;
                    IList <Attachment> attachments = task.GetCurrentRevision().GetAttachments();

                    if (attachments != null && attachments.Count > 0)
                    {
                        Attachment attachment = attachments[0];
                        try
                        {
                            image     = BitmapFactory.DecodeStream(attachment.GetContent());
                            thumbnail = ThumbnailUtils.ExtractThumbnail(image, MainActivity.TasksFragment.ThumbnailSizePx
                                                                        , MainActivity.TasksFragment.ThumbnailSizePx);
                        }
                        catch (Exception e)
                        {
                            Log.E(Application.Tag, "Cannot decode the attached image", e);
                        }
                    }
                    Bitmap    displayImage = image;
                    ImageView imageView    = (ImageView)convertView.FindViewById(Resource.Id.image);

                    if (thumbnail != null)
                    {
                        imageView.SetImageBitmap(thumbnail);
                    }
                    else
                    {
                        imageView.SetImageDrawable(this._enclosing.GetResources().GetDrawable(Resource.Drawable.
                                                                                              ic_camera_light));
                    }
                    imageView.Click += async(sender, e) => {
                        if (displayImage != null)
                        {
                            DispatchImageViewIntent(displayImage);
                        }
                        else
                        {
                            AttachImage(task);
                        }
                    };
                    TextView text = (TextView)convertView.FindViewById(Resource.Id.text);

                    text.SetText((string)task.GetProperty("title"));
                    CheckBox checkBox        = (CheckBox)convertView.FindViewById(Resource.Id.@checked);
                    bool     checkedProperty = (bool)task.GetProperty("checked");
                    bool     @checked        = checkedProperty != null ? checkedProperty : false;

                    checkBox.SetChecked(@checked);
                    checkBox.Click += async(sender, e) => {
                        try
                        {
                            Task.UpdateCheckedStatus(task, checkBox.IsChecked());
                        }
                        catch (CouchbaseLiteException ex)
                        {
                            Log.E(((CouchbaseSample.Android.Application)Application).Tag, "Cannot update checked status", e);
                        }
                    };
                    return(convertView);
                }