diff --git a/fontbox/src/test/java/org/apache/fontbox/afm/AFMParserTest.java b/fontbox/src/test/java/org/apache/fontbox/afm/AFMParserTest.java index 60877095d70..50fa4e0b8f5 100644 --- a/fontbox/src/test/java/org/apache/fontbox/afm/AFMParserTest.java +++ b/fontbox/src/test/java/org/apache/fontbox/afm/AFMParserTest.java @@ -17,12 +17,8 @@ package org.apache.fontbox.afm; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import org.apache.fontbox.util.BoundingBox; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.FileInputStream; @@ -31,95 +27,63 @@ import java.util.List; import java.util.Optional; -import org.apache.fontbox.util.BoundingBox; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** - * * @author Tilman Hausherr */ -class AFMParserTest -{ +class AFMParserTest { + + public static final String HELVETICA_AFM = "src/test/resources/afm/Helvetica.afm"; + @Test - void testStartFontMetrics() throws IOException - { - try - { - new AFMParser(new ByteArrayInputStream("huhu".getBytes(StandardCharsets.US_ASCII))) - .parse(); - fail("The AFMParser should have thrown an IOException because of a missing " - + AFMParser.START_FONT_METRICS); - } - catch (IOException e) - { - // expected exception - } + void testStartFontMetrics() { + assertThrows(IOException.class, + () -> new AFMParser(new ByteArrayInputStream("huhu".getBytes(StandardCharsets.US_ASCII))).parse(), + "The AFMParser should have thrown an IOException because of a missing " + AFMParser.START_FONT_METRICS); } @Test - void testEndFontMetrics() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/NoEndFontMetrics.afm")); - try - { - parser.parse(); - fail("The AFMParser should have thrown an IOException because of a missing " - + AFMParser.END_FONT_METRICS); - } - catch (IOException e) - { - assertTrue(e.getMessage().contains("Unknown AFM key")); - } + void testEndFontMetrics() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/NoEndFontMetrics.afm")); + IOException e = assertThrows(IOException.class, parser::parse, + "The AFMParser should have thrown an IOException because of a missing " + AFMParser.END_FONT_METRICS); + assertTrue(e.getMessage().contains("Unknown AFM key")); } @Test - void testMalformedFloat() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/MalformedFloat.afm")); - try - { - parser.parse(); - fail("The AFMParser should have thrown an IOException because of a malformed float value"); - } - catch (IOException e) - { - assertTrue(e.getCause() instanceof NumberFormatException); - assertTrue(e.getMessage().contains("4,1ab")); - } + void testMalformedFloat() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/MalformedFloat.afm")); + IOException e = assertThrows(IOException.class, parser::parse, + "The AFMParser should have thrown an IOException because of a malformed float value"); + assertInstanceOf(NumberFormatException.class, e.getCause()); + assertTrue(e.getMessage().contains("4,1ab")); } @Test - void testMalformedInteger() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/MalformedInteger.afm")); - try - { - parser.parse(); - fail("The AFMParser should have thrown an IOException because of a malformed int value"); - } - catch (IOException e) - { - assertTrue(e.getCause() instanceof NumberFormatException); - assertTrue(e.getMessage().contains("3.4")); - } + void testMalformedInteger() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/MalformedInteger.afm")); + IOException e = assertThrows(IOException.class, parser::parse, + "The AFMParser should have thrown an IOException because of a malformed int value"); + assertInstanceOf(NumberFormatException.class, e.getCause()); + assertTrue(e.getMessage().contains("3.4")); } @Test - void testHelveticaFontMetrics() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/Helvetica.afm")); + void testHelveticaFontMetrics() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM)); checkHelveticaFontMetrics(parser.parse()); } @Test - void testHelveticaCharMetrics() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/Helvetica.afm")); + void testHelveticaCharMetrics() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM)); FontMetrics fontMetrics = parser.parse(); // char metrics @@ -127,10 +91,8 @@ void testHelveticaCharMetrics() throws IOException } @Test - void testHelveticaKernPairs() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/Helvetica.afm")); + void testHelveticaKernPairs() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM)); FontMetrics fontMetrics = parser.parse(); // KernPairs @@ -149,18 +111,14 @@ void testHelveticaKernPairs() throws IOException } @Test - void testHelveticaFontMetricsReducedDataset() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/Helvetica.afm")); + void testHelveticaFontMetricsReducedDataset() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM)); checkHelveticaFontMetrics(parser.parse(true)); } @Test - void testHelveticaCharMetricsReducedDataset() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/Helvetica.afm")); + void testHelveticaCharMetricsReducedDataset() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM)); FontMetrics fontMetrics = parser.parse(true); // char metrics @@ -168,10 +126,8 @@ void testHelveticaCharMetricsReducedDataset() throws IOException } @Test - void testHelveticaKernPairsReducedDataset() throws IOException - { - AFMParser parser = new AFMParser( - new FileInputStream("src/test/resources/afm/Helvetica.afm")); + void testHelveticaKernPairsReducedDataset() throws IOException { + AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM)); FontMetrics fontMetrics = parser.parse(true); // KernPairs, empty due to reducedDataset == true @@ -184,12 +140,10 @@ void testHelveticaKernPairsReducedDataset() throws IOException assertTrue(fontMetrics.getComposites().isEmpty()); } - private void checkHelveticaCharMetrics(List charMetrics) - { + private void checkHelveticaCharMetrics(List charMetrics) { assertEquals(315, charMetrics.size()); // check "space" metrics - Optional space = charMetrics.stream()// - .filter(c -> "space".equals(c.getName())).findFirst(); + Optional space = charMetrics.stream().filter(c -> "space".equals(c.getName())).findFirst(); assertTrue(space.isPresent()); CharMetric spaceCharMetric = space.get(); assertEquals(278f, spaceCharMetric.getWx(), 0f); @@ -201,8 +155,7 @@ private void checkHelveticaCharMetrics(List charMetrics) assertNull(spaceCharMetric.getW1()); assertNull(spaceCharMetric.getVv()); // check "ring" metrics - Optional ring = charMetrics.stream()// - .filter(c -> "ring".equals(c.getName())).findFirst(); + Optional ring = charMetrics.stream().filter(c -> "ring".equals(c.getName())).findFirst(); assertTrue(ring.isPresent()); CharMetric ringCharMetric = ring.get(); assertEquals(333f, ringCharMetric.getWx(), 0f); @@ -215,8 +168,7 @@ private void checkHelveticaCharMetrics(List charMetrics) assertNull(ringCharMetric.getVv()); } - private void checkHelveticaFontMetrics(FontMetrics fontMetrics) - { + private void checkHelveticaFontMetrics(FontMetrics fontMetrics) { assertEquals(4.1f, fontMetrics.getAFMVersion(), 0f); assertEquals("Helvetica", fontMetrics.getFontName()); assertEquals("Helvetica", fontMetrics.getFullName()); @@ -254,8 +206,7 @@ private void checkHelveticaFontMetrics(FontMetrics fontMetrics) assertFalse(fontMetrics.getIsFixedPitch()); } - private void checkBBox(BoundingBox bBox, float lowerX, float lowerY, float upperX, float upperY) - { + private void checkBBox(BoundingBox bBox, float lowerX, float lowerY, float upperX, float upperY) { assertNotNull(bBox); assertEquals(lowerX, bBox.getLowerLeftX(), 0f); assertEquals(lowerY, bBox.getLowerLeftY(), 0f); @@ -264,8 +215,7 @@ private void checkBBox(BoundingBox bBox, float lowerX, float lowerY, float upper } private void checkKernPair(List kernPairs, String firstKernChar, - String secondKernChar, float x, float y) - { + String secondKernChar, float x, float y) { Optional kernPair = kernPairs.stream() // .filter(k -> firstKernChar.equals(k.getFirstKernCharacter())) // .filter(k -> secondKernChar.equals(k.getSecondKernCharacter())) // @@ -273,6 +223,5 @@ private void checkKernPair(List kernPairs, String firstKernChar, assertTrue(kernPair.isPresent()); assertEquals(x, kernPair.get().getX(), 0f); assertEquals(y, kernPair.get().getY(), 0f); - } } diff --git a/fontbox/src/test/java/org/apache/fontbox/cff/CFFParserTest.java b/fontbox/src/test/java/org/apache/fontbox/cff/CFFParserTest.java index a0f3ea55a1b..6ae316e41f4 100644 --- a/fontbox/src/test/java/org/apache/fontbox/cff/CFFParserTest.java +++ b/fontbox/src/test/java/org/apache/fontbox/cff/CFFParserTest.java @@ -15,13 +15,13 @@ */ package org.apache.fontbox.cff; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; -import java.util.Arrays; import java.util.List; import org.apache.fontbox.util.BoundingBox; @@ -40,8 +40,8 @@ class CFFParserTest @BeforeAll static void loadCFFFont() throws IOException { - List fonts = readFont("target/fonts/SourceSansProBold.otf"); - testCFFType1Font = (CFFType1Font) fonts.get(0); + List fonts = readFont(); + testCFFType1Font = (CFFType1Font) fonts.getFirst(); } @Test @@ -106,8 +106,7 @@ void voidEncoding() { CFFEncoding encoding = testCFFType1Font.getEncoding(); assertNotNull(encoding, "Encoding must not be null"); - assertTrue(encoding instanceof CFFStandardEncoding, - "Encoding is not an instance of CFFStandardEncoding"); + assertInstanceOf(CFFStandardEncoding.class, encoding, "Encoding is not an instance of CFFStandardEncoding"); } @Test @@ -117,25 +116,13 @@ void testCharStringBytess() assertFalse(charStringBytes.isEmpty()); assertEquals(824, testCFFType1Font.getNumCharStrings()); // check some randomly chosen values - assertTrue( - Arrays.equals(new byte[] { -4, 15, 14 }, charStringBytes.get(1)), // - "Other char strings byte values than expected"); - assertTrue( - Arrays.equals(new byte[] { 72, 29, -13, 29, -9, -74, -9, 43, 3, 33, 29, 14 }, - charStringBytes.get(16)), // - "Other char strings byte values than expected"); - assertTrue( - Arrays.equals(new byte[] { -41, 88, 29, -47, -9, 12, 1, -123, 10, 3, 35, 29, -9, - -50, -9, 62, -9, 3, 10, 85, -56, 61, 10 }, charStringBytes.get(195)), // - "Other char strings byte values than expected"); - assertTrue( - Arrays.equals(new byte[] { -5, -69, -61, -8, 28, 1, -9, 57, -39, -65, 29, 14 }, - charStringBytes.get(525)), // - "Other char strings byte values than expected"); - assertTrue( - Arrays.equals(new byte[] { 107, -48, 10, -9, 20, -9, 123, 3, -9, -112, -8, -46, 21, - -10, 115, 10 }, charStringBytes.get(738)), // - "Other char strings byte values than expected"); + assertArrayEquals(new byte[]{-4, 15, 14}, charStringBytes.get(1), "Other char strings byte values than expected"); + assertArrayEquals(new byte[]{72, 29, -13, 29, -9, -74, -9, 43, 3, 33, 29, 14}, charStringBytes.get(16), "Other char strings byte values than expected"); + assertArrayEquals(new byte[]{-41, 88, 29, -47, -9, 12, 1, -123, 10, 3, 35, 29, -9, + -50, -9, 62, -9, 3, 10, 85, -56, 61, 10}, charStringBytes.get(195), "Other char strings byte values than expected"); + assertArrayEquals(new byte[]{-5, -69, -61, -8, 28, 1, -9, 57, -39, -65, 29, 14}, charStringBytes.get(525), "Other char strings byte values than expected"); + assertArrayEquals(new byte[]{107, -48, 10, -9, 20, -9, 123, 3, -9, -112, -8, -46, 21, + -10, 115, 10}, charStringBytes.get(738), "Other char strings byte values than expected"); } @Test @@ -145,28 +132,19 @@ void testGlobalSubrIndex() assertFalse(globalSubrIndex.isEmpty()); assertEquals(278, globalSubrIndex.size()); // check some randomly chosen values - assertTrue( - Arrays.equals(new byte[] { 21, -70, -83, -85, -72, -72, 105, -85, 92, 91, 105, 107, - 10, -83, -9, 62, 10 }, globalSubrIndex.get(12)), // - "Other global subr index values than expected"); - assertTrue( - Arrays.equals(new byte[] { 58, 122, 29, -5, 48, 6, 11 }, globalSubrIndex.get(120)), // - "Other global subr index values than expected"); - assertTrue( - Arrays.equals(new byte[] { 68, 80, 29, -45, -9, 16, -8, -92, 119, 11 }, - globalSubrIndex.get(253)), // - "Other global subr index values than expected"); + assertArrayEquals(new byte[]{21, -70, -83, -85, -72, -72, 105, -85, 92, 91, 105, 107, + 10, -83, -9, 62, 10}, globalSubrIndex.get(12), "Other global subr index values than expected"); + assertArrayEquals(new byte[]{58, 122, 29, -5, 48, 6, 11}, globalSubrIndex.get(120), "Other global subr index values than expected"); + assertArrayEquals(new byte[]{68, 80, 29, -45, -9, 16, -8, -92, 119, 11}, globalSubrIndex.get(253), "Other global subr index values than expected"); } /** * PDFBOX-4038: Test whether BlueValues and other delta encoded lists are read correctly. The * test file is from FOP-2432. * - * @throws IOException */ @Test - void testDeltaLists() throws IOException - { + void testDeltaLists() { @SuppressWarnings("unchecked") List blues = (List) testCFFType1Font.getPrivateDict().get("BlueValues"); @@ -201,9 +179,9 @@ void testDeltaLists() throws IOException new int[]{146, 150}, stemSnapV); } - private static List readFont(String filename) throws IOException + private static List readFont() throws IOException { - RandomAccessReadBufferedFile randomAccessRead = new RandomAccessReadBufferedFile(filename); + RandomAccessReadBufferedFile randomAccessRead = new RandomAccessReadBufferedFile("target/fonts/SourceSansProBold.otf"); CFFParser parser = new CFFParser(); return parser.parse(randomAccessRead); } @@ -225,5 +203,4 @@ private void assertNumberList(String message, float[] expected, List fou assertEquals(expected[i], found.get(i).floatValue(), message); } } - } diff --git a/fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java b/fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java index 414b3175a41..2d36c9954dc 100644 --- a/fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java +++ b/fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java @@ -35,7 +35,6 @@ class PfbParserTest /** * Test parsing a PFB font. * - * @throws IOException */ @Test void testPfb() throws IOException @@ -50,11 +49,11 @@ void testPfb() throws IOException Assertions.assertEquals("Open Sans Regular", font.getFullName()); Assertions.assertEquals("Open Sans", font.getFamilyName()); Assertions.assertEquals("Digitized data copyright (c) 2010-2011, Google Corporation.", font.getNotice()); - Assertions.assertEquals(false, font.isFixedPitch()); - Assertions.assertEquals(false, font.isForceBold()); + Assertions.assertFalse(font.isFixedPitch()); + Assertions.assertFalse(font.isForceBold()); Assertions.assertEquals(0, font.getItalicAngle()); Assertions.assertEquals("Book", font.getWeight()); - Assertions.assertTrue(font.getEncoding() instanceof BuiltInEncoding); + Assertions.assertInstanceOf(BuiltInEncoding.class, font.getEncoding()); Assertions.assertEquals(4498, font.getASCIISegment().length); Assertions.assertEquals(95911, font.getBinarySegment().length); Assertions.assertEquals(938, font.getCharStringsDict().size()); @@ -68,7 +67,6 @@ void testPfb() throws IOException /** * PDFBOX-5713: font with several binary segments. * - * @throws IOException */ @Test void testPfbPDFBox5713() throws IOException @@ -83,11 +81,11 @@ void testPfbPDFBox5713() throws IOException Assertions.assertEquals("DejaVu Serif Condensed", font.getFullName()); Assertions.assertEquals("DejaVu Serif Condensed", font.getFamilyName()); Assertions.assertEquals("Copyright [c] 2003 by Bitstream, Inc. All Rights Reserved.", font.getNotice()); - Assertions.assertEquals(false, font.isFixedPitch()); - Assertions.assertEquals(false, font.isForceBold()); + Assertions.assertFalse(font.isFixedPitch()); + Assertions.assertFalse(font.isForceBold()); Assertions.assertEquals(0, font.getItalicAngle()); Assertions.assertEquals("Book", font.getWeight()); - Assertions.assertTrue(font.getEncoding() instanceof BuiltInEncoding); + Assertions.assertInstanceOf(BuiltInEncoding.class, font.getEncoding()); Assertions.assertEquals(5959, font.getASCIISegment().length); Assertions.assertEquals(1056090, font.getBinarySegment().length); Assertions.assertEquals(3399, font.getCharStringsDict().size()); diff --git a/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java b/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java index 8d903d7c4a5..05490dcec77 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java @@ -16,7 +16,7 @@ package org.apache.pdfbox.multipdf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import java.awt.Color; import java.io.ByteArrayOutputStream; @@ -45,7 +45,6 @@ class PDFCloneUtilityTest /** * original (minimal) test from PDFBOX-2052. * - * @throws IOException */ @Test void testClonePDFWithCosArrayStream() throws IOException @@ -65,7 +64,6 @@ void testClonePDFWithCosArrayStream() throws IOException /** * broader test that saves to a real PDF document. * - * @throws IOException */ @Test void testClonePDFWithCosArrayStream2() throws IOException @@ -118,7 +116,6 @@ void testClonePDFWithCosArrayStream2() throws IOException * PDFBOX-4814: this tests merging a direct and an indirect COSDictionary, when "target" is * indirect in cloneMerge(). * - * @throws IOException */ @Test void testDirectIndirect() throws IOException @@ -133,8 +130,8 @@ void testDirectIndirect() throws IOException { PDFMergerUtility merger = new PDFMergerUtility(); // The OCProperties is a direct object here, but gets saved as an indirect object. - assertTrue(doc1.getDocumentCatalog().getCOSObject().getItem(COSName.OCPROPERTIES) instanceof COSDictionary); - assertTrue(doc2.getDocumentCatalog().getCOSObject().getItem(COSName.OCPROPERTIES) instanceof COSObject); + assertInstanceOf(COSDictionary.class, doc1.getDocumentCatalog().getCOSObject().getItem(COSName.OCPROPERTIES)); + assertInstanceOf(COSObject.class, doc2.getDocumentCatalog().getCOSObject().getItem(COSName.OCPROPERTIES)); merger.appendDocument(doc2, doc1); assertEquals(2, doc2.getNumberOfPages()); } diff --git a/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java b/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java index 13cc06ff026..b5be39d5236 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java @@ -152,7 +152,6 @@ void testJpegCcitt() throws IOException /** * PDFBOX-3972: Test that OpenAction page destination isn't lost after merge. * - * @throws IOException */ @Test void testPDFMergerOpenAction() throws IOException @@ -822,20 +821,19 @@ private class ElementCounter void walk(COSBase base) { - if (base instanceof COSArray) + if (base instanceof COSArray array) { - for (COSBase base2 : (COSArray) base) + for (COSBase base2 : array) { - if (base2 instanceof COSObject) + if (base2 instanceof COSObject object) { - base2 = ((COSObject) base2).getObject(); + base2 = object.getObject(); } walk(base2); } } - else if (base instanceof COSDictionary) + else if (base instanceof COSDictionary kdict) { - COSDictionary kdict = (COSDictionary) base; if (kdict.containsKey(COSName.PG)) { ++cnt; @@ -1023,7 +1021,7 @@ void testSplitWithStructureTree() throws IOException splitter.setSplitAtPage(2); List splitResult = splitter.split(doc); assertEquals(1, splitResult.size()); - try (PDDocument dstDoc = splitResult.get(0)) + try (PDDocument dstDoc = splitResult.getFirst()) { assertEquals(2, dstDoc.getNumberOfPages()); checkForPageOrphans(dstDoc); @@ -1048,7 +1046,7 @@ void testSplitWithStructureTreeAndDestinations() throws IOException splitter.setSplitAtPage(2); List splitResult = splitter.split(doc); assertEquals(1, splitResult.size()); - try (PDDocument dstDoc = splitResult.get(0)) + try (PDDocument dstDoc = splitResult.getFirst()) { assertEquals(2, dstDoc.getNumberOfPages()); checkForPageOrphans(dstDoc); @@ -1066,16 +1064,11 @@ void testSplitWithStructureTreeAndDestinations() throws IOException PDAnnotationLink link3 = (PDAnnotationLink) annotations.get(2); PDAnnotationLink link4 = (PDAnnotationLink) annotations.get(3); PDAnnotationLink link5 = (PDAnnotationLink) annotations.get(4); - PDPageDestination pd1 = - (PDPageDestination) ((PDActionGoTo) link1.getAction()).getDestination(); - PDPageDestination pd2 = - (PDPageDestination) ((PDActionGoTo) link2.getAction()).getDestination(); - PDPageDestination pd3 = - (PDPageDestination) ((PDActionGoTo) link3.getAction()).getDestination(); - PDPageDestination pd4 = - (PDPageDestination) ((PDActionGoTo) link4.getAction()).getDestination(); - PDPageDestination pd5 = - (PDPageDestination) ((PDActionGoTo) link5.getAction()).getDestination(); + PDPageDestination pd1 = (PDPageDestination) ((PDActionGoTo) link1.getAction()).getDestination(); + PDPageDestination pd2 = (PDPageDestination) ((PDActionGoTo) link2.getAction()).getDestination(); + PDPageDestination pd3 = (PDPageDestination) ((PDActionGoTo) link3.getAction()).getDestination(); + PDPageDestination pd4 = (PDPageDestination) ((PDActionGoTo) link4.getAction()).getDestination(); + PDPageDestination pd5 = (PDPageDestination) ((PDActionGoTo) link5.getAction()).getDestination(); PDPageTree pageTree = dstDoc.getPages(); assertEquals(0, pageTree.indexOf(pd1.getPage())); assertEquals(1, pageTree.indexOf(pd2.getPage())); @@ -1090,7 +1083,6 @@ void testSplitWithStructureTreeAndDestinations() throws IOException * Check for the bug that happened in PDFBOX-5792, where a destination was outside a target * document and hit an NPE in the next call of Splitter.fixDestinations(). * - * @throws IOException */ @Test void testSinglePageSplit() throws IOException @@ -1109,7 +1101,7 @@ void testSinglePageSplit() throws IOException { PDAnnotationLink link = (PDAnnotationLink) ann; PDActionGoTo action = (PDActionGoTo) link.getAction(); - PDPageDestination destination = (PDPageDestination) ((PDActionGoTo) action).getDestination(); + PDPageDestination destination = (PDPageDestination) action.getDestination(); assertNull(destination.getPage()); } } @@ -1152,7 +1144,7 @@ void testSplitWithPopupAnnotations() throws IOException List annotations; PDAnnotationText annotationText3; PDAnnotationPopup annotationPopup4; - try (PDDocument dstDoc = splitResult.get(0)) + try (PDDocument dstDoc = splitResult.getFirst()) { checkForPageOrphans(dstDoc); assertEquals(1, dstDoc.getNumberOfPages()); diff --git a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationTest.java b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationTest.java index 0baf8be62d1..f19a4514012 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationTest.java @@ -52,9 +52,8 @@ void loadXFDFAnnotations() throws IOException, URISyntaxException boolean testedPDFBox4345andPDFBox3646 = false; for (FDFAnnotation ann : fdfAnnots) { - if (ann instanceof FDFAnnotationFreeText) + if (ann instanceof FDFAnnotationFreeText annotationFreeText) { - FDFAnnotationFreeText annotationFreeText = (FDFAnnotationFreeText) ann; if ("P&1 P&2 P&3".equals(annotationFreeText.getContents())) { testedPDFBox4345andPDFBox3646 = true; diff --git a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/PNGConverterTest.java b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/PNGConverterTest.java index 25398d845b5..fbad7e0c27e 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/PNGConverterTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/PNGConverterTest.java @@ -185,10 +185,9 @@ private void checkImageConvert(String name) throws IOException assertNotNull(pdImageXObject); ICC_Profile imageProfile = null; - if (pdImageXObject.getColorSpace() instanceof PDICCBased) + if (pdImageXObject.getColorSpace() instanceof PDICCBased iccColorSpace) { // Make sure that ICC profile is a valid one - PDICCBased iccColorSpace = (PDICCBased) pdImageXObject.getColorSpace(); imageProfile = ICC_Profile.getInstance(iccColorSpace.getPDStream().toByteArray()); } PDPage page = new PDPage(); diff --git a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/annotation/AppearanceGenerationTest.java b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/annotation/AppearanceGenerationTest.java index 5e95ab658fd..16259a397ec 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/annotation/AppearanceGenerationTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/annotation/AppearanceGenerationTest.java @@ -68,10 +68,7 @@ class AppearanceGenerationTest // values. // TODO: revisit that number as our code improves private static final float DELTA = 3e-3f; - - // the location of the annotation - static PDRectangle rectangle; - + private PDDocument document; private static final File IN_DIR = new File("src/test/resources/org/apache/pdfbox/pdmodel/interactive/annotation"); @@ -118,15 +115,14 @@ void rectangleFullStrokeNoFill() throws IOException assertEquals(tokenForOriginal.getClass().getName(), tokenForPdfbox.getClass().getName(), "The tokens should have the same type"); - if (tokenForOriginal instanceof Operator) + if (tokenForOriginal instanceof Operator operator) { - assertEquals(((Operator) tokenForOriginal).getName(), - ((Operator) tokenForPdfbox).getName(), + assertEquals(operator.getName(), ((Operator) tokenForPdfbox).getName(), "The operator generated by PDFBox should be the same Operator"); - } else if (tokenForOriginal instanceof COSFloat) + } else if (tokenForOriginal instanceof COSFloat cosFloat) { assertTrue( - Math.abs(((COSFloat) tokenForOriginal).floatValue() + Math.abs(cosFloat.floatValue() - ((COSFloat) tokenForPdfbox).floatValue()) < DELTA, "The difference between the numbers should be smaller than " + DELTA); } @@ -137,8 +133,7 @@ void rectangleFullStrokeNoFill() throws IOException File file = new File(OUT_DIR, NAME_OF_PDF + "-newAP.pdf"); document.save(file); } - - + // we should render similar to Adobe Reader using the original file @Test void renderTest() throws IOException @@ -158,7 +153,6 @@ void renderTest() throws IOException * CreateSimpleForm example where the field is tiny and has a 0 (variable) font size and no * content. * - * @throws IOException */ @Test void testTinyHorizontalFieldWith0FontSize() throws IOException @@ -183,7 +177,7 @@ void testTinyHorizontalFieldWith0FontSize() throws IOException textBox.setDefaultAppearance(defaultAppearanceString); acroForm.getFields().add(textBox); - PDAnnotationWidget widget = textBox.getWidgets().get(0); + PDAnnotationWidget widget = textBox.getWidgets().getFirst(); PDRectangle rect = new PDRectangle(50, 750, 1, 50); widget.setRectangle(rect); widget.setPage(page); diff --git a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java index 3133c40a2d1..5f6fcd2c918 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java @@ -78,8 +78,7 @@ public void setUp() throws IOException } @Test - void characterNUL() throws IOException - { + void characterNUL() { PDField field = acroForm.getField("pdfbox-nul"); assertThrows(IllegalArgumentException.class, () -> field.setValue("NUL\0NUL")); } @@ -131,9 +130,8 @@ public void tearDown() throws IOException private List getStringsFromStream(PDField field) throws IOException { - PDAnnotationWidget widget = field.getWidgets().get(0); - PDFStreamParser parser = new PDFStreamParser( - widget.getNormalAppearanceStream()); + PDAnnotationWidget widget = field.getWidgets().getFirst(); + PDFStreamParser parser = new PDFStreamParser(widget.getNormalAppearanceStream()); List tokens = parser.parse(); diff --git a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/MultilineFieldsTest.java b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/MultilineFieldsTest.java index 08a4c05e546..c6e1b50084b 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/MultilineFieldsTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/MultilineFieldsTest.java @@ -44,7 +44,6 @@ class MultilineFieldsTest private static final String TEST_VALUE = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " + "sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam"; - private PDDocument document; private PDAcroForm acroForm; @@ -159,22 +158,21 @@ void testMultilineBreak() throws IOException } } - private float getFontSizeFromAppearanceStream(PDField field) throws IOException { - PDAnnotationWidget widget = field.getWidgets().get(0); + PDAnnotationWidget widget = field.getWidgets().getFirst(); PDFStreamParser parser = new PDFStreamParser(widget.getNormalAppearanceStream()); Object token = parser.parseNextToken(); while (token != null) { - if (token instanceof COSName && ((COSName) token).getName().equals("Helv")) + if (token instanceof COSName name && name.getName().equals("Helv")) { token = parser.parseNextToken(); - if (token instanceof COSNumber) + if (token instanceof COSNumber number) { - return ((COSNumber) token).floatValue(); + return number.floatValue(); } } token = parser.parseNextToken(); @@ -184,7 +182,7 @@ private float getFontSizeFromAppearanceStream(PDField field) throws IOException private List getTextLinesFromAppearanceStream(PDField field) throws IOException { - PDAnnotationWidget widget = field.getWidgets().get(0); + PDAnnotationWidget widget = field.getWidgets().getFirst(); PDFStreamParser parser = new PDFStreamParser(widget.getNormalAppearanceStream()); Object token = parser.parseNextToken(); @@ -193,9 +191,9 @@ private List getTextLinesFromAppearanceStream(PDField field) throws IOEx while (token != null) { - if (token instanceof COSString) + if (token instanceof COSString string) { - lines.add(((COSString) token).getString()); + lines.add(string.getString()); } token = parser.parseNextToken(); } @@ -208,5 +206,4 @@ public void tearDown() throws IOException { document.close(); } - } diff --git a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java index 3a05129d68f..086ad72dc61 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -122,7 +123,6 @@ void testFlatten() throws IOException // don't fail, rendering is different on different systems, result must be viewed manually System.out.println("Rendering of " + file + " failed or is not identical to expected rendering in " + IN_DIR + " directory"); } - } /* @@ -216,8 +216,7 @@ void testDontAddMissingInformationOnDocumentLoad() System.err.println("Couldn't create test document, test skipped"); } } - - + /* * Test that we add missing ressouce information to an AcroForm * when accessing the AcroForm on the PD level @@ -259,7 +258,6 @@ void testAddMissingInformationOnAcroFormAccess() /** * PDFBOX-4235: a bad /DA string should not result in an NPE. * - * @throws IOException */ @Test void testBadDA() throws IOException @@ -340,8 +338,8 @@ void testAcroFormDefaultFonts() throws IOException assertNotNull(helv); assertNotNull(zadb); // make sure that font wasn't overwritten - assertTrue(helv instanceof PDType1Font); - assertTrue(zadb instanceof PDType1Font); + assertInstanceOf(PDType1Font.class, helv); + assertInstanceOf(PDType1Font.class, zadb); PDType1Font helvType1 = (PDType1Font) helv; PDType1Font zadbType1 = (PDType1Font) zadb; assertEquals(FontName.HELVETICA.getName(), helv.getName()); diff --git a/pom.xml b/pom.xml index 0608dd7e813..02b7586816b 100644 --- a/pom.xml +++ b/pom.xml @@ -68,8 +68,8 @@ --> - 11 - 11 + 21 + 21