فهرست منبع

Merge branch 'master' into fixing_bug

Jhonny Mertz 5 سال پیش
والد
کامیت
52305a7cff

+ 7 - 0
.deepsource.toml

@@ -0,0 +1,7 @@
+version = 1
+
+test_patterns = ["src/test/**"]
+
+[[analyzers]]
+name = "test-coverage"
+enabled = true

+ 12 - 0
.github/PULL_REQUEST_TEMPLATE.md

@@ -0,0 +1,12 @@
+## ChangeLog
+
+* Change 1
+* Change 2
+* ...
+
+## How to test
+
+* See _this_
+* Do _that_
+* Expect _that_
+* ...

+ 7 - 1
pom.xml

@@ -3,7 +3,7 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.github.jhonnymertz</groupId>
     <artifactId>java-wkhtmltopdf-wrapper</artifactId>
-    <version>1.1.11-RELEASE</version>
+    <version>1.1.12-RELEASE</version>
     <packaging>jar</packaging>
 
     <name>Java WkHtmlToPdf Wrapper</name>
@@ -85,6 +85,12 @@
             <scope>test</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>3.3.3</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

+ 5 - 0
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java

@@ -58,6 +58,11 @@ public class Pdf {
 
     private List<Integer> successValues = new ArrayList<Integer>(Arrays.asList(0));
 
+    @Deprecated
+    /**
+     * Default constructor
+     * @deprecated Use the constructor with the WrapperConfig definition
+     */
     public Pdf() {
         this(new WrapperConfig());
     }

+ 18 - 11
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/configurations/WrapperConfig.java

@@ -1,13 +1,14 @@
 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 com.github.jhonnymertz.wkhtmltopdf.wrapper.exceptions.WrapperConfigException;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
+import java.nio.charset.Charset;
+
 /**
  * Provides wrapper configuration such as the xvfb configuration and wkhtmltopdf command to be used
  */
@@ -27,7 +28,10 @@ public class WrapperConfig {
 
     /**
      * Initialize the configuration based on searching for wkhtmltopdf command to be used into the SO's path
+     *
+     * @deprecated Use the constructor specifying the location of wkhtmltopdf. Use the static method findExecutable() if necessary.
      */
+    @Deprecated
     public WrapperConfig() {
         logger.debug("Initialized with default configurations.");
         setWkhtmltopdfCommand(findExecutable());
@@ -35,6 +39,7 @@ public class WrapperConfig {
 
     /**
      * Initialize the configuration based on a provided wkhtmltopdf command to be used
+     *
      * @param wkhtmltopdfCommand the wkhtmltopdf command
      */
     public WrapperConfig(String wkhtmltopdfCommand) {
@@ -44,6 +49,7 @@ public class WrapperConfig {
     /**
      * Gets the wkhtmltopdf command to be used while calling wkhtmltopdf
      * It's default is 'wkhtmltopdf'
+     *
      * @return the wkhtmltopdf command
      */
     public String getWkhtmltopdfCommand() {
@@ -52,6 +58,7 @@ public class WrapperConfig {
 
     /**
      * Sets the configuration based on a provided wkhtmltopdf command to be used
+     *
      * @param wkhtmltopdfCommand the wkhtmltopdf command
      */
     public void setWkhtmltopdfCommand(String wkhtmltopdfCommand) {
@@ -63,7 +70,7 @@ public class WrapperConfig {
      *
      * @return the wkhtmltopdf command according to the OS
      */
-    public String findExecutable() {
+    public static String findExecutable() {
         try {
             String osname = System.getProperty("os.name").toLowerCase();
 
@@ -79,18 +86,16 @@ public class WrapperConfig {
                         "Verify its installation or initialize wrapper configurations with correct path/to/wkhtmltopdf");
 
             logger.debug("Wkhtmltopdf command found in classpath: {}", text);
-            setWkhtmltopdfCommand(text);
-        } catch (InterruptedException e) {
-            logger.error("Fatal:",e);
-        } catch (IOException e) {
-            logger.error("Fatal:",e);
+            return text;
+        } catch (InterruptedException | IOException e) {
+            logger.error("Fatal:", e);
+            throw new WrapperConfigException("Failed while getting wkhtmltopdf executable.", e);
         }
-
-        return getWkhtmltopdfCommand();
     }
 
     /**
      * Verify whether the Xvfb support is enabled and configured
+     *
      * @return status of Xvfb configuration
      */
     public boolean isXvfbEnabled() {
@@ -99,6 +104,7 @@ public class WrapperConfig {
 
     /**
      * Gets the Xvfb configuration
+     *
      * @return the Xvfb configuration
      */
     public XvfbConfig getXvfbConfig() {
@@ -107,6 +113,7 @@ public class WrapperConfig {
 
     /**
      * Sets the Xvfb configuration
+     *
      * @param xvfbConfig the Xvfb configuration
      */
     public void setXvfbConfig(XvfbConfig xvfbConfig) {

+ 9 - 0
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/exceptions/WrapperConfigException.java

@@ -0,0 +1,9 @@
+package com.github.jhonnymertz.wkhtmltopdf.wrapper.exceptions;
+
+public class WrapperConfigException extends RuntimeException {
+
+    public WrapperConfigException(String s, Throwable t) {
+        super(s, t);
+    }
+
+}

+ 0 - 27
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/params/Param.java

@@ -25,37 +25,10 @@ public class Param {
         return key;
     }
 
-    public void setKey(String key) {
-        this.key = key;
-    }
-
-    // This is kept for backwards compatibility it will
-    // only return the first arg if it exists
-    @Deprecated
-    public String getValue() {
-        if (values.size() > 0) {
-            return values.get(0);
-        }
-        return null;
-    }
-
     public List<String> getValues() {
         return values;
     }
 
-    @Deprecated
-    public void setValue(String value) {
-        if (values.isEmpty()) {
-            values.add(value);
-        } else {
-            values.set(0, value);
-        }
-    }
-
-    public void setValues(List<String> values) {
-        this.values = values;
-    }
-
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder().append(Symbol.separator)

+ 75 - 0
src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/unit/PdfTests.java

@@ -0,0 +1,75 @@
+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.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.hamcrest.core.StringContains.containsString;
+
+public class PdfTests {
+
+    private WrapperConfig wc;
+    private Pdf pdf;
+
+    @Before
+    public void setUp() {
+        wc = new WrapperConfig("wkhtmltopdf");
+        pdf = new Pdf(wc);
+    }
+
+    @Test
+    public void testParams() throws Exception {
+        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 testTempDirectory() throws IOException {
+        File f = File.createTempFile("java-wrapper-wkhtmltopdf-test", ".html");
+        pdf.setTempDirectory(new File(f.getParent()));
+        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
+        Assert.assertThat("command params should contain custom temp directory", pdf.getCommand(), containsString(f.getParent()));
+    }
+
+    @Test
+    public void testMissingAssetsProperty() {
+        pdf.addPageFromUrl("http://www.google.com");
+        pdf.setAllowMissingAssets();
+        Assert.assertTrue(pdf.getAllowMissingAssets());
+        pdf.setSuccessValues(Arrays.asList(0, 1));
+        Assert.assertTrue(pdf.getAllowMissingAssets());
+    }
+
+    @Test
+    public void testAddPages() throws IOException {
+        pdf.addPageFromUrl("http://www.google.com");
+        pdf.addPageFromString("<html><head><meta charset=\"utf-8\"></head><h1>Müller</h1></html>");
+        pdf.addPageFromFile("test.html");
+        Assert.assertThat("command params should contain url input", pdf.getCommand(), containsString("http://www.google.com"));
+        Assert.assertThat("command params should contain file input", pdf.getCommand(), containsString("test.html"));
+    }
+
+    @Test
+    public void testTocParams() throws IOException {
+        pdf.addToc();
+        pdf.addTocParam(new Param("--test-param"), new Param("--test-param2", "test-value"));
+        pdf.addPageFromUrl("http://www.google.com");
+        Assert.assertThat("command params should contain toc params", pdf.getCommand(), containsString("--test-param2 test-value"));
+    }
+
+    @Test
+    public void testXvfbCommand() throws Exception {
+        wc.setXvfbConfig(new XvfbConfig());
+        pdf = new Pdf(wc);
+        Assert.assertThat("command should contain xvfb-run config", pdf.getCommand(), containsString("xvfb-run"));
+    }
+}

+ 18 - 11
src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/unit/CommandTests.java → src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/unit/XvfbConfigTests.java

@@ -5,27 +5,34 @@ 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.Before;
 import org.junit.Test;
 
 import static org.hamcrest.core.StringContains.containsString;
 
-public class CommandTests {
+public class XvfbConfigTests {
 
-    @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"));
+    private WrapperConfig wc;
+
+    @Before
+    public void setUp() {
+        wc = new WrapperConfig("wkhtmltopdf");
     }
 
     @Test
-    public void testXvfbCommand() throws Exception {
-        WrapperConfig wc = new WrapperConfig("wkhtmltopdf");
+    public void testXvfbEnable() throws Exception {
         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 testXvfbParams() throws Exception {
+        XvfbConfig xvfbConfig = new XvfbConfig();
+        xvfbConfig.addParams(new Param("--test-param"), new Param("--test-param2", "test-value"));
+        wc.setXvfbConfig(xvfbConfig);
+        Pdf pdf = new Pdf(wc);
+        pdf.addPageFromUrl("http://www.google.com");
+        Assert.assertThat("command params should contain xvfb params", pdf.getCommand(), containsString("xvfb-run --test-param --test-param2 test-value wkhtmltopdf http://www.google.com -"));
+    }
 }