aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/render/intermediate/IFStructureTreeBuilderTestCase.java
blob: 4df3fe278d00adbfade90e34f395f3625856e97f (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.render.intermediate;

import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.InOrder;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import org.apache.fop.fo.FOElementMapping;
import org.apache.fop.fo.extensions.ExtensionElementMapping;
import org.apache.fop.fo.extensions.InternalElementMapping;
import org.apache.fop.util.XMLUtil;

public class IFStructureTreeBuilderTestCase {

    private IFStructureTreeBuilder sut;

    @Before
    public void setUp() {
        sut = new IFStructureTreeBuilder();
    }

    @Test
    public void startAndEndPageSequence() throws SAXException {
        final ContentHandler handler = mock(ContentHandler.class);

        try {
            sut.replayEventsForPageSequence(handler, 0);
            fail("No page sequences created");
        } catch (IndexOutOfBoundsException e) {
            // Expected
        }

        sut.startPageSequence(null, null);
        sut.endPageSequence();

        sut.replayEventsForPageSequence(handler, 0);

        InOrder inOrder = inOrder(handler);

        inOrder.verify(handler).startPrefixMapping(
                InternalElementMapping.STANDARD_PREFIX, InternalElementMapping.URI);
        inOrder.verify(handler).startPrefixMapping(
                ExtensionElementMapping.STANDARD_PREFIX, ExtensionElementMapping.URI);
        inOrder.verify(handler).startElement(eq(IFConstants.NAMESPACE),
                eq(IFConstants.EL_STRUCTURE_TREE),
                eq(IFConstants.EL_STRUCTURE_TREE),
                any(Attributes.class));
        inOrder.verify(handler).endElement(eq(IFConstants.NAMESPACE),
                eq(IFConstants.EL_STRUCTURE_TREE),
                eq(IFConstants.EL_STRUCTURE_TREE));
        inOrder.verify(handler).endPrefixMapping(ExtensionElementMapping.STANDARD_PREFIX);
        inOrder.verify(handler).endPrefixMapping(InternalElementMapping.STANDARD_PREFIX);
    }

    @Test
    public void startNode() throws Exception {
        final String[] attributes = {"struct-id", "1"};
        final String nodeName = "block";
        final ContentHandler handler = mock(ContentHandler.class);

        sut.startPageSequence(null, null);
        sut.startNode(nodeName, createSimpleAttributes(attributes), null);
        sut.endPageSequence();

        sut.replayEventsForPageSequence(handler, 0);

        verify(handler).startElement(eq(FOElementMapping.URI), eq(nodeName),
                eq(FOElementMapping.STANDARD_PREFIX + ":" + nodeName),
                AttributesMatcher.match(createSimpleAttributes(attributes)));
    }

    @Test
    public void endNode() throws Exception {
        final String nodeName = "block";
        final ContentHandler handler = mock(ContentHandler.class);

        sut.startPageSequence(null, null);
        sut.endNode(nodeName);
        sut.endPageSequence();

        sut.replayEventsForPageSequence(handler, 0);

        verify(handler).endElement(eq(FOElementMapping.URI), eq(nodeName),
                eq(FOElementMapping.STANDARD_PREFIX + ":" + nodeName));
    }

    private static Attributes createSimpleAttributes(String... attributes) {
        assert (attributes.length % 2 == 0);
        final AttributesImpl atts = new AttributesImpl();
        for (int i = 0; i < attributes.length; i += 2) {
            String key = attributes[i];
            String value = attributes[i + 1];
            atts.addAttribute("", key, key, XMLUtil.CDATA, value);
        }
        return atts;
    }

    private static final class AttributesMatcher extends ArgumentMatcher<Attributes> {

        private final Attributes expected;

        private AttributesMatcher(Attributes expected) {
            this.expected = expected;
        }

        public static Attributes match(Attributes expected) {
            return argThat(new AttributesMatcher(expected));
        }

        public boolean matches(Object attributes) {
            return attributesEqual(expected, (Attributes) attributes);
        }

        private static boolean attributesEqual(Attributes attributes1, Attributes attributes2) {
            if (attributes1.getLength() != attributes2.getLength()) {
                return false;
            }
            for (int i = 0; i < attributes1.getLength(); i++) {
                if (attributes1.getLocalName(i) != attributes2.getLocalName(i)) {
                    return false;
                }
                if (attributes1.getQName(i) != attributes2.getQName(i)) {
                    return false;
                }
                if (attributes1.getType(i) != attributes2.getType(i)) {
                    return false;
                }
                if (attributes1.getURI(i) != attributes2.getURI(i)) {
                    return false;
                }
                if (attributes1.getValue(i) != attributes2.getValue(i)) {
                    return false;
                }
            }
            return true;
        }
    }
}