diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-09-10 16:40:27 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-09-10 16:40:27 +0100 |
| commit | 444dc1b64c015197bf6b5219da9459bc0cf6f787 (patch) | |
| tree | e34a6f67f643d5fd160d1465444ddd9c9b872439 /src/test/java | |
| parent | 7ef256b4b287af758d5bad0912de7140a4f69fbf (diff) | |
Support disabling external file access
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/MammothTests.java | 23 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/docx/DocumentReaderTests.java | 55 |
2 files changed, 59 insertions, 19 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 85a20ce..a072185 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -121,6 +121,29 @@ public class MammothTests { } @Test + public void warnIfDocumentHasImagesStoredOutsideOfDocumentWhenExternalFileAccessIsDisabled() throws IOException { + Path tempDirectory = Files.createTempDirectory("mammoth-"); + try { + Path documentPath = tempDirectory.resolve("external-picture.docx"); + Files.copy(TestData.file("external-picture.docx").toPath(), documentPath); + Files.copy(TestData.file("tiny-picture.png").toPath(), tempDirectory.resolve("tiny-picture.png")); + assertThat( + new DocumentConverter() + .disableExternalFileAccess() + .convertToHtml(documentPath.toFile()), + allOf( + hasProperty("value", equalTo("")), + hasProperty("warnings", contains( + equalTo("could not open external image 'tiny-picture.png': external file access is disabled") + )) + ) + ); + } finally { + tempDirectory.toFile().delete(); + } + } + + @Test public void warnIfImagesStoredOutsideOfDocumentAreNotFound() throws IOException { Path tempDirectory = Files.createTempDirectory("mammoth-"); try { diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentReaderTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentReaderTests.java index 1c07679..db24404 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentReaderTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentReaderTests.java @@ -38,30 +38,47 @@ public class DocumentReaderTests { @Test public void mainDocumentIsFoundUsingPackageRelationships() { - InternalResult<Document> result = DocumentReader.readDocument(Optional.empty(), InMemoryArchive.fromStrings(map( + Archive archive = InMemoryArchive.fromStrings(map( "word/document2.xml", XmlWriter.toString( - element("w:document", list( - element("w:body", list( - element("w:p", list( - element("w:r", list( - element("w:t", list(text("Hello."))) - )) - )) - )) - )), + element( + "w:document", list( + element( + "w:body", list( + element( + "w:p", list( + element( + "w:r", list( + element("w:t", list(text("Hello."))) + ) + ) + ) + ) + ) + ) + ) + ), mainDocumentNamespaces ), "_rels/.rels", XmlWriter.toString( - element("Relationships", list( - element("Relationship", map( - "Id", "rId1", - "Target", "/word/document2.xml", - "Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" - )) - )), + element( + "Relationships", list( + element( + "Relationship", map( + "Id", "rId1", + "Target", "/word/document2.xml", + "Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" + ) + ) + ) + ), relationshipsNamespaces ) - ))); + )); + InternalResult<Document> result = DocumentReader.readDocument( + Optional.empty(), + archive, + false + ); assertThat(result, isInternalSuccess(document( withChildren(paragraphWithText("Hello.")) @@ -84,7 +101,7 @@ public class DocumentReaderTests { )); PassThroughException exception = assertThrows( PassThroughException.class, - () -> DocumentReader.readDocument(Optional.empty(), archive) + () -> DocumentReader.readDocument(Optional.empty(), archive, false) ); assertThat(exception.getMessage(), equalTo("java.io.IOException: Could not find main document part. Are you sure this is a valid .docx file?")); } |
