Răsfoiți Sursa

Commenting some code, renaming and setting integration tests

Jhonny Mertz 6 ani în urmă
părinte
comite
170e5c17b6

+ 67 - 15
pom.xml

@@ -3,7 +3,7 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.github.jhonnymertz</groupId>
     <artifactId>java-wkhtmltopdf-wrapper</artifactId>
-    <version>1.1.5-RELEASE</version>
+    <version>1.1.6-RELEASE</version>
 
     <licenses>
         <license>
@@ -15,20 +15,6 @@
 
     <dependencies>
 
-        <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-            <version>4.12</version>
-            <scope>test</scope>
-        </dependency>
-
-        <dependency>
-            <groupId>org.apache.pdfbox</groupId>
-            <artifactId>pdfbox</artifactId>
-            <version>1.8.16</version>
-            <scope>test</scope>
-        </dependency>
-
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
@@ -54,6 +40,72 @@
             <scope>test</scope>
         </dependency>
 
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.pdfbox</groupId>
+            <artifactId>pdfbox</artifactId>
+            <version>1.8.16</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
+    <build>
+        <plugins>
+            <plugin>
+                <!-- Separates the unit tests from the integration tests. -->
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <!-- Skip the default running of this plug-in (or everything is run twice...see below) -->
+                    <skip>true</skip>
+                    <!-- Show 100% of the lines from the stack trace (doesn't work) -->
+                    <trimStackTrace>false</trimStackTrace>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>unit-tests</id>
+                        <phase>test</phase>
+                        <goals>
+                            <goal>test</goal>
+                        </goals>
+                        <configuration>
+                            <!-- Never skip running the tests when the test phase is invoked -->
+                            <skip>false</skip>
+                            <includes>
+                                <!-- Include unit tests within integration-test phase. -->
+                                <include>**/*Tests.java</include>
+                            </includes>
+                            <excludes>
+                                <!-- Exclude integration tests within (unit) test phase. -->
+                                <exclude>**/*IntegrationTests.java</exclude>
+                            </excludes>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>integration-tests</id>
+                        <phase>integration-test</phase>
+                        <goals>
+                            <goal>test</goal>
+                        </goals>
+                        <configuration>
+                            <!-- Never skip running the tests when the integration-test phase is invoked -->
+                            <skip>false</skip>
+                            <includes>
+                                <!-- Include integration tests within integration-test phase. -->
+                                <include>**/*IntegrationTests.java</include>
+                            </includes>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
 </project>

+ 20 - 3
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java

@@ -1,6 +1,7 @@
 package com.github.jhonnymertz.wkhtmltopdf.wrapper;
 
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
+import com.github.jhonnymertz.wkhtmltopdf.wrapper.exceptions.PDFExportException;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.page.Page;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.page.PageType;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
@@ -45,6 +46,9 @@ public class Pdf {
 
     private boolean hasToc = false;
 
+    /**
+     * Timeout to wait while generating a PDF, in seconds
+     */
     private int timeout = 10;
 
     private File tempDirectory;
@@ -104,10 +108,18 @@ public class Pdf {
         this.tocParams.add(param, params);
     }
 
+    /**
+     * Sets the timeout to wait while generating a PDF, in seconds
+     */
     public void setTimeout(int timeout) {
         this.timeout = timeout;
     }
 
+    /**
+     * Sets the temporary folder to store files during the process
+     * Default is provided by #File.createTempFile()
+     * @param tempDirectory
+     */
     public void setTempDirectory(File tempDirectory) {
         this.tempDirectory = tempDirectory;
     }
@@ -168,18 +180,18 @@ public class Pdf {
 
         for (Page page : pages) {
             if (page.getType().equals(PageType.htmlAsString)) {
-
+                //htmlAsString pages are first store into a temp file, then the location is passed as parameter to
+                // wkhtmltopdf, this is a workaround to avoid huge commands
                 File temp = File.createTempFile("java-wkhtmltopdf-wrapper" + UUID.randomUUID().toString(), ".html", tempDirectory);
                 FileUtils.writeStringToFile(temp, page.getSource(), "UTF-8");
                 page.setFilePath(temp.getAbsolutePath());
                 commandLine.add(temp.getAbsolutePath());
             } else {
-                //Add source
                 commandLine.add(page.getSource());
             }
         }
         commandLine.add(STDINOUT);
-        logger.debug(commandLine.toString());
+        logger.debug("Command generated: {}", commandLine.toString());
         return commandLine.toArray(new String[commandLine.size()]);
     }
 
@@ -213,6 +225,11 @@ public class Pdf {
         }
     }
 
+    /**
+     * Gets the final wkhtmltopdf command as string
+     * @return the generated command from params
+     * @throws IOException
+     */
     public String getCommand() throws IOException {
         return StringUtils.join(getCommandAsArray(), " ");
     }

+ 40 - 1
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/configurations/WrapperConfig.java

@@ -2,31 +2,58 @@ package com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations;
 
 import java.io.IOException;
 import java.nio.charset.Charset;
+
+import com.github.jhonnymertz.wkhtmltopdf.wrapper.exceptions.WkhtmltopdfConfigurationException;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * Provides wrapper configuration such as the xvfb configuration and wkhtmltopdf command to be used
+ */
 public class WrapperConfig {
 
     private static final Logger logger = LoggerFactory.getLogger(WrapperConfig.class);
 
+    /**
+     * the Xvfb configuration
+     */
     private XvfbConfig xvfbConfig;
 
+    /**
+     * Default wkhtmltopdf command to be used
+     */
     private String wkhtmltopdfCommand = "wkhtmltopdf";
 
+    /**
+     * Initialize the configuration based on searching for wkhtmltopdf command to be used into the SO's path
+     */
     public WrapperConfig() {
         logger.debug("Initialized with default configurations.");
         setWkhtmltopdfCommand(findExecutable());
     }
 
+    /**
+     * Initialize the configuration based on a provided wkhtmltopdf command to be used
+     * @param wkhtmltopdfCommand the wkhtmltopdf command
+     */
     public WrapperConfig(String wkhtmltopdfCommand) {
         setWkhtmltopdfCommand(wkhtmltopdfCommand);
     }
 
+    /**
+     * Gets the wkhtmltopdf command to be used while calling wkhtmltopdf
+     * It's default is 'wkhtmltopdf'
+     * @return the wkhtmltopdf command
+     */
     public String getWkhtmltopdfCommand() {
         return wkhtmltopdfCommand;
     }
 
+    /**
+     * Sets the configuration based on a provided wkhtmltopdf command to be used
+     * @param wkhtmltopdfCommand the wkhtmltopdf command
+     */
     public void setWkhtmltopdfCommand(String wkhtmltopdfCommand) {
         this.wkhtmltopdfCommand = wkhtmltopdfCommand;
     }
@@ -48,7 +75,7 @@ public class WrapperConfig {
             String text = IOUtils.toString(p.getInputStream(), Charset.defaultCharset()).trim();
 
             if (text.isEmpty())
-                throw new RuntimeException("wkhtmltopdf command was not found in your classpath. " +
+                throw new WkhtmltopdfConfigurationException("wkhtmltopdf command was not found in your classpath. " +
                         "Verify its installation or initialize wrapper configurations with correct path/to/wkhtmltopdf");
 
             logger.debug("Wkhtmltopdf command found in classpath: {}", text);
@@ -62,14 +89,26 @@ public class WrapperConfig {
         return getWkhtmltopdfCommand();
     }
 
+    /**
+     * Verify whether the Xvfb support is enabled and configured
+     * @return status of Xvfb configuration
+     */
     public boolean isXvfbEnabled() {
         return xvfbConfig != null;
     }
 
+    /**
+     * Gets the Xvfb configuration
+     * @return the Xvfb configuration
+     */
     public XvfbConfig getXvfbConfig() {
         return xvfbConfig;
     }
 
+    /**
+     * Sets the Xvfb configuration
+     * @param xvfbConfig the Xvfb configuration
+     */
     public void setXvfbConfig(XvfbConfig xvfbConfig) {
         this.xvfbConfig = xvfbConfig;
     }

+ 3 - 0
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/configurations/XvfbConfig.java

@@ -6,6 +6,9 @@ import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Params;
 import java.util.ArrayList;
 import java.util.List;
 
+/**
+ * Wrapper for Xvfb parameters and configuration
+ */
 public class XvfbConfig {
 
     private String command;

+ 2 - 5
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/PDFExportException.java → src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/exceptions/PDFExportException.java

@@ -1,10 +1,7 @@
-package com.github.jhonnymertz.wkhtmltopdf.wrapper;
+package com.github.jhonnymertz.wkhtmltopdf.wrapper.exceptions;
 
 /**
- * PDFExportError.
- *
- * @author evgeni.gordeev
- * @version 1.1.3
+ * Exception to describe and track pdf exporting errors
  */
 public class PDFExportException extends RuntimeException {
 

+ 13 - 0
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/exceptions/WkhtmltopdfConfigurationException.java

@@ -0,0 +1,13 @@
+package com.github.jhonnymertz.wkhtmltopdf.wrapper.exceptions;
+
+
+/**
+ * Exception to describe and track wrapper configuration errors
+ */
+public class WkhtmltopdfConfigurationException extends RuntimeException  {
+
+    public WkhtmltopdfConfigurationException(String s) {
+        super(s);
+    }
+
+}

+ 0 - 26
src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/CleanUpTempFilesTest.java

@@ -1,26 +0,0 @@
-/*
- *  Copyright (C) 2018 kiz, University Ulm
- */
-package com.github.jhonnymertz.wkhtmltopdf.wrapper;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- *
- * @author kdenzel
- */
-public class CleanUpTempFilesTest {
-    
-    @Test
-    public void test(){
-        Pdf pdf = new Pdf();
-        pdf.addPageFromString("<!DOCTYPE html><head><title>title</title></head><body><p>TEST</p></body>");
-        try {
-            byte[] pdfAsByteArray = pdf.getPDF();
-        } catch(Exception ex){
-            Assert.fail(ex.getMessage());
-        }
-    }
-    
-}

+ 0 - 56
src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/XvfbTest.java

@@ -1,56 +0,0 @@
-package com.github.jhonnymertz.wkhtmltopdf.wrapper;
-
-import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
-import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.XvfbConfig;
-import com.github.jhonnymertz.wkhtmltopdf.wrapper.page.PageType;
-import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
-import org.apache.pdfbox.pdfparser.PDFParser;
-import org.apache.pdfbox.pdmodel.PDDocument;
-import org.apache.pdfbox.util.PDFTextStripper;
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.io.ByteArrayInputStream;
-
-import static org.hamcrest.core.StringContains.containsString;
-
-public class XvfbTest {
-
-    @Test
-    public void testXvfbCommand() throws Exception {
-        WrapperConfig wc = new WrapperConfig();
-        wc.setXvfbConfig(new XvfbConfig());
-        Pdf pdf = new Pdf(wc);
-        Assert.assertThat("command should contain xvfb-run config", pdf.getCommand(), containsString("xvfb-run"));
-    }
-
-    @Test
-    public void testPdfWithXvfb() throws Exception {
-        //Dunno i don't need this test, so for
-        WrapperConfig wc = null;
-        if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
-            XvfbConfig xc = new XvfbConfig();
-            xc.addParams(new Param("--auto-servernum"), new Param("--server-num=1"));
-
-            wc = new WrapperConfig();
-            wc.setXvfbConfig(xc);
-        }
-        Pdf pdf = wc != null ? new Pdf(wc) : new Pdf();
-        pdf.addPageFromUrl("http://www.google.com");
-
-        pdf.saveAs("output.pdf");
-
-        // WHEN
-        byte[] pdfBytes = pdf.getPDF();
-
-        PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfBytes));
-
-        // that is a valid PDF (otherwise an IOException occurs)
-        parser.parse();
-        PDFTextStripper pdfTextStripper = new PDFTextStripper();
-        String pdfText = pdfTextStripper.getText(new PDDocument(parser.getDocument()));
-
-        Assert.assertThat("document should be generated", pdfText, containsString("Google"));
-    }
-
-}

+ 44 - 12
src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/PdfTest.java → src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/integration/PdfIntegrationTests.java

@@ -1,8 +1,9 @@
-package com.github.jhonnymertz.wkhtmltopdf.wrapper;
+package com.github.jhonnymertz.wkhtmltopdf.wrapper.integration;
 
+import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
+import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.XvfbConfig;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
-import java.io.IOException;
 import org.apache.pdfbox.pdfparser.PDFParser;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.util.PDFTextStripper;
@@ -11,19 +12,11 @@ import org.junit.Test;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
 
 import static org.hamcrest.core.StringContains.containsString;
 
-public class PdfTest {
-
-    @Test
-    public void testCommand() throws Exception {
-        Pdf pdf = new Pdf();
-        pdf.addToc();
-        pdf.addParam(new Param("--enable-javascript"), new Param("--html-header", "file:///example.html"));
-        pdf.addPageFromUrl("http://www.google.com");
-        Assert.assertThat("command params should contain the --enable-javascript and --html-header", pdf.getCommand(), containsString("--enable-javascript --html-header file:///example.html"));
-    }
+public class PdfIntegrationTests {
 
     @Test
     public void findExecutable() throws Exception {
@@ -106,4 +99,43 @@ public class PdfTest {
         pdDocument.close();
         return text;
     }
+
+    @Test
+    public void CleanUpTempFilesTest(){
+        Pdf pdf = new Pdf();
+        pdf.addPageFromString("<!DOCTYPE html><head><title>title</title></head><body><p>TEST</p></body>");
+        try {
+            pdf.getPDF();
+        } catch(Exception ex){
+            Assert.fail(ex.getMessage());
+        }
+    }
+
+    @Test
+    public void testPdfWithXvfb() throws Exception {
+        WrapperConfig wc = null;
+        if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
+            XvfbConfig xc = new XvfbConfig();
+            xc.addParams(new Param("--auto-servernum"), new Param("--server-num=1"));
+
+            wc = new WrapperConfig();
+            wc.setXvfbConfig(xc);
+        }
+        Pdf pdf = wc != null ? new Pdf(wc) : new Pdf();
+        pdf.addPageFromUrl("http://www.google.com");
+
+        pdf.saveAs("output.pdf");
+
+        // WHEN
+        byte[] pdfBytes = pdf.getPDF();
+
+        PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfBytes));
+
+        // that is a valid PDF (otherwise an IOException occurs)
+        parser.parse();
+        PDFTextStripper pdfTextStripper = new PDFTextStripper();
+        String pdfText = pdfTextStripper.getText(new PDDocument(parser.getDocument()));
+
+        Assert.assertThat("document should be generated", pdfText, containsString("Google"));
+    }
 }

+ 31 - 0
src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/unit/CommandTests.java

@@ -0,0 +1,31 @@
+package com.github.jhonnymertz.wkhtmltopdf.wrapper.unit;
+
+import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;
+import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
+import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.XvfbConfig;
+import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.hamcrest.core.StringContains.containsString;
+
+public class CommandTests {
+
+    @Test
+    public void testCommand() throws Exception {
+        WrapperConfig wc = new WrapperConfig("wkhtmltopdf");
+        Pdf pdf = new Pdf(wc);
+        pdf.addToc();
+        pdf.addParam(new Param("--enable-javascript"), new Param("--html-header", "file:///example.html"));
+        pdf.addPageFromUrl("http://www.google.com");
+        Assert.assertThat("command params should contain the --enable-javascript and --html-header", pdf.getCommand(), containsString("--enable-javascript --html-header file:///example.html"));
+    }
+
+    @Test
+    public void testXvfbCommand() throws Exception {
+        WrapperConfig wc = new WrapperConfig("wkhtmltopdf");
+        wc.setXvfbConfig(new XvfbConfig());
+        Pdf pdf = new Pdf(wc);
+        Assert.assertThat("command should contain xvfb-run config", pdf.getCommand(), containsString("xvfb-run"));
+    }
+}