1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File OpenPdfRendererTest.java

 

Code metrics

0
45
13
1
163
128
13
0.29
3.46
13
1

Classes

Class Line # Actions
OpenPdfRendererTest 30 45 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 9 tests. .

Source view

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"})
 
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    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
43  1 toggle @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   
 
50  1 toggle @AfterAll
51    static void tearDown() {
52    }
53   
 
54  9 toggle @BeforeEach
55    void testCaseInitialization() {
56    }
57   
 
58  9 toggle @AfterEach
59    void testCaseTermination() {
60    }
61    //</editor-fold>
62   
 
63  1 toggle @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   
 
71  1 toggle @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   
 
80  1 toggle @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   
 
88  1 toggle @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   
 
101  1 toggle @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   
 
111  1 toggle @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   
 
127  1 toggle @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   
 
135  1 toggle @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   
 
146  1 toggle @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    }