This project has retired. For details please refer to its Attic page.
CreateBigDocument 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.crud;
20  
21  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.chemistry.opencmis.client.api.Document;
29  import org.apache.chemistry.opencmis.client.api.Folder;
30  import org.apache.chemistry.opencmis.client.api.ObjectId;
31  import org.apache.chemistry.opencmis.client.api.Session;
32  import org.apache.chemistry.opencmis.commons.PropertyIds;
33  import org.apache.chemistry.opencmis.commons.data.ContentStream;
34  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
35  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
36  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
37  import org.apache.chemistry.opencmis.tck.CmisTestResult;
38  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
39  
40  /**
41   * Big document test.
42   */
43  public class CreateBigDocument extends AbstractSessionTest {
44  
45      @Override
46      public void init(Map<String, String> parameters) {
47          super.init(parameters);
48          setName("Create Big Document Test");
49          setDescription("Creates a 10 MiB document and deletes it.");
50      }
51  
52      @Override
53      public void run(Session session) {
54          CmisTestResult f;
55  
56          // create a test folder
57          Folder testFolder = createTestFolder(session);
58  
59          try {
60              String name = "bigdoc";
61              String objectTypeId = getDocumentTestTypeId();
62              String mimetype = "application/octet-stream";
63  
64              final long size = 10 * 1024 * 1024; // 10 MiB
65              InputStream in = new InputStream() {
66                  private int counter = -1;
67  
68                  @Override
69                  public int read() throws IOException {
70                      counter++;
71                      if (counter >= size) {
72                          return -1;
73                      }
74  
75                      return '0' + (counter / 10);
76                  }
77              };
78  
79              // create stream and properties
80              ContentStream contentStream = session.getObjectFactory().createContentStream(name, size, mimetype, in);
81  
82              Map<String, Object> properties = new HashMap<String, Object>();
83              properties.put(PropertyIds.NAME, name);
84              properties.put(PropertyIds.OBJECT_TYPE_ID, objectTypeId);
85  
86              // check type
87              TypeDefinition type = session.getTypeDefinition(objectTypeId);
88              if (!(type instanceof DocumentTypeDefinition)) {
89                  addResult(createResult(FAILURE, "Type is not a document type! Type: " + objectTypeId, true));
90                  return;
91              }
92  
93              DocumentTypeDefinition docType = (DocumentTypeDefinition) type;
94              VersioningState versioningState = (Boolean.TRUE.equals(docType.isVersionable()) ? VersioningState.MAJOR
95                      : VersioningState.NONE);
96  
97              // create and fetch the document
98              ObjectId id = session.createDocument(properties, testFolder, contentStream, versioningState);
99              Document doc = (Document) session.getObject(id);
100 
101             // check the new document
102             addResult(checkObject(session, doc, getAllProperties(doc), "New document object spec compliance"));
103 
104             // check the size
105             f = createResult(FAILURE, "Content stream length doesn't match the uploaded content!", true);
106             assertEquals(size, doc.getContentStreamLength(), null, f);
107 
108             // delete it
109             doc.delete(true);
110         } finally {
111             // delete the test folder
112             deleteTestFolder();
113         }
114     }
115 }