PdfTests.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.github.jhonnymertz.wkhtmltopdf.wrapper.unit;
  2. import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;
  3. import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.FilenameFilterConfig;
  4. import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
  5. import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.XvfbConfig;
  6. import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.Cover;
  7. import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.Page;
  8. import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.TableOfContents;
  9. import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
  10. import org.junit.jupiter.api.AfterEach;
  11. import org.junit.jupiter.api.BeforeEach;
  12. import org.junit.jupiter.api.Test;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.Arrays;
  16. import static org.hamcrest.MatcherAssert.assertThat;
  17. import static org.hamcrest.core.StringContains.containsString;
  18. import static org.junit.jupiter.api.Assertions.assertEquals;
  19. import static org.junit.jupiter.api.Assertions.assertTrue;
  20. class PdfTests {
  21. private WrapperConfig wc;
  22. private Pdf pdf;
  23. @BeforeEach
  24. public void setUp() {
  25. wc = new WrapperConfig("wkhtmltopdf");
  26. pdf = new Pdf(wc);
  27. }
  28. @AfterEach
  29. public void cleanUp() {
  30. pdf.cleanAllTempFiles();
  31. }
  32. @Test
  33. void testParams() throws Exception {
  34. pdf.addParam(new Param("--enable-javascript"), new Param("--html-header", "file:///example.html"));
  35. pdf.addPageFromUrl("http://www.google.com");
  36. assertThat("command params should contain the --enable-javascript and --html-header", pdf.getCommand(), containsString("--enable-javascript --html-header file:///example.html"));
  37. }
  38. @Test
  39. void testUniqueTempFileGenerationDirectory() throws IOException {
  40. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
  41. pdf.getCommand();
  42. pdf.getCommand();
  43. final File dir = new File(System.getProperty("java.io.tmpdir"));
  44. File[] files = dir.listFiles(new FilenameFilterConfig());
  45. assertEquals(1, files.length);
  46. }
  47. @Test
  48. void testTempDirectoryCleanup() throws IOException {
  49. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
  50. pdf.getCommand();
  51. final File dir = new File(System.getProperty("java.io.tmpdir"));
  52. File[] files = dir.listFiles(new FilenameFilterConfig());
  53. assertEquals(1, files.length);
  54. pdf.cleanAllTempFiles();
  55. files = dir.listFiles(new FilenameFilterConfig());
  56. assertEquals(0, files.length);
  57. }
  58. @Test
  59. void testCustomTempDirectory() throws IOException {
  60. File f = File.createTempFile("java-wrapper-wkhtmltopdf-test", ".html");
  61. pdf.setTempDirectory(new File(f.getParent()));
  62. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
  63. assertThat("command params should contain custom temp directory", pdf.getCommand(), containsString(f.getParent()));
  64. }
  65. @Test
  66. void testMissingAssetsProperty() {
  67. pdf.addPageFromUrl("http://www.google.com");
  68. pdf.setAllowMissingAssets();
  69. assertTrue(pdf.getAllowMissingAssets());
  70. pdf.setSuccessValues(Arrays.asList(0, 1));
  71. assertTrue(pdf.getAllowMissingAssets());
  72. }
  73. @Test
  74. void testAddPages() throws IOException {
  75. pdf.addPageFromUrl("http://www.google.com");
  76. pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
  77. pdf.addPageFromFile("test.html");
  78. assertThat("command params should contain url input", pdf.getCommand(), containsString("http://www.google.com"));
  79. assertThat("command params should contain file input", pdf.getCommand(), containsString("test.html"));
  80. }
  81. @Test
  82. void testTocParams() throws IOException {
  83. pdf.addToc();
  84. pdf.addTocParam(new Param("--test-param"), new Param("--test-param2", "test-value"));
  85. pdf.addPageFromUrl("http://www.google.com");
  86. assertThat("command params should contain toc params", pdf.getCommand(), containsString("--test-param2 test-value"));
  87. }
  88. @Test
  89. void testTocParamsUsingTocObject() throws IOException {
  90. TableOfContents toc = pdf.addToc();
  91. toc.addParam(new Param("--test-param"), new Param("--test-param2", "test-value"));
  92. pdf.addPageFromUrl("http://www.google.com");
  93. assertThat("command params should contain toc params", pdf.getCommand(), containsString("--test-param2 test-value"));
  94. }
  95. @Test
  96. void testXvfbCommand() throws Exception {
  97. wc.setXvfbConfig(new XvfbConfig());
  98. pdf = new Pdf(wc);
  99. assertThat("command should contain xvfb-run config", pdf.getCommand(), containsString("xvfb-run"));
  100. }
  101. @Test
  102. void testTocAlwaysFirstByDefault() throws Exception {
  103. pdf.addPageFromUrl("http://www.google.com");
  104. pdf.addToc();
  105. pdf.addPageFromFile("test.html");
  106. assertThat("command params should contain toc before pages", pdf.getCommand(), containsString("wkhtmltopdf toc http://www.google.com test.html -"));
  107. }
  108. @Test
  109. void testTocCustomLocation() throws Exception {
  110. wc.setAlwaysPutTocFirst(false);
  111. pdf.addPageFromUrl("http://www.google.com");
  112. pdf.addToc();
  113. pdf.addPageFromFile("test.html");
  114. assertThat("command params should contain toc after url page and before file page", pdf.getCommand(), containsString("wkhtmltopdf http://www.google.com toc test.html -"));
  115. }
  116. @Test
  117. void testPageParams() throws IOException {
  118. Page page1 = pdf.addPageFromUrl("http://www.google.com");
  119. page1.addParam(new Param("--exclude-from-outline"));
  120. Page page2 = pdf.addPageFromFile("test.html");
  121. page2.addParam( new Param("--zoom", "2"));
  122. assertThat("command url page should contain page specific params", pdf.getCommand(), containsString("http://www.google.com --exclude-from-outline"));
  123. assertThat("command file page should contain page specific params", pdf.getCommand(), containsString("test.html --zoom"));
  124. }
  125. @Test
  126. void testCoverParams() throws IOException {
  127. Cover cover = pdf.addCoverFromFile("cover.html");
  128. cover.addParam(new Param("--test-param"), new Param("--test-param2", "test-value"));
  129. pdf.addPageFromUrl("http://www.google.com");
  130. assertThat("command params should contain cover params", pdf.getCommand(), containsString("cover cover.html --test-param --test-param2 test-value"));
  131. }
  132. @Test
  133. void testMultipleObjects() throws IOException {
  134. wc.setAlwaysPutTocFirst(false);
  135. pdf.addCoverFromFile("cover.html");
  136. pdf.addPageFromFile("foreword.html");
  137. pdf.addToc();
  138. pdf.addPageFromUrl("http://www.google.com");
  139. pdf.addPageFromFile("test.html");
  140. assertThat("command should match the order objects are added", pdf.getCommand(), containsString("wkhtmltopdf cover cover.html foreword.html toc http://www.google.com test.html -"));
  141. }
  142. }