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