mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-06-30 18:14:51 +00:00
MNT-24883 add tests for creating source files from InputStream for DOCX files
This commit is contained in:
parent
1187cf0d33
commit
ba24d8d700
@ -26,9 +26,9 @@
|
||||
*/
|
||||
package org.alfresco.transform.base.transform;
|
||||
|
||||
import org.alfresco.transform.base.CustomTransformer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
@ -42,20 +42,24 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.Part;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.alfresco.transform.base.CustomTransformer;
|
||||
|
||||
/**
|
||||
* Tests {@link StreamHandler}, {@link TransformManagerImpl#createSourceFile()} and
|
||||
* {@link TransformManagerImpl#createTargetFile()} methods.
|
||||
* Tests {@link StreamHandler}, {@link TransformManagerImpl#createSourceFile()} and {@link TransformManagerImpl#createTargetFile()} methods.
|
||||
*/
|
||||
public class StreamHandlerTest
|
||||
{
|
||||
public static final String ORIGINAL = "Original";
|
||||
public static final String CHANGE = " plus some change";
|
||||
public static final String EXPECTED = ORIGINAL+ CHANGE;
|
||||
public static final String EXPECTED = ORIGINAL + CHANGE;
|
||||
|
||||
TransformManagerImpl transformManager = new TransformManagerImpl();
|
||||
@TempDir
|
||||
@ -137,7 +141,7 @@ public class StreamHandlerTest
|
||||
transformManager.setInputStream(inputStream);
|
||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||
|
||||
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
transformManager.getOutputStream().close();
|
||||
@ -162,7 +166,7 @@ public class StreamHandlerTest
|
||||
|
||||
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
||||
assertTrue(sourceFileCreatedByTransform.exists());
|
||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform)+CHANGE);
|
||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
transformManager.getOutputStream().close();
|
||||
@ -190,7 +194,7 @@ public class StreamHandlerTest
|
||||
transformManager.setInputStream(inputStream);
|
||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||
|
||||
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
closeInputStreamWithoutException(inputStream);
|
||||
@ -220,7 +224,7 @@ public class StreamHandlerTest
|
||||
|
||||
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
||||
assertEquals(sourceFile, sourceFileCreatedByTransform);
|
||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform)+CHANGE);
|
||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
closeInputStreamWithoutException(inputStream);
|
||||
@ -254,7 +258,7 @@ public class StreamHandlerTest
|
||||
|
||||
File targetFileCreatedByTransform = transformManager.createTargetFile();
|
||||
assertTrue(targetFileCreatedByTransform.exists());
|
||||
write(targetFileCreatedByTransform, read(inputStream)+CHANGE);
|
||||
write(targetFileCreatedByTransform, read(inputStream) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
transformManager.getOutputStream().close();
|
||||
@ -281,7 +285,7 @@ public class StreamHandlerTest
|
||||
transformManager.setInputStream(inputStream);
|
||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||
|
||||
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
transformManager.getOutputStream().close();
|
||||
@ -311,7 +315,7 @@ public class StreamHandlerTest
|
||||
|
||||
File targetFileCreatedByTransform = transformManager.createTargetFile();
|
||||
assertEquals(targetFile, targetFileCreatedByTransform);
|
||||
write(targetFileCreatedByTransform, read(inputStream)+CHANGE);
|
||||
write(targetFileCreatedByTransform, read(inputStream) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
transformManager.getOutputStream().close();
|
||||
@ -349,7 +353,7 @@ public class StreamHandlerTest
|
||||
transformManager.setInputStream(inputStream);
|
||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||
|
||||
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
closeInputStreamWithoutException(inputStream);
|
||||
@ -380,7 +384,7 @@ public class StreamHandlerTest
|
||||
transformManager.setInputStream(inputStream);
|
||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||
|
||||
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
closeInputStreamWithoutException(inputStream);
|
||||
@ -409,7 +413,7 @@ public class StreamHandlerTest
|
||||
transformManager.setInputStream(inputStream);
|
||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||
|
||||
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
closeInputStreamWithoutException(inputStream);
|
||||
@ -437,7 +441,7 @@ public class StreamHandlerTest
|
||||
@Override
|
||||
protected void transform(CustomTransformer customTransformer) throws Exception
|
||||
{
|
||||
write(outputStream, read(inputStream)+CHANGE);
|
||||
write(outputStream, read(inputStream) + CHANGE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -448,8 +452,7 @@ public class StreamHandlerTest
|
||||
|
||||
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
|
||||
{
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
new FakeStreamHandler() {
|
||||
@Override
|
||||
protected void init() throws IOException
|
||||
{
|
||||
@ -480,8 +483,7 @@ public class StreamHandlerTest
|
||||
File sourceFile = tempFile();
|
||||
write(sourceFile, ORIGINAL);
|
||||
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
new FakeStreamHandler() {
|
||||
@Override
|
||||
protected void init() throws IOException
|
||||
{
|
||||
@ -512,8 +514,7 @@ public class StreamHandlerTest
|
||||
File sourceFile = tempFile();
|
||||
write(sourceFile, ORIGINAL);
|
||||
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
new FakeStreamHandler() {
|
||||
@Override
|
||||
protected InputStream getInputStream() throws IOException
|
||||
{
|
||||
@ -533,8 +534,7 @@ public class StreamHandlerTest
|
||||
{
|
||||
File targetFile = tempFile();
|
||||
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
new FakeStreamHandler() {
|
||||
@Override
|
||||
protected InputStream getInputStream()
|
||||
{
|
||||
@ -560,8 +560,7 @@ public class StreamHandlerTest
|
||||
{
|
||||
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
|
||||
{
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
new FakeStreamHandler() {
|
||||
@Override
|
||||
protected InputStream getInputStream()
|
||||
{
|
||||
@ -576,4 +575,66 @@ public class StreamHandlerTest
|
||||
}.handleTransformRequest();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartWithInputStreamAndCallCreateSourceFileForDocxFiles() throws Exception
|
||||
{
|
||||
try (
|
||||
InputStream in = getSourceInputStreamFromBytes();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutputStream rec = transformManager.setOutputStream(out))
|
||||
{
|
||||
transformManager.setSourceFileName("test.docx");
|
||||
transformManager.setInputStream(in);
|
||||
|
||||
File src = transformManager.createSourceFile();
|
||||
assertTrue(src.exists());
|
||||
write(rec, read(src) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
transformManager.getOutputStream().close();
|
||||
closeInputStreamWithoutException(in);
|
||||
Long outputLength = transformManager.getOutputLength();
|
||||
transformManager.deleteSourceFile();
|
||||
transformManager.deleteTargetFile();
|
||||
|
||||
assertEquals(EXPECTED, read(out));
|
||||
assertEquals(EXPECTED.length(), outputLength);
|
||||
assertFalse(src.exists());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartWithInputStreamAndCallCreateSourceFileForDocxFilesWithHttpRequest() throws Exception
|
||||
{
|
||||
try (
|
||||
InputStream in = getSourceInputStreamFromBytes();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutputStream rec = transformManager.setOutputStream(out))
|
||||
{
|
||||
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
|
||||
Part mockPart = Mockito.mock(Part.class);
|
||||
Mockito.when(mockPart.getSubmittedFileName()).thenReturn("dummy.docx");
|
||||
Mockito.when(mockRequest.getParts()).thenReturn(Arrays.asList(mockPart));
|
||||
|
||||
transformManager.setSourceMimetype("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||
transformManager.setInputStream(in);
|
||||
transformManager.setRequest(mockRequest);
|
||||
|
||||
File src = transformManager.createSourceFile();
|
||||
assertTrue(src.exists());
|
||||
write(rec, read(src) + CHANGE);
|
||||
|
||||
transformManager.copyTargetFileToOutputStream();
|
||||
transformManager.getOutputStream().close();
|
||||
closeInputStreamWithoutException(in);
|
||||
Long outputLength = transformManager.getOutputLength();
|
||||
transformManager.deleteSourceFile();
|
||||
transformManager.deleteTargetFile();
|
||||
|
||||
assertEquals(EXPECTED, read(out));
|
||||
assertEquals(EXPECTED.length(), outputLength);
|
||||
assertFalse(src.exists());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user