1 27 package org.htmlparser.tests.lexerTests; 28 29 import java.io.BufferedInputStream ; 30 import java.io.ByteArrayInputStream ; 31 import java.io.IOException ; 32 import java.io.InputStreamReader ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.net.URLConnection ; 36 import org.htmlparser.lexer.InputStreamSource; 37 38 import org.htmlparser.lexer.Stream; 39 import org.htmlparser.lexer.Source; 40 import org.htmlparser.lexer.StringSource; 41 import org.htmlparser.tests.ParserTestCase; 42 43 public class SourceTests extends ParserTestCase 44 { 45 static 46 { 47 System.setProperty ("org.htmlparser.tests.lexerTests.SourceTests", "SourceTests"); 48 } 49 50 56 public static final String DEFAULT_CHARSET = "ISO-8859-1"; 57 58 61 public SourceTests (String name) 62 { 63 super (name); 64 } 65 66 69 public void testInputStreamSourceNull () throws IOException 70 { 71 Source source; 72 73 source = new InputStreamSource (null); 74 assertTrue ("erroneous character", -1 == source.read ()); 75 } 76 77 80 public void testInputStreamSourceEmpty () throws IOException 81 { 82 Source source; 83 84 source = new InputStreamSource (new Stream (new ByteArrayInputStream (new byte[0])), null); 85 assertTrue ("erroneous character", -1 == source.read ()); 86 } 87 88 91 public void testInputStreamSourceOneByte () throws IOException 92 { 93 Source source; 94 95 source = new InputStreamSource (new Stream (new ByteArrayInputStream (new byte[] { (byte)0x42 })), null); 96 assertTrue ("erroneous character", 'B' == source.read ()); 97 assertTrue ("extra character", -1 == source.read ()); 98 } 99 100 103 public void testInputStreamSourceClose () throws IOException 104 { 105 Source source; 106 107 source = new InputStreamSource (new Stream (new ByteArrayInputStream ("hello word".getBytes ())), null); 108 assertTrue ("no character", -1 != source.read ()); 109 source.destroy (); 110 try 111 { 112 source.read (); 113 fail ("not closed"); 114 } 115 catch (IOException ioe) 116 { 117 } 119 } 120 121 124 public void testInputStreamSourceReset () throws IOException 125 { 126 String reference; 127 Source source; 128 StringBuffer buffer; 129 int c; 130 131 reference = "Now is the time for all good men to come to the aid of the party"; 132 source = new InputStreamSource (new Stream (new ByteArrayInputStream (reference.getBytes (DEFAULT_CHARSET))), null); 133 buffer = new StringBuffer (reference.length ()); 134 while (-1 != (c = source.read ())) 135 buffer.append ((char)c); 136 assertTrue ("string incorrect", reference.equals (buffer.toString ())); 137 source.reset (); 138 buffer.setLength (0); 139 while (-1 != (c = source.read ())) 140 buffer.append ((char)c); 141 assertTrue ("string incorrect", reference.equals (buffer.toString ())); 142 source.close (); 143 } 144 145 148 public void testInputStreamSourceMidReset () throws IOException 149 { 150 String reference; 151 Source source; 152 StringBuffer buffer; 153 int c; 154 155 reference = "Now is the time for all good men to come to the aid of the party"; 156 source = new InputStreamSource (new Stream (new ByteArrayInputStream (reference.getBytes (DEFAULT_CHARSET))), null); 157 buffer = new StringBuffer (reference.length ()); 158 for (int i = 0; i < 25; i++) 159 buffer.append ((char)source.read ()); 160 source.reset (); 161 for (int i = 0; i < 25; i++) 162 source.read (); 163 while (-1 != (c = source.read ())) 164 buffer.append ((char)c); 165 assertTrue ("string incorrect", reference.equals (buffer.toString ())); 166 source.close (); 167 } 168 169 172 public void testInputStreamSourceMarkReset () throws IOException 173 { 174 String reference; 175 Source source; 176 StringBuffer buffer; 177 int c; 178 179 reference = "Now is the time for all good men to come to the aid of the party"; 180 source = new InputStreamSource (new Stream (new ByteArrayInputStream (reference.getBytes (DEFAULT_CHARSET))), null); 181 assertTrue ("not markable", source.markSupported ()); 182 buffer = new StringBuffer (reference.length ()); 183 for (int i = 0; i < 25; i++) 184 buffer.append ((char)source.read ()); 185 source.mark (88); 186 for (int i = 0; i < 25; i++) 187 source.read (); 188 source.reset (); 189 while (-1 != (c = source.read ())) 190 buffer.append ((char)c); 191 assertTrue ("string incorrect", reference.equals (buffer.toString ())); 192 source.close (); 193 } 194 195 198 public void testInputStreamSourceSkip () throws IOException 199 { 200 String part1; 201 String part2; 202 String part3; 203 String reference; 204 Source source; 205 StringBuffer buffer; 206 int c; 207 208 part1 = "Now is the time "; 209 part2 = "for all good men "; 210 part3 = "to come to the aid of the party"; 211 reference = part1 + part2 + part3; 212 source = new InputStreamSource (new Stream (new ByteArrayInputStream (reference.getBytes (DEFAULT_CHARSET))), null); 213 buffer = new StringBuffer (reference.length ()); 214 for (int i = 0; i < part1.length (); i++) 215 buffer.append ((char)source.read ()); 216 source.skip (part2.length ()); 217 while (-1 != (c = source.read ())) 218 buffer.append ((char)c); 219 assertTrue ("string incorrect", (part1 + part3).equals (buffer.toString ())); 220 source.close (); 221 } 222 223 226 public void testInputStreamSourceMultByte () throws IOException 227 { 228 String reference; 229 Source source; 230 char[] buffer; 231 232 reference = "Now is the time for all good men to come to the aid of the party"; 233 source = new InputStreamSource (new Stream (new ByteArrayInputStream (reference.getBytes (DEFAULT_CHARSET))), null); 234 buffer = new char[reference.length ()]; 235 source.read (buffer, 0, buffer.length); 236 assertTrue ("string incorrect", reference.equals (new String (buffer))); 237 assertTrue ("extra character", -1 == source.read ()); 238 source.close (); 239 } 240 241 244 public void testInputStreamSourcePositionedMultByte () throws IOException 245 { 246 String part1; 247 String part2; 248 String part3; 249 String reference; 250 Source source; 251 char[] buffer; 252 int length; 253 254 part1 = "Now is the time "; 255 part2 = "for all good men "; 256 part3 = "to come to the aid of the party"; 257 reference = part1 + part2 + part3; 258 source = new InputStreamSource (new Stream (new ByteArrayInputStream (reference.getBytes (DEFAULT_CHARSET))), null); 259 buffer = new char[reference.length ()]; 260 for (int i = 0; i < part1.length (); i++) 261 buffer[i] = (char)source.read (); 262 length = source.read (buffer, part1.length (), part2.length ()); 263 assertTrue ("incorrect length", part2.length () == length); 264 length += part1.length (); 265 for (int i = 0; i < part3.length (); i++) 266 buffer[i + length] = (char)source.read (); 267 assertTrue ("string incorrect", reference.equals (new String (buffer))); 268 assertTrue ("extra character", -1 == source.read ()); 269 source.close (); 270 } 271 272 275 public void testInputStreamSourceReady () throws IOException 276 { 277 Source source; 278 279 source = new InputStreamSource (new Stream (new ByteArrayInputStream (new byte[] { (byte)0x42, (byte)0x62 })), null); 280 assertTrue ("ready?", !source.ready ()); 281 assertTrue ("erroneous character", 'B' == source.read ()); 282 assertTrue ("not ready", source.ready ()); 283 assertTrue ("erroneous character", 'b' == source.read ()); 284 assertTrue ("ready?", !source.ready ()); 285 assertTrue ("extra character", -1 == source.read ()); 286 } 287 288 291 public void testSameChars () throws IOException 292 { 293 String link; 294 URL url; 295 URLConnection connection1; 296 URLConnection connection2; 297 InputStreamReader in; 298 int c1; 299 int c2; 300 Source source; 301 int index; 302 303 link = "http://htmlparser.sourceforge.net"; 304 try 305 { 306 url = new URL (link); 307 connection1 = url.openConnection (); 308 connection1.connect (); 309 in = new InputStreamReader (new BufferedInputStream (connection1.getInputStream ()), "UTF-8"); 310 connection2 = url.openConnection (); 311 connection2.connect (); 312 source = new InputStreamSource (new Stream (connection2.getInputStream ()), "UTF-8"); 313 index = 0; 314 while (-1 != (c1 = in.read ())) 315 { 316 c2 = source.read (); 317 if (c1 != c2) 318 fail ("characters differ at position " + index + ", expected " + c1 + ", actual " + c2); 319 index++; 320 } 321 c2 = source.read (); 322 assertTrue ("extra characters", -1 == c2); 323 source.close (); 324 in.close (); 325 } 326 catch (MalformedURLException murle) 327 { 328 fail ("bad url " + link); 329 } 330 } 331 332 335 public void testStringSourceNull () throws IOException 336 { 337 Source source; 338 339 source = new StringSource (null); 340 assertTrue ("erroneous character", -1 == source.read ()); 341 } 342 343 346 public void testStringSourceEmpty () throws IOException 347 { 348 Source source; 349 350 source = new StringSource (""); 351 assertTrue ("erroneous character", -1 == source.read ()); 352 } 353 354 357 public void testStringSourceOneCharacter () throws IOException 358 { 359 Source source; 360 361 source = new StringSource (new String ("B")); 362 assertTrue ("erroneous character", 'B' == source.read ()); 363 assertTrue ("extra character", -1 == source.read ()); 364 } 365 366 369 public void testStringSourceClose () throws IOException 370 { 371 Source source; 372 373 source = new StringSource ("hello word"); 374 assertTrue ("no character", -1 != source.read ()); 375 source.destroy (); 376 try 377 { 378 source.read (); 379 fail ("not closed"); 380 } 381 catch (IOException ioe) 382 { 383 } 385 } 386 387 390 public void testStringSourceReset () throws IOException 391 { 392 String reference; 393 Source source; 394 StringBuffer buffer; 395 int c; 396 397 reference = "Now is the time for all good men to come to the aid of the party"; 398 source = new StringSource (reference); 399 buffer = new StringBuffer (reference.length ()); 400 while (-1 != (c = source.read ())) 401 buffer.append ((char)c); 402 assertTrue ("string incorrect", reference.equals (buffer.toString ())); 403 source.reset (); 404 buffer.setLength (0); 405 while (-1 != (c = source.read ())) 406 buffer.append ((char)c); 407 assertTrue ("string incorrect", reference.equals (buffer.toString ())); 408 source.close (); 409 } 410 411 414 public void testStringSourceMidReset () throws IOException 415 { 416 String reference; 417 Source source; 418 StringBuffer buffer; 419 int c; 420 421 reference = "Now is the time for all good men to come to the aid of the party"; 422 source = new StringSource (reference); 423 buffer = new StringBuffer (reference.length ()); 424 for (int i = 0; i < 25; i++) 425 buffer.append ((char)source.read ()); 426 source.reset (); 427 for (int i = 0; i < 25; i++) 428 source.read (); 429 while (-1 != (c = source.read ())) 430 buffer.append ((char)c); 431 assertTrue ("string incorrect", reference.equals (buffer.toString ())); 432 source.close (); 433 } 434 435 438 public void testStringSourceMarkReset () throws IOException 439 { 440 String reference; 441 Source source; 442 StringBuffer buffer; 443 int c; 444 445 reference = "Now is the time for all good men to come to the aid of the party"; 446 source = new StringSource (reference); 447 assertTrue ("not markable", source.markSupported ()); 448 buffer = new StringBuffer (reference.length ()); 449 for (int i = 0; i < 25; i++) 450 buffer.append ((char)source.read ()); 451 source.mark (88); 452 for (int i = 0; i < 25; i++) 453 source.read (); 454 source.reset (); 455 while (-1 != (c = source.read ())) 456 buffer.append ((char)c); 457 assertTrue ("string incorrect", reference.equals (buffer.toString ())); 458 source.close (); 459 } 460 461 464 public void testStringSourceSkip () throws IOException 465 { 466 String part1; 467 String part2; 468 String part3; 469 String reference; 470 Source source; 471 StringBuffer buffer; 472 int c; 473 474 part1 = "Now is the time "; 475 part2 = "for all good men "; 476 part3 = "to come to the aid of the party"; 477 reference = part1 + part2 + part3; 478 source = new StringSource (reference); 479 buffer = new StringBuffer (reference.length ()); 480 for (int i = 0; i < part1.length (); i++) 481 buffer.append ((char)source.read ()); 482 source.skip (part2.length ()); 483 while (-1 != (c = source.read ())) 484 buffer.append ((char)c); 485 assertTrue ("string incorrect", (part1 + part3).equals (buffer.toString ())); 486 source.close (); 487 } 488 489 492 public void testStringSourceMultByte () throws IOException 493 { 494 String reference; 495 Source source; 496 char[] buffer; 497 498 reference = "Now is the time for all good men to come to the aid of the party"; 499 source = new StringSource (reference); 500 buffer = new char[reference.length ()]; 501 source.read (buffer, 0, buffer.length); 502 assertTrue ("string incorrect", reference.equals (new String (buffer))); 503 assertTrue ("extra character", -1 == source.read ()); 504 source.close (); 505 } 506 507 510 public void testStringSourcePositionedMultByte () throws IOException 511 { 512 String part1; 513 String part2; 514 String part3; 515 String reference; 516 Source source; 517 char[] buffer; 518 int length; 519 520 part1 = "Now is the time "; 521 part2 = "for all good men "; 522 part3 = "to come to the aid of the party"; 523 reference = part1 + part2 + part3; 524 source = new StringSource (reference); 525 buffer = new char[reference.length ()]; 526 for (int i = 0; i < part1.length (); i++) 527 buffer[i] = (char)source.read (); 528 length = source.read (buffer, part1.length (), part2.length ()); 529 assertTrue ("incorrect length", part2.length () == length); 530 length += part1.length (); 531 for (int i = 0; i < part3.length (); i++) 532 buffer[i + length] = (char)source.read (); 533 assertTrue ("string incorrect", reference.equals (new String (buffer))); 534 assertTrue ("extra character", -1 == source.read ()); 535 source.close (); 536 } 537 538 541 public void testStringSourceReady () throws IOException 542 { 543 Source source; 544 545 source = new StringSource ("Bb"); 546 assertTrue ("ready?", source.ready ()); 547 assertTrue ("erroneous character", 'B' == source.read ()); 548 assertTrue ("not ready", source.ready ()); 549 assertTrue ("erroneous character", 'b' == source.read ()); 550 assertTrue ("ready?", !source.ready ()); 551 assertTrue ("extra character", -1 == source.read ()); 552 } 553 } 554 | Popular Tags |