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;
|
||||
|
||||
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.alfresco.transform.base.CustomTransformer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
@@ -42,19 +42,14 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
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 org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.alfresco.transform.base.CustomTransformer;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
@@ -453,7 +448,8 @@ public class StreamHandlerTest
|
||||
|
||||
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
|
||||
{
|
||||
new FakeStreamHandler() {
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
@Override
|
||||
protected void init() throws IOException
|
||||
{
|
||||
@@ -484,7 +480,8 @@ public class StreamHandlerTest
|
||||
File sourceFile = tempFile();
|
||||
write(sourceFile, ORIGINAL);
|
||||
|
||||
new FakeStreamHandler() {
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
@Override
|
||||
protected void init() throws IOException
|
||||
{
|
||||
@@ -515,7 +512,8 @@ public class StreamHandlerTest
|
||||
File sourceFile = tempFile();
|
||||
write(sourceFile, ORIGINAL);
|
||||
|
||||
new FakeStreamHandler() {
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
@Override
|
||||
protected InputStream getInputStream() throws IOException
|
||||
{
|
||||
@@ -535,7 +533,8 @@ public class StreamHandlerTest
|
||||
{
|
||||
File targetFile = tempFile();
|
||||
|
||||
new FakeStreamHandler() {
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
@Override
|
||||
protected InputStream getInputStream()
|
||||
{
|
||||
@@ -561,7 +560,8 @@ public class StreamHandlerTest
|
||||
{
|
||||
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
|
||||
{
|
||||
new FakeStreamHandler() {
|
||||
new FakeStreamHandler()
|
||||
{
|
||||
@Override
|
||||
protected InputStream getInputStream()
|
||||
{
|
||||
@@ -576,65 +576,4 @@ public class StreamHandlerTest
|
||||
}.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