You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IFStructureTreeBuilderTestCase.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.intermediate;
  19. import static org.junit.Assert.fail;
  20. import static org.mockito.Matchers.any;
  21. import static org.mockito.Matchers.argThat;
  22. import static org.mockito.Matchers.eq;
  23. import static org.mockito.Mockito.inOrder;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.verify;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import org.mockito.ArgumentMatcher;
  29. import org.mockito.InOrder;
  30. import org.xml.sax.Attributes;
  31. import org.xml.sax.ContentHandler;
  32. import org.xml.sax.SAXException;
  33. import org.xml.sax.helpers.AttributesImpl;
  34. import org.apache.fop.fo.FOElementMapping;
  35. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  36. import org.apache.fop.fo.extensions.InternalElementMapping;
  37. import org.apache.fop.util.XMLUtil;
  38. public class IFStructureTreeBuilderTestCase {
  39. private IFStructureTreeBuilder sut;
  40. @Before
  41. public void setUp() {
  42. sut = new IFStructureTreeBuilder();
  43. }
  44. @Test
  45. public void startAndEndPageSequence() throws SAXException {
  46. final ContentHandler handler = mock(ContentHandler.class);
  47. try {
  48. sut.replayEventsForPageSequence(handler, 0);
  49. fail("No page sequences created");
  50. } catch (IndexOutOfBoundsException e) {
  51. // Expected
  52. }
  53. sut.startPageSequence(null);
  54. sut.endPageSequence();
  55. sut.replayEventsForPageSequence(handler, 0);
  56. InOrder inOrder = inOrder(handler);
  57. inOrder.verify(handler).startPrefixMapping(
  58. InternalElementMapping.STANDARD_PREFIX, InternalElementMapping.URI);
  59. inOrder.verify(handler).startPrefixMapping(
  60. ExtensionElementMapping.STANDARD_PREFIX, ExtensionElementMapping.URI);
  61. inOrder.verify(handler).startElement(eq(IFConstants.NAMESPACE),
  62. eq(IFConstants.EL_STRUCTURE_TREE),
  63. eq(IFConstants.EL_STRUCTURE_TREE),
  64. any(Attributes.class));
  65. inOrder.verify(handler).endElement(eq(IFConstants.NAMESPACE),
  66. eq(IFConstants.EL_STRUCTURE_TREE),
  67. eq(IFConstants.EL_STRUCTURE_TREE));
  68. inOrder.verify(handler).endPrefixMapping(ExtensionElementMapping.STANDARD_PREFIX);
  69. inOrder.verify(handler).endPrefixMapping(InternalElementMapping.STANDARD_PREFIX);
  70. }
  71. @Test
  72. public void startNode() throws Exception {
  73. final String[] attributes = {"struct-id", "1"};
  74. final String nodeName = "block";
  75. final ContentHandler handler = mock(ContentHandler.class);
  76. sut.startPageSequence(null);
  77. sut.startNode(nodeName, createSimpleAttributes(attributes));
  78. sut.endPageSequence();
  79. sut.replayEventsForPageSequence(handler, 0);
  80. verify(handler).startElement(eq(FOElementMapping.URI), eq(nodeName),
  81. eq(FOElementMapping.STANDARD_PREFIX + ":" + nodeName),
  82. AttributesMatcher.match(createSimpleAttributes(attributes)));
  83. }
  84. @Test
  85. public void endNode() throws Exception {
  86. final String nodeName = "block";
  87. final ContentHandler handler = mock(ContentHandler.class);
  88. sut.startPageSequence(null);
  89. sut.endNode(nodeName);
  90. sut.endPageSequence();
  91. sut.replayEventsForPageSequence(handler, 0);
  92. verify(handler).endElement(eq(FOElementMapping.URI), eq(nodeName),
  93. eq(FOElementMapping.STANDARD_PREFIX + ":" + nodeName));
  94. }
  95. private static Attributes createSimpleAttributes(String... attributes) {
  96. assert (attributes.length % 2 == 0);
  97. final AttributesImpl atts = new AttributesImpl();
  98. for (int i = 0; i < attributes.length; i += 2) {
  99. String key = attributes[i];
  100. String value = attributes[i + 1];
  101. atts.addAttribute("", key, key, XMLUtil.CDATA, value);
  102. }
  103. return atts;
  104. }
  105. private static final class AttributesMatcher extends ArgumentMatcher<Attributes> {
  106. private final Attributes expected;
  107. private AttributesMatcher(Attributes expected) {
  108. this.expected = expected;
  109. }
  110. public static Attributes match(Attributes expected) {
  111. return argThat(new AttributesMatcher(expected));
  112. }
  113. public boolean matches(Object attributes) {
  114. return attributesEqual(expected, (Attributes) attributes);
  115. }
  116. private static boolean attributesEqual(Attributes attributes1, Attributes attributes2) {
  117. if (attributes1.getLength() != attributes2.getLength()) {
  118. return false;
  119. }
  120. for (int i = 0; i < attributes1.getLength(); i++) {
  121. if (attributes1.getLocalName(i) != attributes2.getLocalName(i)) {
  122. return false;
  123. }
  124. if (attributes1.getQName(i) != attributes2.getQName(i)) {
  125. return false;
  126. }
  127. if (attributes1.getType(i) != attributes2.getType(i)) {
  128. return false;
  129. }
  130. if (attributes1.getURI(i) != attributes2.getURI(i)) {
  131. return false;
  132. }
  133. if (attributes1.getValue(i) != attributes2.getValue(i)) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. }
  140. }