View Javadoc

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  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
23  
24  import java.io.ByteArrayInputStream;
25  import java.math.BigInteger;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.chemistry.opencmis.client.api.Folder;
30  import org.apache.chemistry.opencmis.client.api.Session;
31  import org.apache.chemistry.opencmis.commons.PropertyIds;
32  import org.apache.chemistry.opencmis.commons.data.ContentStream;
33  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
34  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
35  import org.apache.chemistry.opencmis.commons.impl.IOUtils;
36  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
37  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
38  
39  public class CreateInvalidTypeTest extends AbstractSessionTest {
40  
41      @Override
42      public void init(Map<String, String> parameters) {
43          super.init(parameters);
44          setName("Create Object With Invalid Type Test");
45          setDescription("Tries to create document with a folder type and folder with a document type.");
46      }
47  
48      @Override
49      public void run(Session session) {
50          // create a test folder
51          Folder testFolder = createTestFolder(session);
52  
53          try {
54              // test document creation
55              try {
56                  Map<String, Object> properties = new HashMap<String, Object>();
57                  properties.put(PropertyIds.NAME, "never.txt");
58                  properties.put(PropertyIds.OBJECT_TYPE_ID, getFolderTestTypeId());
59  
60                  byte[] contentBytes = IOUtils.toUTF8Bytes("nothing");
61                  ContentStream contentStream = new ContentStreamImpl("never.txt",
62                          BigInteger.valueOf(contentBytes.length), "text/plain", new ByteArrayInputStream(contentBytes));
63  
64                  testFolder.createDocument(properties, contentStream, null);
65  
66                  addResult(createResult(FAILURE, "Creation of a document with a folder type shouldn't work!"));
67              } catch (Exception e) {
68                  if (!(e instanceof CmisInvalidArgumentException) && !(e instanceof CmisConstraintException)) {
69                      addResult(createResult(WARNING,
70                              "Creation of a document with a folder type threw an unexcpeted exception: " + e.toString()));
71                  }
72              }
73  
74              // test folder creation
75              try {
76                  Map<String, Object> properties = new HashMap<String, Object>();
77                  properties.put(PropertyIds.NAME, "never");
78                  properties.put(PropertyIds.OBJECT_TYPE_ID, getDocumentTestTypeId());
79  
80                  testFolder.createFolder(properties);
81  
82                  addResult(createResult(FAILURE, "Creation of a folder with a document type shouldn't work!"));
83              } catch (Exception e) {
84                  if (!(e instanceof CmisInvalidArgumentException) && !(e instanceof CmisConstraintException)) {
85                      addResult(createResult(WARNING,
86                              "Creation of a folder with a document type threw an unexcpeted exception: " + e.toString()));
87                  }
88              }
89          } finally {
90              // delete the test folder
91              deleteTestFolder();
92          }
93      }
94  }