Skip to content

Commit 482fed2

Browse files
committed
PDFBOX-5660: Sonar fix
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1925039 13f79535-47bb-0310-9956-ffa450edef68
1 parent f0f6703 commit 482fed2

File tree

1 file changed

+34
-76
lines changed

1 file changed

+34
-76
lines changed

pdfbox/src/test/java/org/apache/pdfbox/pdfwriter/COSDocumentCompressionTest.java

Lines changed: 34 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.junit.jupiter.api.Assertions.assertEquals;
4141
import static org.junit.jupiter.api.Assertions.assertNotNull;
4242
import static org.junit.jupiter.api.Assertions.assertTrue;
43+
import org.junit.jupiter.api.BeforeAll;
4344
import org.junit.jupiter.api.Test;
4445

4546
/**
@@ -51,38 +52,32 @@
5152
*/
5253
class COSDocumentCompressionTest
5354
{
55+
private static final File INDIR = new File("src/test/resources/input/compression/");
56+
private static final File OUTDIR = new File("target/test-output/compression/");
5457

55-
static final File inDir = new File("src/test/resources/input/compression/");
56-
static final File outDir = new File("target/test-output/compression/");
57-
58-
COSDocumentCompressionTest()
58+
@BeforeAll
59+
static void init()
5960
{
60-
outDir.mkdirs();
61+
OUTDIR.mkdirs();
6162
}
6263

6364
/**
6465
* Compress a document, that contains acroform fields and touch the expected fields.
6566
*
66-
* @throws Exception Shall be thrown, when compressing the document failed.
67+
* @throws IOException Shall be thrown, when compressing the document failed.
6768
*/
6869
@Test
69-
void testCompressAcroformDoc() throws Exception
70+
void testCompressAcroformDoc() throws IOException
7071
{
71-
File source = new File(inDir, "acroform.pdf");
72-
File target = new File(outDir, "acroform.pdf");
72+
File source = new File(INDIR, "acroform.pdf");
73+
File target = new File(OUTDIR, "acroform.pdf");
7374

74-
PDDocument document = Loader.loadPDF(source);
75-
try
75+
try (PDDocument document = Loader.loadPDF(source))
7676
{
7777
document.save(target);
7878
}
79-
finally
80-
{
81-
document.close();
82-
}
8379

84-
document = Loader.loadPDF(target);
85-
try
80+
try (PDDocument document = Loader.loadPDF(target))
8681
{
8782
assertEquals(1, document.getNumberOfPages(),
8883
"The number of pages should not have changed, during compression.");
@@ -129,35 +124,25 @@ void testCompressAcroformDoc() throws Exception
129124
assertEquals("Signature", annotations.get(12).getCOSObject().getNameAsString(COSName.T),
130125
"The 13. annotation should have been a Signature.");
131126
}
132-
finally
133-
{
134-
document.close();
135-
}
136127
}
137128

138129
/**
139130
* Compress a document, that contains an attachment and touch the expected attachment.
140131
*
141-
* @throws Exception Shall be thrown, when compressing the document failed.
132+
* @throws IOException Shall be thrown, when compressing the document failed.
142133
*/
143134
@Test
144-
void testCompressAttachmentsDoc() throws Exception
135+
void testCompressAttachmentsDoc() throws IOException
145136
{
146-
File source = new File(inDir, "attachment.pdf");
147-
File target = new File(outDir, "attachment.pdf");
137+
File source = new File(INDIR, "attachment.pdf");
138+
File target = new File(OUTDIR, "attachment.pdf");
148139

149-
PDDocument document = Loader.loadPDF(source);
150-
try
140+
try (PDDocument document = Loader.loadPDF(source))
151141
{
152142
document.save(target);
153143
}
154-
finally
155-
{
156-
document.close();
157-
}
158144

159-
document = Loader.loadPDF(target);
160-
try
145+
try (PDDocument document = Loader.loadPDF(target))
161146
{
162147
assertEquals(2, document.getNumberOfPages(),
163148
"The number of pages should not have changed, during compression.");
@@ -171,96 +156,69 @@ void testCompressAttachmentsDoc() throws Exception
171156
assertEquals(14997, attachment.getEmbeddedFile().getLength(),
172157
"The attachments length is not as expected.");
173158
}
174-
finally
175-
{
176-
document.close();
177-
}
178159
}
179160

180161
/**
181162
* Compress and encrypt the given document, without causing an exception to be thrown.
182163
*
183-
* @throws Exception Shall be thrown, when compressing/encrypting the document failed.
164+
* @throws IOException Shall be thrown, when compressing/encrypting the document failed.
184165
*/
185166
@Test
186-
void testCompressEncryptedDoc() throws Exception
167+
void testCompressEncryptedDoc() throws IOException
187168
{
188-
File source = new File(inDir, "unencrypted.pdf");
189-
File target = new File(outDir, "encrypted.pdf");
169+
File source = new File(INDIR, "unencrypted.pdf");
170+
File target = new File(OUTDIR, "encrypted.pdf");
190171

191-
PDDocument document = Loader.loadPDF(source, "user");
192-
try
172+
try (PDDocument document = Loader.loadPDF(source, "user"))
193173
{
194174
document.protect(
195175
new StandardProtectionPolicy("owner", "user", new AccessPermission(0)));
196176
document.save(target);
197177
}
198-
finally
178+
179+
try (PDDocument document = Loader.loadPDF(target, "user"))
199180
{
200-
document.close();
181+
// If this didn't fail, the encryption dictionary should be present and working.
182+
assertEquals(2, document.getNumberOfPages());
201183
}
202-
203-
document = Loader.loadPDF(target, "user");
204-
// If this didn't fail, the encryption dictionary should be present and working.
205-
assertEquals(2, document.getNumberOfPages());
206-
document.close();
207184
}
208185

209186
/**
210187
* Adds a page to an existing document, compresses it and touches the resulting page content stream.
211188
*
212-
* @throws Exception Shall be thrown, if compressing the document failed.
189+
* @throws IOException Shall be thrown, if compressing the document failed.
213190
*/
214191
@Test
215-
void testAlteredDoc() throws Exception
192+
void testAlteredDoc() throws IOException
216193
{
217-
File source = new File(inDir, "unencrypted.pdf");
218-
File target = new File(outDir, "altered.pdf");
194+
File source = new File(INDIR, "unencrypted.pdf");
195+
File target = new File(OUTDIR, "altered.pdf");
219196

220-
PDDocument document = Loader.loadPDF(source);
221-
try
197+
try (PDDocument document = Loader.loadPDF(source))
222198
{
223199
PDPage page = new PDPage(new PDRectangle(100, 100));
224200
document.addPage(page);
225-
PDPageContentStream contentStream = new PDPageContentStream(document, page);
226201

227-
try
202+
try (PDPageContentStream contentStream = new PDPageContentStream(document, page))
228203
{
229204
contentStream.beginText();
230205
contentStream.newLineAtOffset(20, 80);
231206
contentStream.setFont(new PDType1Font(FontName.HELVETICA), 12);
232207
contentStream.showText("Test");
233208
contentStream.endText();
234209
}
235-
finally
236-
{
237-
contentStream.close();
238-
}
239210

240211
document.save(target);
241212
}
242-
catch (Throwable t)
243-
{
244-
t.printStackTrace();
245-
}
246-
finally
247-
{
248-
document.close();
249-
}
250213

251-
document = Loader.loadPDF(target);
252-
try
214+
try (PDDocument document = Loader.loadPDF(target))
253215
{
254216
assertEquals(3, document.getNumberOfPages(),
255217
"The number of pages should not have changed, during compression.");
256218
PDPage page = document.getPage(2);
257219
assertEquals(43, page.getContentStreams().next().getLength(),
258220
"The stream length of the new page is not as expected.");
259221
}
260-
finally
261-
{
262-
document.close();
263-
}
264222
}
265223

266224
/**

0 commit comments

Comments
 (0)