pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1791387 - in /pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox: pdfparser/TestPDFParser.java text/TestTextStripper.java
Date Fri, 14 Apr 2017 15:54:51 GMT
Author: tilman
Date: Fri Apr 14 15:54:51 2017
New Revision: 1791387

URL: http://svn.apache.org/viewvc?rev=1791387&view=rev
Log:
PDFBOX-2852: optimize, use JDK7 syntax

Modified:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java?rev=1791387&r1=1791386&r2=1791387&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java Fri Apr
14 15:54:51 2017
@@ -28,6 +28,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
+import java.net.URISyntaxException;
 
 import org.apache.pdfbox.cos.COSDocument;
 import org.apache.pdfbox.io.MemoryUsageSetting;
@@ -108,10 +109,10 @@ public class TestPDFParser
     }
     
     @Test
-    public void testPDFParserMissingCatalog() throws IOException
+    public void testPDFParserMissingCatalog() throws IOException, URISyntaxException
     {
         // PDFBOX-3060
-        PDDocument.load(TestPDFParser.class.getResourceAsStream("MissingCatalog.pdf")).close();
       
+        PDDocument.load(new File(TestPDFParser.class.getResource("MissingCatalog.pdf").toURI())).close();
     }
 
     private void executeParserTest(RandomAccessRead source, MemoryUsageSetting memUsageSetting)
throws IOException
@@ -119,9 +120,10 @@ public class TestPDFParser
         ScratchFile scratchFile = new ScratchFile(memUsageSetting);
         PDFParser pdfParser = new PDFParser(source, scratchFile);
         pdfParser.parse();
-        COSDocument doc = pdfParser.getDocument();
-        assertNotNull(doc);
-        doc.close();
+        try (COSDocument doc = pdfParser.getDocument())
+        {
+            assertNotNull(doc);
+        }
         source.close();
         // number tmp file must be the same
         assertEquals(numberOfTmpFiles, getNumberOfTempFile());

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java?rev=1791387&r1=1791386&r2=1791387&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java Fri Apr
14 15:54:51 2017
@@ -34,6 +34,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.Writer;
+import java.net.URISyntaxException;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -244,9 +245,8 @@ public class TestTextStripper extends Te
         }
 
         //System.out.println("  " + inFile + (bSort ? " (sorted)" : ""));
-        PDDocument document = PDDocument.load(inFile);
-        try
-        {            
+        try (PDDocument document = PDDocument.load(inFile))
+        {
             File outFile;
             File diffFile;
             File expectedFile;
@@ -267,30 +267,20 @@ public class TestTextStripper extends Te
             // delete possible leftover
             diffFile.delete();
 
-            OutputStream os = new FileOutputStream(outFile);
-            try
+            try (OutputStream os = new FileOutputStream(outFile))
             {
                 os.write (0xEF);
                 os.write (0xBB);
                 os.write (0xBF);
 
-                Writer writer = new BufferedWriter(new OutputStreamWriter(os, ENCODING));
-                try
+                try (Writer writer = new BufferedWriter(new OutputStreamWriter(os, ENCODING)))
                 {
                     //Allows for sorted tests 
                     stripper.setSortByPosition(bSort);
                     stripper.writeText(document, writer);
-                }
-                finally
-                {
                     // close the written file before reading it again
-                    writer.close();
                 }
             }
-            finally
-            {
-                os.close();
-            }
 
             if (bLogResult)
             {
@@ -308,45 +298,44 @@ public class TestTextStripper extends Te
             
             boolean localFail = false;
 
-            LineNumberReader expectedReader =
-                new LineNumberReader(new InputStreamReader(new FileInputStream(expectedFile),
ENCODING));
-            LineNumberReader actualReader =
-                new LineNumberReader(new InputStreamReader(new FileInputStream(outFile),
ENCODING));
-
-            while (true)
-            {
-                String expectedLine = expectedReader.readLine();
-                while( expectedLine != null && expectedLine.trim().length() == 0
)
-                {
-                    expectedLine = expectedReader.readLine();
-                }
-                String actualLine = actualReader.readLine();
-                while( actualLine != null && actualLine.trim().length() == 0 )
-                {
-                    actualLine = actualReader.readLine();
-                }
-                if (!stringsEqual(expectedLine, actualLine))
-                {
-                    this.bFail = true;
-                    localFail = true;
-                    log.error("FAILURE: Line mismatch for file " + inFile.getName() +
-                            " (sort = "+bSort+")" +
-                            " at expected line: " + expectedReader.getLineNumber() +
-                            " at actual line: " + actualReader.getLineNumber() +
-                            "\nexpected line was: \"" + expectedLine + "\"" +
-                            "\nactual line was:   \"" + actualLine + "\"" + "\n");
-
-                    //lets report all lines, even though this might produce some verbose
logging
-                    //break;
-                }
-
-                if( expectedLine == null || actualLine==null)
+            try (LineNumberReader expectedReader = 
+                    new LineNumberReader(new InputStreamReader(new FileInputStream(expectedFile),
ENCODING));
+                 LineNumberReader actualReader = 
+                    new LineNumberReader(new InputStreamReader(new FileInputStream(outFile),
ENCODING)))
+            {    
+                while (true)
                 {
-                    break;
+                    String expectedLine = expectedReader.readLine();
+                    while( expectedLine != null && expectedLine.trim().length() ==
0 )
+                    {
+                        expectedLine = expectedReader.readLine();
+                    }
+                    String actualLine = actualReader.readLine();
+                    while( actualLine != null && actualLine.trim().length() == 0
)
+                    {
+                        actualLine = actualReader.readLine();
+                    }
+                    if (!stringsEqual(expectedLine, actualLine))
+                    {
+                        this.bFail = true;
+                        localFail = true;
+                        log.error("FAILURE: Line mismatch for file " + inFile.getName() +
+                                " (sort = "+bSort+")" +
+                                " at expected line: " + expectedReader.getLineNumber() +
+                                " at actual line: " + actualReader.getLineNumber() +
+                                "\nexpected line was: \"" + expectedLine + "\"" +
+                                "\nactual line was:   \"" + actualLine + "\"" + "\n");
+                        
+                        //lets report all lines, even though this might produce some verbose
logging
+                        //break;
+                    }
+                    
+                    if( expectedLine == null || actualLine==null)
+                    {
+                        break;
+                    }
                 }
             }
-            expectedReader.close();
-            actualReader.close();
             if (!localFail)
             {
                 outFile.delete();
@@ -360,57 +349,55 @@ public class TestTextStripper extends Te
                 // Compute diff. Get the Patch object. Patch is the container for computed
deltas.
                 Patch patch = DiffUtils.diff(original, revised);
 
-                PrintStream diffPS = new PrintStream(diffFile, ENCODING);
-                for (Object delta : patch.getDeltas())
+                try (PrintStream diffPS = new PrintStream(diffFile, ENCODING))
                 {
-                    if (delta instanceof ChangeDelta)
-                    {
-                        ChangeDelta cdelta = (ChangeDelta) delta;
-                        diffPS.println("Org: " + cdelta.getOriginal());
-                        diffPS.println("New: " + cdelta.getRevised());
-                        diffPS.println();
-                    }
-                    else if (delta instanceof DeleteDelta)
+                    for (Object delta : patch.getDeltas())
                     {
-                        DeleteDelta ddelta = (DeleteDelta) delta;
-                        diffPS.println("Org: " + ddelta.getOriginal());
-                        diffPS.println("New: " + ddelta.getRevised());
-                        diffPS.println();
-                    }
-                    else if (delta instanceof InsertDelta)
-                    {
-                        InsertDelta idelta = (InsertDelta) delta;
-                        diffPS.println("Org: " + idelta.getOriginal());
-                        diffPS.println("New: " + idelta.getRevised());
-                        diffPS.println();
-                    }
-                    else
-                    {
-                        diffPS.println(delta);
+                        if (delta instanceof ChangeDelta)
+                        {
+                            ChangeDelta cdelta = (ChangeDelta) delta;
+                            diffPS.println("Org: " + cdelta.getOriginal());
+                            diffPS.println("New: " + cdelta.getRevised());
+                            diffPS.println();
+                        }
+                        else if (delta instanceof DeleteDelta)
+                        {
+                            DeleteDelta ddelta = (DeleteDelta) delta;
+                            diffPS.println("Org: " + ddelta.getOriginal());
+                            diffPS.println("New: " + ddelta.getRevised());
+                            diffPS.println();
+                        }
+                        else if (delta instanceof InsertDelta)
+                        {
+                            InsertDelta idelta = (InsertDelta) delta;
+                            diffPS.println("Org: " + idelta.getOriginal());
+                            diffPS.println("New: " + idelta.getRevised());
+                            diffPS.println();
+                        }
+                        else
+                        {
+                            diffPS.println(delta);
+                        }
                     }
                 }
-                diffPS.close();
             }
         }
-        finally
-        {
-            document.close();
-        }
     }
     
     // Helper method for get the file content
     private static List<String> fileToLines(File file)
     {
         List<String> lines = new LinkedList<>();
-        String line = "";
+        String line;
         try
         {
-            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),
ENCODING));
-            while ((line = in.readLine()) != null)
+            try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),
ENCODING)))
             {
-                lines.add(line);
+                while ((line = in.readLine()) != null)
+                {
+                    lines.add(line);
+                }
             }
-            in.close();
         }
         catch (IOException e)
         {
@@ -441,10 +428,11 @@ public class TestTextStripper extends Te
      * must be empty.
      *
      * @throws IOException
+     * @throws URISyntaxException
      */
-    public void testStripByOutlineItems() throws IOException
+    public void testStripByOutlineItems() throws IOException, URISyntaxException
     {
-        PDDocument doc = PDDocument.load(TestPDPageTree.class.getResourceAsStream("with_outline.pdf"));
+        PDDocument doc = PDDocument.load(new File(TestPDPageTree.class.getResource("with_outline.pdf").toURI()));
         PDDocumentOutline outline = doc.getDocumentCatalog().getDocumentOutline();
         Iterable<PDOutlineItem> children = outline.children();
         Iterator<PDOutlineItem> it = children.iterator();



Mime
View raw message