/// <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); }
/// <summary>https://github.com/couchbase/couchbase-lite-android/issues/134</summary> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> /// <exception cref="System.IO.IOException"></exception> public virtual void TestGetAttachmentBodyUsingPrefetch() { // add a doc with an attachment Document doc = database.CreateDocument(); UnsavedRevision rev = doc.CreateRevision(); IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("foo", "bar"); rev.SetUserProperties(properties); byte[] attachBodyBytes = Sharpen.Runtime.GetBytesForString("attach body"); Attachment attachment = new Attachment(new ByteArrayInputStream(attachBodyBytes), "text/plain"); string attachmentName = "test_attachment.txt"; rev.AddAttachment(attachment, attachmentName); rev.Save(); // do query that finds that doc with prefetch View view = database.GetView("aview"); view.SetMapReduce(new _Mapper_432(), null, "1"); // try to get the attachment Query query = view.CreateQuery(); query.SetPrefetch(true); QueryEnumerator results = query.Run(); while (results.HasNext()) { QueryRow row = results.Next(); // This returns the revision just fine, but the sequence number // is set to 0. SavedRevision revision = row.GetDocument().GetCurrentRevision(); IList <string> attachments = revision.GetAttachmentNames(); // This returns an Attachment object which looks ok, except again // its sequence number is 0. The metadata property knows about // the length and mime type of the attachment. It also says // "stub" -> "true". Attachment attachmentRetrieved = revision.GetAttachment(attachmentName); // This throws a CouchbaseLiteException with Status.NOT_FOUND. InputStream @is = attachmentRetrieved.GetContent(); NUnit.Framework.Assert.IsNotNull(@is); byte[] attachmentDataRetrieved = TextUtils.Read(@is); string attachmentDataRetrievedString = Sharpen.Runtime.GetStringForBytes(attachmentDataRetrieved ); string attachBodyString = Sharpen.Runtime.GetStringForBytes(attachBodyBytes); NUnit.Framework.Assert.AreEqual(attachBodyString, attachmentDataRetrievedString); } }