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

import org.junit.jupiter.api.Test;
import org.zwobble.mammoth.internal.docx.Styles;
import org.zwobble.mammoth.internal.xml.XmlElement;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.zwobble.mammoth.internal.docx.StylesXml.readStylesXmlElement;
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;

public class StylesXmlTests {
    @Test
    public void paragraphStyleIsNoneIfNoStyleWithThatIdExists() {
        XmlElement element = element("w:styles");

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.empty(), styles.findParagraphStyleById("Heading1"));
    }

    @Test
    public void paragraphStyleCanBeFoundById() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "paragraph", "w:styleId", "Heading1"), list(
                nameElement("Heading 1")))));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.of("Heading 1"), styles.findParagraphStyleById("Heading1").get().getName());
    }

    @Test
    public void characterStyleCanBeFoundById() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "character", "w:styleId", "Heading1Char"), list(
                nameElement("Heading 1 Char")))));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.of("Heading 1 Char"), styles.findCharacterStyleById("Heading1Char").get().getName());
    }

    @Test
    public void tableStyleCanBeFoundById() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "table", "w:styleId", "TableNormal"), list(
                nameElement("Normal Table")))));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.of("Normal Table"), styles.findTableStyleById("TableNormal").get().getName());
    }

    @Test
    public void paragraphAndCharacterStylesAreDistinct() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "paragraph", "w:styleId", "Heading1"), list(
                nameElement("Heading 1"))),
            element("w:style", map("w:type", "character", "w:styleId", "Heading1Char"), list(
                nameElement("Heading 1 Char")))));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.empty(), styles.findCharacterStyleById("Heading1"));
        assertEquals(Optional.empty(), styles.findParagraphStyleById("Heading1Char"));
    }

    @Test
    public void styleNameIsNoneIfNameElementDoesNotExist() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "paragraph", "w:styleId", "Heading1")),
            element("w:style", map("w:type", "character", "w:styleId", "Heading1Char"))));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.empty(), styles.findParagraphStyleById("Heading1").get().getName());
        assertEquals(Optional.empty(), styles.findCharacterStyleById("Heading1Char").get().getName());
    }

    @Test
    public void numberingStyleIsNoneIfNoStyleWithThatIdExists() {
        XmlElement element = element("w:styles");

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.empty(), styles.findNumberingStyleById("List1"));
    }

    @Test
    public void numberingStyleHasNoneNumIdIfStyleHasNoParagraphProperties() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "numbering", "w:styleId", "List1"))
        ));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.empty(), styles.findNumberingStyleById("List1").get().getNumId());
    }

    @Test
    public void numberingStyleHasNumIdReadFromParagraphProperties() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "numbering", "w:styleId", "List1"), list(
                element("w:pPr", list(
                    element("w:numPr", list(
                        element("w:numId", map("w:val", "42"))
                    ))
                ))
            ))
        ));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.of("42"), styles.findNumberingStyleById("List1").get().getNumId());
    }

    @Test
    public void whenMultipleNonNumberingStyleElementsHaveSameStyleIdThenOnlyFirstElementIsUsed() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "table", "w:styleId", "TableNormal"), list(
                nameElement("Normal Table")
            )),
            element("w:style", map("w:type", "table", "w:styleId", "TableNormal"), list(
                nameElement("Table Normal")
            ))
        ));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.of("Normal Table"), styles.findTableStyleById("TableNormal").get().getName());
    }

    @Test
    public void whenMultipleNumberingStyleElementsHaveSameStyleIdThenOnlyFirstElementIsUsed() {
        XmlElement element = element("w:styles", list(
            element("w:style", map("w:type", "numbering", "w:styleId", "List1"), list(
                element("w:pPr", list(
                    element("w:numPr", list(
                        element("w:numId", map("w:val", "42"))
                    ))
                ))
            )),
            element("w:style", map("w:type", "numbering", "w:styleId", "List1"), list(
                element("w:pPr", list(
                    element("w:numPr", list(
                        element("w:numId", map("w:val", "43"))
                    ))
                ))
            ))
        ));

        Styles styles = readStylesXmlElement(element);

        assertEquals(Optional.of("42"), styles.findNumberingStyleById("List1").get().getNumId());
    }

    private XmlElement nameElement(String name) {
        return element("w:name", map("w:val", name));
    }
}