ATS-531 : Reformat code

This commit is contained in:
Cezar.Leahu
2019-08-14 22:21:06 +03:00
committed by CezarLeahu
parent 70ab0241dd
commit 485347729b
58 changed files with 1310 additions and 1074 deletions

View File

@@ -26,30 +26,7 @@
*/
package org.alfresco.transformer;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transformer.transformers.AppleIWorksContentTransformer;
import org.alfresco.transformer.transformers.HtmlParserContentTransformer;
import org.alfresco.transformer.transformers.SelectingTransformer;
import org.alfresco.transformer.transformers.StringExtractingContentTransformer;
import org.alfresco.transformer.transformers.TextToPdfContentTransformer;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IMAGE_JPEG;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_KEYNOTE;
@@ -64,6 +41,26 @@ import static org.springframework.http.HttpStatus.OK;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transformer.transformers.SelectingTransformer;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
@RunWith(SpringRunner.class)
@WebMvcTest(MiscController.class)
@Import({SelectingTransformer.class})
@@ -88,12 +85,13 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
expectedSourceFileBytes = readTestFile(sourceExtension);
expectedTargetFileBytes = readTestFile(targetExtension);
//expectedTargetFileBytes = null;
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype,
expectedSourceFileBytes);
}
@Override
protected void mockTransformCommand(String sourceExtension, String targetExtension, String sourceMimetype, boolean readTargetFileBytes) throws IOException
protected void mockTransformCommand(String sourceExtension, String targetExtension,
String sourceMimetype, boolean readTargetFileBytes) throws IOException
{
}
@@ -110,13 +108,14 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Override
// Add extra required parameters to the request.
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params)
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile,
String... params)
{
return super.mockMvcRequest(url, sourceFile, params)
.param("targetEncoding", targetEncoding)
.param("sourceEncoding", sourceEncoding)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
.param("targetEncoding", targetEncoding)
.param("sourceEncoding", sourceEncoding)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
}
@Test
@@ -130,28 +129,29 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Test
public void testHTMLtoString() throws Exception
{
final String NEWLINE = System.getProperty ("line.separator");
final String NEWLINE = System.getProperty("line.separator");
final String TITLE = "Testing!";
final String TEXT_P1 = "This is some text in English";
final String TEXT_P2 = "This is more text in English";
final String TEXT_P3 = "C'est en Fran\u00e7ais et Espa\u00f1ol";
String partA = "<html><head><title>" + TITLE + "</title></head>" + NEWLINE;
String partB = "<body><p>" + TEXT_P1 + "</p>" + NEWLINE +
"<p>" + TEXT_P2 + "</p>" + NEWLINE +
"<p>" + TEXT_P3 + "</p>" + NEWLINE;
"<p>" + TEXT_P2 + "</p>" + NEWLINE +
"<p>" + TEXT_P3 + "</p>" + NEWLINE;
String partC = "</body></html>";
final String expected = TITLE + NEWLINE + TEXT_P1 + NEWLINE + TEXT_P2 + NEWLINE + TEXT_P3 + NEWLINE;
MvcResult result = sendText("html",
"UTF-8",
MIMETYPE_HTML,
"txt",
MIMETYPE_TEXT_PLAIN,
"UTF-8",
expected.getBytes());
"UTF-8",
MIMETYPE_HTML,
"txt",
MIMETYPE_TEXT_PLAIN,
"UTF-8",
expected.getBytes());
String contentResult = new String(result.getResponse().getContentAsByteArray(), targetEncoding);
assertTrue("The content did not include \""+expected, contentResult.contains(expected));
String contentResult = new String(result.getResponse().getContentAsByteArray(),
targetEncoding);
assertTrue("The content did not include \"" + expected, contentResult.contains(expected));
}
@Test
@@ -161,7 +161,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
byte[] content = null;
try
{
content = "azAz10!<21>$%^&*()\t\r\n".getBytes("UTF-8");
content = "azAz10!<21>$%^&*()\t\r\n".getBytes(UTF_8);
expected = new String(content, "MacDingbat");
}
catch (UnsupportedEncodingException e)
@@ -170,32 +170,34 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
}
MvcResult result = sendText("txt",
"MacDingbat",
MIMETYPE_TEXT_PLAIN,
"txt",
MIMETYPE_TEXT_PLAIN,
"UTF-8",
content);
"MacDingbat",
MIMETYPE_TEXT_PLAIN,
"txt",
MIMETYPE_TEXT_PLAIN,
"UTF-8",
content);
String contentResult = new String(result.getResponse().getContentAsByteArray(), targetEncoding);
assertTrue("The content did not include \""+expected, contentResult.contains(expected));
String contentResult = new String(result.getResponse().getContentAsByteArray(),
targetEncoding);
assertTrue("The content did not include \"" + expected, contentResult.contains(expected));
}
@Test
public void testEmptyTextFileReturnsEmptyFile() throws Exception
{
// Use empty content to create an empty source file
byte[] content = new byte[0];
byte[] content = new byte[0];
MvcResult result = sendText("txt",
"UTF-8",
MIMETYPE_TEXT_PLAIN,
"txt",
MIMETYPE_TEXT_PLAIN,
"UTF-8",
content);
"UTF-8",
MIMETYPE_TEXT_PLAIN,
"txt",
MIMETYPE_TEXT_PLAIN,
"UTF-8",
content);
assertEquals("Returned content should be empty for an empty source file", 0, result.getResponse().getContentLength());
assertEquals("Returned content should be empty for an empty source file", 0,
result.getResponse().getContentLength());
}
@Test
@@ -203,7 +205,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
{
StringBuilder sb = new StringBuilder();
String expected = null;
for (int i=1; i<=5; i++)
for (int i = 1; i <= 5; i++)
{
sb.append(i);
sb.append(" I must not talk in class or feed my homework to my cat.\n");
@@ -212,12 +214,12 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
expected = sb.toString();
MvcResult result = sendText("txt",
"UTF-8",
MIMETYPE_TEXT_PLAIN,
"pdf",
MIMETYPE_PDF,
"UTF-8",
expected.getBytes());
"UTF-8",
MIMETYPE_TEXT_PLAIN,
"pdf",
MIMETYPE_PDF,
"UTF-8",
expected.getBytes());
// Read back in the PDF and check it
PDDocument doc = PDDocument.load(result.getResponse().getContentAsByteArray());
@@ -257,45 +259,54 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
imageBasedTransform("docx", MIMETYPE_OPENXML_WORDPROCESSING, MIMETYPE_IMAGE_JPEG, "jpeg");
}
private void imageBasedTransform(String sourceExtension, String sourceMimetype, String targetMimetype, String targetExtension) throws Exception
private void imageBasedTransform(String sourceExtension, String sourceMimetype,
String targetMimetype, String targetExtension) throws Exception
{
MockMultipartFile sourceFilex = new MockMultipartFile("file", "test_file." + sourceExtension, sourceMimetype, readTestFile(sourceExtension));
MockMultipartFile sourceFilex = new MockMultipartFile("file",
"test_file." + sourceExtension, sourceMimetype, readTestFile(sourceExtension));
MockHttpServletRequestBuilder requestBuilder = super.mockMvcRequest("/transform", sourceFilex)
.param("targetExtension", "jpeg")
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
MockHttpServletRequestBuilder requestBuilder = super
.mockMvcRequest("/transform", sourceFilex)
.param("targetExtension", "jpeg")
.param("targetMimetype", targetMimetype)
.param("sourceMimetype",
sourceMimetype);
MvcResult result = mockMvc.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''test_file." + targetExtension))
.andReturn();
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L);
MvcResult result = mockMvc
.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''test_file." + targetExtension))
.andReturn();
assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
}
private MvcResult sendText(String sourceExtension,
String sourceEncoding,
String sourceMimetype,
String targetExtension,
String targetMimetype,
String targetEncoding,
byte[] content) throws Exception
String sourceEncoding,
String sourceMimetype,
String targetExtension,
String targetMimetype,
String targetEncoding,
byte[] content) throws Exception
{
MockMultipartFile sourceFilex = new MockMultipartFile("file", "test_file." + sourceExtension, sourceMimetype, content);
MockMultipartFile sourceFilex = new MockMultipartFile("file",
"test_file." + sourceExtension, sourceMimetype, content);
MockHttpServletRequestBuilder requestBuilder = super.mockMvcRequest("/transform", sourceFilex)
.param("targetExtension", targetExtension)
.param("targetEncoding", targetEncoding)
.param("targetMimetype", targetMimetype)
.param("sourceEncoding", sourceEncoding)
.param("sourceMimetype", sourceMimetype);
MockHttpServletRequestBuilder requestBuilder = super
.mockMvcRequest("/transform", sourceFilex)
.param("targetExtension", targetExtension)
.param("targetEncoding", targetEncoding)
.param("targetMimetype", targetMimetype)
.param("sourceEncoding", sourceEncoding)
.param("sourceMimetype", sourceMimetype);
MvcResult result = mockMvc.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= "+targetEncoding+"''test_file." + targetExtension)).
andReturn();
MvcResult result = mockMvc
.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition",
"attachment; filename*= " + targetEncoding + "''test_file." + targetExtension))
.andReturn();
return result;
}
@@ -307,5 +318,4 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
text = text.replaceAll("\\n", "");
return text;
}
}

View File

@@ -26,16 +26,16 @@
*/
package org.alfresco.transformer;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import java.util.UUID;
import org.alfresco.transform.client.model.TransformRequest;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.UUID;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {"activemq.url=nio://localhost:61616"})
@@ -44,14 +44,15 @@ public class MiscQueueTransformServiceIT extends AbstractQueueTransformServiceIT
@Override
protected TransformRequest buildRequest()
{
return TransformRequest.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_HTML)
.withTargetMediaType(MIMETYPE_TEXT_PLAIN)
.withTargetExtension("txt")
.withSchema(1)
.withClientData("ACS")
.withSourceReference(UUID.randomUUID().toString())
.withSourceSize(32L).build();
return TransformRequest
.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_HTML)
.withTargetMediaType(MIMETYPE_TEXT_PLAIN)
.withTargetExtension("txt")
.withSchema(1)
.withClientData("ACS")
.withSourceReference(UUID.randomUUID().toString())
.withSourceSize(32L).build();
}
}

View File

@@ -26,13 +26,8 @@
*/
package org.alfresco.transformer.transformers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import static org.alfresco.transformer.transformers.StringExtractingContentTransformer.SOURCE_ENCODING;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileOutputStream;
@@ -41,9 +36,11 @@ import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transformer.transformers.StringExtractingContentTransformer.SOURCE_ENCODING;
import static org.alfresco.transformer.transformers.StringExtractingContentTransformer.TARGET_ENCODING;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@Import(HtmlParserContentTransformer.class)
@@ -54,24 +51,24 @@ public class HtmlParserContentTransformerTest
/**
* Checks that we correctly handle text in different encodings,
* no matter if the encoding is specified on the Content Property
* or in a meta tag within the HTML itself. (ALF-10466)
* no matter if the encoding is specified on the Content Property
* or in a meta tag within the HTML itself. (ALF-10466)
*
* On Windows, org.htmlparser.beans.StringBean.carriageReturn() appends a new system dependent new line
* so we must be careful when checking the returned text
* On Windows, org.htmlparser.beans.StringBean.carriageReturn() appends a new system dependent new line
* so we must be careful when checking the returned text
*/
@Test
public void testEncodingHandling() throws Exception
{
final String NEWLINE = System.getProperty ("line.separator");
final String NEWLINE = System.getProperty("line.separator");
final String TITLE = "Testing!";
final String TEXT_P1 = "This is some text in English";
final String TEXT_P2 = "This is more text in English";
final String TEXT_P3 = "C'est en Fran\u00e7ais et Espa\u00f1ol";
String partA = "<html><head><title>" + TITLE + "</title></head>" + NEWLINE;
String partB = "<body><p>" + TEXT_P1 + "</p>" + NEWLINE +
"<p>" + TEXT_P2 + "</p>" + NEWLINE +
"<p>" + TEXT_P3 + "</p>" + NEWLINE;
"<p>" + TEXT_P2 + "</p>" + NEWLINE +
"<p>" + TEXT_P3 + "</p>" + NEWLINE;
String partC = "</body></html>";
final String expected = TITLE + NEWLINE + TEXT_P1 + NEWLINE + TEXT_P2 + NEWLINE + TEXT_P3 + NEWLINE;
@@ -82,7 +79,7 @@ public class HtmlParserContentTransformerTest
{
// Content set to ISO 8859-1
tmpS = File.createTempFile("AlfrescoTestSource_", ".html");
writeToFile(tmpS, partA+partB+partC, "ISO-8859-1");
writeToFile(tmpS, partA + partB + partC, "ISO-8859-1");
tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt");
@@ -96,7 +93,7 @@ public class HtmlParserContentTransformerTest
// Content set to UTF-8
tmpS = File.createTempFile("AlfrescoTestSource_", ".html");
writeToFile(tmpS, partA+partB+partC, "UTF-8");
writeToFile(tmpS, partA + partB + partC, "UTF-8");
tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt");
parameters = new HashMap<>();
@@ -106,10 +103,9 @@ public class HtmlParserContentTransformerTest
tmpS.delete();
tmpD.delete();
// Content set to UTF-16
tmpS = File.createTempFile("AlfrescoTestSource_", ".html");
writeToFile(tmpS, partA+partB+partC, "UTF-16");
writeToFile(tmpS, partA + partB + partC, "UTF-16");
tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt");
parameters = new HashMap<>();
@@ -129,9 +125,9 @@ public class HtmlParserContentTransformerTest
// Content set to ISO 8859-1, meta set to UTF-8
tmpS = File.createTempFile("AlfrescoTestSource_", ".html");
String str = partA+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +
partB+partC;
String str = partA +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +
partB + partC;
writeToFile(tmpS, str, "UTF-8");
@@ -144,7 +140,6 @@ public class HtmlParserContentTransformerTest
tmpS.delete();
tmpD.delete();
// Note - we can't test UTF-16 with only a meta encoding,
// because without that the parser won't know about the
// 2 byte format so won't be able to identify the meta tag
@@ -166,7 +161,7 @@ public class HtmlParserContentTransformerTest
private String readFromFile(File file, String encoding) throws Exception
{
String content = "wrong content";
String content = "wrong content";
content = new String(Files.readAllBytes(file.toPath()), encoding);
return content;
}

View File

@@ -26,15 +26,8 @@
*/
package org.alfresco.transformer.transformers;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import static org.alfresco.transformer.transformers.TextToPdfContentTransformer.PAGE_LIMIT;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileOutputStream;
@@ -43,8 +36,14 @@ import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transformer.transformers.TextToPdfContentTransformer.PAGE_LIMIT;
import static org.junit.Assert.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@Import(TextToPdfContentTransformer.class)
@@ -60,7 +59,6 @@ public class TextToPdfContentTransformerTest
transformer.setFontSize(20);
}
@Test
public void testUnlimitedPages() throws Exception
{
@@ -88,11 +86,11 @@ public class TextToPdfContentTransformerTest
private void transformTextAndCheckPageLength(int pageLimit) throws Exception
{
int pageLength = 32;
int lines = (pageLength+10) * ((pageLimit > 0) ? pageLimit : 1);
int lines = (pageLength + 10) * ((pageLimit > 0) ? pageLimit : 1);
StringBuilder sb = new StringBuilder();
String checkText = null;
int cutoff = pageLimit * pageLength;
for (int i=1; i<=lines; i++)
for (int i = 1; i <= lines; i++)
{
sb.append(i);
sb.append(" I must not talk in class or feed my homework to my cat.\n");
@@ -105,12 +103,12 @@ public class TextToPdfContentTransformerTest
transformTextAndCheck(text, "UTF-8", checkText, String.valueOf(pageLimit));
}
private void transformTextAndCheck(String text, String encoding, String checkText, String pageLimit) throws Exception
private void transformTextAndCheck(String text, String encoding, String checkText,
String pageLimit) throws Exception
{
// Get a reader for the text
File sourceFile = File.createTempFile("AlfrescoTestSource_", ".txt");
writeToFile(sourceFile,text, encoding);
writeToFile(sourceFile, text, encoding);
// And a temp writer
File targetFile = File.createTempFile("AlfrescoTestTarget_", ".pdf");
@@ -130,8 +128,8 @@ public class TextToPdfContentTransformerTest
String roundTrip = clean(textWriter.toString());
assertEquals(
"Incorrect text in PDF when starting from text in " + encoding,
checkText, roundTrip
"Incorrect text in PDF when starting from text in " + encoding,
checkText, roundTrip
);
sourceFile.delete();