Fehlerpfade im PDF-Extraktionsadapter gezielt getestet
This commit is contained in:
@@ -14,6 +14,9 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -64,6 +67,54 @@ class PdfTextExtractionPortAdapterTest {
|
||||
assertTrue(error.errorMessage().contains("not found"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnreadableFileReturnsTechnicalError() throws Exception {
|
||||
// Create a PDF file
|
||||
Path pdfFile = tempDir.resolve("unreadable.pdf");
|
||||
createSimplePdf(pdfFile);
|
||||
|
||||
// Make file unreadable (Unix-like systems)
|
||||
try {
|
||||
Set<PosixFilePermission> perms = new HashSet<>();
|
||||
Files.setPosixFilePermissions(pdfFile, perms);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// On Windows, we can't easily make a file unreadable while keeping it existing
|
||||
// So we'll skip this test on Windows by just returning early
|
||||
return;
|
||||
}
|
||||
|
||||
SourceDocumentCandidate candidate = new SourceDocumentCandidate(
|
||||
"unreadable.pdf",
|
||||
Files.size(pdfFile),
|
||||
new SourceDocumentLocator(pdfFile.toAbsolutePath().toString())
|
||||
);
|
||||
|
||||
PdfExtractionResult result = adapter.extractTextAndPageCount(candidate);
|
||||
|
||||
assertInstanceOf(PdfExtractionTechnicalError.class, result);
|
||||
PdfExtractionTechnicalError error = (PdfExtractionTechnicalError) result;
|
||||
assertTrue(error.errorMessage().contains("not readable"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testZeroPagePdfReturnsTechnicalError() throws Exception {
|
||||
// Create a PDF with zero pages
|
||||
Path pdfFile = tempDir.resolve("zero-pages.pdf");
|
||||
createZeroPagePdf(pdfFile);
|
||||
|
||||
SourceDocumentCandidate candidate = new SourceDocumentCandidate(
|
||||
"zero-pages.pdf",
|
||||
Files.size(pdfFile),
|
||||
new SourceDocumentLocator(pdfFile.toAbsolutePath().toString())
|
||||
);
|
||||
|
||||
PdfExtractionResult result = adapter.extractTextAndPageCount(candidate);
|
||||
|
||||
assertInstanceOf(PdfExtractionTechnicalError.class, result);
|
||||
PdfExtractionTechnicalError error = (PdfExtractionTechnicalError) result;
|
||||
assertTrue(error.errorMessage().contains("zero pages"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSimplePdfExtractionSuccess() throws Exception {
|
||||
// Create a simple single-page PDF
|
||||
@@ -185,4 +236,14 @@ class PdfTextExtractionPortAdapterTest {
|
||||
document.save(filePath.toAbsolutePath().toString());
|
||||
document.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a PDF with zero pages.
|
||||
*/
|
||||
private void createZeroPagePdf(Path filePath) throws Exception {
|
||||
PDDocument document = new PDDocument();
|
||||
// Add no pages
|
||||
document.save(filePath.toAbsolutePath().toString());
|
||||
document.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user