You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2021/04/29 18:07:31 UTC

svn commit: r1889304 - in /pdfbox/trunk/tools/src: main/java/org/apache/pdfbox/tools/PDFBox.java test/java/org/apache/pdfbox/tools/PDFBoxHeadlessTest.java

Author: msahyoun
Date: Thu Apr 29 18:07:30 2021
New Revision: 1889304

URL: http://svn.apache.org/viewvc?rev=1889304&view=rev
Log:
PDFBOX-5181: enable PDFDebugger command only in a non headless environment

Added:
    pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/PDFBoxHeadlessTest.java
Modified:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java?rev=1889304&r1=1889303&r2=1889304&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java Thu Apr 29 18:07:30 2021
@@ -16,6 +16,8 @@
  */
 package org.apache.pdfbox.tools;
 
+import java.awt.GraphicsEnvironment;
+
 import org.apache.pdfbox.debugger.PDFDebugger;
 
 import picocli.CommandLine;
@@ -48,7 +50,10 @@ public final class PDFBox implements Run
         System.setProperty("apple.awt.UIElement", "true");
 
         CommandLine commandLine = new CommandLine(new PDFBox()).setSubcommandsCaseInsensitive(true);
-        commandLine.addSubcommand("debug", PDFDebugger.class);
+        if (!GraphicsEnvironment.isHeadless())
+        {
+            commandLine.addSubcommand("debug", PDFDebugger.class);
+        }
         commandLine.addSubcommand("decrypt", Decrypt.class);
         commandLine.addSubcommand("encrypt", Encrypt.class);
         commandLine.addSubcommand("decode", WriteDecodedDoc.class);

Added: pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/PDFBoxHeadlessTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/PDFBoxHeadlessTest.java?rev=1889304&view=auto
==============================================================================
--- pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/PDFBoxHeadlessTest.java (added)
+++ pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/PDFBoxHeadlessTest.java Thu Apr 29 18:07:30 2021
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.tools;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+import java.awt.GraphicsEnvironment;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for running the PDFBox CLI in a headless environment.
+ *
+ */
+class PDFBoxHeadlessTest
+{
+    final PrintStream originalOut = System.out;
+    final PrintStream originalErr = System.err;
+    final ByteArrayOutputStream out = new ByteArrayOutputStream();
+    final ByteArrayOutputStream err = new ByteArrayOutputStream();
+
+    @BeforeEach
+    public void setUpStreams() {
+        out.reset();
+        err.reset();
+        System.setOut(new PrintStream(out));
+        System.setErr(new PrintStream(err));
+    }
+
+    @AfterEach
+    public void restoreStreams() {
+        System.setOut(originalOut);
+        System.setErr(originalErr);
+    }
+   
+    @Test
+    void isHeadlessTest()
+    {
+        System.setProperty("java.awt.headless", "true");
+        assertTrue(GraphicsEnvironment.isHeadless());
+    }
+
+    @Test
+    void isNonHeadlessPDFBoxTest()
+    {
+        final String[] args = {"debug"};
+        assertDoesNotThrow(() -> {
+            PDFBox.main(args);
+        });
+    }
+
+    @Test
+    void isHeadlessPDFBoxTest()
+    {
+        System.setProperty("java.awt.headless", "true");
+        final String[] args = {"debug"};
+        assertDoesNotThrow(() -> {
+            PDFBox.main(args);
+        });
+        assertTrue(err.toString().contains("Unmatched argument at index 0: 'debug'"));
+    }
+}