Skip to content

Commit d416a69

Browse files
committed
Added some special characters testing
1 parent fe26d63 commit d416a69

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

code-samples-pdfbox2/src/test/java/test/org/fugerit/java/code/samples/pdfbox2/TestSpecialCharacters.java

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import org.junit.jupiter.api.Assertions;
88
import org.junit.jupiter.api.Test;
99

10-
import java.io.File;
11-
import java.io.FileOutputStream;
12-
import java.io.IOException;
13-
import java.io.InputStream;
10+
import java.io.*;
1411

1512
/*
1613
* Proof of Concept to handle special characters like U+2002 ('enspace')
@@ -26,30 +23,62 @@
2623
@Slf4j
2724
class TestSpecialCharacters {
2825

29-
private static final String TEST_LINE = "Row with special character :  ."; // test line with U+2002 ('enspace')
26+
private static final String TEST_LINE = "Row with special character : %s."; // test line with U+2002 ('enspace')
27+
28+
private static final String[] SPECIAL_CHARACTERS = {
29+
"\u2002", // 'enspace'
30+
"\u2010", // 'hyphentwo'
31+
"\u2033", // 'second'
32+
"\u03BC", // 'mugreek'
33+
"\u039C", // 'Mu'
34+
"\u2212", // 'minus'
35+
"\u0141" , // 'Lslash'
36+
"\u2103", // 'centigrade'
37+
};
3038

3139
@Test
3240
void testWithPdfBoxFontHelvetica() throws IOException {
33-
try (FileOutputStream fos = new FileOutputStream( "target/test_font_from_pdfbox.pdf" )) {
34-
SpecialCharacters sp = new SpecialCharacters();
35-
Assertions.assertThrows( IllegalArgumentException.class, () -> {
36-
sp.generatePDF( fos, PDType1Font.HELVETICA, TEST_LINE );
37-
});
41+
SpecialCharacters sp = new SpecialCharacters();
42+
for ( int k=0; k<SPECIAL_CHARACTERS.length; k++ ) {
43+
String special = SPECIAL_CHARACTERS[k];
44+
String line = String.format( TEST_LINE, special );
45+
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
46+
log.info( "testWithPdfBoxFontHelvetica index:{}, char:{}, line:{}", k, special, line );
47+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
48+
sp.generatePDF(os, PDType1Font.HELVETICA, line);
49+
});
50+
}
3851
}
39-
4052
}
4153

4254
@Test
4355
void testWithExternalHelvetica() throws IOException {
44-
File pdfFile = new File( "target/test_font_external.pdf" );
45-
log.info( "pdfFile: {}, delete: {}", pdfFile, pdfFile.delete() );
46-
Assertions.assertFalse( pdfFile.exists() );
47-
try ( InputStream fontIS = ClassHelper.loadFromClassLoader( "font/Helvetica.ttf" );
48-
FileOutputStream fos = new FileOutputStream( pdfFile ) ) {
56+
// some characters are still not available on all font, for instance in this example :
57+
// "\uFFFD", // '.notdef'
58+
// "\u25AA", // 'H18543'
59+
SpecialCharacters sp = new SpecialCharacters();
60+
for ( int k=0; k<SPECIAL_CHARACTERS.length; k++ ) {
61+
String special = SPECIAL_CHARACTERS[k];
62+
String line = String.format( TEST_LINE, special );
63+
File pdfFile = new File( String.format( "target/test_font_external_%s.pdf", k ) );
64+
log.info( "pdfFile: {}, delete: {}", pdfFile, pdfFile.delete() );
65+
Assertions.assertFalse( pdfFile.exists() );
66+
try (InputStream fontIS = ClassHelper.loadFromClassLoader( "font/Helvetica.ttf" );
67+
OutputStream os = new FileOutputStream( pdfFile ) ) {
68+
log.info( "testWithExternalHelvetica index:{}, char:{}, line:{}", k, special, line );
69+
sp.generatePDF(os, fontIS, line);
70+
}
71+
Assertions.assertTrue( pdfFile.exists() );
72+
}
73+
}
74+
75+
@Test
76+
void testWithPdfBoxFontHelveticaNormalLine() throws IOException {
77+
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
4978
SpecialCharacters sp = new SpecialCharacters();
50-
sp.generatePDF( fos, fontIS, TEST_LINE );
79+
sp.generatePDF( os, PDType1Font.HELVETICA, "Normal line" );
80+
Assertions.assertNotEquals( 0, os.toByteArray().length );
5181
}
52-
Assertions.assertTrue( pdfFile.exists() );
5382
}
5483

5584
}

0 commit comments

Comments
 (0)