001package org.apache.maven.doxia.module.twiki.parser; 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 static org.junit.Assert.assertArrayEquals; 023 024/** 025 * tests the WikiWord parsing (and things like that) 026 * 027 * @author Juan F. Codagnone 028 * @since Nov 4, 2005 029 */ 030public class WordsTest 031 extends AbstractBlockTestCase 032{ 033 /** 034 * used to convert lists to arrays 035 */ 036 private static final Block[] TOARRAY = new Block[] {}; 037 038 /** 039 * Resolves links for wikiWords 040 */ 041 private final WikiWordLinkResolver resolver = new XHTMLWikiWordLinkResolver(); 042 043 /** 044 * ... 045 */ 046 public final void testText() 047 { 048 Block[] blocks, expected; 049 050 expected = new Block[] { new TextBlock( " Some text " ) }; 051 blocks = (Block[]) textParser.parse( " Some text " ).toArray( TOARRAY ); 052 assertArrayEquals( expected, blocks ); 053 } 054 055 /** 056 * ... 057 */ 058 public final void testWikiWords() 059 { 060 Block[] blocks, expected; 061 062 expected = new Block[] { new WikiWordBlock( "WikiWord", resolver ) }; 063 blocks = (Block[]) textParser.parse( "WikiWord" ).toArray( TOARRAY ); 064 assertArrayEquals( expected, blocks ); 065 066 // this is not a wiki word 067 expected = new Block[] { new TextBlock( "Wiki" ) }; 068 blocks = (Block[]) textParser.parse( "Wiki" ).toArray( TOARRAY ); 069 assertArrayEquals( expected, blocks ); 070 071 expected = new Block[] { new TextBlock( "Web." ) }; 072 blocks = (Block[]) textParser.parse( "Web." ).toArray( TOARRAY ); 073 assertArrayEquals( expected, blocks ); 074 075 expected = new Block[] { new TextBlock( "fooWikiBar" ) }; 076 blocks = (Block[]) textParser.parse( "fooWikiBar" ).toArray( TOARRAY ); 077 assertArrayEquals( expected, blocks ); 078 079 expected = new Block[] { new WikiWordBlock( "WikiWord", resolver ), new TextBlock( "...." ) }; 080 blocks = (Block[]) textParser.parse( "WikiWord...." ).toArray( TOARRAY ); 081 assertArrayEquals( expected, blocks ); 082 } 083 084 /** 085 * ... 086 */ 087 public final void testWebWikiWords() 088 { 089 Block[] blocks, expected; 090 091 expected = new Block[] { new WikiWordBlock( "Web.WikiWord", resolver ) }; 092 blocks = (Block[]) textParser.parse( "Web.WikiWord" ).toArray( TOARRAY ); 093 assertArrayEquals( expected, blocks ); 094 095 expected = new Block[] { new WikiWordBlock( "My1Web.WikiWord", resolver ) }; 096 blocks = (Block[]) textParser.parse( "My1Web.WikiWord" ).toArray( TOARRAY ); 097 assertArrayEquals( expected, blocks ); 098 } 099 100 /** 101 * ... 102 */ 103 public final void testWebAnchorWikiWords() 104 { 105 Block[] blocks, expected; 106 107 expected = new Block[] { new WikiWordBlock( "WikiWord#anchor", resolver ) }; 108 blocks = (Block[]) textParser.parse( "WikiWord#anchor" ).toArray( TOARRAY ); 109 assertArrayEquals( expected, blocks ); 110 111 expected = new Block[] { new WikiWordBlock( "MyWeb.WikiWord#anchor", resolver ) }; 112 blocks = (Block[]) textParser.parse( "MyWeb.WikiWord#anchor" ).toArray( TOARRAY ); 113 assertArrayEquals( expected, blocks ); 114 115 } 116 117 /** 118 * test Specific Links 119 */ 120 public final void testURLSpecificLinks() 121 { 122 Block[] blocks, expected; 123 124 expected = new Block[] { new LinkBlock( "http://reference.com", new TextBlock( "text" ) ) }; 125 blocks = (Block[]) textParser.parse( "[[http://reference.com][text]]" ).toArray( TOARRAY ); 126 assertArrayEquals( expected, blocks ); 127 128 expected = 129 new Block[] { new TextBlock( "foo" ), 130 new LinkBlock( "http://reference.com", new TextBlock( "text" ) ), new TextBlock( "bar" ) }; 131 blocks = (Block[]) textParser.parse( "foo[[http://reference.com][text]]bar" ).toArray( TOARRAY ); 132 assertArrayEquals( expected, blocks ); 133 134 expected = 135 new Block[] { new TextBlock( " foo " ), 136 new LinkBlock( "http://reference.com", new TextBlock( "text" ) ), new TextBlock( " bar " ) }; 137 blocks = (Block[]) textParser.parse( " foo [[http://reference.com][text]] bar " ).toArray( TOARRAY ); 138 assertArrayEquals( expected, blocks ); 139 140 expected = 141 new Block[] { 142 new LinkBlock( "http://www.apache.org/licenses/LICENSE-2.0", 143 new TextBlock( "Apache License, version 2.0" ) ), 144 new TextBlock( ". You can download it " ), 145 new WikiWordBlock( "DoxiaDownload", new TextBlock( "here" ), resolver ) }; 146 blocks = 147 (Block[]) textParser.parse( 148 "[[http://www.apache.org/licenses/LICENSE-2.0]" 149 + "[Apache License, version 2.0]]. You can download it " 150 + "[[DoxiaDownload][here]]" ).toArray( TOARRAY ); 151 assertArrayEquals( expected, blocks ); 152 153 } 154 155 /** 156 * test Specific Links with wikiWords 157 */ 158 public final void testWikiSpecificLinks() 159 { 160 Block[] blocks, expected; 161 162 expected = new Block[] { new WikiWordBlock( "Reference", new TextBlock( "text" ), resolver ) }; 163 blocks = (Block[]) textParser.parse( "[[reference][text]]" ).toArray( TOARRAY ); 164 assertArrayEquals( expected, blocks ); 165 166 expected = 167 new Block[] { new TextBlock( "foo" ), 168 new WikiWordBlock( "ReferenceLink", new TextBlock( "text" ), resolver ), new TextBlock( "bar" ) }; 169 blocks = (Block[]) textParser.parse( "foo[[referenceLink][text]]bar" ).toArray( TOARRAY ); 170 assertArrayEquals( expected, blocks ); 171 172 expected = 173 new Block[] { new TextBlock( " foo " ), 174 new WikiWordBlock( "ReferenceLink", new TextBlock( "text" ), resolver ), new TextBlock( " bar " ) }; 175 blocks = (Block[]) textParser.parse( " foo [[reference link][text]] bar " ).toArray( TOARRAY ); 176 assertArrayEquals( expected, blocks ); 177 } 178 179 /** 180 * test Specific Links 181 */ 182 public final void testSpecificLinkPrevention() 183 { 184 Block[] blocks, expected; 185 186 expected = new Block[] { new TextBlock( "[[reference][text]]" ) }; 187 blocks = (Block[]) textParser.parse( "![[reference][text]]" ).toArray( TOARRAY ); 188 assertArrayEquals( expected, blocks ); 189 } 190 191 /** 192 * ... 193 */ 194 public final void testPreventLinkingWikiWord() 195 { 196 Block[] blocks, expected; 197 198 expected = new Block[] { new TextBlock( " " ), new TextBlock( "WikiWord" ), new TextBlock( " " ) }; 199 blocks = (Block[]) textParser.parse( " !WikiWord " ).toArray( TOARRAY ); 200 assertArrayEquals( expected, blocks ); 201 202 expected = new Block[] { new TextBlock( " !!WikiWord " ) }; 203 blocks = (Block[]) textParser.parse( " !!WikiWord " ).toArray( TOARRAY ); 204 assertArrayEquals( expected, blocks ); 205 } 206 207 /** 208 * ej [[Main.TWiki rules]] would be wikiword Main.TWikiRules 209 */ 210 public final void testForcedLinks() 211 { 212 Block[] blocks, expected; 213 214 expected = new Block[] { new WikiWordBlock( "WikiSyntax", new TextBlock( "wiki syntax" ), resolver ) }; 215 blocks = (Block[]) textParser.parse( "[[wiki syntax]]" ).toArray( TOARRAY ); 216 assertArrayEquals( expected, blocks ); 217 218 expected = new Block[] { new TextBlock( "[[wiki syntax]]" ) }; 219 blocks = (Block[]) textParser.parse( "![[wiki syntax]]" ).toArray( TOARRAY ); 220 assertArrayEquals( expected, blocks ); 221 222 expected = 223 new Block[] { new TextBlock( "foo" ), 224 new WikiWordBlock( "WikiSyntax", new TextBlock( "wiki syntax" ), resolver ), 225 new TextBlock( "bar" ) }; 226 blocks = (Block[]) textParser.parse( "foo[[wiki syntax]]bar" ).toArray( TOARRAY ); 227 assertArrayEquals( expected, blocks ); 228 229 expected = 230 new Block[] { new TextBlock( "foo" ), 231 new LinkBlock( "http://twiki.com", new TextBlock( "http://twiki.com" ) ), new TextBlock( "bar" ) }; 232 blocks = (Block[]) textParser.parse( "foo[[http://twiki.com]]bar" ).toArray( TOARRAY ); 233 assertArrayEquals( expected, blocks ); 234 } 235 236 /** 237 * ... 238 */ 239 public final void testMailtoForcedLinks() 240 { 241 Block[] blocks, expected; 242 243 expected = new Block[] { new LinkBlock( "mailto:a@z.com", new TextBlock( "Mail" ) ) }; 244 blocks = (Block[]) textParser.parse( "[[mailto:a@z.com Mail]]" ).toArray( TOARRAY ); 245 assertArrayEquals( expected, blocks ); 246 } 247 248 /** 249 * ... 250 */ 251 public final void testAnchors() 252 { 253 Block[] blocks, expected; 254 255 expected = new Block[] { new TextBlock( "mary has #anchor a little lamb" ) }; 256 blocks = (Block[]) textParser.parse( "mary has #anchor a little lamb" ).toArray( TOARRAY ); 257 assertArrayEquals( expected, blocks ); 258 259 expected = 260 new Block[] { new TextBlock( "mary has " ), new AnchorBlock( "AnchorName" ), 261 new TextBlock( " a little lamb" ) }; 262 blocks = (Block[]) textParser.parse( "mary has #AnchorName a little lamb" ).toArray( TOARRAY ); 263 assertArrayEquals( expected, blocks ); 264 265 expected = new Block[] { new TextBlock( "mary has #AnchorName1233 a little lamb" ) }; 266 blocks = (Block[]) textParser.parse( "mary has #AnchorName1233 a little lamb" ).toArray( TOARRAY ); 267 assertArrayEquals( expected, blocks ); 268 } 269 270 /** 271 * unit test 272 */ 273 public final void testAutomaticLink() 274 { 275 Block[] blocks, expected; 276 277 expected = 278 new Block[] { new TextBlock( "Go to " ), 279 new LinkBlock( "http://twiki.com", new TextBlock( "http://twiki.com" ) ), 280 new TextBlock( " and ..." ) }; 281 blocks = (Block[]) textParser.parse( "Go to http://twiki.com and ..." ).toArray( TOARRAY ); 282 assertArrayEquals( expected, blocks ); 283 } 284 285 /** unit test */ 286 public final void testAutomaticImage() 287 { 288 Block[] blocks, expected; 289 290 expected = 291 new Block[] { new LinkBlock( "http://twiki.org", new ImageBlock( "http://twiki.org/logo.png" ) ) }; 292 blocks = (Block[]) textParser.parse( "[[http://twiki.org][http://twiki.org/logo.png]]" ).toArray( TOARRAY ); 293 assertArrayEquals( expected, blocks ); 294 } 295 296 /** unit test */ 297 public final void testLinkImage() 298 { 299 Block[] blocks, expected; 300 301 expected = 302 new Block[] { new TextBlock( "Go to " ), new ImageBlock( "http://twiki.com/image.png" ), 303 new TextBlock( " thisisnotanimage.png and ..." ) }; 304 blocks = 305 (Block[]) textParser.parse( "Go to http://twiki.com/image.png " + "thisisnotanimage.png and ..." ) 306 .toArray( TOARRAY ); 307 assertArrayEquals( expected, blocks ); 308 } 309 310 /** 311 * Test image inserted with a html img tag 312 */ 313 public final void testRelativeImage() 314 { 315 Block[] blocks, expected; 316 317 expected = 318 new Block[] { new TextBlock( "My summer house: " ), new ImageBlock( "images/summerhouse.png" ), 319 new TextBlock( " isn't it great?!" ) }; 320 blocks = 321 (Block[]) textParser 322 .parse( 323 "My summer house: <img class=\"some_class\" src=\"images/summerhouse.png\"/> isn't it great?!" ) 324 .toArray( TOARRAY ); 325 assertArrayEquals( expected, blocks ); 326 } 327}