KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > lexerTests > SourceTests


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/lexerTests/SourceTests.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:31 $
10
// $Revision: 1.18 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests.lexerTests;
28
29 import java.io.BufferedInputStream JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.net.URLConnection JavaDoc;
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     /**
51      * The default charset.
52      * This should be <code>ISO-8859-1</code>,
53      * see RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt?number=2616) section 3.7.1
54      * Another alias is "8859_1".
55      */

56     public static final String JavaDoc DEFAULT_CHARSET = "ISO-8859-1";
57
58     /**
59      * Test the first level stream class.
60      */

61     public SourceTests (String JavaDoc name)
62     {
63         super (name);
64     }
65
66     /**
67      * Test initialization with a null value.
68      */

69     public void testInputStreamSourceNull () throws IOException JavaDoc
70     {
71         Source source;
72
73         source = new InputStreamSource (null);
74         assertTrue ("erroneous character", -1 == source.read ());
75     }
76
77     /**
78      * Test initialization of a InputStreamSource with a zero length byte array.
79      */

80     public void testInputStreamSourceEmpty () throws IOException JavaDoc
81     {
82         Source source;
83
84         source = new InputStreamSource (new Stream JavaDoc (new ByteArrayInputStream JavaDoc (new byte[0])), null);
85         assertTrue ("erroneous character", -1 == source.read ());
86     }
87
88     /**
89      * Test initialization of a InputStreamSource with an input stream having only one byte.
90      */

91     public void testInputStreamSourceOneByte () throws IOException JavaDoc
92     {
93         Source source;
94
95         source = new InputStreamSource (new Stream JavaDoc (new ByteArrayInputStream JavaDoc (new byte[] { (byte)0x42 })), null);
96         assertTrue ("erroneous character", 'B' == source.read ());
97         assertTrue ("extra character", -1 == source.read ());
98     }
99
100     /**
101      * Test closing a InputStreamSource.
102      */

103     public void testInputStreamSourceClose () throws IOException JavaDoc
104     {
105         Source source;
106
107         source = new InputStreamSource (new Stream JavaDoc (new ByteArrayInputStream JavaDoc ("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 JavaDoc ioe)
116         {
117             // expected outcome
118
}
119    }
120
121     /**
122      * Test resetting a InputStreamSource.
123      */

124     public void testInputStreamSourceReset () throws IOException JavaDoc
125     {
126         String JavaDoc reference;
127         Source source;
128         StringBuffer JavaDoc 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 JavaDoc (new ByteArrayInputStream JavaDoc (reference.getBytes (DEFAULT_CHARSET))), null);
133         buffer = new StringBuffer JavaDoc (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     /**
146      * Test resetting a InputStreamSource in the middle of reading.
147      */

148     public void testInputStreamSourceMidReset () throws IOException JavaDoc
149     {
150         String JavaDoc reference;
151         Source source;
152         StringBuffer JavaDoc 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 JavaDoc (new ByteArrayInputStream JavaDoc (reference.getBytes (DEFAULT_CHARSET))), null);
157         buffer = new StringBuffer JavaDoc (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     /**
170      * Test mark/reset of a InputStreamSource in the middle of reading.
171      */

172     public void testInputStreamSourceMarkReset () throws IOException JavaDoc
173     {
174         String JavaDoc reference;
175         Source source;
176         StringBuffer JavaDoc 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 JavaDoc (new ByteArrayInputStream JavaDoc (reference.getBytes (DEFAULT_CHARSET))), null);
181         assertTrue ("not markable", source.markSupported ());
182         buffer = new StringBuffer JavaDoc (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     /**
196      * Test skipping a InputStreamSource.
197      */

198     public void testInputStreamSourceSkip () throws IOException JavaDoc
199     {
200         String JavaDoc part1;
201         String JavaDoc part2;
202         String JavaDoc part3;
203         String JavaDoc reference;
204         Source source;
205         StringBuffer JavaDoc 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 JavaDoc (new ByteArrayInputStream JavaDoc (reference.getBytes (DEFAULT_CHARSET))), null);
213         buffer = new StringBuffer JavaDoc (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     /**
224      * Test multi-byte read with a InputStreamSource.
225      */

226     public void testInputStreamSourceMultByte () throws IOException JavaDoc
227     {
228         String JavaDoc 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 JavaDoc (new ByteArrayInputStream JavaDoc (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 JavaDoc (buffer)));
237         assertTrue ("extra character", -1 == source.read ());
238         source.close ();
239     }
240
241     /**
242      * Test positioned multi-byte read with a InputStreamSource.
243      */

244     public void testInputStreamSourcePositionedMultByte () throws IOException JavaDoc
245     {
246         String JavaDoc part1;
247         String JavaDoc part2;
248         String JavaDoc part3;
249         String JavaDoc 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 JavaDoc (new ByteArrayInputStream JavaDoc (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 JavaDoc (buffer)));
268         assertTrue ("extra character", -1 == source.read ());
269         source.close ();
270     }
271
272     /**
273      * Test ready of a InputStreamSource.
274      */

275     public void testInputStreamSourceReady () throws IOException JavaDoc
276     {
277         Source source;
278
279         source = new InputStreamSource (new Stream JavaDoc (new ByteArrayInputStream JavaDoc (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     /**
289      * Test that the same characters are returned as with another reader.
290      */

291     public void testSameChars () throws IOException JavaDoc
292     {
293         String JavaDoc link;
294         URL JavaDoc url;
295         URLConnection JavaDoc connection1;
296         URLConnection JavaDoc connection2;
297         InputStreamReader JavaDoc 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 JavaDoc (link);
307             connection1 = url.openConnection ();
308             connection1.connect ();
309             in = new InputStreamReader JavaDoc (new BufferedInputStream JavaDoc (connection1.getInputStream ()), "UTF-8");
310             connection2 = url.openConnection ();
311             connection2.connect ();
312             source = new InputStreamSource (new Stream JavaDoc (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 JavaDoc murle)
327         {
328             fail ("bad url " + link);
329         }
330     }
331
332     /**
333      * Test initialization of a StringSource with a null value.
334      */

335     public void testStringSourceNull () throws IOException JavaDoc
336     {
337         Source source;
338
339         source = new StringSource (null);
340         assertTrue ("erroneous character", -1 == source.read ());
341     }
342
343     /**
344      * Test initialization of a StringSource with a zero length string.
345      */

346     public void testStringSourceEmpty () throws IOException JavaDoc
347     {
348         Source source;
349
350         source = new StringSource ("");
351         assertTrue ("erroneous character", -1 == source.read ());
352     }
353
354     /**
355      * Test initialization of a StringSource with a one character string.
356      */

357     public void testStringSourceOneCharacter () throws IOException JavaDoc
358     {
359         Source source;
360
361         source = new StringSource (new String JavaDoc ("B"));
362         assertTrue ("erroneous character", 'B' == source.read ());
363         assertTrue ("extra character", -1 == source.read ());
364     }
365
366     /**
367      * Test closing a StringSource.
368      */

369     public void testStringSourceClose () throws IOException JavaDoc
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 JavaDoc ioe)
382         {
383             // expected outcome
384
}
385    }
386
387     /**
388      * Test resetting a StringSource.
389      */

390     public void testStringSourceReset () throws IOException JavaDoc
391     {
392         String JavaDoc reference;
393         Source source;
394         StringBuffer JavaDoc 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 JavaDoc (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     /**
412      * Test resetting a StringSource in the middle of reading.
413      */

414     public void testStringSourceMidReset () throws IOException JavaDoc
415     {
416         String JavaDoc reference;
417         Source source;
418         StringBuffer JavaDoc 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 JavaDoc (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     /**
436      * Test mark/reset of a StringSource in the middle of reading.
437      */

438     public void testStringSourceMarkReset () throws IOException JavaDoc
439     {
440         String JavaDoc reference;
441         Source source;
442         StringBuffer JavaDoc 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 JavaDoc (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     /**
462      * Test skipping a StringSource.
463      */

464     public void testStringSourceSkip () throws IOException JavaDoc
465     {
466         String JavaDoc part1;
467         String JavaDoc part2;
468         String JavaDoc part3;
469         String JavaDoc reference;
470         Source source;
471         StringBuffer JavaDoc 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 JavaDoc (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     /**
490      * Test multi-byte read with a StringSource.
491      */

492     public void testStringSourceMultByte () throws IOException JavaDoc
493     {
494         String JavaDoc 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 JavaDoc (buffer)));
503         assertTrue ("extra character", -1 == source.read ());
504         source.close ();
505     }
506
507     /**
508      * Test positioned multi-byte read with a StringSource.
509      */

510     public void testStringSourcePositionedMultByte () throws IOException JavaDoc
511     {
512         String JavaDoc part1;
513         String JavaDoc part2;
514         String JavaDoc part3;
515         String JavaDoc 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 JavaDoc (buffer)));
534         assertTrue ("extra character", -1 == source.read ());
535         source.close ();
536     }
537
538     /**
539      * Test ready of a StringSource.
540      */

541     public void testStringSourceReady () throws IOException JavaDoc
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