View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.macro.toc;
20  
21  import java.io.File;
22  import java.io.StringWriter;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import org.apache.maven.doxia.macro.MacroExecutionException;
28  import org.apache.maven.doxia.macro.MacroRequest;
29  import org.apache.maven.doxia.markup.Markup;
30  import org.apache.maven.doxia.parser.AbstractParserTest;
31  import org.apache.maven.doxia.parser.ParseException;
32  import org.apache.maven.doxia.parser.Xhtml5BaseParser;
33  import org.apache.maven.doxia.sink.Sink;
34  import org.apache.maven.doxia.sink.impl.CreateAnchorsForIndexEntriesFactory;
35  import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
36  import org.apache.maven.doxia.sink.impl.SinkEventElement;
37  import org.apache.maven.doxia.sink.impl.SinkEventTestingSink;
38  import org.apache.maven.doxia.sink.impl.Xhtml5BaseSink;
39  import org.junit.jupiter.api.Test;
40  
41  import static org.junit.jupiter.api.Assertions.assertEquals;
42  import static org.junit.jupiter.api.Assertions.assertFalse;
43  import static org.junit.jupiter.api.Assertions.assertTrue;
44  
45  /**
46   * Test toc macro.
47   *
48   * @author ltheussl
49   */
50  public class TocMacroTest {
51      /**
52       * Test of execute method, of class TocMacro.
53       *
54       * @throws MacroExecutionException if a macro fails during testing.
55       */
56      @Test
57      public void testExecute() throws MacroExecutionException {
58          String sourceContent = "<div><h1>h11</h1><h1>h12</h1><h2>h2</h2><h3>h3</h3><h1>h13</h1></div>";
59  
60          Xhtml5BaseParser parser = new Xhtml5BaseParser();
61  
62          Map<String, Object> macroParameters = new HashMap<>();
63          macroParameters.put("section", "sec1");
64  
65          File basedir = new File("");
66  
67          SinkEventTestingSink sink = new SinkEventTestingSink();
68          MacroRequest request = new MacroRequest(sourceContent, parser, macroParameters, basedir);
69          TocMacro macro = new TocMacro();
70          macro.execute(sink, request);
71  
72          Iterator<SinkEventElement> it = sink.getEventList().iterator();
73          assertEquals("list", (it.next()).getName());
74          assertEquals("listItem", (it.next()).getName());
75          assertEquals("link", (it.next()).getName());
76          assertEquals("text", (it.next()).getName());
77          assertEquals("link_", (it.next()).getName());
78          assertEquals("listItem_", (it.next()).getName());
79          assertEquals("listItem", (it.next()).getName());
80          assertEquals("link", (it.next()).getName());
81          assertEquals("text", (it.next()).getName());
82          assertEquals("link_", (it.next()).getName());
83          assertEquals("list", (it.next()).getName());
84          assertEquals("listItem", (it.next()).getName());
85          assertEquals("link", (it.next()).getName());
86          assertEquals("text", (it.next()).getName());
87          assertEquals("link_", (it.next()).getName());
88          assertEquals("list", (it.next()).getName());
89          assertEquals("listItem", (it.next()).getName());
90          assertEquals("link", (it.next()).getName());
91          assertEquals("text", (it.next()).getName());
92          assertEquals("link_", (it.next()).getName());
93          assertEquals("listItem_", (it.next()).getName());
94          assertEquals("list_", (it.next()).getName());
95          assertEquals("listItem_", (it.next()).getName());
96          assertEquals("list_", (it.next()).getName());
97          assertEquals("listItem_", (it.next()).getName());
98          assertEquals("listItem", (it.next()).getName());
99          assertEquals("link", (it.next()).getName());
100         assertEquals("text", (it.next()).getName());
101         assertEquals("link_", (it.next()).getName());
102         assertEquals("listItem_", (it.next()).getName());
103         assertEquals("list_", (it.next()).getName());
104         assertFalse(it.hasNext());
105 
106         // test parameters
107         parser = new Xhtml5BaseParser();
108         macroParameters.put("section", "2");
109         macroParameters.put("fromDepth", "1");
110         macroParameters.put("toDepth", "2");
111         macroParameters.put("class", "myClass");
112         macroParameters.put("id", "myId");
113 
114         sink.reset();
115         request = new MacroRequest(sourceContent, parser, macroParameters, basedir);
116         macro.execute(sink, request);
117 
118         it = sink.getEventList().iterator();
119         SinkEventElement event = it.next();
120         assertEquals("list", event.getName());
121         SinkEventAttributeSet atts = (SinkEventAttributeSet) event.getArgs()[0];
122         assertEquals("myId", atts.getAttribute("id"));
123         assertEquals("myClass", atts.getAttribute("class"));
124         assertEquals("listItem", (it.next()).getName());
125         assertEquals("link", (it.next()).getName());
126         event = it.next();
127         assertEquals("text", event.getName());
128         assertEquals("h12", event.getArgs()[0]);
129         assertEquals("link_", (it.next()).getName());
130         assertEquals("list", (it.next()).getName());
131         assertEquals("listItem", (it.next()).getName());
132         assertEquals("link", (it.next()).getName());
133         event = it.next();
134         assertEquals("text", event.getName());
135         assertEquals("h2", event.getArgs()[0]);
136         assertEquals("link_", (it.next()).getName());
137         assertEquals("listItem_", (it.next()).getName());
138         assertEquals("list_", (it.next()).getName());
139         assertEquals("listItem_", (it.next()).getName());
140         assertEquals("list_", (it.next()).getName());
141         assertFalse(it.hasNext());
142     }
143 
144     /**
145      * Test DOXIA-366.
146      *
147      * @throws MacroExecutionException if a macro fails during testing.
148      */
149     @Test
150     public void testTocStyle() throws MacroExecutionException {
151         String sourceContent =
152                 "<div><h1>h<b>11</b></h1><h1>h<i>12</i></h1><h2>h<tt>2</tt></h2><h3>h3</h3><h1>h13</h1></div>";
153 
154         Xhtml5BaseParser parser = new Xhtml5BaseParser();
155 
156         Map<String, Object> macroParameters = new HashMap<>();
157         macroParameters.put("section", "sec1");
158 
159         File basedir = new File("");
160 
161         StringWriter out = new StringWriter();
162         Xhtml5BaseSink sink = new Xhtml5BaseSink(out);
163         MacroRequest request = new MacroRequest(sourceContent, parser, macroParameters, basedir);
164         TocMacro macro = new TocMacro();
165         macro.execute(sink, request);
166 
167         assertTrue(out.toString().contains("<a href=\"#h11\">h11</a>"));
168         assertTrue(out.toString().contains("<a href=\"#h12\">h12</a>"));
169         assertTrue(out.toString().contains("<a href=\"#h2\">h2</a>"));
170     }
171 
172     @Test
173     public void testGenerateAnchors() throws ParseException, MacroExecutionException {
174         String sourceContent = "<h1>1 Headline</h1>";
175         File basedir = new File("");
176         Xhtml5BaseParser parser = new Xhtml5BaseParser();
177         MacroRequest request = new MacroRequest(sourceContent, parser, new HashMap<>(), basedir);
178         TocMacro macro = new TocMacro();
179         SinkEventTestingSink sink = new SinkEventTestingSink();
180 
181         macro.execute(sink, request);
182         parser.parse(sourceContent, sink);
183 
184         Iterator<SinkEventElement> it = sink.getEventList().iterator();
185         AbstractParserTest.assertSinkStartsWith(it, "list", "listItem");
186         SinkEventElement link = it.next();
187         assertEquals("link", link.getName());
188         String actualLinkTarget = (String) link.getArgs()[0];
189         AbstractParserTest.assertSinkEquals(it.next(), "text", "1 Headline", null);
190         AbstractParserTest.assertSinkEquals(
191                 it, "link_", "listItem_", "list_", "section1", "sectionTitle1", "text", "sectionTitle1_");
192 
193         // check html output as well (without the actual TOC)
194         StringWriter out = new StringWriter();
195         Sink sink2 = new Xhtml5BaseSink(out);
196         parser.addSinkWrapperFactory(new CreateAnchorsForIndexEntriesFactory());
197         parser.parse(sourceContent, sink2);
198         assertEquals(
199                 "<section><a id=\"" + actualLinkTarget.substring(1) + "\"></a>" + Markup.EOL + "<h1>1 Headline</h1>",
200                 out.toString());
201     }
202 }