|
23 | 23 | import com.cloudant.sync.documentstore.AttachmentException; |
24 | 24 | import com.cloudant.sync.documentstore.AttachmentNotSavedException; |
25 | 25 | import com.cloudant.sync.documentstore.ConflictException; |
| 26 | +import com.cloudant.sync.documentstore.DocumentBodyFactory; |
26 | 27 | import com.cloudant.sync.documentstore.DocumentException; |
27 | 28 | import com.cloudant.sync.documentstore.DocumentRevision; |
| 29 | +import com.cloudant.sync.documentstore.DocumentStore; |
28 | 30 | import com.cloudant.sync.documentstore.UnsavedFileAttachment; |
29 | 31 | import com.cloudant.sync.documentstore.UnsavedStreamAttachment; |
30 | 32 | import com.cloudant.sync.documentstore.encryption.NullKeyProvider; |
|
45 | 47 | import java.nio.charset.Charset; |
46 | 48 | import java.util.ArrayList; |
47 | 49 | import java.util.Collections; |
| 50 | +import java.util.HashMap; |
48 | 51 | import java.util.List; |
49 | 52 | import java.util.Map; |
50 | 53 | import java.util.regex.Matcher; |
@@ -411,4 +414,54 @@ public void attachmentOrderingTest() throws Exception { |
411 | 414 | Matcher m = p.matcher(regex); |
412 | 415 | Assert.assertTrue("Should match pattern", m.matches()); |
413 | 416 | } |
| 417 | + |
| 418 | + // test for regression on fetching attachments after deleting revision which previously had no |
| 419 | + // attachments |
| 420 | + @Test |
| 421 | + public void getAttachmentsOnDeletedRevisionNoAttachments() throws Exception { |
| 422 | + |
| 423 | + DocumentRevision revision = new DocumentRevision(); |
| 424 | + Map<String, Object> body = new HashMap<String, Object>(); |
| 425 | + body.put("animal", "cat"); |
| 426 | + revision.setBody(DocumentBodyFactory.create(body)); |
| 427 | + |
| 428 | + // create rev |
| 429 | + DocumentRevision saved = documentStore.database().create(revision); |
| 430 | + Map<String, Attachment> attachments = saved.getAttachments(); |
| 431 | + Assert.assertTrue(attachments.isEmpty()); |
| 432 | + |
| 433 | + DocumentRevision deleted = documentStore.database().delete(saved); |
| 434 | + attachments = deleted.getAttachments(); |
| 435 | + |
| 436 | + Assert.assertTrue(attachments.isEmpty()); |
| 437 | + } |
| 438 | + |
| 439 | + |
| 440 | + // test for regression on fetching attachments after deleting revision which previously had some |
| 441 | + // attachments |
| 442 | + @Test |
| 443 | + public void getAttachmentsOnDeletedRevisionSomeAttachments() throws Exception { |
| 444 | + |
| 445 | + DocumentRevision revision = new DocumentRevision(); |
| 446 | + Map<String, Object> body = new HashMap<String, Object>(); |
| 447 | + body.put("animal", "cat"); |
| 448 | + revision.setBody(DocumentBodyFactory.create(body)); |
| 449 | + |
| 450 | + // add attachment |
| 451 | + String imageAttachmentName = "bonsai-boston.jpg"; |
| 452 | + File imageFile = TestUtils.loadFixture("fixture/" + imageAttachmentName); |
| 453 | + Attachment att = new UnsavedFileAttachment(imageFile, "image/jpeg"); |
| 454 | + revision.getAttachments().put(imageAttachmentName, att); |
| 455 | + |
| 456 | + //create rev |
| 457 | + DocumentRevision saved = documentStore.database().create(revision); |
| 458 | + Map<String, Attachment> attachments = saved.getAttachments(); |
| 459 | + Assert.assertEquals(attachments.size(), 1); |
| 460 | + |
| 461 | + DocumentRevision deleted = documentStore.database().delete(saved); |
| 462 | + attachments = deleted.getAttachments(); |
| 463 | + |
| 464 | + Assert.assertTrue(attachments.isEmpty()); |
| 465 | + } |
| 466 | + |
414 | 467 | } |
0 commit comments