aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java
blob: d973e789ce7811c7717ea2f8dc92bedf1ced93ee (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
package org.zwobble.mammoth.tests.docx;

import org.junit.jupiter.api.Test;
import org.zwobble.mammoth.internal.archives.Archives;
import org.zwobble.mammoth.internal.archives.InMemoryArchive;
import org.zwobble.mammoth.internal.archives.MutableArchive;
import org.zwobble.mammoth.internal.docx.EmbeddedStyleMap;
import org.zwobble.mammoth.internal.util.Streams;
import org.zwobble.mammoth.internal.xml.XmlElement;
import org.zwobble.mammoth.internal.xml.XmlNode;
import org.zwobble.mammoth.internal.xml.XmlNodes;
import org.zwobble.mammoth.internal.xml.parsing.XmlParser;

import java.io.IOException;
import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.util.Maps.map;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;

public class EmbeddedStyleMapTests {
    private static final String ORIGINAL_RELATIONSHIPS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
        "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">" +
        "<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\"/>" +
        "</Relationships>";

    private static final XmlElement EXPECTED_RELATIONSHIPS_XML = XmlNodes.element("Relationships", list(
        XmlNodes.element("Relationship", map(
            "Id", "rId3",
            "Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
            "Target", "settings.xml"
        )),
        XmlNodes.element("Relationship", map(
            "Id", "rMammothStyleMap",
            "Type", "http://schemas.zwobble.org/mammoth/style-map",
            "Target", "/mammoth/style-map"
        ))
    ));

    private static final String ORIGINAL_CONTENT_TYPES_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
        "<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">" +
        "<Default Extension=\"png\" ContentType=\"image/png\"/>" +
        "</Types>";

    private static final XmlElement EXPECTED_CONTENT_TYPES_XML = XmlNodes.element("Types", list(
        XmlNodes.element("Default", map("Extension", "png", "ContentType", "image/png")),
        XmlNodes.element("Override", map("PartName", "/mammoth/style-map", "ContentType", "text/prs.mammoth.style-map"))
    ));

    @Test
    public void embeddedStyleMapPreservesUnrelatedFiles() throws IOException {
        MutableArchive archive = normalDocx();
        EmbeddedStyleMap.embedStyleMap(archive, "p => h1");
        assertEquals("placeholder text", readString(archive, "placeholder"));
    }

    @Test
    public void embeddedStyleMapCanBeReadAfterBeingEmbedded() throws IOException {
        MutableArchive archive = normalDocx();
        EmbeddedStyleMap.embedStyleMap(archive, "p => h1");
        assertEquals(Optional.of("p => h1"), EmbeddedStyleMap.readStyleMap(archive));
    }

    @Test
    public void embeddedStyleMapIsReferencedInRelationships() throws IOException {
        MutableArchive archive = normalDocx();
        EmbeddedStyleMap.embedStyleMap(archive, "p => h1");
        assertThat(readRelationships(archive), deepEquals(EXPECTED_RELATIONSHIPS_XML));
    }

    @Test
    public void embeddedStyleMapHasOverrideContentTypeInContentTypesXml() throws IOException {
        MutableArchive archive = normalDocx();
        EmbeddedStyleMap.embedStyleMap(archive, "p => h1");
        assertThat(readContentTypes(archive), deepEquals(EXPECTED_CONTENT_TYPES_XML));
    }

    @Test
    public void canOverwriteExistingStyleMap() throws IOException {
        MutableArchive archive = normalDocx();
        EmbeddedStyleMap.embedStyleMap(archive, "p => h1");
        EmbeddedStyleMap.embedStyleMap(archive, "p => h1");
        assertEquals(Optional.of("p => h1"), EmbeddedStyleMap.readStyleMap(archive));
        assertThat(readRelationships(archive), deepEquals(EXPECTED_RELATIONSHIPS_XML));
        assertThat(readContentTypes(archive), deepEquals(EXPECTED_CONTENT_TYPES_XML));
    }

    private XmlNode readRelationships(MutableArchive archive) throws IOException {
        return new XmlParser(EmbeddedStyleMap.RELATIONSHIPS_NAMESPACES)
            .parseString(readString(archive, "word/_rels/document.xml.rels"));
    }

    private XmlNode readContentTypes(MutableArchive archive) throws IOException {
        return new XmlParser(EmbeddedStyleMap.CONTENT_TYPES_NAMESPACES)
            .parseString(readString(archive, "[Content_Types].xml"));
    }

    private static MutableArchive normalDocx() {
        return InMemoryArchive.fromStrings(map(
            "placeholder", "placeholder text",
            "word/_rels/document.xml.rels", ORIGINAL_RELATIONSHIPS_XML,
            "[Content_Types].xml", ORIGINAL_CONTENT_TYPES_XML
        ));
    }

    private String readString(MutableArchive archive, String path) throws IOException {
        return Streams.toString(Archives.getInputStream(archive, path));
    }
}