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.math.BigInteger;
25  import java.util.Map;
26  
27  import org.apache.chemistry.opencmis.client.api.Document;
28  import org.apache.chemistry.opencmis.client.api.Folder;
29  import org.apache.chemistry.opencmis.client.api.Session;
30  import org.apache.chemistry.opencmis.commons.data.ContentStream;
31  import org.apache.chemistry.opencmis.commons.data.PartialContentStream;
32  import org.apache.chemistry.opencmis.tck.CmisTestResult;
33  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
34  
35  /**
36   * Content Range Test.
37   */
38  public class ContentRangesTest extends AbstractSessionTest {
39  
40      private static final String CONTENT = "0123456789012345678901234567890";
41  
42      @Override
43      public void init(Map<String, String> parameters) {
44          super.init(parameters);
45          setName("Content Ranges Test");
46          setDescription("Creates a document and reads different excerpts of the content.");
47      }
48  
49      @Override
50      public void run(Session session) {
51          CmisTestResult f;
52  
53          // create a test folder
54          Folder testFolder = createTestFolder(session);
55          Document doc = null;
56  
57          try {
58              // create the document
59              doc = createDocument(session, testFolder, "testcontent.txt", CONTENT);
60  
61              String excerpt;
62              ContentStream content;
63              long contentLength = doc.getContentStreamLength();
64  
65              // no offset, no length -> full content
66              try {
67                  content = doc.getContentStream(null, null);
68                  excerpt = getStringFromContentStream(content);
69  
70                  if (contentLength > -1 && content.getLength() > -1) {
71                      f = createResult(WARNING, "Content length does not match {offset=null, length=null}!", false);
72                      addResult(assertEquals(contentLength, content.getLength(), null, f));
73                  }
74  
75                  if (CONTENT.equals(excerpt)) {
76                      addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(FAILURE,
77                              "Retrieved stream is marked as partial stream "
78                                      + "although the full stream {offset=null, length=null} was expected!")));
79                  } else {
80                      addResult(createResult(FAILURE, "Retrieved stream doesn't match the document content!"));
81                  }
82              } catch (Exception e) {
83                  addResult(createResult(FAILURE,
84                          "Unexpected exception while retrieving full stream {offset=null, length=null}: " + e, e, false));
85              }
86  
87              // offset = 0, no length -> full content
88              try {
89                  content = doc.getContentStream(BigInteger.ZERO, null);
90                  excerpt = getStringFromContentStream(content);
91  
92                  if (contentLength > -1 && content.getLength() > -1) {
93                      f = createResult(WARNING, "Content length does not match {offset=0, length=null}!", false);
94                      addResult(assertEquals(contentLength, content.getLength(), null, f));
95                  }
96  
97                  if (CONTENT.equals(excerpt)) {
98                      addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING,
99                              "Retrieved stream is marked as partial stream "
100                                     + "although the full stream {offset=0, length=null} was expected!")));
101                 } else {
102                     addResult(createResult(FAILURE, "Retrieved stream doesn't match the document content!"));
103                 }
104             } catch (Exception e) {
105                 addResult(createResult(FAILURE,
106                         "Unexpected exception while retrieving full stream {offset=0, length=null}: " + e, e, false));
107             }
108 
109             // offset, no length
110             try {
111                 content = doc.getContentStream(BigInteger.valueOf(3), null);
112                 excerpt = getStringFromContentStream(content);
113 
114                 if (contentLength > -1 && content.getLength() > -1) {
115                     f = createResult(WARNING, "Content length does not match {offset=3, length=null}!", false);
116                     addResult(assertEquals(contentLength - 3, content.getLength(), null, f));
117                 }
118 
119                 if (CONTENT.equals(excerpt)) {
120                     addResult(createResult(WARNING,
121                             "Retrieved full stream instead of an excerpt {offset=3, length=null}! Content ranges supported?"));
122                     addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING,
123                             "Retrieved stream is marked as partial stream, " + "although the full stream is returned!")));
124                 } else {
125                     f = createResult(FAILURE, "Retrieved stream excerpt {offset=3, length=null} doesn't match!");
126                     addResult(assertEquals(CONTENT.substring(3), excerpt, null, f));
127                     addResult(assertIsTrue(content instanceof PartialContentStream, null, createResult(WARNING,
128                             "Retrieved stream is not marked as partial stream. "
129                                     + "(AtomPub and Browser Binding should return the HTTP status code 206.)")));
130                 }
131             } catch (Exception e) {
132                 addResult(createResult(FAILURE,
133                         "Unexpected exception while retrieving stream {offset=3, length=null}: " + e, e, false));
134             }
135 
136             // no offset, length
137             try {
138                 content = doc.getContentStream(null, BigInteger.valueOf(12));
139                 excerpt = getStringFromContentStream(content);
140 
141                 if (content.getLength() > -1) {
142                     f = createResult(WARNING, "Content length does not match {offset=null, length=12}!", false);
143                     addResult(assertEquals(12L, content.getLength(), null, f));
144                 }
145 
146                 if (CONTENT.equals(excerpt)) {
147                     addResult(createResult(WARNING,
148                             "Retrieved full stream instead of an excerpt {offset=null, length=12}! Content ranges supported?"));
149                     addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING,
150                             "Retrieved stream is marked as partial stream, " + "although the full stream is returned!")));
151                 } else {
152                     f = createResult(FAILURE, "Retrieved stream excerpt {offset=null, length=12} doesn't match!");
153                     addResult(assertEquals(CONTENT.substring(0, 12), excerpt, null, f));
154                     addResult(assertIsTrue(content instanceof PartialContentStream, null, createResult(WARNING,
155                             "Retrieved stream is not marked as partial stream. "
156                                     + "(AtomPub and Browser Binding should return the HTTP status code 206.)")));
157                 }
158             } catch (Exception e) {
159                 addResult(createResult(FAILURE,
160                         "Unexpected exception while retrieving stream {offset=null, length=12}: " + e, e, false));
161             }
162 
163             // offset and length
164             try {
165                 content = doc.getContentStream(BigInteger.valueOf(5), BigInteger.valueOf(17));
166                 excerpt = getStringFromContentStream(content);
167 
168                 if (content.getLength() > -1) {
169                     f = createResult(WARNING, "Content length does not match {offset=5, length=17}!", false);
170                     addResult(assertEquals(17L, content.getLength(), null, f));
171                 }
172 
173                 if (CONTENT.equals(excerpt)) {
174                     addResult(createResult(WARNING,
175                             "Retrieved full stream instead of an excerpt {offset=5, length=17}! Content ranges supported?"));
176                     addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING,
177                             "Retrieved stream is marked as partial stream, " + "although the full stream is returned!")));
178                 } else {
179                     f = createResult(FAILURE, "Retrieved stream excerpt {offset=5, length=17} doesn't match!");
180                     addResult(assertEquals(CONTENT.substring(5, 5 + 17), excerpt, null, f));
181                     addResult(assertIsTrue(content instanceof PartialContentStream, null, createResult(WARNING,
182                             "Retrieved stream is not marked as partial stream. "
183                                     + "(AtomPub and Browser Binding should return the HTTP status code 206.)")));
184                 }
185             } catch (Exception e) {
186                 addResult(createResult(FAILURE, "Unexpected exception while retrieving stream {offset=5, length=17}: "
187                         + e, e, false));
188             }
189 
190             // offset and length > content size
191             try {
192                 content = doc.getContentStream(BigInteger.valueOf(9), BigInteger.valueOf(123));
193                 excerpt = getStringFromContentStream(content);
194 
195                 if (content.getLength() > -1) {
196                     f = createResult(WARNING, "Content length does not match {offset=9, length=123}!", false);
197                     addResult(assertEquals((long) (CONTENT.length() - 9), content.getLength(), null, f));
198                 }
199 
200                 if (CONTENT.equals(excerpt)) {
201                     addResult(createResult(WARNING,
202                             "Retrieved full stream instead of an excerpt {offset=9, length=123}! Content ranges supported?"));
203                     addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING,
204                             "Retrieved stream is marked as partial stream, " + "although the full stream is returned!")));
205                 } else {
206                     f = createResult(FAILURE, "Retrieved stream excerpt {offset=9, length=123} doesn't match!");
207                     addResult(assertEquals(CONTENT.substring(9), excerpt, null, f));
208                     addResult(assertIsTrue(content instanceof PartialContentStream, null, createResult(WARNING,
209                             "Retrieved stream is not marked as partial stream. "
210                                     + "(AtomPub and Browser Binding should return the HTTP status code 206.)")));
211                 }
212             } catch (Exception e) {
213                 addResult(createResult(FAILURE, "Unexpected exception while retrieving stream {offset=9, length=123}: "
214                         + e, e, false));
215             }
216         } finally {
217             // clean up
218             deleteObject(doc);
219             deleteTestFolder();
220         }
221     }
222 }