use of org.apache.chemistry.opencmis.commons.data.PartialContentStream in project copper-cms by PogeyanOSS.
the class ContentRangesTest method run.
@Override
public void run(Session session) {
CmisTestResult f;
// create a test folder
Folder testFolder = createTestFolder(session);
Document doc = null;
try {
// create the document
doc = createDocument(session, testFolder, "testcontent.txt", CONTENT);
String excerpt;
ContentStream content;
long contentLength = doc.getContentStreamLength();
// no offset, no length -> full content
try {
content = doc.getContentStream(null, null);
excerpt = getStringFromContentStream(content);
if (contentLength > -1 && content.getLength() > -1) {
f = createResult(WARNING, "Content length does not match {offset=null, length=null}!", false);
addResult(assertEquals(contentLength, content.getLength(), null, f));
}
if (CONTENT.equals(excerpt)) {
addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(FAILURE, "Retrieved stream is marked as partial stream " + "although the full stream {offset=null, length=null} was expected!")));
} else {
addResult(createResult(FAILURE, "Retrieved stream doesn't match the document content!"));
}
} catch (Exception e) {
addResult(createResult(FAILURE, "Unexpected exception while retrieving full stream {offset=null, length=null}: " + e, e, false));
}
// offset = 0, no length -> full content
try {
content = doc.getContentStream(BigInteger.ZERO, null);
excerpt = getStringFromContentStream(content);
if (contentLength > -1 && content.getLength() > -1) {
f = createResult(WARNING, "Content length does not match {offset=0, length=null}!", false);
addResult(assertEquals(contentLength, content.getLength(), null, f));
}
if (CONTENT.equals(excerpt)) {
addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is marked as partial stream " + "although the full stream {offset=0, length=null} was expected!")));
} else {
addResult(createResult(FAILURE, "Retrieved stream doesn't match the document content!"));
}
} catch (Exception e) {
addResult(createResult(FAILURE, "Unexpected exception while retrieving full stream {offset=0, length=null}: " + e, e, false));
}
// offset, no length
try {
content = doc.getContentStream(BigInteger.valueOf(3), null);
excerpt = getStringFromContentStream(content);
if (contentLength > -1 && content.getLength() > -1) {
f = createResult(WARNING, "Content length does not match {offset=3, length=null}!", false);
addResult(assertEquals(contentLength - 3, content.getLength(), null, f));
}
if (CONTENT.equals(excerpt)) {
addResult(createResult(WARNING, "Retrieved full stream instead of an excerpt {offset=3, length=null}! Content ranges supported?"));
addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is marked as partial stream, " + "although the full stream is returned!")));
} else {
f = createResult(FAILURE, "Retrieved stream excerpt {offset=3, length=null} doesn't match!");
addResult(assertEquals(CONTENT.substring(3), excerpt, null, f));
addResult(assertIsTrue(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is not marked as partial stream. " + "(AtomPub and Browser Binding should return the HTTP status code 206.)")));
}
} catch (Exception e) {
addResult(createResult(FAILURE, "Unexpected exception while retrieving stream {offset=3, length=null}: " + e, e, false));
}
// no offset, length
try {
content = doc.getContentStream(null, BigInteger.valueOf(12));
excerpt = getStringFromContentStream(content);
if (content.getLength() > -1) {
f = createResult(WARNING, "Content length does not match {offset=null, length=12}!", false);
addResult(assertEquals(12L, content.getLength(), null, f));
}
if (CONTENT.equals(excerpt)) {
addResult(createResult(WARNING, "Retrieved full stream instead of an excerpt {offset=null, length=12}! Content ranges supported?"));
addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is marked as partial stream, " + "although the full stream is returned!")));
} else {
f = createResult(FAILURE, "Retrieved stream excerpt {offset=null, length=12} doesn't match!");
addResult(assertEquals(CONTENT.substring(0, 12), excerpt, null, f));
addResult(assertIsTrue(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is not marked as partial stream. " + "(AtomPub and Browser Binding should return the HTTP status code 206.)")));
}
} catch (Exception e) {
addResult(createResult(FAILURE, "Unexpected exception while retrieving stream {offset=null, length=12}: " + e, e, false));
}
// offset and length
try {
content = doc.getContentStream(BigInteger.valueOf(5), BigInteger.valueOf(17));
excerpt = getStringFromContentStream(content);
if (content.getLength() > -1) {
f = createResult(WARNING, "Content length does not match {offset=5, length=17}!", false);
addResult(assertEquals(17L, content.getLength(), null, f));
}
if (CONTENT.equals(excerpt)) {
addResult(createResult(WARNING, "Retrieved full stream instead of an excerpt {offset=5, length=17}! Content ranges supported?"));
addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is marked as partial stream, " + "although the full stream is returned!")));
} else {
f = createResult(FAILURE, "Retrieved stream excerpt {offset=5, length=17} doesn't match!");
addResult(assertEquals(CONTENT.substring(5, 5 + 17), excerpt, null, f));
addResult(assertIsTrue(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is not marked as partial stream. " + "(AtomPub and Browser Binding should return the HTTP status code 206.)")));
}
} catch (Exception e) {
addResult(createResult(FAILURE, "Unexpected exception while retrieving stream {offset=5, length=17}: " + e, e, false));
}
// offset and length > content size
try {
content = doc.getContentStream(BigInteger.valueOf(9), BigInteger.valueOf(123));
excerpt = getStringFromContentStream(content);
if (content.getLength() > -1) {
f = createResult(WARNING, "Content length does not match {offset=9, length=123}!", false);
addResult(assertEquals((long) (CONTENT.length() - 9), content.getLength(), null, f));
}
if (CONTENT.equals(excerpt)) {
addResult(createResult(WARNING, "Retrieved full stream instead of an excerpt {offset=9, length=123}! Content ranges supported?"));
addResult(assertIsFalse(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is marked as partial stream, " + "although the full stream is returned!")));
} else {
f = createResult(FAILURE, "Retrieved stream excerpt {offset=9, length=123} doesn't match!");
addResult(assertEquals(CONTENT.substring(9), excerpt, null, f));
addResult(assertIsTrue(content instanceof PartialContentStream, null, createResult(WARNING, "Retrieved stream is not marked as partial stream. " + "(AtomPub and Browser Binding should return the HTTP status code 206.)")));
}
} catch (Exception e) {
addResult(createResult(FAILURE, "Unexpected exception while retrieving stream {offset=9, length=123}: " + e, e, false));
}
} finally {
// clean up
deleteObject(doc);
deleteTestFolder();
}
}
Aggregations