1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.tck.tests.versioning;
20
21 import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
22 import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.SKIPPED;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.UnsupportedEncodingException;
26 import java.math.BigInteger;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.chemistry.opencmis.client.api.Document;
31 import org.apache.chemistry.opencmis.client.api.Folder;
32 import org.apache.chemistry.opencmis.client.api.ObjectId;
33 import org.apache.chemistry.opencmis.client.api.Session;
34 import org.apache.chemistry.opencmis.commons.data.ContentStream;
35 import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
36 import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
37 import org.apache.chemistry.opencmis.tck.CmisTestResult;
38 import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
39
40 public class VersionDeleteTest extends AbstractSessionTest {
41
42 @Override
43 public void init(Map<String, String> parameters) {
44 super.init(parameters);
45 setName("Versioning Delete Test");
46 setDescription("Creates a document, adds three versions and deletes the current version until the document is gone.");
47 }
48
49 @Override
50 public void run(Session session) {
51 try {
52
53 Folder testFolder = createTestFolder(session);
54 Document doc = createDocument(session, testFolder, "versiondeletetest.txt", "v1");
55 DocumentTypeDefinition docType = (DocumentTypeDefinition) doc.getType();
56
57 if (!docType.isVersionable()) {
58 addResult(createResult(SKIPPED, "Test type is not versionable. Test skipped!"));
59 doc.delete(true);
60 return;
61 }
62
63
64 Document doc2 = createVersion(session, doc, "v2", 2);
65 Document doc3 = createVersion(session, doc2, "v3", 3);
66 Document doc4 = createVersion(session, doc3, "v4", 4);
67
68
69 deleteVersion(doc4, doc3, 4);
70 deleteVersion(doc3, doc2, 3);
71 deleteVersion(doc2, doc, 2);
72 deleteVersion(doc, null, 1);
73
74 } finally {
75 deleteTestFolder();
76 }
77 }
78
79 private Document createVersion(Session session, Document doc, String content, int version) {
80 CmisTestResult f;
81
82
83 ObjectId pwcId = doc.checkOut();
84 Document pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
85
86 addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC " + version + " compliance"));
87
88
89 byte[] contentBytes;
90 try {
91 contentBytes = content.getBytes("UTF-8");
92 } catch (UnsupportedEncodingException e) {
93 contentBytes = content.getBytes();
94 }
95 ContentStream contentStream = new ContentStreamImpl(doc.getName(), BigInteger.valueOf(contentBytes.length),
96 "text/plain", new ByteArrayInputStream(contentBytes));
97
98 ObjectId newVersionId = pwc.checkIn(true, null, contentStream, "test version " + version);
99 Document newVersion = (Document) session.getObject(newVersionId, SELECT_ALL_NO_CACHE_OC);
100
101 addResult(checkObject(session, newVersion, getAllProperties(newVersion), "Version " + version + " compliance"));
102
103
104 List<Document> versions = doc.getAllVersions();
105
106 f = createResult(FAILURE, "Version series should have " + version + " versions but has " + versions.size()
107 + "!");
108 addResult(assertEquals(version, versions.size(), null, f));
109
110 if (versions.size() > 0) {
111 f = createResult(FAILURE, "Newly created version " + version + " is not the latest version!");
112 addResult(assertEquals(newVersion.getId(), versions.get(0).getId(), null, f));
113
114 if (versions.size() > 1) {
115 f = createResult(FAILURE, "The previous version of version " + version
116 + " is not the document it has been created from!");
117 addResult(assertEquals(doc.getId(), versions.get(1).getId(), null, f));
118 }
119 }
120
121 return newVersion;
122 }
123
124 private void deleteVersion(Document versionDoc, Document previousDoc, int version) {
125 CmisTestResult f;
126
127
128 List<Document> versionsBefore = versionDoc.getAllVersions();
129
130
131 versionDoc.delete(false);
132
133 f = createResult(FAILURE, "Deleted version " + version + " still exists!");
134 addResult(assertIsFalse(exists(versionDoc), null, f));
135
136
137 if (previousDoc != null) {
138 List<Document> versionsAfter = previousDoc.getAllVersions();
139
140 f = createResult(FAILURE, "After version " + version
141 + " has been deleted, the version history should consist of " + (versionsBefore.size() - 1)
142 + " documents but is has " + versionsAfter.size() + " !");
143 addResult(assertEquals(versionsBefore.size() - 1, versionsAfter.size(), null, f));
144 }
145 }
146 }