001package org.apache.maven.doxia.module.xhtml; 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.StringWriter; 023import java.io.Writer; 024 025import org.apache.maven.doxia.markup.HtmlMarkup; 026import org.apache.maven.doxia.sink.Sink; 027import org.apache.maven.doxia.sink.impl.AbstractSinkTest; 028import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet; 029 030/** 031 * @author Jason van Zyl 032 * @version $Id: XhtmlSinkTest.html 979316 2016-02-02 21:51:43Z hboutemy $ 033 * @since 1.0 034 */ 035public class XhtmlSinkTest 036 extends AbstractSinkTest 037{ 038 /** {@inheritDoc} */ 039 protected String outputExtension() 040 { 041 return "xhtml"; 042 } 043 044 /** {@inheritDoc} */ 045 protected Sink createSink( Writer writer ) 046 { 047 return new XhtmlSink( writer, "UTF-8" ); 048 } 049 050 /** {@inheritDoc} */ 051 protected boolean isXmlSink() 052 { 053 return true; 054 } 055 056 /** 057 * Test link generation. 058 * 059 * @throws java.lang.Exception if any. 060 */ 061 public void testLinks() 062 throws Exception 063 { 064 XhtmlSink sink = null; 065 Writer writer = new StringWriter(); 066 try 067 { 068 sink = (XhtmlSink) createSink( writer ); 069 sink.link( "http:/www.xdoc.com" ); 070 sink.link_(); 071 sink.link( "./index.html#anchor" ); 072 sink.link_(); 073 sink.link( "../index.html#anchor" ); 074 sink.link_(); 075 sink.link( "index.html" ); 076 sink.link_(); 077 } 078 finally 079 { 080 if ( sink != null ) 081 { 082 sink.close(); 083 } 084 } 085 086 String actual = writer.toString(); 087 assertTrue( actual.indexOf( "<a class=\"externalLink\" href=\"http:/www.xdoc.com\"></a>" ) != -1 ); 088 assertTrue( actual.indexOf( "<a href=\"./index.html#anchor\"></a>" ) != -1 ); 089 assertTrue( actual.indexOf( "<a href=\"../index.html#anchor\"></a>" ) != -1 ); 090 assertTrue( actual.indexOf( "<a href=\"index.html\"></a>" ) != -1 ); 091 } 092 093 /** {@inheritDoc} */ 094 protected String getTitleBlock( String title ) 095 { 096 return "<title>" + title + "</title>"; 097 } 098 099 /** {@inheritDoc} */ 100 protected String getAuthorBlock( String author ) 101 { 102 return author; 103 } 104 105 /** {@inheritDoc} */ 106 protected String getDateBlock( String date ) 107 { 108 return date; 109 } 110 111 /** {@inheritDoc} */ 112 protected String getHeadBlock() 113 { 114 return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + 115 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title></title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head>"; 116 } 117 118 /** {@inheritDoc} */ 119 protected String getBodyBlock() 120 { 121 return "<body></body></html>"; 122 } 123 124 /** {@inheritDoc} */ 125 protected String getSectionTitleBlock( String title ) 126 { 127 return title; 128 } 129 130 /** {@inheritDoc} */ 131 protected String getSection1Block( String title ) 132 { 133 return "<div class=\"section\">\n<h2>" + title + "</h2></div>"; 134 } 135 136 /** {@inheritDoc} */ 137 protected String getSection2Block( String title ) 138 { 139 return "<div class=\"section\">\n<h3>" + title + "</h3></div>"; 140 } 141 142 /** {@inheritDoc} */ 143 protected String getSection3Block( String title ) 144 { 145 return "<div class=\"section\">\n<h4>" + title + "</h4></div>"; 146 } 147 148 /** {@inheritDoc} */ 149 protected String getSection4Block( String title ) 150 { 151 return "<div class=\"section\">\n<h5>" + title + "</h5></div>"; 152 } 153 154 /** {@inheritDoc} */ 155 protected String getSection5Block( String title ) 156 { 157 return "<div class=\"section\">\n<h6>" + title + "</h6></div>"; 158 } 159 160 /** {@inheritDoc} */ 161 protected String getListBlock( String item ) 162 { 163 return "<ul>\n<li>" + item + "</li></ul>"; 164 } 165 166 /** {@inheritDoc} */ 167 protected String getNumberedListBlock( String item ) 168 { 169 return "<ol style=\"list-style-type: lower-roman\">\n<li>" + item + "</li></ol>"; 170 } 171 172 /** {@inheritDoc} */ 173 protected String getDefinitionListBlock( String definum, String definition ) 174 { 175 return "<dl>\n<dt>" + definum + "</dt>\n<dd>" + definition + "</dd></dl>"; 176 } 177 178 /** {@inheritDoc} */ 179 protected String getFigureBlock( String source, String caption ) 180 { 181 String figureBlock = "<img src=\"" + source + "\""; 182 if( caption != null ) 183 { 184 figureBlock += " alt=\"" + caption + "\""; 185 } 186 else //@todo fix DOXIA-361 187 { 188 figureBlock += " alt=\"\""; 189 } 190 figureBlock += " />"; 191 return figureBlock; 192 } 193 194 /** {@inheritDoc} */ 195 protected String getTableBlock( String cell, String caption ) 196 { 197 return "<table border=\"0\" class=\"bodyTable\">" 198 + "<caption>Table caption</caption><tr class=\"a\">\n<td>cell</td></tr>" 199 + "</table>"; 200 } 201 202 // Disable testTable until the order of attributes issue is clarified 203 // TODO: remove 204 /** {@inheritDoc} */ 205 public void testTable() 206 { 207 assertEquals( "Dummy!", "", "" ); 208 } 209 210 /** {@inheritDoc} */ 211 protected String getParagraphBlock( String text ) 212 { 213 return "<p>" + text + "</p>"; 214 } 215 216 /** {@inheritDoc} */ 217 protected String getVerbatimBlock( String text ) 218 { 219 return "<div class=\"source\">\n<pre>" + text + "</pre></div>"; 220 } 221 222 /** {@inheritDoc} */ 223 protected String getHorizontalRuleBlock() 224 { 225 return "<hr />"; 226 } 227 228 /** {@inheritDoc} */ 229 protected String getPageBreakBlock() 230 { 231 return "<!-- PB -->"; 232 } 233 234 /** {@inheritDoc} */ 235 protected String getAnchorBlock( String anchor ) 236 { 237 return "<a name=\"" + anchor + "\">" + anchor + "</a>"; 238 } 239 240 /** {@inheritDoc} */ 241 protected String getLinkBlock( String link, String text ) 242 { 243 return "<a href=\"" + link + "\">" + text + "</a>"; 244 } 245 246 /** {@inheritDoc} */ 247 protected String getItalicBlock( String text ) 248 { 249 return "<i>" + text + "</i>"; 250 } 251 252 /** {@inheritDoc} */ 253 protected String getBoldBlock( String text ) 254 { 255 return "<b>" + text + "</b>"; 256 } 257 258 /** {@inheritDoc} */ 259 protected String getMonospacedBlock( String text ) 260 { 261 return "<tt>" + text + "</tt>"; 262 } 263 264 /** {@inheritDoc} */ 265 protected String getLineBreakBlock() 266 { 267 return "<br />"; 268 } 269 270 /** {@inheritDoc} */ 271 protected String getNonBreakingSpaceBlock() 272 { 273 return " "; 274 } 275 276 /** {@inheritDoc} */ 277 protected String getTextBlock( String text ) 278 { 279 // TODO: need to be able to retreive those from outside the sink 280 return "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\"; 281 } 282 283 /** {@inheritDoc} */ 284 protected String getRawTextBlock( String text ) 285 { 286 return text; 287 } 288 289 /** 290 * Test entities is section titles and paragraphs. 291 */ 292 public void testEntities() 293 { 294 XhtmlSink sink = null; 295 Writer writer = new StringWriter(); 296 297 try 298 { 299 sink = new XhtmlSink( writer ); 300 sink.section( Sink.SECTION_LEVEL_1, null ); 301 sink.sectionTitle( Sink.SECTION_LEVEL_1, null ); 302 sink.text( "&", null ); 303 sink.sectionTitle_( Sink.SECTION_LEVEL_1 ); 304 sink.paragraph( null ); 305 sink.text( "&", null ); 306 sink.paragraph_(); 307 sink.section_( Sink.SECTION_LEVEL_1 ); 308 } 309 finally 310 { 311 sink.close(); 312 } 313 314 assertEquals( "<div class=\"section\">\n<h2>&</h2>\n<p>&</p></div>", writer.toString() ); 315 } 316 317 /** 318 * Test head events. 319 */ 320 public void testHead() 321 { 322 XhtmlSink sink = null; 323 Writer writer = new StringWriter(); 324 325 try 326 { 327 sink = new XhtmlSink( writer ); 328 sink.head(); 329 sink.title(); 330 sink.text( "Title" ); 331 sink.title_(); 332 sink.comment( "A comment" ); 333 sink.author(); 334 // note: this is really illegal, there should be no un-resolved entities emitted into text() 335 sink.text( "ģ&" ); 336 sink.author_(); 337 SinkEventAttributeSet atts = new SinkEventAttributeSet( 1 ); 338 atts.addAttribute( "href", "http://maven.apache.org/" ); 339 sink.unknown( "base", new Object[] {new Integer( HtmlMarkup.TAG_TYPE_SIMPLE )}, atts ); 340 sink.head_(); 341 } 342 finally 343 { 344 sink.close(); 345 } 346 347 String expected = 348 "<head>\n<title>Title</title><!--A comment--><meta name=\"author\" content=\"ģ&\" />" 349 + "<base href=\"http://maven.apache.org/\" /></head>"; 350 String actual = writer.toString(); 351 assertTrue( actual, actual.indexOf( expected ) != -1 ); 352 } 353 354 /** {@inheritDoc} */ 355 protected String getCommentBlock( String text ) 356 { 357 return "<!--" + toXmlComment( text ) + "-->"; 358 } 359}