1 |
|
package org.europa.together.application; |
2 |
|
|
3 |
|
import static com.google.code.beanmatchers.BeanMatchers.*; |
4 |
|
import com.lowagie.text.pdf.PdfReader; |
5 |
|
import java.io.File; |
6 |
|
import org.europa.together.JUnit5Preperator; |
7 |
|
import org.europa.together.application.internal.PdfDocument; |
8 |
|
import org.europa.together.business.Logger; |
9 |
|
import org.europa.together.business.PdfRenderer; |
10 |
|
import org.europa.together.domain.LogLevel; |
11 |
|
import org.europa.together.utils.Constraints; |
12 |
|
import org.europa.together.utils.StringUtils; |
13 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
14 |
|
import org.junit.jupiter.api.AfterAll; |
15 |
|
import org.junit.jupiter.api.AfterEach; |
16 |
|
import static org.junit.jupiter.api.Assertions.*; |
17 |
|
import org.junit.jupiter.api.Assumptions; |
18 |
|
import org.junit.jupiter.api.BeforeAll; |
19 |
|
import org.junit.jupiter.api.BeforeEach; |
20 |
|
import org.junit.jupiter.api.Test; |
21 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
22 |
|
import org.springframework.beans.factory.annotation.Autowired; |
23 |
|
import org.springframework.test.context.ContextConfiguration; |
24 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
25 |
|
|
26 |
|
@SuppressWarnings("unchecked") |
27 |
|
@ExtendWith({JUnit5Preperator.class}) |
28 |
|
@ExtendWith(SpringExtension.class) |
29 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
|
|
| 100% |
Uncovered Elements: 0 (58) |
Complexity: 13 |
Complexity Density: 0.29 |
|
30 |
|
public class OpenPdfRendererTest { |
31 |
|
|
32 |
|
private static final Logger LOGGER |
33 |
|
= new LogbackLogger(OpenPdfRendererTest.class); |
34 |
|
private static final String FILE_PATH |
35 |
|
= "org/europa/together/pdf"; |
36 |
|
private static final String DIRECTORY |
37 |
|
= Constraints.SYSTEM_APP_DIR + "/target/test-classes/"; |
38 |
|
|
39 |
|
@Autowired |
40 |
|
private PdfRenderer pdf; |
41 |
|
|
42 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
43 |
1 |
@BeforeAll... |
44 |
|
static void setUp() { |
45 |
1 |
Assumptions.assumeTrue(true, "Assumtion failed."); |
46 |
|
|
47 |
1 |
LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG); |
48 |
|
} |
49 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
50 |
1 |
@AfterAll... |
51 |
|
static void tearDown() { |
52 |
|
} |
53 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
54 |
9 |
@BeforeEach... |
55 |
|
void testCaseInitialization() { |
56 |
|
} |
57 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
58 |
9 |
@AfterEach... |
59 |
|
void testCaseTermination() { |
60 |
|
} |
61 |
|
|
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
63 |
1 |
@Test... |
64 |
|
void constructor() { |
65 |
1 |
LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG); |
66 |
|
|
67 |
1 |
assertThat(OpenPdfRenderer.class, hasValidBeanConstructor()); |
68 |
1 |
assertThat(OpenPdfRenderer.class, hasValidGettersAndSetters()); |
69 |
|
} |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
71 |
1 |
@Test... |
72 |
|
void loadPdf() throws Exception { |
73 |
1 |
LOGGER.log("TEST CASE: loadPdf", LogLevel.DEBUG); |
74 |
|
|
75 |
1 |
File file = new File(DIRECTORY + FILE_PATH + "/document.pdf"); |
76 |
1 |
PdfReader document = pdf.loadDocument(file); |
77 |
1 |
assertEquals(5, document.getNumberOfPages()); |
78 |
|
} |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
80 |
1 |
@Test... |
81 |
|
void failLoadPdf() throws Exception { |
82 |
1 |
LOGGER.log("TEST CASE: failLoadPdf", LogLevel.DEBUG); |
83 |
1 |
assertThrows(Exception.class, () -> { |
84 |
1 |
pdf.loadDocument(null); |
85 |
|
}); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
88 |
1 |
@Test... |
89 |
|
void loadAndWritePdf() throws Exception { |
90 |
1 |
LOGGER.log("TEST CASE: loadAndWritePdf", LogLevel.DEBUG); |
91 |
|
|
92 |
1 |
File file = new File(DIRECTORY + FILE_PATH + "/document.pdf"); |
93 |
1 |
PdfDocument document = pdf.loadDocument(file); |
94 |
|
|
95 |
1 |
String out = DIRECTORY + FILE_PATH + "/copy.pdf"; |
96 |
1 |
pdf.writeDocument(document, out); |
97 |
|
|
98 |
1 |
assertEquals(true, new File(out).exists()); |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
101 |
1 |
@Test... |
102 |
|
void failWritePdf() throws Exception { |
103 |
1 |
LOGGER.log("TEST CASE: failWritePdf", LogLevel.DEBUG); |
104 |
|
|
105 |
1 |
assertThrows(Exception.class, () -> { |
106 |
1 |
String out = DIRECTORY + FILE_PATH + "/fail.pdf"; |
107 |
1 |
pdf.writeDocument(pdf.loadDocument(null), null); |
108 |
|
}); |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
111 |
1 |
@Test... |
112 |
|
void removePages() throws Exception { |
113 |
1 |
LOGGER.log("TEST CASE: removePages", LogLevel.DEBUG); |
114 |
|
|
115 |
1 |
File file = new File(DIRECTORY + FILE_PATH + "/document.pdf"); |
116 |
1 |
PdfDocument document = pdf.loadDocument(file); |
117 |
1 |
assertEquals(5, document.getNumberOfPages()); |
118 |
|
|
119 |
1 |
PdfDocument reduced = pdf.removePage(document, 1, 3, 5); |
120 |
|
|
121 |
1 |
String out = DIRECTORY + FILE_PATH + "/reduced.pdf"; |
122 |
1 |
pdf.writeDocument(reduced, out); |
123 |
|
|
124 |
1 |
assertEquals(2, reduced.getNumberOfPages()); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
127 |
1 |
@Test... |
128 |
|
void failRemovePages() throws Exception { |
129 |
1 |
LOGGER.log("TEST CASE: failRemovePages", LogLevel.DEBUG); |
130 |
1 |
assertThrows(Exception.class, () -> { |
131 |
1 |
pdf.removePage(pdf.loadDocument(null), 1); |
132 |
|
}); |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
135 |
1 |
@Test... |
136 |
|
void failRenderHtmlToPdf() throws Exception { |
137 |
1 |
LOGGER.log("TEST CASE: failRenderHtmlToPdf", LogLevel.DEBUG); |
138 |
|
|
139 |
1 |
assertThrows(Exception.class, |
140 |
|
() -> { |
141 |
1 |
pdf.renderDocumentFromHtml( |
142 |
|
null, "no content"); |
143 |
|
}); |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
void simpleRenderHtmlToPdf() throws Exception { |
148 |
1 |
LOGGER.log("TEST CASE: renderHtmlToPdf", LogLevel.DEBUG); |
149 |
|
|
150 |
1 |
String html = "<h1>My First PDF Document</h1 > <p>" |
151 |
|
+ StringUtils.generateLoremIpsum(0) + "</p>"; |
152 |
|
|
153 |
1 |
pdf.setTitle("OpenPDF test document"); |
154 |
1 |
pdf.setAuthor("Elmar Dott"); |
155 |
1 |
pdf.setSubject("JUnit test"); |
156 |
1 |
pdf.setKeywords("test, junit5, openPDF"); |
157 |
1 |
pdf.renderDocumentFromHtml(DIRECTORY + "test.pdf", html); |
158 |
|
|
159 |
1 |
assertTrue(new File(DIRECTORY + "test.pdf").exists()); |
160 |
1 |
assertNotEquals(0, new File(DIRECTORY + "test.pdf").length()); |
161 |
|
} |
162 |
|
|
163 |
|
} |