1
0

Fehlerpfade im PDF-Extraktionsadapter gezielt getestet

This commit is contained in:
2026-04-02 17:44:04 +02:00
parent 0c0faf2286
commit 53818ea376

View File

@@ -14,6 +14,9 @@ import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -64,6 +67,54 @@ class PdfTextExtractionPortAdapterTest {
assertTrue(error.errorMessage().contains("not found")); 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 @Test
void testSimplePdfExtractionSuccess() throws Exception { void testSimplePdfExtractionSuccess() throws Exception {
// Create a simple single-page PDF // Create a simple single-page PDF
@@ -185,4 +236,14 @@ class PdfTextExtractionPortAdapterTest {
document.save(filePath.toAbsolutePath().toString()); document.save(filePath.toAbsolutePath().toString());
document.close(); 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();
}
} }