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.

SAXEventRecorderTestCase.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 org.junit.Before;
  20. import org.junit.Test;
  21. import org.mockito.InOrder;
  22. import org.xml.sax.Attributes;
  23. import org.xml.sax.ContentHandler;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.helpers.AttributesImpl;
  26. import static org.mockito.Mockito.inOrder;
  27. import static org.mockito.Mockito.mock;
  28. import static org.mockito.Mockito.verify;
  29. import org.apache.fop.render.intermediate.IFStructureTreeBuilder.SAXEventRecorder;
  30. import org.apache.fop.util.XMLUtil;
  31. /**
  32. * Tests {@link SAXEventRecorder}.
  33. */
  34. public class SAXEventRecorderTestCase {
  35. private static final String URI = "http://www.example.com/";
  36. private SAXEventRecorder sut;
  37. @Before
  38. public void setUp() {
  39. sut = new SAXEventRecorder();
  40. }
  41. @Test
  42. public void testStartEvent() throws SAXException {
  43. final String localName = "element";
  44. final String qName = "prefix:" + localName;
  45. final Attributes attributes = new AttributesImpl();
  46. sut.startElement(URI, localName, qName, attributes);
  47. ContentHandler handler = mock(ContentHandler.class);
  48. sut.replay(handler);
  49. verify(handler).startElement(URI, localName, qName, attributes);
  50. }
  51. @Test
  52. public void testEndEvent() throws SAXException {
  53. final String localName = "element";
  54. final String qName = "prefix:" + localName;
  55. sut.endElement(URI, localName, qName);
  56. ContentHandler handler = mock(ContentHandler.class);
  57. sut.replay(handler);
  58. verify(handler).endElement(URI, localName, qName);
  59. }
  60. @Test
  61. public void testStartPrefixMapping() throws SAXException {
  62. final String prefix = "prefix";
  63. sut.startPrefixMapping(URI, prefix);
  64. ContentHandler handler = mock(ContentHandler.class);
  65. sut.replay(handler);
  66. verify(handler).startPrefixMapping(URI, prefix);
  67. }
  68. @Test
  69. public void testEndPrefixMapping() throws SAXException {
  70. final String prefix = "prefix";
  71. sut.endPrefixMapping(prefix);
  72. ContentHandler handler = mock(ContentHandler.class);
  73. sut.replay(handler);
  74. verify(handler).endPrefixMapping(prefix);
  75. }
  76. @Test
  77. public void completeTest() throws SAXException {
  78. final String localName1 = "element";
  79. final String qName1 = "prefix:" + localName1;
  80. final Attributes attributes1 = createAttributes(URI, localName1, qName1, "value-1");
  81. final String localName2 = "element2";
  82. final String qName2 = "prefix:" + localName2;
  83. final Attributes attributes2 = createAttributes(URI, localName2, qName2, "value-2");
  84. final ContentHandler handler = mock(ContentHandler.class);
  85. final String extensionUrl = "http://www.example.com/extension";
  86. final String extensionPrefix = "ext";
  87. sut.startPrefixMapping(extensionPrefix, extensionUrl);
  88. sut.startElement(URI, localName1, qName1, attributes1);
  89. sut.startElement(URI, localName2, qName2, attributes2);
  90. sut.endElement(URI, localName2, qName2);
  91. sut.endElement(URI, localName1, qName1);
  92. sut.endPrefixMapping(extensionPrefix);
  93. sut.replay(handler);
  94. InOrder inOrder = inOrder(handler);
  95. inOrder.verify(handler).startPrefixMapping(extensionPrefix, extensionUrl);
  96. inOrder.verify(handler).startElement(URI, localName1, qName1, attributes1);
  97. inOrder.verify(handler).startElement(URI, localName2, qName2, attributes2);
  98. inOrder.verify(handler).endElement(URI, localName2, qName2);
  99. inOrder.verify(handler).endElement(URI, localName1, qName1);
  100. inOrder.verify(handler).endPrefixMapping(extensionPrefix);
  101. }
  102. private static Attributes createAttributes(String uri, String localName,
  103. String qName, String value) {
  104. final AttributesImpl atts = new AttributesImpl();
  105. atts.addAttribute(uri, localName, qName, XMLUtil.CDATA, value);
  106. return atts;
  107. }
  108. }