summaryrefslogtreecommitdiff
path: root/src/test/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-02-18 11:11:55 +0000
committerMichael Williamson <mike@zwobble.org>2024-02-18 11:11:55 +0000
commitcb8360ca25dd74e3afa82fb5626df932f71013e8 (patch)
tree46c2a5d34120ac8ed4e22d956dc72dbb2e6ea67e /src/test/java/org
parent33359a6d13376f64e8cd3e41f185fc6d23fb8524 (diff)
Throw error when failing to find the body element in a document
Diffstat (limited to 'src/test/java/org')
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java37
1 files changed, 28 insertions, 9 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java
index 3aa6a62..92674d0 100644
--- a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java
@@ -11,6 +11,7 @@ import org.zwobble.mammoth.internal.xml.XmlElement;
import org.zwobble.mammoth.internal.xml.XmlNodes;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.xml.XmlNodes.element;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
@@ -19,22 +20,21 @@ import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.document
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.paragraphWithText;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.withChildren;
import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.bodyReader;
+import static org.zwobble.mammoth.tests.util.MammothAsserts.assertThrows;
public class DocumentXmlTests {
-
@Test
- public void canReadTextWithinDocument() {
- XmlElement documentElement = element("w:document", list(
- element("w:body", list(
- element("w:p", list(
- element("w:r", list(
- element("w:t", list(
- XmlNodes.text("Hello!")))))))))));
+ public void whenBodyElementIsPresentThenBodyIsRead() {
+ XmlElement textXml = element("w:t", list(XmlNodes.text("Hello!")));
+ XmlElement runXml = element("w:r", list(textXml));
+ XmlElement paragraphXml = element("w:p", list(runXml));
+ XmlElement bodyXml = element("w:body", list(paragraphXml));
+ XmlElement documentXml = element("w:document", list(bodyXml));
DocumentXmlReader reader = new DocumentXmlReader(bodyReader(), Notes.EMPTY, list());
assertThat(
- reader.readElement(documentElement),
+ reader.readElement(documentXml),
isInternalSuccess(document(
withChildren(paragraphWithText("Hello!"))
))
@@ -42,6 +42,25 @@ public class DocumentXmlTests {
}
@Test
+ public void whenBodyElementIsNotPresentThenErrorIsThrown() {
+ XmlElement paragraphXml = element("w:p");
+ XmlElement bodyXml = element("w:body2", list(paragraphXml));
+ XmlElement documentXml = element("w:document", list(bodyXml));
+
+ DocumentXmlReader reader = new DocumentXmlReader(bodyReader(), Notes.EMPTY, list());
+
+ IllegalArgumentException exception = assertThrows(
+ IllegalArgumentException.class,
+ () -> reader.readElement(documentXml)
+ );
+
+ assertThat(
+ exception.getMessage(),
+ equalTo("Could not find the body element: are you sure this is a docx file?")
+ );
+ }
+
+ @Test
public void notesOfDocumentAreIncludedInDocument() {
Note note = new Note(NoteType.FOOTNOTE, "4", list(paragraphWithText("Hello")));
Notes notes = new Notes(list(note));