This project has retired. For details please refer to its Attic page.
VersioningStateCreateTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
24  
25  import java.io.ByteArrayInputStream;
26  import java.math.BigInteger;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.chemistry.opencmis.client.api.Document;
32  import org.apache.chemistry.opencmis.client.api.Folder;
33  import org.apache.chemistry.opencmis.client.api.Session;
34  import org.apache.chemistry.opencmis.commons.PropertyIds;
35  import org.apache.chemistry.opencmis.commons.data.ContentStream;
36  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
37  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
38  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
39  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
40  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
41  import org.apache.chemistry.opencmis.tck.CmisTestResult;
42  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
43  
44  public class VersioningStateCreateTest extends AbstractSessionTest {
45  
46      @Override
47      public void init(Map<String, String> parameters) {
48          super.init(parameters);
49          setName("Versioning State Create Test");
50          setDescription("Creates documents in different versioing states.");
51      }
52  
53      @Override
54      public void run(Session session) {
55          CmisTestResult f;
56  
57          try {
58              // create folder and document
59              Folder testFolder = createTestFolder(session);
60  
61              DocumentTypeDefinition docType = (DocumentTypeDefinition) session
62                      .getTypeDefinition(getDocumentTestTypeId());
63  
64              if (!docType.isVersionable()) {
65                  addResult(createResult(SKIPPED, "Test type is not versionable. Test skipped!"));
66                  return;
67              }
68  
69              // major version
70              Document docMajor = testFolder.createDocument(getProperties("major.txt"), getContentStream(),
71                      VersioningState.MAJOR, null, null, null, SELECT_ALL_NO_CACHE_OC);
72              addResult(checkObject(session, docMajor, getAllProperties(docMajor), "Major version compliance"));
73  
74              f = createResult(FAILURE, "Document should be major version.");
75              addResult(assertIsTrue(docMajor.isMajorVersion(), null, f));
76  
77              List<Document> versions = docMajor.getAllVersions();
78  
79              f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
80              addResult(assertEquals(1, versions.size(), null, f));
81  
82              deleteObject(docMajor);
83  
84              // minor version
85              try {
86                  Document docMinor = testFolder.createDocument(getProperties("minor.txt"), getContentStream(),
87                          VersioningState.MINOR, null, null, null, SELECT_ALL_NO_CACHE_OC);
88                  addResult(checkObject(session, docMinor, getAllProperties(docMinor), "Minor version compliance"));
89  
90                  f = createResult(FAILURE, "Document should be minor version.");
91                  addResult(assertIsFalse(docMinor.isMajorVersion(), null, f));
92  
93                  versions = docMinor.getAllVersions();
94  
95                  f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
96                  addResult(assertEquals(1, versions.size(), null, f));
97  
98                  deleteObject(docMinor);
99              } catch (CmisConstraintException ce) {
100                 addResult(createResult(WARNING, "Creating a minor version failed! "
101                         + "The repository might not support minor versions. Exception: " + ce, ce, false));
102             } catch (CmisInvalidArgumentException iae) {
103                 addResult(createResult(WARNING, "Creating a minor version failed! "
104                         + "The repository might not support minor versions.  Exception: " + iae, iae, false));
105             }
106 
107             // checked out version
108             try {
109                 Document docCheckedOut = testFolder.createDocument(getProperties("checkout.txt"), getContentStream(),
110                         VersioningState.CHECKEDOUT, null, null, null, SELECT_ALL_NO_CACHE_OC);
111                 addResult(checkObject(session, docCheckedOut, getAllProperties(docCheckedOut),
112                         "Checked out version compliance"));
113 
114                 f = createResult(FAILURE, "Version series should be checked out.");
115                 addResult(assertIsTrue(docCheckedOut.isVersionSeriesCheckedOut(), null, f));
116 
117                 versions = docCheckedOut.getAllVersions();
118 
119                 f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
120                 addResult(assertEquals(1, versions.size(), null, f));
121 
122                 docCheckedOut.cancelCheckOut();
123             } catch (CmisConstraintException ce) {
124                 addResult(createResult(WARNING, "Creating a checked out version failed! "
125                         + "The repository might not support creating checked out versions. Exception: " + ce, ce, false));
126             } catch (CmisInvalidArgumentException iae) {
127                 addResult(createResult(WARNING, "Creating a checked out version failed! "
128                         + "The repository might not  support creating checked out versions.  Exception: " + iae, iae,
129                         false));
130             }
131 
132         } finally {
133             deleteTestFolder();
134         }
135     }
136 
137     private Map<String, Object> getProperties(String name) {
138         Map<String, Object> properties = new HashMap<String, Object>();
139         properties.put(PropertyIds.NAME, name);
140         properties.put(PropertyIds.OBJECT_TYPE_ID, getDocumentTestTypeId());
141 
142         return properties;
143     }
144 
145     private ContentStream getContentStream() {
146         byte[] contentBytes = null;
147         try {
148             contentBytes = "some content".getBytes("UTF-8");
149         } catch (Exception e) {
150             contentBytes = "some content".getBytes();
151         }
152 
153         return new ContentStreamImpl("content.txt", BigInteger.valueOf(contentBytes.length), "text/plain",
154                 new ByteArrayInputStream(contentBytes));
155     }
156 }