| 1 |
|
package org.europa.together.application; |
| 2 |
|
|
| 3 |
|
import static com.google.code.beanmatchers.BeanMatchers.*; |
| 4 |
|
import java.io.File; |
| 5 |
|
import java.util.HashMap; |
| 6 |
|
import java.util.Map; |
| 7 |
|
import org.europa.together.JUnit5Preperator; |
| 8 |
|
import org.europa.together.application.internal.PdfDocument; |
| 9 |
|
import org.europa.together.business.Logger; |
| 10 |
|
import org.europa.together.business.PdfRenderer; |
| 11 |
|
import org.europa.together.business.TemplateRenderer; |
| 12 |
|
import org.europa.together.domain.LogLevel; |
| 13 |
|
import org.europa.together.utils.Constraints; |
| 14 |
|
import org.europa.together.utils.StringUtils; |
| 15 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
| 16 |
|
import org.junit.jupiter.api.AfterAll; |
| 17 |
|
import org.junit.jupiter.api.AfterEach; |
| 18 |
|
import static org.junit.jupiter.api.Assertions.*; |
| 19 |
|
import org.junit.jupiter.api.Assumptions; |
| 20 |
|
import org.junit.jupiter.api.BeforeAll; |
| 21 |
|
import org.junit.jupiter.api.BeforeEach; |
| 22 |
|
import org.junit.jupiter.api.Test; |
| 23 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
| 24 |
|
import org.openpdf.text.pdf.PdfReader; |
| 25 |
|
import org.springframework.beans.factory.annotation.Autowired; |
| 26 |
|
import org.springframework.test.context.ContextConfiguration; |
| 27 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 28 |
|
|
| 29 |
|
@SuppressWarnings("unchecked") |
| 30 |
|
@ExtendWith({SpringExtension.class, JUnit5Preperator.class}) |
| 31 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
| |
|
| 100% |
Uncovered Elements: 0 (94) |
Complexity: 16 |
Complexity Density: 0.21 |
|
| 32 |
|
public class OpenPdfRendererTest { |
| 33 |
|
|
| 34 |
|
private static final Logger LOGGER |
| 35 |
|
= new LogbackLogger(OpenPdfRendererTest.class); |
| 36 |
|
private static final String FILE_PATH |
| 37 |
|
= "org/europa/together/pdf"; |
| 38 |
|
private static final String DIRECTORY |
| 39 |
|
= Constraints.SYSTEM_APP_DIR + "/target/test-classes/"; |
| 40 |
|
|
| 41 |
|
@Autowired |
| 42 |
|
private PdfRenderer pdf; |
| 43 |
|
|
| 44 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 45 |
1 |
@BeforeAll... |
| 46 |
|
static void setUp() { |
| 47 |
1 |
Assumptions.assumeTrue(true, "Assumtion failed."); |
| 48 |
|
|
| 49 |
1 |
LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG); |
| 50 |
|
} |
| 51 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 52 |
1 |
@AfterAll... |
| 53 |
|
static void tearDown() { |
| 54 |
|
} |
| 55 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 56 |
12 |
@BeforeEach... |
| 57 |
|
void testCaseInitialization() { |
| 58 |
|
} |
| 59 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 60 |
12 |
@AfterEach... |
| 61 |
|
void testCaseTermination() { |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 65 |
1 |
@Test... |
| 66 |
|
void constructor() { |
| 67 |
1 |
LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG); |
| 68 |
|
|
| 69 |
1 |
assertThat(OpenPdfRenderer.class, hasValidBeanConstructor()); |
| 70 |
1 |
assertThat(OpenPdfRenderer.class, hasValidGettersAndSetters()); |
| 71 |
|
} |
| 72 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 73 |
1 |
@Test... |
| 74 |
|
void loadPdf() throws Exception { |
| 75 |
1 |
LOGGER.log("TEST CASE: loadPdf", LogLevel.DEBUG); |
| 76 |
|
|
| 77 |
1 |
File file = new File(DIRECTORY + FILE_PATH + "/document.pdf"); |
| 78 |
1 |
PdfReader document = pdf.loadDocument(file); |
| 79 |
1 |
assertEquals(5, document.getNumberOfPages()); |
| 80 |
|
} |
| 81 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 82 |
1 |
@Test... |
| 83 |
|
void failLoadPdfNotExist() throws Exception { |
| 84 |
1 |
LOGGER.log("TEST CASE: failLoadPdfNotExist", LogLevel.DEBUG); |
| 85 |
1 |
assertThrows(Exception.class, () -> { |
| 86 |
1 |
pdf.loadDocument(new File("notexist.pdf")); |
| 87 |
|
}); |
| 88 |
|
} |
| 89 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 90 |
1 |
@Test... |
| 91 |
|
void failLoadPdf() throws Exception { |
| 92 |
1 |
LOGGER.log("TEST CASE: failLoadPdf", LogLevel.DEBUG); |
| 93 |
1 |
assertThrows(Exception.class, () -> { |
| 94 |
1 |
pdf.loadDocument(null); |
| 95 |
|
}); |
| 96 |
|
} |
| 97 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 98 |
1 |
@Test... |
| 99 |
|
void loadAndWritePdf() throws Exception { |
| 100 |
1 |
LOGGER.log("TEST CASE: loadAndWritePdf", LogLevel.DEBUG); |
| 101 |
|
|
| 102 |
1 |
File file = new File(DIRECTORY + FILE_PATH + "/document.pdf"); |
| 103 |
1 |
PdfDocument document = pdf.loadDocument(file); |
| 104 |
|
|
| 105 |
1 |
String out = DIRECTORY + FILE_PATH + "/copy.pdf"; |
| 106 |
1 |
pdf.writeDocument(document, out); |
| 107 |
|
|
| 108 |
1 |
assertEquals(true, new File(out).exists()); |
| 109 |
|
} |
| 110 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 111 |
1 |
@Test... |
| 112 |
|
void failWritePdf() throws Exception { |
| 113 |
1 |
LOGGER.log("TEST CASE: failWritePdf", LogLevel.DEBUG); |
| 114 |
|
|
| 115 |
1 |
assertThrows(Exception.class, () -> { |
| 116 |
1 |
String out = DIRECTORY + FILE_PATH + "/fail.pdf"; |
| 117 |
1 |
pdf.writeDocument(pdf.loadDocument(null), null); |
| 118 |
|
}); |
| 119 |
|
} |
| 120 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 121 |
1 |
@Test... |
| 122 |
|
void removePages() throws Exception { |
| 123 |
1 |
LOGGER.log("TEST CASE: removePages", LogLevel.DEBUG); |
| 124 |
|
|
| 125 |
1 |
File file = new File(DIRECTORY + FILE_PATH + "/document.pdf"); |
| 126 |
1 |
PdfDocument document = pdf.loadDocument(file); |
| 127 |
1 |
assertEquals(5, document.getNumberOfPages()); |
| 128 |
|
|
| 129 |
1 |
PdfDocument reduced = pdf.removePage(document, 1, 3, 5); |
| 130 |
|
|
| 131 |
1 |
String out = DIRECTORY + FILE_PATH + "/reduced.pdf"; |
| 132 |
1 |
pdf.writeDocument(reduced, out); |
| 133 |
|
|
| 134 |
1 |
assertEquals(2, reduced.getNumberOfPages()); |
| 135 |
|
} |
| 136 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 137 |
1 |
@Test... |
| 138 |
|
void failRemovePages() throws Exception { |
| 139 |
1 |
LOGGER.log("TEST CASE: failRemovePages", LogLevel.DEBUG); |
| 140 |
1 |
assertThrows(Exception.class, () -> { |
| 141 |
1 |
pdf.removePage(pdf.loadDocument(null), 1); |
| 142 |
|
}); |
| 143 |
|
} |
| 144 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 145 |
1 |
@Test... |
| 146 |
|
void failRenderHtmlToPdf() throws Exception { |
| 147 |
1 |
LOGGER.log("TEST CASE: failRenderHtmlToPdf", LogLevel.DEBUG); |
| 148 |
|
|
| 149 |
1 |
assertThrows(Exception.class, () -> { |
| 150 |
1 |
pdf.renderDocumentFromHtml(null, "no content"); |
| 151 |
|
}); |
| 152 |
|
} |
| 153 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 154 |
1 |
@Test... |
| 155 |
|
void simpleRenderHtmlToPdf() throws Exception { |
| 156 |
1 |
LOGGER.log("TEST CASE: renderHtmlToPdf", LogLevel.DEBUG); |
| 157 |
|
|
| 158 |
1 |
String html = "<h1>My First PDF Document</h1 > <p>" |
| 159 |
|
+ StringUtils.generateLoremIpsum(0) + "</p>"; |
| 160 |
|
|
| 161 |
1 |
pdf.setTitle("OpenPDF test document"); |
| 162 |
1 |
pdf.setAuthor("Elmar Dott"); |
| 163 |
1 |
pdf.setSubject("JUnit test"); |
| 164 |
1 |
pdf.setKeywords("test, junit5, openPDF"); |
| 165 |
1 |
pdf.renderDocumentFromHtml(DIRECTORY + "test.pdf", html); |
| 166 |
|
|
| 167 |
1 |
assertTrue(new File(DIRECTORY + "test.pdf").exists()); |
| 168 |
1 |
assertNotEquals(0, new File(DIRECTORY + "test.pdf").length()); |
| 169 |
|
} |
| 170 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 171 |
1 |
@Test... |
| 172 |
|
void renderTextPdfFromVelocity() throws Exception { |
| 173 |
1 |
LOGGER.log("TEST CASE: renderTextPdfFromVelocity", LogLevel.DEBUG); |
| 174 |
|
|
| 175 |
1 |
Map<String, String> properties = new HashMap<>(); |
| 176 |
1 |
properties.put("prop_01", "value 01"); |
| 177 |
1 |
properties.put("prop_02", "value 02"); |
| 178 |
1 |
properties.put("prop_03", "value 03"); |
| 179 |
|
|
| 180 |
1 |
String directory = Constraints.SYSTEM_APP_DIR |
| 181 |
|
+ "/target/test-classes/org/europa/together/velocity"; |
| 182 |
|
|
| 183 |
1 |
TemplateRenderer templateEngine = new VelocityRenderer(); |
| 184 |
1 |
String template = templateEngine |
| 185 |
|
.loadContentByFileResource(directory, "/html2pdf.vm", properties); |
| 186 |
|
|
| 187 |
1 |
pdf.setTitle("Velocity HTML Template"); |
| 188 |
1 |
pdf.setAuthor("Elmar Dott"); |
| 189 |
1 |
pdf.setSubject("JUnit test"); |
| 190 |
1 |
pdf.setKeywords("test, junit5, openPDF"); |
| 191 |
1 |
pdf.renderDocumentFromHtml(DIRECTORY + "velocityText.pdf", template); |
| 192 |
|
|
| 193 |
1 |
String testDocument = DIRECTORY + "velocityText.pdf"; |
| 194 |
1 |
String referenceDocument = Constraints.SYSTEM_APP_DIR |
| 195 |
|
+ "/target/test-classes/org/europa/together/pdf/velocityText.pdf"; |
| 196 |
|
|
| 197 |
1 |
assertTrue(new File(testDocument).exists()); |
| 198 |
1 |
assertEquals(new File(testDocument).length(), new File(referenceDocument).length()); |
| 199 |
|
} |
| 200 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 201 |
1 |
@Test... |
| 202 |
|
void includeLocalImages() throws Exception { |
| 203 |
1 |
LOGGER.log("TEST CASE: includeLocalImages", LogLevel.DEBUG); |
| 204 |
|
|
| 205 |
1 |
String image = Constraints.SYSTEM_APP_DIR |
| 206 |
|
+ "/src/test/resources/org/europa/together/images/135621.jpg"; |
| 207 |
1 |
String template = "<html><head></head><body><img src=\"" + image + "\" /></body></html>"; |
| 208 |
1 |
assertTrue(new File(image).exists()); |
| 209 |
|
|
| 210 |
1 |
pdf.setTitle("Image Template"); |
| 211 |
1 |
pdf.setAuthor("Elmar Dott"); |
| 212 |
1 |
pdf.setSubject("JUnit test"); |
| 213 |
1 |
pdf.setKeywords("test, junit5, openPDF"); |
| 214 |
1 |
pdf.renderDocumentFromHtml(DIRECTORY + "images.pdf", template); |
| 215 |
|
|
| 216 |
1 |
String testDocument = DIRECTORY + "images.pdf"; |
| 217 |
1 |
String referenceDocument = Constraints.SYSTEM_APP_DIR |
| 218 |
|
+ "/target/test-classes/org/europa/together/pdf/images.pdf"; |
| 219 |
|
|
| 220 |
1 |
assertTrue(new File(testDocument).exists()); |
| 221 |
1 |
assertEquals(new File(testDocument).length(), new File(referenceDocument).length()); |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
} |