This project has retired. For details please refer to its
Attic page.
SetAndDeleteContentTest xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.chemistry.opencmis.tck.tests.crud;
2021importstatic org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
22importstatic org.apache.chemistry.opencmis.tck.CmisTestResultStatus.SKIPPED;
23importstatic org.apache.chemistry.opencmis.tck.CmisTestResultStatus.UNEXPECTED_EXCEPTION;
24importstatic org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
2526import java.io.ByteArrayInputStream;
27import java.io.IOException;
28import java.math.BigInteger;
29import java.util.List;
30import java.util.Map;
3132import org.apache.chemistry.opencmis.client.api.Document;
33import org.apache.chemistry.opencmis.client.api.Folder;
34import org.apache.chemistry.opencmis.client.api.ObjectId;
35import org.apache.chemistry.opencmis.client.api.Session;
36import org.apache.chemistry.opencmis.commons.data.ContentStream;
37import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
38import org.apache.chemistry.opencmis.commons.enums.Action;
39import org.apache.chemistry.opencmis.commons.enums.BindingType;
40import org.apache.chemistry.opencmis.commons.enums.CapabilityContentStreamUpdates;
41import org.apache.chemistry.opencmis.commons.enums.ContentStreamAllowed;
42import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
43import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
44import org.apache.chemistry.opencmis.tck.CmisTestResult;
45import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
4647/**48 * Copy test.49 */50publicclassSetAndDeleteContentTestextendsAbstractSessionTest {
5152privatestaticfinal String CONTENT1 = "one";
53privatestaticfinal String CONTENT2 = "two";
5455 @Override
56publicvoid init(Map<String, String> parameters) {
57super.init(parameters);
58 setName("Set and Delete content Test");
59 setDescription("Creates a new document and tries to set and delete its content.");
60 }
6162 @Override
63publicvoid run(Session session) {
64CmisTestResult f;
6566if (getContentStreamUpdatesCapbility(session) == CapabilityContentStreamUpdates.NONE) {
67 addResult(createInfoResult("Stream updates are not supported. Test skipped!"));
68return;
69 }
7071try {
72// create folder and document73 Folder testFolder = createTestFolder(session);
74 Document doc = createDocument(session, testFolder, "contenttest.txt", CONTENT1);
75 Document workDoc = doc;
76 DocumentTypeDefinition docType = (DocumentTypeDefinition) doc.getType();
7778// test if check out is required and possible79boolean checkedout = false;
80if (!doc.getAllowableActions().getAllowableActions().contains(Action.CAN_SET_CONTENT_STREAM)) {
81if (!docType.isVersionable()) {
82 addResult(createResult(SKIPPED,
83"The test document does not accept a new content stream. Test skipped!"));
84 doc.delete(true);
85return;
86 } else {
87 workDoc = (Document) session.getObject(doc.checkOut(), SELECT_ALL_NO_CACHE_OC);
88 checkedout = true;
8990if (!workDoc.getAllowableActions().getAllowableActions().contains(Action.CAN_SET_CONTENT_STREAM)) {
91 addResult(createResult(SKIPPED,
92"The test PWC does not accept a new content stream. Test skipped!"));
93 workDoc.cancelCheckOut();
94 doc.delete(true);
95return;
96 }
97 }
98 }
99100// test if the content stream can be deleted101if (docType.getContentStreamAllowed() == ContentStreamAllowed.REQUIRED) {
102 addResult(createResult(SKIPPED,
103"A content stream is required for this docuemnt type. deleteContentStream() test skipped!"));
104 } else {
105// delete content stream106try {
107 ObjectId newObjectId = workDoc.deleteContentStream(true);
108109// deleteContentStream may have created a new version110 Document contentDoc = getNewVersion(session, workDoc, checkedout, newObjectId,
111"deleteContentStream()");
112113 f = createResult(FAILURE, "Document still has content after deleteContentStream() has been called!");
114 addResult(assertNull(contentDoc.getContentStream(), null, f));
115116 workDoc = contentDoc;
117 } catch (CmisNotSupportedException e) {
118 addResult(createResult(WARNING, "deleteContentStream() is not supported!"));
119 }
120 }
121122// set a new content stream123 byte[] contentBytes = new byte[0];
124try {
125 contentBytes = CONTENT2.getBytes("UTF-8");
126 } catch (Exception e) {
127 }
128129 ContentStream contentStream = new ContentStreamImpl(workDoc.getName(),
130 BigInteger.valueOf(contentBytes.length), "text/plain", new ByteArrayInputStream(contentBytes));
131132 ObjectId newObjectId = workDoc.setContentStream(contentStream, true, true);
133134// setContentStream may have created a new version135 Document contentDoc = getNewVersion(session, workDoc, checkedout, newObjectId, "setContentStream()");
136137// test new content138try {
139 String content = getStringFromContentStream(contentDoc.getContentStream());
140 f = createResult(FAILURE, "Document content doesn't match the content set by setContentStream()!");
141 addResult(assertEquals(CONTENT2, content, null, f));
142 } catch (IOException e) {
143 addResult(createResult(UNEXPECTED_EXCEPTION,
144"Document content couldn't be read! Exception: " + e.getMessage(), e, true));
145 }
146147// cancel a possible check out148if (checkedout) {
149 workDoc.cancelCheckOut();
150 }
151152// remove the document153 deleteObject(doc);
154 } finally {
155 deleteTestFolder();
156 }
157 }
158159private CapabilityContentStreamUpdates getContentStreamUpdatesCapbility(Session session) {
160if (session.getRepositoryInfo().getCapabilities() == null) {
161returnnull;
162 }
163164return session.getRepositoryInfo().getCapabilities().getContentStreamUpdatesCapability();
165 }
166167private Document getNewVersion(Session session, Document orgDoc, boolean checkedout, ObjectId newObjectId,
168 String operation) {
169 Document result = orgDoc;
170171if (newObjectId != null) {
172// -> Non AtomPub binding173if (!orgDoc.getId().equals(newObjectId.getId())) {
174if (checkedout) {
175 addResult(createResult(FAILURE, operation + " created a new version from a PWC!"));
176 } else {
177 result = (Document) session.getObject(newObjectId, SELECT_ALL_NO_CACHE_OC);
178 addResult(checkObject(session, result, getAllProperties(result), "Version created by " + operation
179 + " compliance"));
180 }
181 }
182 } else {
183if (getBinding() != BindingType.ATOMPUB) {
184 addResult(createResult(FAILURE, operation + " did not return an object id!"));
185 }
186187// -> AtomPub binding or incompliant other binding188if (checkedout) {
189// we cannot check if the repository does the right thing,190// but if there is a problem the versioning tests should191// catch it192 } elseif (Boolean.TRUE.equals(((DocumentTypeDefinition) orgDoc.getType()).isVersionable())) {
193 List<Document> versions = orgDoc.getAllVersions();
194if (versions == null || versions.isEmpty()) {
195 addResult(createResult(FAILURE, operation
196 + " created a new version but the version history is empty!"));
197 } elseif (!orgDoc.getId().equals(versions.get(0).getId())) {
198 result = (Document) session.getObject(versions.get(0), SELECT_ALL_NO_CACHE_OC);
199 addResult(checkObject(session, result, getAllProperties(result), "Version created by " + operation
200 + " compliance"));
201 }
202 }
203 }
204205return result;
206 }
207 }