Author: msahyoun
Date: Fri Feb  9 21:51:21 2018
New Revision: 1823711

URL: http://svn.apache.org/viewvc?rev=1823711&view=rev
Log:
PDFBOX-4071: use try-with

Modified:
    
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDDocumentInformation.java
    
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java
    
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageTransitions.java

Modified: 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDDocumentInformation.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDDocumentInformation.java?rev=1823711&r1=1823710&r2=1823711&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDDocumentInformation.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDDocumentInformation.java
 Fri Feb  9 21:51:21 2018
@@ -32,11 +32,9 @@ public class TestPDDocumentInformation e
 
     public void testMetadataExtraction() throws Exception
     {
-        PDDocument doc = null;
-        try
+        // This document has been selected for this test as it contains custom 
metadata.
+        try (PDDocument doc = PDDocument.load( new 
File("src/test/resources/input/hello3.pdf")))
         {
-           // This document has been selected for this test as it contains 
custom metadata.
-           doc = PDDocument.load( new 
File("src/test/resources/input/hello3.pdf"));
            PDDocumentInformation info = doc.getDocumentInformation();
            
            assertEquals("Wrong author", "Brian Carrier", info.getAuthor());
@@ -62,13 +60,6 @@ public class TestPDDocumentInformation e
            assertEquals("Wrong company", "Basis Technology Corp.", 
info.getCustomMetadataValue("Company"));
            assertEquals("Wrong sourceModified", "D:20080819181502", 
info.getCustomMetadataValue("SourceModified"));
         }
-        finally
-        {
-            if( doc != null )
-            {
-                doc.close();
-            }
-        }
     }
     
     /**
@@ -78,10 +69,11 @@ public class TestPDDocumentInformation e
      */
     public void testPDFBox3068() throws Exception
     {
-        PDDocument doc = 
PDDocument.load(TestPDDocumentInformation.class.getResourceAsStream("PDFBOX-3068.pdf"));
-        PDDocumentInformation documentInformation = 
doc.getDocumentInformation();
-        assertEquals("Title", documentInformation.getTitle());
-        doc.close();
+        try (PDDocument doc = 
PDDocument.load(TestPDDocumentInformation.class.getResourceAsStream("PDFBOX-3068.pdf")))
+        {
+            PDDocumentInformation documentInformation = 
doc.getDocumentInformation();
+            assertEquals("Title", documentInformation.getTitle());
+        }
     }
     
 }

Modified: 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java?rev=1823711&r1=1823710&r2=1823711&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java
 Fri Feb  9 21:51:21 2018
@@ -31,56 +31,61 @@ public class TestPDPageContentStream ext
 {
     public void testSetCmykColors() throws IOException
     {
-        PDDocument doc = new PDDocument();
-
-        PDPage page = new PDPage();
-        doc.addPage(page);
-
-        PDPageContentStream contentStream = new PDPageContentStream(doc, page, 
AppendMode.OVERWRITE, true);
-        // pass a non-stroking color in CMYK color space
-        contentStream.setNonStrokingColor(0.1f, 0.2f, 0.3f, 0.4f);
-        contentStream.close();
-
-        // now read the PDF stream and verify that the CMYK values are correct
-        PDFStreamParser parser = new PDFStreamParser(page);
-        parser.parse();
-        java.util.List<Object>  pageTokens = parser.getTokens();
-        // expected five tokens :
-        // [0] = COSFloat{0.1}
-        // [1] = COSFloat{0.2}
-        // [2] = COSFloat{0.3}
-        // [3] = COSFloat{0.4}
-        // [4] = PDFOperator{"k"}
-        assertEquals(0.1f, ((COSFloat)pageTokens.get(0)).floatValue());
-        assertEquals(0.2f, ((COSFloat)pageTokens.get(1)).floatValue());
-        assertEquals(0.3f, ((COSFloat)pageTokens.get(2)).floatValue());
-        assertEquals(0.4f, ((COSFloat)pageTokens.get(3)).floatValue());
-        assertEquals("k", ((Operator) pageTokens.get(4)).getName());
-
-        // same as above but for PDPageContentStream#setStrokingColor
-        page = new PDPage();
-        doc.addPage(page);
-
-        contentStream = new PDPageContentStream(doc, page, 
AppendMode.OVERWRITE, false);
-        // pass a non-stroking color in CMYK color space
-        contentStream.setStrokingColor(0.5f, 0.6f, 0.7f, 0.8f);
-        contentStream.close();
-
-        // now read the PDF stream and verify that the CMYK values are correct
-        parser = new PDFStreamParser(page);
-        parser.parse();
-        pageTokens = parser.getTokens();
-        // expected five tokens  :
-        // [0] = COSFloat{0.5}
-        // [1] = COSFloat{0.6}
-        // [2] = COSFloat{0.7}
-        // [3] = COSFloat{0.8}
-        // [4] = PDFOperator{"K"}
-        assertEquals(0.5f, ((COSFloat)pageTokens.get(0)).floatValue());
-        assertEquals(0.6f, ((COSFloat)pageTokens.get(1)).floatValue());
-        assertEquals(0.7f, ((COSFloat)pageTokens.get(2)).floatValue());
-        assertEquals(0.8f, ((COSFloat)pageTokens.get(3)).floatValue());
-        assertEquals("K", ((Operator)pageTokens.get(4)).getName());
+        try (PDDocument doc = new PDDocument())
+        {
+            PDPage page = new PDPage();
+            doc.addPage(page);
+
+            try (PDPageContentStream contentStream = new 
PDPageContentStream(doc, page, AppendMode.OVERWRITE, true))
+            {
+                // pass a non-stroking color in CMYK color space
+                contentStream.setNonStrokingColor(0.1f, 0.2f, 0.3f, 0.4f);
+                contentStream.close();
+            }
+
+                // now read the PDF stream and verify that the CMYK values are 
correct
+            PDFStreamParser parser = new PDFStreamParser(page);
+            parser.parse();
+            java.util.List<Object>  pageTokens = parser.getTokens();
+            // expected five tokens :
+            // [0] = COSFloat{0.1}
+            // [1] = COSFloat{0.2}
+            // [2] = COSFloat{0.3}
+            // [3] = COSFloat{0.4}
+            // [4] = PDFOperator{"k"}
+            assertEquals(0.1f, ((COSFloat)pageTokens.get(0)).floatValue());
+            assertEquals(0.2f, ((COSFloat)pageTokens.get(1)).floatValue());
+            assertEquals(0.3f, ((COSFloat)pageTokens.get(2)).floatValue());
+            assertEquals(0.4f, ((COSFloat)pageTokens.get(3)).floatValue());
+            assertEquals("k", ((Operator) pageTokens.get(4)).getName());
+
+            // same as above but for PDPageContentStream#setStrokingColor
+            page = new PDPage();
+            doc.addPage(page);
+
+            try ( PDPageContentStream contentStream = new 
PDPageContentStream(doc, page, AppendMode.OVERWRITE, false))
+            {
+                // pass a non-stroking color in CMYK color space
+                contentStream.setStrokingColor(0.5f, 0.6f, 0.7f, 0.8f);
+                contentStream.close();
+            }
+
+            // now read the PDF stream and verify that the CMYK values are 
correct
+            parser = new PDFStreamParser(page);
+            parser.parse();
+            pageTokens = parser.getTokens();
+            // expected five tokens  :
+            // [0] = COSFloat{0.5}
+            // [1] = COSFloat{0.6}
+            // [2] = COSFloat{0.7}
+            // [3] = COSFloat{0.8}
+            // [4] = PDFOperator{"K"}
+            assertEquals(0.5f, ((COSFloat)pageTokens.get(0)).floatValue());
+            assertEquals(0.6f, ((COSFloat)pageTokens.get(1)).floatValue());
+            assertEquals(0.7f, ((COSFloat)pageTokens.get(2)).floatValue());
+            assertEquals(0.8f, ((COSFloat)pageTokens.get(3)).floatValue());
+            assertEquals("K", ((Operator)pageTokens.get(4)).getName());
+        }
     }
 
     /**

Modified: 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageTransitions.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageTransitions.java?rev=1823711&r1=1823710&r2=1823711&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageTransitions.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageTransitions.java
 Fri Feb  9 21:51:21 2018
@@ -39,38 +39,42 @@ public class TestPDPageTransitions
     @Test
     public void readTransitions() throws IOException, URISyntaxException
     {
-        PDDocument doc = PDDocument.load(new File(this.getClass().getResource(
-                
"/org/apache/pdfbox/pdmodel/interactive/pagenavigation/transitions_test.pdf").toURI()));
-        PDTransition firstTransition = doc.getPages().get(0).getTransition();
-        assertEquals(PDTransitionStyle.Glitter.name(), 
firstTransition.getStyle());
-        assertEquals(2, firstTransition.getDuration(), 0);
-        
assertEquals(PDTransitionDirection.TOP_LEFT_TO_BOTTOM_RIGHT.getCOSBase(),
-                firstTransition.getDirection());
-        doc.close();
+        try (PDDocument doc = PDDocument.load(new 
File(this.getClass().getResource(
+                
"/org/apache/pdfbox/pdmodel/interactive/pagenavigation/transitions_test.pdf").toURI())))
+        {
+            PDTransition firstTransition = 
doc.getPages().get(0).getTransition();
+            assertEquals(PDTransitionStyle.Glitter.name(), 
firstTransition.getStyle());
+            assertEquals(2, firstTransition.getDuration(), 0);
+            
assertEquals(PDTransitionDirection.TOP_LEFT_TO_BOTTOM_RIGHT.getCOSBase(),
+                    firstTransition.getDirection());
+        }
     }
 
     @Test
     public void saveAndReadTransitions() throws IOException
     {
-        // save
-        PDDocument document = new PDDocument();
-        PDPage page = new PDPage();
-        document.addPage(page);
-        PDTransition transition = new PDTransition(PDTransitionStyle.Fly);
-        transition.setDirection(PDTransitionDirection.NONE);
-        transition.setFlyScale(0.5f);
-        page.setTransition(transition, 2);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        document.save(baos);
-        document.close();
 
+        // save
+        try (PDDocument document = new PDDocument())
+        {
+            PDPage page = new PDPage();
+            document.addPage(page);
+            PDTransition transition = new PDTransition(PDTransitionStyle.Fly);
+            transition.setDirection(PDTransitionDirection.NONE);
+            transition.setFlyScale(0.5f);
+            page.setTransition(transition, 2);
+            document.save(baos);
+        }
+        
         // read
-        PDDocument doc = PDDocument.load(baos.toByteArray());
-        page = doc.getPages().get(0);
-        PDTransition loadedTransition = page.getTransition();
-        assertEquals(PDTransitionStyle.Fly.name(), 
loadedTransition.getStyle());
-        assertEquals(2, page.getCOSObject().getFloat(COSName.DUR), 0);
-        assertEquals(PDTransitionDirection.NONE.getCOSBase(), 
loadedTransition.getDirection());
-        doc.close();
+        try (PDDocument doc = PDDocument.load(baos.toByteArray()))
+        {
+            PDPage page = doc.getPages().get(0);
+            PDTransition loadedTransition = page.getTransition();
+            assertEquals(PDTransitionStyle.Fly.name(), 
loadedTransition.getStyle());
+            assertEquals(2, page.getCOSObject().getFloat(COSName.DUR), 0);
+            assertEquals(PDTransitionDirection.NONE.getCOSBase(), 
loadedTransition.getDirection());
+        }
     }
 }


Reply via email to