Skip to content

Commit d8f7665

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

File tree

6 files changed

+34
-42
lines changed

6 files changed

+34
-42
lines changed

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ private List<COSObject> getIndirectResourceObjects(COSDictionary pageResources,
169169
return Collections.emptyList();
170170
}
171171
return resourcesDictionary.getValues().stream() //
172-
.filter(f -> f instanceof COSObject) //
173-
.map(f -> (COSObject) f) //
172+
.filter(COSObject.class::isInstance) //
173+
.map(COSObject.class::cast) //
174174
.filter(COSObject::isDereferenced) //
175175
.collect(Collectors.toList());
176176
}

pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSStream.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void testHasStreamData() throws IOException
168168
try (COSStream stream = new COSStream())
169169
{
170170
assertFalse(stream.hasData());
171-
Assertions.assertThrows(IOException.class, () -> stream.createInputStream(),
171+
Assertions.assertThrows(IOException.class, stream::createInputStream,
172172
"createInputStream should have thrown an IOException");
173173

174174
byte[] testString = "This is a test string to be used as input for TestCOSStream"
@@ -201,17 +201,21 @@ private COSStream createStream(byte[] testString, COSBase filters) throws IOExce
201201

202202
private void validateEncoded(COSStream stream, byte[] expected) throws IOException
203203
{
204-
InputStream in = stream.createRawInputStream();
205-
byte[] decoded = in.readAllBytes();
206-
stream.close();
207-
assertTrue(Arrays.equals(expected, decoded), "Encoded data doesn't match input");
204+
try (stream)
205+
{
206+
InputStream in = stream.createRawInputStream();
207+
byte[] decoded = in.readAllBytes();
208+
assertTrue(Arrays.equals(expected, decoded), "Encoded data doesn't match input");
209+
}
208210
}
209211

210212
private void validateDecoded(COSStream stream, byte[] expected) throws IOException
211213
{
212-
InputStream in = stream.createInputStream();
213-
byte[] encoded = in.readAllBytes();
214-
stream.close();
215-
assertTrue(Arrays.equals(expected, encoded), "Decoded data doesn't match input");
214+
try (stream)
215+
{
216+
InputStream in = stream.createInputStream();
217+
byte[] encoded = in.readAllBytes();
218+
assertTrue(Arrays.equals(expected, encoded), "Decoded data doesn't match input");
219+
}
216220
}
217221
}

pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ void testSplitWithBrokenDestination() throws IOException
13431343
annotations = doc.getPage(1).getAnnotations();
13441344
assertEquals(1, annotations.size());
13451345
PDAnnotationLink link = (PDAnnotationLink) annotations.get(0);
1346-
assertThrows(IOException.class, () -> link.getDestination());
1346+
assertThrows(IOException.class, link::getDestination);
13471347
}
13481348
}
13491349

@@ -1389,9 +1389,10 @@ void testSplitWithNamedDestinations() throws IOException
13891389

13901390
ByteArrayOutputStream baos = new ByteArrayOutputStream();
13911391
dstDoc.save(baos);
1392-
PDDocument reloadedDoc = Loader.loadPDF(baos.toByteArray());
1393-
assertNotNull(reloadedDoc.getDocumentCatalog().getMetadata());
1394-
reloadedDoc.close();
1392+
try (PDDocument reloadedDoc = Loader.loadPDF(baos.toByteArray()))
1393+
{
1394+
assertNotNull(reloadedDoc.getDocumentCatalog().getMetadata());
1395+
}
13951396
}
13961397
// Check that source document is unchanged
13971398
annotations = doc.getPage(0).getAnnotations();

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -251,32 +251,19 @@ void testGeneralGraphicStateOperatorTextMode() throws IOException
251251
() -> contentStream.moveTo(0, 0));
252252
Assertions.assertThrows(IllegalStateException.class,
253253
() -> contentStream.lineTo(1, 1));
254-
Assertions.assertThrows(IllegalStateException.class,
255-
() -> contentStream.stroke());
256-
Assertions.assertThrows(IllegalStateException.class,
257-
() -> contentStream.closeAndStroke());
258-
Assertions.assertThrows(IllegalStateException.class,
259-
() -> contentStream.closeAndFillAndStroke());
260-
Assertions.assertThrows(IllegalStateException.class,
261-
() -> contentStream.closeAndFillAndStrokeEvenOdd());
262-
Assertions.assertThrows(IllegalStateException.class,
263-
() -> contentStream.fill());
264-
Assertions.assertThrows(IllegalStateException.class,
265-
() -> contentStream.fillAndStroke());
266-
Assertions.assertThrows(IllegalStateException.class,
267-
() -> contentStream.fillAndStrokeEvenOdd());
268-
Assertions.assertThrows(IllegalStateException.class,
269-
() -> contentStream.fillEvenOdd());
270-
Assertions.assertThrows(IllegalStateException.class,
271-
() -> contentStream.fill());
272254
Assertions.assertThrows(IllegalStateException.class,
273255
() -> contentStream.shadingFill(new PDShadingType1(new COSDictionary())));
274-
Assertions.assertThrows(IllegalStateException.class,
275-
() -> contentStream.closePath());
276-
Assertions.assertThrows(IllegalStateException.class,
277-
() -> contentStream.clip());
278-
Assertions.assertThrows(IllegalStateException.class,
279-
() -> contentStream.clipEvenOdd());
256+
Assertions.assertThrows(IllegalStateException.class, contentStream::stroke);
257+
Assertions.assertThrows(IllegalStateException.class, contentStream::closeAndStroke);
258+
Assertions.assertThrows(IllegalStateException.class, contentStream::closeAndFillAndStroke);
259+
Assertions.assertThrows(IllegalStateException.class, contentStream::closeAndFillAndStrokeEvenOdd);
260+
Assertions.assertThrows(IllegalStateException.class, contentStream::fill);
261+
Assertions.assertThrows(IllegalStateException.class, contentStream::fillAndStroke);
262+
Assertions.assertThrows(IllegalStateException.class, contentStream::fillAndStrokeEvenOdd);
263+
Assertions.assertThrows(IllegalStateException.class, contentStream::fillEvenOdd);
264+
Assertions.assertThrows(IllegalStateException.class, contentStream::closePath);
265+
Assertions.assertThrows(IllegalStateException.class, contentStream::clip);
266+
Assertions.assertThrows(IllegalStateException.class, contentStream::clipEvenOdd);
280267

281268
// J
282269
contentStream.setLineCapStyle(0);

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureElementTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void testPDFBox4197() throws IOException
6161

6262
// collect attributes and check their count.
6363
assertEquals(117, attributeSet.size());
64-
int cnt = attributeSet.stream().map(attributes -> attributes.size()).reduce(0, Integer::sum);
64+
int cnt = attributeSet.stream().map(Revisions::size).reduce(0, Integer::sum);
6565
assertEquals(111, cnt); // this one was 105 before PDFBOX-4197 was fixed
6666
assertEquals(0, classSet.size());
6767
}
@@ -86,7 +86,7 @@ void testClassMap() throws IOException
8686

8787
// collect attributes and check their count.
8888
assertEquals(72, attributeSet.size());
89-
int cnt = attributeSet.stream().map(attributes -> attributes.size()).reduce(0, Integer::sum);
89+
int cnt = attributeSet.stream().map(Revisions::size).reduce(0, Integer::sum);
9090
assertEquals(45, cnt);
9191
assertEquals(10, classSet.size());
9292
}

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/outline/PDOutlineItemIteratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void multipleItem()
5858
void removeUnsupported()
5959
{
6060
PDOutlineItemIterator pdOutlineItemIterator = new PDOutlineItemIterator(new PDOutlineItem());
61-
assertThrows(UnsupportedOperationException.class, () -> pdOutlineItemIterator.remove());
61+
assertThrows(UnsupportedOperationException.class, pdOutlineItemIterator::remove);
6262
}
6363

6464
@Test

0 commit comments

Comments
 (0)