1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.application

File OpenPdfRendererTest.java

 

Code metrics

0
78
16
1
224
176
16
0.21
4.88
16
1

Classes

Class Line # Actions
OpenPdfRendererTest 32 78 0% 16 0
1.0100%
 

Contributing tests

This file is covered by 12 tests. .

Source view

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"})
 
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    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
45  1 toggle @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   
 
52  1 toggle @AfterAll
53    static void tearDown() {
54    }
55   
 
56  12 toggle @BeforeEach
57    void testCaseInitialization() {
58    }
59   
 
60  12 toggle @AfterEach
61    void testCaseTermination() {
62    }
63    //</editor-fold>
64   
 
65  1 toggle @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   
 
73  1 toggle @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   
 
82  1 toggle @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   
 
90  1 toggle @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   
 
98  1 toggle @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   
 
111  1 toggle @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   
 
121  1 toggle @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   
 
137  1 toggle @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   
 
145  1 toggle @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   
 
154  1 toggle @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   
 
171  1 toggle @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   
 
201  1 toggle @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    }