PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin/src/test/java/net/customware/confluence/plugin/toc/StaxDocumentOutlineCreatorTest.java

https://bitbucket.org/atlassian/confluence-toc-plugin
Java | 178 lines | 118 code | 35 blank | 25 comment | 0 complexity | f452f16d6a695bb10d454c21a143e6fa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package net.customware.confluence.plugin.toc;
  2. import com.atlassian.confluence.content.render.xhtml.DefaultXmlEventReaderFactory;
  3. import com.atlassian.confluence.content.render.xhtml.DelegateXmlOutputFactory;
  4. import com.ctc.wstx.api.WstxOutputProperties;
  5. import net.customware.confluence.plugin.toc.DocumentOutline.Heading;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import javax.xml.stream.XMLOutputFactory;
  9. import java.util.Iterator;
  10. import static org.junit.Assert.assertEquals;
  11. public class StaxDocumentOutlineCreatorTest {
  12. private StaxDocumentOutlineCreator staxBuilder;
  13. @Before
  14. public void setUp() throws Exception {
  15. XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
  16. xmlOutputFactory.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE, Boolean.FALSE);
  17. xmlOutputFactory.setProperty(WstxOutputProperties.P_AUTOMATIC_END_ELEMENTS, Boolean.FALSE);
  18. staxBuilder = new StaxDocumentOutlineCreator(new DefaultXmlEventReaderFactory(), new DelegateXmlOutputFactory(xmlOutputFactory));
  19. }
  20. @Test
  21. public void testMissingTopHeadings() throws Exception {
  22. Iterator<Heading> iterator = staxBuilder.getOutline("<body><p>nothing</p><h3 id=\"anchor1\">Heading 3</h3><h2 id=\"anchor2\">Heading 2</h2><p>nothing</p><h3 id=\"anchor3\">SubHeading 3</h3></body>").iterator();
  23. Heading heading = iterator.next();
  24. assertEquals("Heading 3", heading.getName());
  25. assertEquals("anchor1", heading.getAnchor());
  26. assertEquals(2, heading.getEffectiveLevel());
  27. assertEquals(0, heading.getChildCount());
  28. heading = iterator.next();
  29. assertEquals("Heading 2", heading.getName());
  30. assertEquals("anchor2", heading.getAnchor());
  31. assertEquals(1, heading.getEffectiveLevel());
  32. assertEquals(1, heading.getChildCount());
  33. heading = iterator.next();
  34. assertEquals("SubHeading 3", heading.getName());
  35. assertEquals("anchor3", heading.getAnchor());
  36. assertEquals(2, heading.getEffectiveLevel());
  37. assertEquals(0, heading.getChildCount());
  38. }
  39. @Test
  40. public void testOnlyOneLevelOfHeadings() throws Exception {
  41. Iterator<Heading> iterator = staxBuilder.getOutline("<body><p>nothing</p><h1>a</h1><p>stuff</p><H1>b</H1><H1>c</H1></body>").iterator();
  42. Heading heading = iterator.next();
  43. assertEquals("a", heading.getName());
  44. assertEquals(1, heading.getEffectiveLevel());
  45. assertEquals(0, heading.getChildCount());
  46. heading = iterator.next();
  47. assertEquals("b", heading.getName());
  48. assertEquals(1, heading.getEffectiveLevel());
  49. assertEquals(0, heading.getChildCount());
  50. heading = iterator.next();
  51. assertEquals("c", heading.getName());
  52. assertEquals(1, heading.getEffectiveLevel());
  53. assertEquals(0, heading.getChildCount());
  54. }
  55. @Test
  56. public void testABigJumbleOfHeadings() throws Exception {
  57. Iterator<Heading> iterator = staxBuilder.getOutline("<body><h1>Level-1-Heading-1</h1><h2>Level-2-Heading-1</h2><h3>Level-3-Heading-<b>1</b></h3><h2>Level-2-Heading-2</h2><h5>Level-5-Heading-1</h5><h6 id='TOCtest-Level6Heading1'>Level-6-Heading-1</h6><h4>Level-4-Heading-1</h4><h2>Level-2-Heading-3</h2></body>").iterator();
  58. Heading heading = iterator.next();
  59. assertEquals("Level-1-Heading-1", heading.getName());
  60. assertEquals(1, heading.getEffectiveLevel());
  61. heading = iterator.next();
  62. assertEquals("Level-2-Heading-1", heading.getName());
  63. assertEquals(2, heading.getEffectiveLevel());
  64. heading = iterator.next();
  65. assertEquals("Level-3-Heading-1", heading.getName());
  66. assertEquals(3, heading.getEffectiveLevel());
  67. heading = iterator.next();
  68. assertEquals("Level-2-Heading-2", heading.getName());
  69. assertEquals(2, heading.getEffectiveLevel());
  70. heading = iterator.next();
  71. assertEquals("Level-5-Heading-1", heading.getName());
  72. assertEquals(4, heading.getEffectiveLevel());
  73. heading = iterator.next();
  74. assertEquals("Level-6-Heading-1", heading.getName());
  75. assertEquals(5, heading.getEffectiveLevel());
  76. heading = iterator.next();
  77. assertEquals("Level-4-Heading-1", heading.getName());
  78. assertEquals(3, heading.getEffectiveLevel());
  79. heading = iterator.next();
  80. assertEquals("Level-2-Heading-3", heading.getName());
  81. assertEquals(2, heading.getEffectiveLevel());
  82. }
  83. @Test
  84. public void testAnchorSupport() throws Exception {
  85. Iterator<Heading> iterator = staxBuilder.getOutline("<body><h2><a href=\"#\">Header</a> <i>with</i><a name='anchor1'> <b>two</b></a> <a name=\"anchor2\">anchors</a></h2></body>").iterator();
  86. Heading heading = iterator.next();
  87. assertEquals("Header with two anchors", heading.getName());
  88. assertEquals("anchor1", heading.getAnchor());
  89. }
  90. @Test
  91. public void testNumberedHeadingsWithAnchors() throws Exception {
  92. /*
  93. * <h1 id="numberedheadings-Heading-1heading1">
  94. * <span class="nh-number">1. </span>
  95. * Heading-1
  96. * <span class="confluence-anchor-link" id="numberedheadings-heading1"></span>
  97. * </h1>
  98. */
  99. Iterator<Heading> iterator = staxBuilder.getOutline("<body><h1 id=\"numberedheadings-Heading-1heading1\"><span class=\"nh-number\">1. </span>Heading-1<span class=\"confluence-anchor-link\" id=\"numberedheadings-heading1\"></span></h1></body>").iterator();
  100. Heading heading = iterator.next();
  101. assertEquals("1. Heading-1", heading.getName());
  102. assertEquals("numberedheadings-Heading-1heading1", heading.getAnchor());
  103. }
  104. @Test
  105. public void testHeadingsStartWithAnchors() throws Exception {
  106. /*
  107. * <h1 id="heading-h1Heading-1">
  108. * <span class="confluence-anchor-link" id="heading-h1"></span>
  109. * Heading-1
  110. * </h1>
  111. */
  112. Iterator<Heading> iterator = staxBuilder.getOutline("<body><h1 id=\"heading-h1Heading-1\"><span class=\"confluence-anchor-link\" id=\"heading-h1\"></span>Heading-1</h1></body>").iterator();
  113. Heading heading = iterator.next();
  114. assertEquals("Heading-1", heading.getName());
  115. assertEquals("heading-h1Heading-1", heading.getAnchor());
  116. }
  117. @Test
  118. public void testHeadingsEndWithAnchors() throws Exception {
  119. /*
  120. * <h1 id="heading-Heading-1heading1">
  121. * Heading-1&nbsp;
  122. * <span class="confluence-anchor-link" id="heading-heading1"></span>
  123. * </h1>
  124. */
  125. Iterator<Heading> iterator = staxBuilder.getOutline("<body><h1 id=\"heading-Heading-1heading1\">Heading-1<span class=\"confluence-anchor-link\" id=\"heading-heading1\"></span></h1></body>").iterator();
  126. Heading heading = iterator.next();
  127. assertEquals("Heading-1", heading.getName());
  128. assertEquals("heading-Heading-1heading1", heading.getAnchor());
  129. }
  130. @Test
  131. public void testColorHeadings() throws Exception {
  132. /*
  133. * <h1 id="colorheadings-Heading1withtext">
  134. * <span style="color: rgb(255,0,0);">Heading 1</span>
  135. * with text
  136. * </h1>
  137. */
  138. Iterator<Heading> iterator = staxBuilder.getOutline("<body><h1 id=\"colorheadings-Heading1withtext\"><span style=\"color: rgb(255,0,0);\">Heading 1</span> with text</h1></body>").iterator();
  139. Heading heading = iterator.next();
  140. assertEquals("Heading 1 with text", heading.getName());
  141. assertEquals("colorheadings-Heading1withtext", heading.getAnchor());
  142. }
  143. }