001package org.apache.maven.doxia.macro.toc; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.io.StringWriter; 024 025import java.util.HashMap; 026import java.util.Iterator; 027import java.util.Map; 028 029import junit.framework.TestCase; 030 031import org.apache.maven.doxia.macro.MacroExecutionException; 032import org.apache.maven.doxia.macro.MacroRequest; 033import org.apache.maven.doxia.parser.XhtmlBaseParser; 034import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet; 035import org.apache.maven.doxia.sink.impl.SinkEventElement; 036import org.apache.maven.doxia.sink.impl.SinkEventTestingSink; 037import org.apache.maven.doxia.sink.impl.XhtmlBaseSink; 038 039/** 040 * Test toc macro. 041 * 042 * @author ltheussl 043 * @version $Id$ 044 */ 045public class TocMacroTest 046 extends TestCase 047{ 048 /** 049 * Test of execute method, of class TocMacro. 050 * 051 * @throws MacroExecutionException if a macro fails during testing. 052 */ 053 public void testExecute() 054 throws MacroExecutionException 055 { 056 String sourceContent = "<div><h2>h21</h2><h2>h22</h2><h3>h3</h3><h4>h4</h4><h2>h23</h2></div>"; 057 058 XhtmlBaseParser parser = new XhtmlBaseParser(); 059 parser.setSecondParsing( true ); 060 061 Map<String, Object> macroParameters = new HashMap<String, Object>(); 062 macroParameters.put( "parser", parser ); 063 macroParameters.put( "sourceContent", sourceContent ); 064 macroParameters.put( "section", "sec1" ); 065 066 File basedir = new File( "" ); 067 068 SinkEventTestingSink sink = new SinkEventTestingSink(); 069 MacroRequest request = new MacroRequest( macroParameters, basedir ); 070 TocMacro macro = new TocMacro(); 071 macro.execute( sink, request ); 072 073 Iterator<SinkEventElement> it = sink.getEventList().iterator(); 074 assertEquals( "list", ( it.next() ).getName() ); 075 assertEquals( "listItem", ( it.next() ).getName() ); 076 assertEquals( "link", ( it.next() ).getName() ); 077 assertEquals( "text", ( it.next() ).getName() ); 078 assertEquals( "link_", ( it.next() ).getName() ); 079 assertEquals( "listItem_", ( it.next() ).getName() ); 080 assertEquals( "listItem", ( it.next() ).getName() ); 081 assertEquals( "link", ( it.next() ).getName() ); 082 assertEquals( "text", ( it.next() ).getName() ); 083 assertEquals( "link_", ( it.next() ).getName() ); 084 assertEquals( "list", ( it.next() ).getName() ); 085 assertEquals( "listItem", ( it.next() ).getName() ); 086 assertEquals( "link", ( it.next() ).getName() ); 087 assertEquals( "text", ( it.next() ).getName() ); 088 assertEquals( "link_", ( it.next() ).getName() ); 089 assertEquals( "list", ( it.next() ).getName() ); 090 assertEquals( "listItem", ( it.next() ).getName() ); 091 assertEquals( "link", ( it.next() ).getName() ); 092 assertEquals( "text", ( it.next() ).getName() ); 093 assertEquals( "link_", ( it.next() ).getName() ); 094 assertEquals( "listItem_", ( it.next() ).getName() ); 095 assertEquals( "list_", ( it.next() ).getName() ); 096 assertEquals( "listItem_", ( it.next() ).getName() ); 097 assertEquals( "list_", ( it.next() ).getName() ); 098 assertEquals( "listItem_", ( it.next() ).getName() ); 099 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}