aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/docx/DocumentReaderTests.java
blob: bce431a803943f84acf2f367e078c6e05f801c9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.zwobble.mammoth.tests.docx;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.zwobble.mammoth.internal.archives.Archive;
import org.zwobble.mammoth.internal.archives.InMemoryArchive;
import org.zwobble.mammoth.internal.documents.Document;
import org.zwobble.mammoth.internal.docx.DocumentReader;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.util.PassThroughException;
import org.zwobble.mammoth.internal.xml.NamespacePrefixes;
import org.zwobble.mammoth.internal.xml.XmlWriter;

import java.util.Optional;

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.util.Maps.map;
import static org.zwobble.mammoth.internal.xml.XmlNodes.element;
import static org.zwobble.mammoth.internal.xml.XmlNodes.text;
import static org.zwobble.mammoth.tests.ResultMatchers.isInternalSuccess;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*;
import static org.zwobble.mammoth.tests.util.MammothAsserts.assertThrows;

public class DocumentReaderTests {
    private final NamespacePrefixes mainDocumentNamespaces = NamespacePrefixes.builder()
        .put("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")
        .build();
    private final NamespacePrefixes relationshipsNamespaces = NamespacePrefixes.builder()
        .defaultPrefix("http://schemas.openxmlformats.org/package/2006/relationships")
        .build();

    @Test
    public void mainDocumentIsFoundUsingPackageRelationships() {
        InternalResult<Document> result = DocumentReader.readDocument(Optional.empty(), 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.")))
                            ))
                        ))
                    ))
                )),
                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"
                    ))
                )),
                relationshipsNamespaces
            )
        )));

        assertThat(result, isInternalSuccess(document(
            withChildren(paragraphWithText("Hello."))
        )));
    }

    @Test
    public void errorIsThrownWhenMainDocumentPartDoesNotExist() {
        Archive archive = InMemoryArchive.fromStrings(map(
            "_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"
                    ))
                )),
                relationshipsNamespaces
            )
        ));
        PassThroughException exception = assertThrows(
            PassThroughException.class,
            () -> DocumentReader.readDocument(Optional.empty(), archive)
        );
        assertThat(exception.getMessage(), equalTo("java.io.IOException: Could not find main document part. Are you sure this is a valid .docx file?"));
    }

    @Nested
    public class PartPathTests {
        @Test
        public void mainDocumentPartIsFoundUsingPackageRelationships() {
            InMemoryArchive archive = InMemoryArchive.fromStrings(map(
                "word/document2.xml", " ",
                "_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"
                        ))
                    )),
                    relationshipsNamespaces
                )
            ));

            DocumentReader.PartPaths partPaths = DocumentReader.findPartPaths(archive);
            assertThat(partPaths.getMainDocument(), equalTo("word/document2.xml"));
        }

        @Test
        public void whenRelationshipForMainDocumentCannotBeFoundThenFallbackIsUsed() {
            InMemoryArchive archive = InMemoryArchive.fromStrings(map(
                "word/document.xml", " "
            ));

            DocumentReader.PartPaths partPaths = DocumentReader.findPartPaths(archive);
            assertThat(partPaths.getMainDocument(), equalTo("word/document.xml"));
        }
        @Test
        public void commentsPartIsFoundUsingMainDocumentRelationships() {
            InMemoryArchive archive = InMemoryArchive.fromStrings(map(
                "_rels/.rels", XmlWriter.toString(
                    element("Relationships", list(
                        element("Relationship", map(
                            "Id", "rId1",
                            "Target", "/word/document.xml",
                            "Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
                        ))
                    )),
                    relationshipsNamespaces
                ),
                "word/document.xml", " ",
                "word/_rels/document.xml.rels", XmlWriter.toString(
                    element("Relationships", list(
                        element("Relationship", map(
                            "Id", "rId2",
                            "Target", "target-path.xml",
                            "Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
                        ))
                    )),
                    relationshipsNamespaces
                ),
                "word/target-path.xml", " "
            ));

            DocumentReader.PartPaths partPaths = DocumentReader.findPartPaths(archive);
            assertThat(partPaths.getComments(), equalTo("word/target-path.xml"));
        }

        @Test
        public void whenRelationshipsForCommentsCannotBeFoundThenFallbackIsUsed() {
            InMemoryArchive archive = InMemoryArchive.fromStrings(map(
                "word/document.xml", " ",
                "_rels/.rels", XmlWriter.toString(
                    element("Relationships", list(
                        element("Relationship", map(
                            "Id", "rId1",
                            "Target", "/word/document.xml",
                            "Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
                        ))
                    )),
                    relationshipsNamespaces
                )
            ));

            DocumentReader.PartPaths partPaths = DocumentReader.findPartPaths(archive);
            assertThat(partPaths.getComments(), equalTo("word/comments.xml"));
        }
    }
}