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 java.io.ByteArrayInputStream;
22  import java.math.BigInteger;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.chemistry.opencmis.client.api.Document;
27  import org.apache.chemistry.opencmis.client.api.Folder;
28  import org.apache.chemistry.opencmis.client.api.Session;
29  import org.apache.chemistry.opencmis.commons.PropertyIds;
30  import org.apache.chemistry.opencmis.commons.data.ContentStream;
31  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
32  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
33  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
34  
35  public class WhitespaceInNameTest extends AbstractSessionTest {
36  
37      @Override
38      public void init(Map<String, String> parameters) {
39          super.init(parameters);
40          setName("Whitespace in Name Test");
41          setDescription("Creates documents with spaces in cmis:name.");
42      }
43  
44      @Override
45      public void run(Session session) {
46          // create a test folder
47          Folder testFolder = createTestFolder(session);
48  
49          try {
50              testCenterSpaceSpace(session, testFolder);
51              testMultipleCenterSpaceSpace(session, testFolder);
52              testLeadingSpace(session, testFolder);
53              testTrailingSpace(session, testFolder);
54          } finally {
55              // delete the test folder
56              deleteTestFolder();
57          }
58      }
59  
60      private void testLeadingSpace(Session session, Folder testFolder) {
61          String name = "leading.txt";
62  
63          try {
64              Document doc = createDocumentWithoutChecks(testFolder, " " + name);
65  
66              if (doc.getName().equals(" " + name)) {
67                  addResult(createInfoResult("Repository does supports document names with a leading space."));
68              } else {
69                  if (doc.getName().equals(name)) {
70                      addResult(createInfoResult("Repository removes leading space from document name."));
71                  } else {
72                      addResult(createInfoResult("Repository renames documents with a leading space."));
73                  }
74              }
75          } catch (CmisBaseException e) {
76              addResult(createInfoResult("Repository does not support document names with a leading space. Exception: "
77                      + e.toString()));
78          }
79      }
80  
81      private void testTrailingSpace(Session session, Folder testFolder) {
82          String name = "trailing.txt";
83  
84          try {
85              Document doc = createDocumentWithoutChecks(testFolder, name + " ");
86  
87              if (doc.getName().equals(name + " ")) {
88                  addResult(createInfoResult("Repository does supports document names with a trailing space."));
89              } else {
90                  if (doc.getName().equals(name)) {
91                      addResult(createInfoResult("Repository removes trailing space from document name."));
92                  } else {
93                      addResult(createInfoResult("Repository renames documents with a trailing space."));
94                  }
95              }
96          } catch (CmisBaseException e) {
97              addResult(createInfoResult("Repository does not support document names with a trailing space. Exception: "
98                      + e.toString()));
99          }
100     }
101 
102     private void testCenterSpaceSpace(Session session, Folder testFolder) {
103         String name = "center space.txt";
104 
105         try {
106             Document doc = createDocumentWithoutChecks(testFolder, name);
107 
108             if (doc.getName().equals(name)) {
109                 addResult(createInfoResult("Repository does supports document names with a space."));
110             } else {
111                 if (doc.getName().equals("centerspace.txt")) {
112                     addResult(createInfoResult("Repository removes spaces from document name."));
113                 } else {
114                     addResult(createInfoResult("Repository renames documents with a space."));
115                 }
116             }
117         } catch (CmisBaseException e) {
118             addResult(createInfoResult("Repository does not support document names with a space. Exception: "
119                     + e.toString()));
120         }
121     }
122 
123     private void testMultipleCenterSpaceSpace(Session session, Folder testFolder) {
124         String name = "twocenter  spaces.txt";
125 
126         try {
127             Document doc = createDocumentWithoutChecks(testFolder, name);
128 
129             if (doc.getName().equals(name)) {
130                 addResult(createInfoResult("Repository does supports document names with more than one successive spaces."));
131             } else {
132                 if (doc.getName().equals("twocenterspaces.txt")) {
133                     addResult(createInfoResult("Repository removes spaces from document name."));
134                 } else if (doc.getName().equals("twocenter spaces.txt")) {
135                     addResult(createInfoResult("Repository combines multiple spaces into one in document names."));
136                 } else {
137                     addResult(createInfoResult("Repository renames documents with a space."));
138                 }
139             }
140         } catch (CmisBaseException e) {
141             addResult(createInfoResult("Repository does not support document names with a space. Exception: "
142                     + e.toString()));
143         }
144     }
145 
146     private Document createDocumentWithoutChecks(Folder parent, String name) {
147         // prepare properties
148         Map<String, Object> properties = new HashMap<String, Object>();
149         properties.put(PropertyIds.NAME, name);
150         properties.put(PropertyIds.OBJECT_TYPE_ID, getDocumentTestTypeId());
151 
152         // prepare empty content
153         ContentStream contentStream = new ContentStreamImpl(name, BigInteger.ZERO, "text/plain",
154                 new ByteArrayInputStream(new byte[0]));
155 
156         // create the document
157         return parent.createDocument(properties, contentStream, null, null, null, null, SELECT_ALL_NO_CACHE_OC);
158 
159     }
160 }