1
+ package com .baeldung .detect ;
2
+
3
+ import org .apache .pdfbox .Loader ;
4
+ import org .apache .pdfbox .pdmodel .PDDocument ;
5
+ import org .apache .tika .Tika ;
6
+ import org .junit .jupiter .api .Test ;
7
+
8
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
9
+
10
+ import java .io .*;
11
+ import java .util .Objects ;
12
+
13
+ import com .itextpdf .commons .exceptions .ITextException ;
14
+ import com .itextpdf .kernel .pdf .PdfDocument ;
15
+ import com .itextpdf .kernel .pdf .PdfReader ;
16
+
17
+
18
+ public class PdfDetectUnitTest {
19
+
20
+ private static final File PDF_FILE = new File ("src/test/resources/input.pdf" );
21
+
22
+ @ Test
23
+ void whenDetectPdfByPdfBox_thenCorrect () {
24
+ boolean isPdf ;
25
+ try (PDDocument document = Loader .loadPDF (PDF_FILE )) {
26
+ isPdf = true ;
27
+ } catch (IOException ioe ) {
28
+ isPdf = false ;
29
+ }
30
+ assertTrue (isPdf );
31
+ }
32
+
33
+ @ Test
34
+ void whenDetectPdfByItext_thenCorrect () {
35
+ boolean isPdf ;
36
+ try (PdfDocument pdfDoc = new PdfDocument (new PdfReader (PDF_FILE ))) {
37
+ isPdf = true ;
38
+ } catch (ITextException | IOException e ) {
39
+ isPdf = false ;
40
+ }
41
+ assertTrue (isPdf );
42
+ }
43
+
44
+ @ Test
45
+ void whenDetectPdfByFileSignature_thenCorrect () throws IOException {
46
+ boolean isPdf = false ;
47
+ try (InputStream fis = new BufferedInputStream (new FileInputStream (PDF_FILE ))) {
48
+ byte [] bytes = new byte [5 ];
49
+ if (fis .read (bytes ) == 5 ) {
50
+ String header = new String (bytes );
51
+ isPdf = Objects .equals (header , "%PDF-" );
52
+ }
53
+ }
54
+ assertTrue (isPdf );
55
+ }
56
+
57
+ @ Test
58
+ void whenDetectPdfByTika_thenCorrect () throws IOException {
59
+ Tika tika = new Tika ();
60
+ boolean isPdf = Objects .equals (tika .detect (PDF_FILE ), "application/pdf" );
61
+ assertTrue (isPdf );
62
+ }
63
+
64
+ }
0 commit comments