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

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import org.zwobble.mammoth.docx.Styles;
import org.zwobble.mammoth.xml.XmlElement;

import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.zwobble.mammoth.docx.StylesXml.readStylesXmlElement;
import static org.zwobble.mammoth.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", ImmutableList.of(
            element("w:style", ImmutableMap.of("w:type", "paragraph", "w:styleId", "Heading1"), ImmutableList.of(
                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", ImmutableList.of(
            element("w:style", ImmutableMap.of("w:type", "character", "w:styleId", "Heading1Char"), ImmutableList.of(
                nameElement("Heading 1 Char")))));

        Styles styles = readStylesXmlElement(element);

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

    @Test
    public void paragraphAndCharacterStylesAreDistinct() {
        XmlElement element = element("w:styles", ImmutableList.of(
            element("w:style", ImmutableMap.of("w:type", "paragraph", "w:styleId", "Heading1"), ImmutableList.of(
                nameElement("Heading 1"))),
            element("w:style", ImmutableMap.of("w:type", "character", "w:styleId", "Heading1Char"), ImmutableList.of(
                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", ImmutableList.of(
            element("w:style", ImmutableMap.of("w:type", "paragraph", "w:styleId", "Heading1")),
            element("w:style", ImmutableMap.of("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());
    }

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