Author: tilman
Date: Sat Apr 12 11:46:05 2025
New Revision: 1925039
URL: http://svn.apache.org/viewvc?rev=1925039&view=rev
Log:
PDFBOX-5660: Sonar fix
Modified:
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfwriter/COSDocumentCompressionTest.java
Modified:
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfwriter/COSDocumentCompressionTest.java
URL:
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfwriter/COSDocumentCompressionTest.java?rev=1925039&r1=1925038&r2=1925039&view=diff
==============================================================================
---
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfwriter/COSDocumentCompressionTest.java
(original)
+++
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfwriter/COSDocumentCompressionTest.java
Sat Apr 12 11:46:05 2025
@@ -40,6 +40,7 @@ import org.apache.pdfbox.pdmodel.interac
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
@@ -51,38 +52,32 @@ import org.junit.jupiter.api.Test;
*/
class COSDocumentCompressionTest
{
+ private static final File INDIR = new
File("src/test/resources/input/compression/");
+ private static final File OUTDIR = new
File("target/test-output/compression/");
- static final File inDir = new
File("src/test/resources/input/compression/");
- static final File outDir = new File("target/test-output/compression/");
-
- COSDocumentCompressionTest()
+ @BeforeAll
+ static void init()
{
- outDir.mkdirs();
+ OUTDIR.mkdirs();
}
/**
* Compress a document, that contains acroform fields and touch the
expected fields.
*
- * @throws Exception Shall be thrown, when compressing the document failed.
+ * @throws IOException Shall be thrown, when compressing the document
failed.
*/
@Test
- void testCompressAcroformDoc() throws Exception
+ void testCompressAcroformDoc() throws IOException
{
- File source = new File(inDir, "acroform.pdf");
- File target = new File(outDir, "acroform.pdf");
+ File source = new File(INDIR, "acroform.pdf");
+ File target = new File(OUTDIR, "acroform.pdf");
- PDDocument document = Loader.loadPDF(source);
- try
+ try (PDDocument document = Loader.loadPDF(source))
{
document.save(target);
}
- finally
- {
- document.close();
- }
- document = Loader.loadPDF(target);
- try
+ try (PDDocument document = Loader.loadPDF(target))
{
assertEquals(1, document.getNumberOfPages(),
"The number of pages should not have changed, during
compression.");
@@ -129,35 +124,25 @@ class COSDocumentCompressionTest
assertEquals("Signature",
annotations.get(12).getCOSObject().getNameAsString(COSName.T),
"The 13. annotation should have been a Signature.");
}
- finally
- {
- document.close();
- }
}
/**
* Compress a document, that contains an attachment and touch the expected
attachment.
*
- * @throws Exception Shall be thrown, when compressing the document failed.
+ * @throws IOException Shall be thrown, when compressing the document
failed.
*/
@Test
- void testCompressAttachmentsDoc() throws Exception
+ void testCompressAttachmentsDoc() throws IOException
{
- File source = new File(inDir, "attachment.pdf");
- File target = new File(outDir, "attachment.pdf");
+ File source = new File(INDIR, "attachment.pdf");
+ File target = new File(OUTDIR, "attachment.pdf");
- PDDocument document = Loader.loadPDF(source);
- try
+ try (PDDocument document = Loader.loadPDF(source))
{
document.save(target);
}
- finally
- {
- document.close();
- }
- document = Loader.loadPDF(target);
- try
+ try (PDDocument document = Loader.loadPDF(target))
{
assertEquals(2, document.getNumberOfPages(),
"The number of pages should not have changed, during
compression.");
@@ -171,60 +156,50 @@ class COSDocumentCompressionTest
assertEquals(14997, attachment.getEmbeddedFile().getLength(),
"The attachments length is not as expected.");
}
- finally
- {
- document.close();
- }
}
/**
* Compress and encrypt the given document, without causing an exception
to be thrown.
*
- * @throws Exception Shall be thrown, when compressing/encrypting the
document failed.
+ * @throws IOException Shall be thrown, when compressing/encrypting the
document failed.
*/
@Test
- void testCompressEncryptedDoc() throws Exception
+ void testCompressEncryptedDoc() throws IOException
{
- File source = new File(inDir, "unencrypted.pdf");
- File target = new File(outDir, "encrypted.pdf");
+ File source = new File(INDIR, "unencrypted.pdf");
+ File target = new File(OUTDIR, "encrypted.pdf");
- PDDocument document = Loader.loadPDF(source, "user");
- try
+ try (PDDocument document = Loader.loadPDF(source, "user"))
{
document.protect(
new StandardProtectionPolicy("owner", "user", new
AccessPermission(0)));
document.save(target);
}
- finally
+
+ try (PDDocument document = Loader.loadPDF(target, "user"))
{
- document.close();
+ // If this didn't fail, the encryption dictionary should be
present and working.
+ assertEquals(2, document.getNumberOfPages());
}
-
- document = Loader.loadPDF(target, "user");
- // If this didn't fail, the encryption dictionary should be present
and working.
- assertEquals(2, document.getNumberOfPages());
- document.close();
}
/**
* Adds a page to an existing document, compresses it and touches the
resulting page content stream.
*
- * @throws Exception Shall be thrown, if compressing the document failed.
+ * @throws IOException Shall be thrown, if compressing the document failed.
*/
@Test
- void testAlteredDoc() throws Exception
+ void testAlteredDoc() throws IOException
{
- File source = new File(inDir, "unencrypted.pdf");
- File target = new File(outDir, "altered.pdf");
+ File source = new File(INDIR, "unencrypted.pdf");
+ File target = new File(OUTDIR, "altered.pdf");
- PDDocument document = Loader.loadPDF(source);
- try
+ try (PDDocument document = Loader.loadPDF(source))
{
PDPage page = new PDPage(new PDRectangle(100, 100));
document.addPage(page);
- PDPageContentStream contentStream = new
PDPageContentStream(document, page);
- try
+ try (PDPageContentStream contentStream = new
PDPageContentStream(document, page))
{
contentStream.beginText();
contentStream.newLineAtOffset(20, 80);
@@ -232,24 +207,11 @@ class COSDocumentCompressionTest
contentStream.showText("Test");
contentStream.endText();
}
- finally
- {
- contentStream.close();
- }
document.save(target);
}
- catch (Throwable t)
- {
- t.printStackTrace();
- }
- finally
- {
- document.close();
- }
- document = Loader.loadPDF(target);
- try
+ try (PDDocument document = Loader.loadPDF(target))
{
assertEquals(3, document.getNumberOfPages(),
"The number of pages should not have changed, during
compression.");
@@ -257,10 +219,6 @@ class COSDocumentCompressionTest
assertEquals(43, page.getContentStreams().next().getLength(),
"The stream length of the new page is not as expected.");
}
- finally
- {
- document.close();
- }
}
/**