Skip to content

Commit 11b9e43

Browse files
committed
PDFBOX-5660: small code improvements suggested by Jens Kaiser in pr 192
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1929975 13f79535-47bb-0310-9956-ffa450edef68
1 parent aa02c1b commit 11b9e43

File tree

7 files changed

+62
-104
lines changed

7 files changed

+62
-104
lines changed

fontbox/src/test/java/org/apache/fontbox/afm/AFMParserTest.java

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static org.junit.jupiter.api.Assertions.assertNotNull;
2323
import static org.junit.jupiter.api.Assertions.assertNull;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
25-
import static org.junit.jupiter.api.Assertions.fail;
2625

2726
import java.io.ByteArrayInputStream;
2827
import java.io.FileInputStream;
@@ -33,6 +32,8 @@
3332
import java.util.Optional;
3433

3534
import org.apache.fontbox.util.BoundingBox;
35+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
36+
import static org.junit.jupiter.api.Assertions.assertThrows;
3637
import org.junit.jupiter.api.Test;
3738

3839
/**
@@ -41,83 +42,49 @@
4142
*/
4243
class AFMParserTest
4344
{
45+
public static final String HELVETICA_AFM = "src/test/resources/afm/Helvetica.afm";
46+
4447
@Test
4548
void testStartFontMetrics() throws IOException
4649
{
47-
try
48-
{
49-
new AFMParser(new ByteArrayInputStream("huhu".getBytes(StandardCharsets.US_ASCII)))
50-
.parse();
51-
fail("The AFMParser should have thrown an IOException because of a missing "
52-
+ AFMParser.START_FONT_METRICS);
53-
}
54-
catch (IOException e)
55-
{
56-
// expected exception
57-
}
50+
assertThrows(IOException.class,
51+
() -> new AFMParser(new ByteArrayInputStream("huhu".getBytes(StandardCharsets.US_ASCII))).parse(),
52+
"The AFMParser should have thrown an IOException because of a missing " + AFMParser.START_FONT_METRICS);
5853
}
5954

6055
@Test
6156
void testEndFontMetrics() throws IOException
6257
{
63-
try (InputStream is = new FileInputStream("src/test/resources/afm/NoEndFontMetrics.afm"))
64-
{
65-
AFMParser parser = new AFMParser(is);
66-
try
67-
{
68-
parser.parse();
69-
fail("The AFMParser should have thrown an IOException because of a missing "
70-
+ AFMParser.END_FONT_METRICS);
71-
}
72-
catch (IOException e)
73-
{
74-
assertTrue(e.getMessage().contains("Unknown AFM key"));
75-
}
76-
}
58+
AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/NoEndFontMetrics.afm"));
59+
IOException e = assertThrows(IOException.class, parser::parse,
60+
"The AFMParser should have thrown an IOException because of a missing " + AFMParser.END_FONT_METRICS);
61+
assertTrue(e.getMessage().contains("Unknown AFM key"));
7762
}
7863

7964
@Test
8065
void testMalformedFloat() throws IOException
8166
{
82-
try (InputStream is = new FileInputStream("src/test/resources/afm/MalformedFloat.afm"))
83-
{
84-
AFMParser parser = new AFMParser(is);
85-
try
86-
{
87-
parser.parse();
88-
fail("The AFMParser should have thrown an IOException because of a malformed float value");
89-
}
90-
catch (IOException e)
91-
{
92-
assertTrue(e.getCause() instanceof NumberFormatException);
93-
assertTrue(e.getMessage().contains("4,1ab"));
94-
}
95-
}
67+
AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/MalformedFloat.afm"));
68+
IOException e = assertThrows(IOException.class, parser::parse,
69+
"The AFMParser should have thrown an IOException because of a malformed float value");
70+
assertInstanceOf(NumberFormatException.class, e.getCause());
71+
assertTrue(e.getMessage().contains("4,1ab"));
9672
}
9773

9874
@Test
9975
void testMalformedInteger() throws IOException
10076
{
101-
try (InputStream is = new FileInputStream("src/test/resources/afm/MalformedInteger.afm"))
102-
{
103-
try
104-
{
105-
AFMParser parser = new AFMParser(is);
106-
parser.parse();
107-
fail("The AFMParser should have thrown an IOException because of a malformed int value");
108-
}
109-
catch (IOException e)
110-
{
111-
assertTrue(e.getCause() instanceof NumberFormatException);
112-
assertTrue(e.getMessage().contains("3.4"));
113-
}
114-
}
77+
AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/MalformedInteger.afm"));
78+
IOException e = assertThrows(IOException.class, parser::parse,
79+
"The AFMParser should have thrown an IOException because of a malformed int value");
80+
assertInstanceOf(NumberFormatException.class, e.getCause());
81+
assertTrue(e.getMessage().contains("3.4"));
11582
}
11683

11784
@Test
11885
void testHelveticaFontMetrics() throws IOException
11986
{
120-
try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm"))
87+
try (InputStream is = new FileInputStream(HELVETICA_AFM))
12188
{
12289
AFMParser parser = new AFMParser(is);
12390
checkHelveticaFontMetrics(parser.parse());
@@ -127,7 +94,7 @@ void testHelveticaFontMetrics() throws IOException
12794
@Test
12895
void testHelveticaCharMetrics() throws IOException
12996
{
130-
try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm"))
97+
try (InputStream is = new FileInputStream(HELVETICA_AFM))
13198
{
13299
AFMParser parser = new AFMParser(is);
133100
FontMetrics fontMetrics = parser.parse();
@@ -140,7 +107,7 @@ void testHelveticaCharMetrics() throws IOException
140107
@Test
141108
void testHelveticaKernPairs() throws IOException
142109
{
143-
try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm"))
110+
try (InputStream is = new FileInputStream(HELVETICA_AFM))
144111
{
145112
AFMParser parser = new AFMParser(is);
146113
FontMetrics fontMetrics = parser.parse();
@@ -164,7 +131,7 @@ void testHelveticaKernPairs() throws IOException
164131
@Test
165132
void testHelveticaFontMetricsReducedDataset() throws IOException
166133
{
167-
try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm"))
134+
try (InputStream is = new FileInputStream(HELVETICA_AFM))
168135
{
169136
AFMParser parser = new AFMParser(is);
170137
checkHelveticaFontMetrics(parser.parse(true));
@@ -174,7 +141,7 @@ void testHelveticaFontMetricsReducedDataset() throws IOException
174141
@Test
175142
void testHelveticaCharMetricsReducedDataset() throws IOException
176143
{
177-
try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm"))
144+
try (InputStream is = new FileInputStream(HELVETICA_AFM))
178145
{
179146
AFMParser parser = new AFMParser(is);
180147
FontMetrics fontMetrics = parser.parse(true);
@@ -187,7 +154,7 @@ void testHelveticaCharMetricsReducedDataset() throws IOException
187154
@Test
188155
void testHelveticaKernPairsReducedDataset() throws IOException
189156
{
190-
try (InputStream is = new FileInputStream("src/test/resources/afm/Helvetica.afm"))
157+
try (InputStream is = new FileInputStream(HELVETICA_AFM))
191158
{
192159
AFMParser parser = new AFMParser(is);//
193160
FontMetrics fontMetrics = parser.parse(true);

fontbox/src/test/java/org/apache/fontbox/cff/CFFParserTest.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
*/
1616
package org.apache.fontbox.cff;
1717

18+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1819
import static org.junit.jupiter.api.Assertions.assertEquals;
1920
import static org.junit.jupiter.api.Assertions.assertFalse;
2021
import static org.junit.jupiter.api.Assertions.assertNotNull;
21-
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import java.io.IOException;
24-
import java.util.Arrays;
2524
import java.util.List;
2625
import java.util.concurrent.CountDownLatch;
2726
import java.util.concurrent.atomic.AtomicBoolean;
2827

2928
import org.apache.fontbox.util.BoundingBox;
3029
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
30+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
3131
import org.junit.jupiter.api.BeforeAll;
3232
import org.junit.jupiter.api.Test;
3333

@@ -108,7 +108,7 @@ void voidEncoding()
108108
{
109109
CFFEncoding encoding = testCFFType1Font.getEncoding();
110110
assertNotNull(encoding, "Encoding must not be null");
111-
assertTrue(encoding instanceof CFFStandardEncoding,
111+
assertInstanceOf(CFFStandardEncoding.class, encoding,
112112
"Encoding is not an instance of CFFStandardEncoding");
113113
}
114114

@@ -119,23 +119,19 @@ void testCharStringBytess()
119119
assertFalse(charStringBytes.isEmpty());
120120
assertEquals(824, testCFFType1Font.getNumCharStrings());
121121
// check some randomly chosen values
122-
assertTrue(Arrays.equals(new byte[] { -4, 15, 14 }, charStringBytes.get(1)), //
122+
assertArrayEquals(new byte[] { -4, 15, 14 }, charStringBytes.get(1), //
123123
"Other char strings byte values than expected");
124-
assertTrue(
125-
Arrays.equals(new byte[] { 72, 29, -13, 29, -9, -74, -9, 43, 3, 33, 29, 14 },
126-
charStringBytes.get(16)), //
124+
assertArrayEquals(new byte[] { 72, 29, -13, 29, -9, -74, -9, 43, 3, 33, 29, 14 },
125+
charStringBytes.get(16), //
127126
"Other char strings byte values than expected");
128-
assertTrue(
129-
Arrays.equals(new byte[] { -41, 88, 29, -47, -9, 12, 1, -123, 10, 3, 35, 29, -9,
130-
-50, -9, 62, -9, 3, 10, 85, -56, 61, 10 }, charStringBytes.get(195)), //
127+
assertArrayEquals(new byte[] { -41, 88, 29, -47, -9, 12, 1, -123, 10, 3, 35, 29, -9,
128+
-50, -9, 62, -9, 3, 10, 85, -56, 61, 10 }, charStringBytes.get(195), //
131129
"Other char strings byte values than expected");
132-
assertTrue(
133-
Arrays.equals(new byte[] { -5, -69, -61, -8, 28, 1, -9, 57, -39, -65, 29, 14 },
134-
charStringBytes.get(525)), //
130+
assertArrayEquals(new byte[] { -5, -69, -61, -8, 28, 1, -9, 57, -39, -65, 29, 14 },
131+
charStringBytes.get(525), //
135132
"Other char strings byte values than expected");
136-
assertTrue(
137-
Arrays.equals(new byte[] { 107, -48, 10, -9, 20, -9, 123, 3, -9, -112, -8, -46, 21,
138-
-10, 115, 10 }, charStringBytes.get(738)), //
133+
assertArrayEquals(new byte[] { 107, -48, 10, -9, 20, -9, 123, 3, -9, -112, -8, -46, 21,
134+
-10, 115, 10 }, charStringBytes.get(738), //
139135
"Other char strings byte values than expected");
140136
}
141137

@@ -146,16 +142,13 @@ void testGlobalSubrIndex()
146142
assertFalse(globalSubrIndex.isEmpty());
147143
assertEquals(278, globalSubrIndex.size());
148144
// check some randomly chosen values
149-
assertTrue(
150-
Arrays.equals(new byte[] { 21, -70, -83, -85, -72, -72, 105, -85, 92, 91, 105, 107,
151-
10, -83, -9, 62, 10 }, globalSubrIndex.get(12)), //
145+
assertArrayEquals(new byte[] { 21, -70, -83, -85, -72, -72, 105, -85, 92, 91, 105, 107,
146+
10, -83, -9, 62, 10 }, globalSubrIndex.get(12), //
152147
"Other global subr index values than expected");
153-
assertTrue(
154-
Arrays.equals(new byte[] { 58, 122, 29, -5, 48, 6, 11 }, globalSubrIndex.get(120)), //
148+
assertArrayEquals(new byte[] { 58, 122, 29, -5, 48, 6, 11 }, globalSubrIndex.get(120), //
155149
"Other global subr index values than expected");
156-
assertTrue(
157-
Arrays.equals(new byte[] { 68, 80, 29, -45, -9, 16, -8, -92, 119, 11 },
158-
globalSubrIndex.get(253)), //
150+
assertArrayEquals(new byte[] { 68, 80, 29, -45, -9, 16, -8, -92, 119, 11 },
151+
globalSubrIndex.get(253), //
159152
"Other global subr index values than expected");
160153
}
161154

fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ void testPfb() throws IOException
5050
Assertions.assertEquals("Open Sans Regular", font.getFullName());
5151
Assertions.assertEquals("Open Sans", font.getFamilyName());
5252
Assertions.assertEquals("Digitized data copyright (c) 2010-2011, Google Corporation.", font.getNotice());
53-
Assertions.assertEquals(false, font.isFixedPitch());
54-
Assertions.assertEquals(false, font.isForceBold());
53+
Assertions.assertFalse(font.isFixedPitch());
54+
Assertions.assertFalse(font.isForceBold());
5555
Assertions.assertEquals(0, font.getItalicAngle());
5656
Assertions.assertEquals("Book", font.getWeight());
57-
Assertions.assertTrue(font.getEncoding() instanceof BuiltInEncoding);
57+
Assertions.assertInstanceOf(BuiltInEncoding.class, font.getEncoding());
5858
Assertions.assertEquals(4498, font.getASCIISegment().length);
5959
Assertions.assertEquals(95911, font.getBinarySegment().length);
6060
Assertions.assertEquals(938, font.getCharStringsDict().size());
@@ -83,11 +83,11 @@ void testPfbPDFBox5713() throws IOException
8383
Assertions.assertEquals("DejaVu Serif Condensed", font.getFullName());
8484
Assertions.assertEquals("DejaVu Serif Condensed", font.getFamilyName());
8585
Assertions.assertEquals("Copyright [c] 2003 by Bitstream, Inc. All Rights Reserved.", font.getNotice());
86-
Assertions.assertEquals(false, font.isFixedPitch());
87-
Assertions.assertEquals(false, font.isForceBold());
86+
Assertions.assertFalse(font.isFixedPitch());
87+
Assertions.assertFalse(font.isForceBold());
8888
Assertions.assertEquals(0, font.getItalicAngle());
8989
Assertions.assertEquals("Book", font.getWeight());
90-
Assertions.assertTrue(font.getEncoding() instanceof BuiltInEncoding);
90+
Assertions.assertInstanceOf(BuiltInEncoding.class, font.getEncoding());
9191
Assertions.assertEquals(5959, font.getASCIISegment().length);
9292
Assertions.assertEquals(1056090, font.getBinarySegment().length);
9393
Assertions.assertEquals(3399, font.getCharStringsDict().size());

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.apache.pdfbox.multipdf;
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2020

2121
import java.awt.Color;
2222
import java.io.ByteArrayOutputStream;
@@ -156,11 +156,11 @@ void testDirectIndirect() throws IOException
156156
{
157157
PDFMergerUtility merger = new PDFMergerUtility();
158158
// The OCProperties is a direct object here, but gets saved as an indirect object.
159-
assertTrue(doc1.getDocumentCatalog().getCOSObject().getItem(COSName.OCPROPERTIES) instanceof COSDictionary);
160-
assertTrue(doc2.getDocumentCatalog().getCOSObject().getItem(COSName.OCPROPERTIES) instanceof COSObject);
159+
assertInstanceOf(COSDictionary.class, doc1.getDocumentCatalog().getCOSObject().getItem(COSName.OCPROPERTIES));
160+
assertInstanceOf(COSObject.class, doc2.getDocumentCatalog().getCOSObject().getItem(COSName.OCPROPERTIES));
161161
merger.appendDocument(doc2, doc1);
162162
assertEquals(2, doc2.getNumberOfPages());
163163
}
164164
}
165165
}
166-
}
166+
}

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/annotation/AppearanceGenerationTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ class AppearanceGenerationTest
6868
// values.
6969
// TODO: revisit that number as our code improves
7070
private static final float DELTA = 3e-3f;
71-
72-
// the location of the annotation
73-
static PDRectangle rectangle;
74-
71+
7572
private PDDocument document;
7673

7774
private static final File IN_DIR = new File("src/test/resources/org/apache/pdfbox/pdmodel/interactive/annotation");
@@ -123,7 +120,8 @@ void rectangleFullStrokeNoFill() throws IOException
123120
assertEquals(((Operator) tokenForOriginal).getName(),
124121
((Operator) tokenForPdfbox).getName(),
125122
"The operator generated by PDFBox should be the same Operator");
126-
} else if (tokenForOriginal instanceof COSFloat)
123+
}
124+
else if (tokenForOriginal instanceof COSFloat)
127125
{
128126
assertTrue(
129127
Math.abs(((COSFloat) tokenForOriginal).floatValue()

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void setUp() throws IOException
7878
}
7979

8080
@Test
81-
void characterNUL() throws IOException
81+
void characterNUL()
8282
{
8383
PDField field = acroForm.getField("pdfbox-nul");
8484
assertThrows(IllegalArgumentException.class, () -> field.setValue("NUL\0NUL"));
@@ -132,8 +132,7 @@ void tearDown() throws IOException
132132
private List<String> getStringsFromStream(PDField field) throws IOException
133133
{
134134
PDAnnotationWidget widget = field.getWidgets().get(0);
135-
PDFStreamParser parser = new PDFStreamParser(
136-
widget.getNormalAppearanceStream());
135+
PDFStreamParser parser = new PDFStreamParser(widget.getNormalAppearanceStream());
137136

138137
List<Object> tokens = parser.parse();
139138

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2122
import static org.junit.jupiter.api.Assertions.assertNotNull;
2223
import static org.junit.jupiter.api.Assertions.assertNull;
2324
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -342,8 +343,8 @@ void testAcroFormDefaultFonts() throws IOException
342343
assertNotNull(helv);
343344
assertNotNull(zadb);
344345
// make sure that font wasn't overwritten
345-
assertTrue(helv instanceof PDType1Font);
346-
assertTrue(zadb instanceof PDType1Font);
346+
assertInstanceOf(PDType1Font.class, helv);
347+
assertInstanceOf(PDType1Font.class, zadb);
347348
PDType1Font helvType1 = (PDType1Font) helv;
348349
PDType1Font zadbType1 = (PDType1Font) zadb;
349350
assertEquals(FontName.HELVETICA.getName(), helv.getName());

0 commit comments

Comments
 (0)