mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-10-01 14:41:17 +00:00
MNT-24883 base engine changes to maintain source file name
This commit is contained in:
@@ -26,9 +26,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.alfresco.transform.base.transform;
|
package org.alfresco.transform.base.transform;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import org.alfresco.transform.base.CustomTransformer;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
@@ -42,25 +42,20 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.Part;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import org.mockito.Mockito;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
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 class StreamHandlerTest
|
||||||
{
|
{
|
||||||
public static final String ORIGINAL = "Original";
|
public static final String ORIGINAL = "Original";
|
||||||
public static final String CHANGE = " plus some change";
|
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();
|
TransformManagerImpl transformManager = new TransformManagerImpl();
|
||||||
@TempDir
|
@TempDir
|
||||||
@@ -137,12 +132,12 @@ public class StreamHandlerTest
|
|||||||
public void testStartWithInputStream() throws Exception
|
public void testStartWithInputStream() throws Exception
|
||||||
{
|
{
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
transformManager.getOutputStream().close();
|
transformManager.getOutputStream().close();
|
||||||
@@ -160,14 +155,14 @@ public class StreamHandlerTest
|
|||||||
public void testStartWithInputStreamAndCallCreateSourceFile() throws Exception
|
public void testStartWithInputStreamAndCallCreateSourceFile() throws Exception
|
||||||
{
|
{
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
||||||
assertTrue(sourceFileCreatedByTransform.exists());
|
assertTrue(sourceFileCreatedByTransform.exists());
|
||||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
transformManager.getOutputStream().close();
|
transformManager.getOutputStream().close();
|
||||||
@@ -190,12 +185,12 @@ public class StreamHandlerTest
|
|||||||
transformManager.setSourceFile(sourceFile);
|
transformManager.setSourceFile(sourceFile);
|
||||||
|
|
||||||
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
|
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
closeInputStreamWithoutException(inputStream);
|
closeInputStreamWithoutException(inputStream);
|
||||||
@@ -218,14 +213,14 @@ public class StreamHandlerTest
|
|||||||
transformManager.setSourceFile(sourceFile);
|
transformManager.setSourceFile(sourceFile);
|
||||||
|
|
||||||
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
|
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
||||||
assertEquals(sourceFile, sourceFileCreatedByTransform);
|
assertEquals(sourceFile, sourceFileCreatedByTransform);
|
||||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
closeInputStreamWithoutException(inputStream);
|
closeInputStreamWithoutException(inputStream);
|
||||||
@@ -252,14 +247,14 @@ public class StreamHandlerTest
|
|||||||
public void testStartWithOutputStreamAndCallCreateTargetFile() throws Exception
|
public void testStartWithOutputStreamAndCallCreateTargetFile() throws Exception
|
||||||
{
|
{
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
transformManager.setOutputStream(outputStream);
|
transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
File targetFileCreatedByTransform = transformManager.createTargetFile();
|
File targetFileCreatedByTransform = transformManager.createTargetFile();
|
||||||
assertTrue(targetFileCreatedByTransform.exists());
|
assertTrue(targetFileCreatedByTransform.exists());
|
||||||
write(targetFileCreatedByTransform, read(inputStream) + CHANGE);
|
write(targetFileCreatedByTransform, read(inputStream)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
transformManager.getOutputStream().close();
|
transformManager.getOutputStream().close();
|
||||||
@@ -281,12 +276,12 @@ public class StreamHandlerTest
|
|||||||
transformManager.setTargetFile(targetFile);
|
transformManager.setTargetFile(targetFile);
|
||||||
|
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
||||||
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
transformManager.getOutputStream().close();
|
transformManager.getOutputStream().close();
|
||||||
@@ -309,14 +304,14 @@ public class StreamHandlerTest
|
|||||||
transformManager.setTargetFile(targetFile);
|
transformManager.setTargetFile(targetFile);
|
||||||
|
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
||||||
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
transformManager.setOutputStream(outputStream);
|
transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
File targetFileCreatedByTransform = transformManager.createTargetFile();
|
File targetFileCreatedByTransform = transformManager.createTargetFile();
|
||||||
assertEquals(targetFile, targetFileCreatedByTransform);
|
assertEquals(targetFile, targetFileCreatedByTransform);
|
||||||
write(targetFileCreatedByTransform, read(inputStream) + CHANGE);
|
write(targetFileCreatedByTransform, read(inputStream)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
transformManager.getOutputStream().close();
|
transformManager.getOutputStream().close();
|
||||||
@@ -349,12 +344,12 @@ public class StreamHandlerTest
|
|||||||
transformManager.keepTargetFile();
|
transformManager.keepTargetFile();
|
||||||
|
|
||||||
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
|
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
|
||||||
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
closeInputStreamWithoutException(inputStream);
|
closeInputStreamWithoutException(inputStream);
|
||||||
@@ -380,12 +375,12 @@ public class StreamHandlerTest
|
|||||||
transformManager.setTargetFile(targetFile);
|
transformManager.setTargetFile(targetFile);
|
||||||
|
|
||||||
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
|
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile));
|
||||||
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
closeInputStreamWithoutException(inputStream);
|
closeInputStreamWithoutException(inputStream);
|
||||||
@@ -409,12 +404,12 @@ public class StreamHandlerTest
|
|||||||
transformManager.setTargetFile(targetFile);
|
transformManager.setTargetFile(targetFile);
|
||||||
|
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
||||||
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile)))
|
||||||
{
|
{
|
||||||
transformManager.setInputStream(inputStream);
|
transformManager.setInputStream(inputStream);
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
write(outputStreamLengthRecorder, read(inputStream) + CHANGE);
|
write(outputStreamLengthRecorder, read(inputStream)+CHANGE);
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
transformManager.copyTargetFileToOutputStream();
|
||||||
closeInputStreamWithoutException(inputStream);
|
closeInputStreamWithoutException(inputStream);
|
||||||
@@ -442,7 +437,7 @@ public class StreamHandlerTest
|
|||||||
@Override
|
@Override
|
||||||
protected void transform(CustomTransformer customTransformer) throws Exception
|
protected void transform(CustomTransformer customTransformer) throws Exception
|
||||||
{
|
{
|
||||||
write(outputStream, read(inputStream) + CHANGE);
|
write(outputStream, read(inputStream)+CHANGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,7 +448,8 @@ public class StreamHandlerTest
|
|||||||
|
|
||||||
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
|
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
|
||||||
{
|
{
|
||||||
new FakeStreamHandler() {
|
new FakeStreamHandler()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
protected void init() throws IOException
|
protected void init() throws IOException
|
||||||
{
|
{
|
||||||
@@ -484,7 +480,8 @@ public class StreamHandlerTest
|
|||||||
File sourceFile = tempFile();
|
File sourceFile = tempFile();
|
||||||
write(sourceFile, ORIGINAL);
|
write(sourceFile, ORIGINAL);
|
||||||
|
|
||||||
new FakeStreamHandler() {
|
new FakeStreamHandler()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
protected void init() throws IOException
|
protected void init() throws IOException
|
||||||
{
|
{
|
||||||
@@ -515,7 +512,8 @@ public class StreamHandlerTest
|
|||||||
File sourceFile = tempFile();
|
File sourceFile = tempFile();
|
||||||
write(sourceFile, ORIGINAL);
|
write(sourceFile, ORIGINAL);
|
||||||
|
|
||||||
new FakeStreamHandler() {
|
new FakeStreamHandler()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
protected InputStream getInputStream() throws IOException
|
protected InputStream getInputStream() throws IOException
|
||||||
{
|
{
|
||||||
@@ -535,7 +533,8 @@ public class StreamHandlerTest
|
|||||||
{
|
{
|
||||||
File targetFile = tempFile();
|
File targetFile = tempFile();
|
||||||
|
|
||||||
new FakeStreamHandler() {
|
new FakeStreamHandler()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
protected InputStream getInputStream()
|
protected InputStream getInputStream()
|
||||||
{
|
{
|
||||||
@@ -561,7 +560,8 @@ public class StreamHandlerTest
|
|||||||
{
|
{
|
||||||
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
|
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
|
||||||
{
|
{
|
||||||
new FakeStreamHandler() {
|
new FakeStreamHandler()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
protected InputStream getInputStream()
|
protected InputStream getInputStream()
|
||||||
{
|
{
|
||||||
@@ -576,65 +576,4 @@ public class StreamHandlerTest
|
|||||||
}.handleTransformRequest();
|
}.handleTransformRequest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testStartWithInputStreamAndCallCreateSourceFileForDocxFiles() throws Exception
|
|
||||||
{
|
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
|
||||||
{
|
|
||||||
transformManager.setSourceFileName("test.docx");
|
|
||||||
transformManager.setInputStream(inputStream);
|
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
|
||||||
|
|
||||||
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
|
||||||
assertTrue(sourceFileCreatedByTransform.exists());
|
|
||||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
|
||||||
transformManager.getOutputStream().close();
|
|
||||||
closeInputStreamWithoutException(inputStream);
|
|
||||||
Long outputLength = transformManager.getOutputLength();
|
|
||||||
transformManager.deleteSourceFile();
|
|
||||||
transformManager.deleteTargetFile();
|
|
||||||
|
|
||||||
assertEquals(EXPECTED, read(outputStream));
|
|
||||||
assertEquals(EXPECTED.length(), outputLength);
|
|
||||||
assertFalse(sourceFileCreatedByTransform.exists());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testStartWithInputStreamAndCallCreateSourceFileForDocxFilesWithHttpRequest() throws Exception
|
|
||||||
{
|
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
|
||||||
{
|
|
||||||
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
|
|
||||||
Part mockPart = Mockito.mock(Part.class);
|
|
||||||
Mockito.when(mockPart.getSubmittedFileName()).thenReturn("dummy.docx");
|
|
||||||
Collection<Part> parts = Arrays.asList(mockPart);
|
|
||||||
Mockito.when(mockRequest.getParts()).thenReturn(parts);
|
|
||||||
|
|
||||||
transformManager.setSourceMimetype("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
|
||||||
transformManager.setInputStream(inputStream);
|
|
||||||
transformManager.setRequest(mockRequest);
|
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
|
||||||
|
|
||||||
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
|
||||||
assertTrue(sourceFileCreatedByTransform.exists());
|
|
||||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
|
||||||
transformManager.getOutputStream().close();
|
|
||||||
closeInputStreamWithoutException(inputStream);
|
|
||||||
Long outputLength = transformManager.getOutputLength();
|
|
||||||
transformManager.deleteSourceFile();
|
|
||||||
transformManager.deleteTargetFile();
|
|
||||||
|
|
||||||
assertEquals(EXPECTED, read(outputStream));
|
|
||||||
assertEquals(EXPECTED.length(), outputLength);
|
|
||||||
assertFalse(sourceFileCreatedByTransform.exists());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user