刘光辉
4 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package jnpf.dmsService;
 
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
 
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
 
class DmsDocumentPageCountServiceTest {
    @TempDir
    Path directory;
 
    private final DmsDocumentPageCountService service = new DmsDocumentPageCountService();
 
    @Test
    void readsPdfPageCount() throws Exception {
        Path file = directory.resolve("manual.pdf");
        try (PDDocument document = new PDDocument()) {
            document.addPage(new PDPage());
            document.addPage(new PDPage());
            document.addPage(new PDPage());
            document.save(file.toFile());
        }
 
        assertEquals(3, service.getPageCount(file, null));
    }
 
    @Test
    void neverTreatsSavedDocxMetadataAsActualPageCount() throws Exception {
        Path file = directory.resolve("manual.docx");
        try (XWPFDocument document = new XWPFDocument();
             OutputStream output = Files.newOutputStream(file)) {
            document.createParagraph().createRun().setText("Document body");
            document.getProperties().getExtendedProperties().setPages(20);
            document.write(output);
        }
 
        IOException exception = assertThrows(IOException.class,
                () -> service.getPageCount(file, "MANUAL.DOCX"));
        assertTrue(exception.getMessage().contains("OnlyOffice"));
    }
 
    @Test
    void rejectsDocxWithoutSavedPageCount() throws Exception {
        Path file = directory.resolve("draft.docx");
        try (XWPFDocument document = new XWPFDocument();
             OutputStream output = Files.newOutputStream(file)) {
            document.createParagraph().createRun().setText("Draft");
            document.write(output);
        }
 
        IOException exception = assertThrows(IOException.class,
                () -> service.getPageCount(file, file.getFileName().toString()));
 
        assertTrue(exception.getMessage().contains("OnlyOffice"));
    }
 
    @Test
    void rejectsUnsupportedFileType() throws Exception {
        Path file = Files.write(directory.resolve("manual.txt"), new byte[]{1});
 
        IOException exception = assertThrows(IOException.class,
                () -> service.getPageCount(file, file.getFileName().toString()));
 
        assertTrue(exception.getMessage().contains("仅支持 doc、docx、pdf"));
    }
}