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"));
|
}
|
}
|