KickJava   Java API By Example, From Geeks To Geeks.

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


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/PageTests.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.IOException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLConnection JavaDoc;
32
33 import org.htmlparser.lexer.Page;
34 import org.htmlparser.tests.ParserTestCase;
35 import org.htmlparser.util.ParserException;
36
37 public class PageTests extends ParserTestCase
38 {
39     static
40     {
41         System.setProperty ("org.htmlparser.tests.lexerTests.PageTests", "PageTests");
42     }
43
44     /**
45      * The default charset.
46      * This should be <code>ISO-8859-1</code>,
47      * see RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt?number=2616) section 3.7.1
48      * Another alias is "8859_1".
49      */

50     public static final String JavaDoc DEFAULT_CHARSET = "ISO-8859-1";
51
52     /**
53      * Base URI for absolute URL tests.
54      */

55     static final String JavaDoc BASEURI = "http://a/b/c/d;p?q";
56
57     /**
58      * Page for absolute URL tests.
59      */

60     public static Page mPage;
61     static
62     {
63         mPage = new Page ();
64         mPage.setBaseUrl (BASEURI);
65     }
66         
67     /**
68      * Test the third level page class.
69      */

70     public PageTests (String JavaDoc name)
71     {
72         super (name);
73     }
74
75     /**
76      * Test initialization with a null value.
77      */

78     public void testNull () throws ParserException
79     {
80         try
81         {
82             new Page ((URLConnection JavaDoc)null);
83             assertTrue ("null value in constructor", false);
84         }
85         catch (IllegalArgumentException JavaDoc iae)
86         {
87             // expected outcome
88
}
89
90         try
91         {
92             new Page ((String JavaDoc)null);
93             assertTrue ("null value in constructor", false);
94         }
95         catch (IllegalArgumentException JavaDoc iae)
96         {
97             // expected outcome
98
}
99     }
100
101     /**
102      * Test initialization with a real value.
103      */

104     public void testURLConnection () throws ParserException, IOException JavaDoc
105     {
106         String JavaDoc link;
107         URL JavaDoc url;
108
109         link = "http://www.ibm.com/jp/";
110         url = new URL JavaDoc (link);
111         new Page (url.openConnection ());
112     }
113
114     /**
115      * Test initialization with non-existant URL.
116      */

117     public void testBadURLConnection () throws IOException JavaDoc
118     {
119         String JavaDoc link;
120         URL JavaDoc url;
121
122         link = "http://www.bigbogosity.org/";
123         url = new URL JavaDoc (link);
124         try
125         {
126            new Page (url.openConnection ());
127         }
128         catch (ParserException pe)
129         {
130             // expected response
131
}
132     }
133
134     //
135
// Tests from Appendix C Examples of Resolving Relative URI References
136
// RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax
137
// T. Berners-Lee et al.
138
// http://www.ietf.org/rfc/rfc2396.txt
139

140     // Within an object with a well-defined base URI of
141
// http://a/b/c/d;p?q
142
// the relative URI would be resolved as follows:
143

144     // C.1. Normal Examples
145
// g:h = g:h
146
// g = http://a/b/c/g
147
// ./g = http://a/b/c/g
148
// g/ = http://a/b/c/g/
149
// /g = http://a/g
150
// //g = http://g
151
// ?y = http://a/b/c/?y
152
// g?y = http://a/b/c/g?y
153
// #s = (current document)#s
154
// g#s = http://a/b/c/g#s
155
// g?y#s = http://a/b/c/g?y#s
156
// ;x = http://a/b/c/;x
157
// g;x = http://a/b/c/g;x
158
// g;x?y#s = http://a/b/c/g;x?y#s
159
// . = http://a/b/c/
160
// ./ = http://a/b/c/
161
// .. = http://a/b/
162
// ../ = http://a/b/
163
// ../g = http://a/b/g
164
// ../.. = http://a/
165
// ../../ = http://a/
166
// ../../g = http://a/g
167

168     public void test1 () throws ParserException
169     {
170         assertEquals ("test1 failed", "https:h", mPage.getAbsoluteURL ("https:h"));
171     }
172     public void test2 () throws ParserException
173     {
174         assertEquals ("test2 failed", "http://a/b/c/g", mPage.getAbsoluteURL ("g"));
175     }
176     public void test3 () throws ParserException
177     {
178         assertEquals ("test3 failed", "http://a/b/c/g", mPage.getAbsoluteURL ("./g"));
179     }
180     public void test4 () throws ParserException
181     {
182         assertEquals ("test4 failed", "http://a/b/c/g/", mPage.getAbsoluteURL ("g/"));
183     }
184     public void test5 () throws ParserException
185     {
186         assertEquals ("test5 failed", "http://a/g", mPage.getAbsoluteURL ("/g"));
187     }
188     public void test6 () throws ParserException
189     {
190         assertEquals ("test6 failed", "http://g", mPage.getAbsoluteURL ("//g"));
191     }
192     public void test7 () throws ParserException
193     {
194         assertEquals ("test7 failed", "http://a/b/c/?y", mPage.getAbsoluteURL ("?y"));
195     }
196     public void test8 () throws ParserException
197     {
198         assertEquals ("test8 failed", "http://a/b/c/g?y", mPage.getAbsoluteURL ("g?y"));
199     }
200     public void test9 () throws ParserException
201     {
202         assertEquals ("test9 failed", "https:h", mPage.getAbsoluteURL ("https:h"));
203     }
204     public void test10 () throws ParserException
205     {
206         assertEquals ("test10 failed", "https:h", mPage.getAbsoluteURL ("https:h"));
207     }
208     // #s = (current document)#s
209
public void test11 () throws ParserException
210     {
211         assertEquals ("test11 failed", "http://a/b/c/g#s", mPage.getAbsoluteURL ("g#s"));
212     }
213     public void test12 () throws ParserException
214     {
215         assertEquals ("test12 failed", "http://a/b/c/g?y#s", mPage.getAbsoluteURL ("g?y#s"));
216     }
217     public void test13 () throws ParserException
218     {
219         assertEquals ("test13 failed", "http://a/b/c/;x", mPage.getAbsoluteURL (";x"));
220     }
221     public void test14 () throws ParserException
222     {
223         assertEquals ("test14 failed", "http://a/b/c/g;x", mPage.getAbsoluteURL ("g;x"));
224     }
225     public void test15 () throws ParserException
226     {
227         assertEquals ("test15 failed", "http://a/b/c/g;x?y#s", mPage.getAbsoluteURL ("g;x?y#s"));
228     }
229     public void test16 () throws ParserException
230     {
231         assertEquals ("test16 failed", "http://a/b/c/", mPage.getAbsoluteURL ("."));
232     }
233     public void test17 () throws ParserException
234     {
235         assertEquals ("test17 failed", "http://a/b/c/", mPage.getAbsoluteURL ("./"));
236     }
237     public void test18 () throws ParserException
238     {
239         assertEquals ("test18 failed", "http://a/b/", mPage.getAbsoluteURL (".."));
240     }
241     public void test19 () throws ParserException
242     {
243         assertEquals ("test19 failed", "http://a/b/", mPage.getAbsoluteURL ("../"));
244     }
245     public void test20 () throws ParserException
246     {
247         assertEquals ("test20 failed", "http://a/b/g", mPage.getAbsoluteURL ("../g"));
248     }
249     public void test21 () throws ParserException
250     {
251         assertEquals ("test21 failed", "http://a/", mPage.getAbsoluteURL ("../.."));
252     }
253     public void test22 () throws ParserException
254     {
255         assertEquals ("test22 failed", "http://a/g", mPage.getAbsoluteURL ("../../g"));
256     }
257
258     // C.2. Abnormal Examples
259
// Although the following abnormal examples are unlikely to occur in
260
// normal practice, all URI parsers should be capable of resolving them
261
// consistently. Each example uses the same base as above.
262
//
263
// An empty reference refers to the start of the current document.
264
//
265
// <> = (current document)
266
//
267
// Parsers must be careful in handling the case where there are more
268
// relative path ".." segments than there are hierarchical levels in the
269
// base URI's path. Note that the ".." syntax cannot be used to change
270
// the authority component of a URI.
271
//
272
// ../../../g = http://a/../g
273
// ../../../../g = http://a/../../g
274
//
275
// In practice, some implementations strip leading relative symbolic
276
// elements (".", "..") after applying a relative URI calculation, based
277
// on the theory that compensating for obvious author errors is better
278
// than allowing the request to fail. Thus, the above two references
279
// will be interpreted as "http://a/g" by some implementations.
280
//
281
// Similarly, parsers must avoid treating "." and ".." as special when
282
// they are not complete components of a relative path.
283
//
284
// /./g = http://a/./g
285
// /../g = http://a/../g
286
// g. = http://a/b/c/g.
287
// .g = http://a/b/c/.g
288
// g.. = http://a/b/c/g..
289
// ..g = http://a/b/c/..g
290
//
291
// Less likely are cases where the relative URI uses unnecessary or
292
// nonsensical forms of the "." and ".." complete path segments.
293
//
294
// ./../g = http://a/b/g
295
// ./g/. = http://a/b/c/g/
296
// g/./h = http://a/b/c/g/h
297
// g/../h = http://a/b/c/h
298
// g;x=1/./y = http://a/b/c/g;x=1/y
299
// g;x=1/../y = http://a/b/c/y
300
//
301
// All client applications remove the query component from the base URI
302
// before resolving relative URI. However, some applications fail to
303
// separate the reference's query and/or fragment components from a
304
// relative path before merging it with the base path. This error is
305
// rarely noticed, since typical usage of a fragment never includes the
306
// hierarchy ("/") character, and the query component is not normally
307
// used within relative references.
308
//
309
// g?y/./x = http://a/b/c/g?y/./x
310
// g?y/../x = http://a/b/c/g?y/../x
311
// g#s/./x = http://a/b/c/g#s/./x
312
// g#s/../x = http://a/b/c/g#s/../x
313
//
314
// Some parsers allow the scheme name to be present in a relative URI if
315
// it is the same as the base URI scheme. This is considered to be a
316
// loophole in prior specifications of partial URI [RFC1630]. Its use
317
// should be avoided.
318
//
319
// http:g = http:g ; for validating parsers
320
// | http://a/b/c/g ; for backwards compatibility
321

322 // public void test23 () throws HTMLParserException
323
// {
324
// assertEquals ("test23 failed", "http://a/../g", mPage.getAbsoluteURL ("../../../g"));
325
// }
326
// public void test24 () throws HTMLParserException
327
// {
328
// assertEquals ("test24 failed", "http://a/../../g", mPage.getAbsoluteURL ("../../../../g"));
329
// }
330
public void test23 () throws ParserException
331     {
332         assertEquals ("test23 failed", "http://a/g", mPage.getAbsoluteURL ("../../../g"));
333     }
334     public void test24 () throws ParserException
335     {
336         assertEquals ("test24 failed", "http://a/g", mPage.getAbsoluteURL ("../../../../g"));
337     }
338     public void test25 () throws ParserException
339     {
340         assertEquals ("test25 failed", "http://a/./g", mPage.getAbsoluteURL ("/./g"));
341     }
342     public void test26 () throws ParserException
343     {
344         assertEquals ("test26 failed", "http://a/../g", mPage.getAbsoluteURL ("/../g"));
345     }
346     public void test27 () throws ParserException
347     {
348         assertEquals ("test27 failed", "http://a/b/c/g.", mPage.getAbsoluteURL ("g."));
349     }
350     public void test28 () throws ParserException
351     {
352         assertEquals ("test28 failed", "http://a/b/c/.g", mPage.getAbsoluteURL (".g"));
353     }
354     public void test29 () throws ParserException
355     {
356         assertEquals ("test29 failed", "http://a/b/c/g..", mPage.getAbsoluteURL ("g.."));
357     }
358     public void test30 () throws ParserException
359     {
360         assertEquals ("test30 failed", "http://a/b/c/..g", mPage.getAbsoluteURL ("..g"));
361     }
362     public void test31 () throws ParserException
363     {
364         assertEquals ("test31 failed", "http://a/b/g", mPage.getAbsoluteURL ("./../g"));
365     }
366     public void test32 () throws ParserException
367     {
368         assertEquals ("test32 failed", "http://a/b/c/g/", mPage.getAbsoluteURL ("./g/."));
369     }
370     public void test33 () throws ParserException
371     {
372         assertEquals ("test33 failed", "http://a/b/c/g/h", mPage.getAbsoluteURL ("g/./h"));
373     }
374     public void test34 () throws ParserException
375     {
376         assertEquals ("test34 failed", "http://a/b/c/h", mPage.getAbsoluteURL ("g/../h"));
377     }
378     public void test35 () throws ParserException
379     {
380         assertEquals ("test35 failed", "http://a/b/c/g;x=1/y", mPage.getAbsoluteURL ("g;x=1/./y"));
381     }
382     public void test36 () throws ParserException
383     {
384         assertEquals ("test36 failed", "http://a/b/c/y", mPage.getAbsoluteURL ("g;x=1/../y"));
385     }
386     public void test37 () throws ParserException
387     {
388         assertEquals ("test37 failed", "http://a/b/c/g?y/./x", mPage.getAbsoluteURL ("g?y/./x"));
389     }
390     public void test38 () throws ParserException
391     {
392         assertEquals ("test38 failed", "http://a/b/c/g?y/../x", mPage.getAbsoluteURL ("g?y/../x"));
393     }
394     public void test39 () throws ParserException
395     {
396         assertEquals ("test39 failed", "http://a/b/c/g#s/./x", mPage.getAbsoluteURL ("g#s/./x"));
397     }
398     public void test40 () throws ParserException
399     {
400         assertEquals ("test40 failed", "http://a/b/c/g#s/../x", mPage.getAbsoluteURL ("g#s/../x"));
401     }
402 // public void test41 () throws HTMLParserException
403
// {
404
// assertEquals ("test41 failed", "http:g", mPage.getAbsoluteURL ("http:g"));
405
// }
406
public void test41 () throws ParserException
407     {
408         assertEquals ("test41 failed", "http://a/b/c/g", mPage.getAbsoluteURL ("http:g"));
409     }
410
411 }
Popular Tags