KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > core > util > html > URLFinderTest


1 package net.javacoding.jspider.core.util.html;
2
3 import junit.framework.TestCase;
4
5 import java.net.URL JavaDoc;
6
7 /**
8  * $Id: URLFinderTest.java,v 1.6 2003/04/29 17:53:50 vanrogu Exp $
9  */

10 public class URLFinderTest extends TestCase implements URLFinderCallback {
11
12     protected int count;
13     protected int malformed;
14     protected int malformedContextURL;
15     protected URL JavaDoc baseURL;
16     protected URL JavaDoc lastURL;
17     protected URL JavaDoc contextURL;
18
19     public URLFinderTest ( ) {
20         super ( "URLFinderTest" );
21     }
22
23     protected void setUp() throws Exception JavaDoc {
24         count = 0;
25         malformed = 0;
26         malformedContextURL = 0;
27         baseURL = new URL JavaDoc("http://j-spider.sourceforge.net");
28         contextURL = baseURL;
29     }
30
31     public void urlFound(URL JavaDoc foundURL) {
32         count++;
33         lastURL = foundURL;
34     }
35
36     public void malformedUrlFound(String JavaDoc malformedURL) {
37         malformed++;
38     }
39
40     public URL JavaDoc getContextURL() {
41         return contextURL;
42     }
43
44     public void setContextURL(URL JavaDoc url) {
45         this.contextURL = url;
46     }
47
48     public void malformedContextURLFound(String JavaDoc malformedURL) {
49         malformedContextURL++;
50     }
51
52     public void testStringWithNoURL ( ) throws Exception JavaDoc {
53         URLFinder.findURLs(this, "this is a line with no url in it");
54         int expected = 0;
55         assertEquals("actual nr of urls found differs from expected", expected, count);
56         assertEquals("malformed URLs reported", 0, malformed);
57     }
58
59     public void testStringWithBaseButNoURL ( ) throws Exception JavaDoc {
60         URLFinder.findURLs(this, "<BaSe HrEf='http://www.somehost.com'> this is a line with no url in it, expect for the base one");
61         int expected = 1;
62         assertEquals("actual nr of urls found differs from expected", expected, count);
63         assertEquals("malformed URLs reported", 0, malformed);
64         assertEquals("contextURL error", "http://www.somehost.com", contextURL.toString() );
65         assertEquals("lastURL error", "http://www.somehost.com", lastURL.toString() );
66     }
67
68     public void testStringWithBaseButNoURLCaseSensitivity ( ) throws Exception JavaDoc {
69         URLFinder.findURLs(this, "<BaSe HrEf='http://www.SomeHost.com'> this is a line with no url in it, expect for the base one");
70         int expected = 1;
71         assertEquals("actual nr of urls found differs from expected", expected, count);
72         assertEquals("malformed URLs reported", 0, malformed);
73         assertEquals("contextURL error", "http://www.SomeHost.com", contextURL.toString() );
74         assertEquals("lastURL error", "http://www.SomeHost.com", lastURL.toString() );
75     }
76
77     public void testStringWithBaseInFolderButNoURL ( ) throws Exception JavaDoc {
78         URLFinder.findURLs(this, "<base HREF='http://www.somehost.com/folder/subfolder'> this is a line with no url in it, expect for the base one");
79         int expected = 1;
80         assertEquals("actual nr of urls found differs from expected", expected, count);
81         assertEquals("malformed URLs reported", 0, malformed);
82         assertEquals("contextURL error", "http://www.somehost.com/folder/subfolder", contextURL.toString() );
83         assertEquals("lastURL error", "http://www.somehost.com/folder/subfolder", lastURL.toString() );
84     }
85
86     public void testStringWithNoURLNullBaseURL ( ) throws Exception JavaDoc {
87         URLFinder.findURLs(this, "this is a line with no url in it");
88         int expected = 0;
89         assertEquals("actual nr of urls found differs from expected", expected, count);
90         assertEquals("malformed URLs reported", 0, malformed);
91     }
92
93     public void testStringWithOneURL ( ) throws Exception JavaDoc {
94         URLFinder.findURLs(this, "this is a line with a <a HREF='index.html'>url</a> in it");
95         int expected = 1;
96         assertEquals("actual nr of urls found differs from expected", expected, count);
97         assertEquals("url found wasn't the one expected", new URL JavaDoc("http://j-spider.sourceforge.net/index.html"), lastURL);
98         assertEquals("malformed URLs reported", 0, malformed);
99     }
100
101     public void testStringWithOneURLNullBaseURL ( ) throws Exception JavaDoc {
102         URLFinder.findURLs(this, "this is a line with a <a HREF='http://index.html'>url</a> in it");
103         int expected = 1;
104         assertEquals("actual nr of urls found differs from expected", expected, count);
105         assertEquals("malformed URLs reported", 0, malformed);
106     }
107
108     public void testStringWithOneURLAndBaseURLWithFile ( ) throws Exception JavaDoc {
109         URLFinder.findURLs(this, "this <base HREF='http://www.somehost.com/folder/subfolder/test.html'> is a line with a <a HREF='index.html'>url</a> in it");
110         int expected = 2;
111         assertEquals("actual nr of urls found differs from expected", expected, count);
112         assertEquals("wrong contextURL reported", "http://www.somehost.com/folder/subfolder/test.html", contextURL.toString());
113         assertEquals("wrong last URLs reported","http://www.somehost.com/folder/subfolder/index.html", this.lastURL.toString());
114     }
115
116     public void testStringWithOneURLAndBaseURLWithFolder ( ) throws Exception JavaDoc {
117         URLFinder.findURLs(this, "this <base HREF='http://www.somehost.com/folder/subfolder'> is a line with a <a HREF='index.html'>url</a> in it");
118         int expected = 2;
119         assertEquals("actual nr of urls found differs from expected", expected, count);
120         assertEquals("wrong contextURL reported", "http://www.somehost.com/folder/subfolder", contextURL.toString());
121         assertEquals("wrong last URLs reported","http://www.somehost.com/folder/subfolder/index.html", this.lastURL.toString());
122     }
123
124     public void testStringWithOneURLAndBaseURLWithFolderAndSlash ( ) throws Exception JavaDoc {
125         URLFinder.findURLs(this, "this <base HREF='http://www.somehost.com/folder/subfolder/'> is a line with a <a HREF='index.html'>url</a> in it");
126         int expected = 2;
127         assertEquals("actual nr of urls found differs from expected", expected, count);
128         assertEquals("wrong contextURL reported", "http://www.somehost.com/folder/subfolder/", contextURL.toString());
129         assertEquals("wrong last URLs reported","http://www.somehost.com/folder/subfolder/index.html", this.lastURL.toString());
130     }
131
132     public void testStringWithTwoURLs ( ) throws Exception JavaDoc {
133         URLFinder.findURLs(this, "this is a line with a <a HREF='index.html'>url</a> in it, and a <a HREF='second.html'>second one</a> also");
134         int expected = 2;
135         assertEquals("actual nr of urls found differs from expected", expected, count);
136         assertEquals("url found wasn't the one expected", new URL JavaDoc("http://j-spider.sourceforge.net/second.html"), lastURL);
137         assertEquals("malformed URLs reported", 0, malformed);
138     }
139
140     public void testStringWithTwoURLsNullBaseURL ( ) throws Exception JavaDoc {
141         URLFinder.findURLs(this, "this is a line with a <a HREF='http://index.html'>url</a> in it, and a <a HREF='http://second.html'>second one</a> also");
142         int expected = 2;
143         assertEquals("actual nr of urls found differs from expected", expected, count);
144         assertEquals("malformed URLs reported", 0, malformed);
145     }
146
147     public void testStringSingleQuote ( ) throws Exception JavaDoc {
148         URLFinder.findURLs(this, "this is a line with a <a HREF='index.html'>url</a> in it");
149         int expected = 1;
150         assertEquals("actual nr of urls found differs from expected", expected, count);
151         assertEquals("malformed URLs reported", 0, malformed);
152     }
153
154     public void testStringDoubleQuote ( ) throws Exception JavaDoc {
155         URLFinder.findURLs(this, "this is a line with a <a HREF=\"index.html\">url</a> in it");
156         int expected = 1;
157         assertEquals("actual nr of urls found differs from expected", expected, count);
158         assertEquals("malformed URLs reported", 0, malformed);
159     }
160
161     public void testStringDoubleNoQuotes ( ) throws Exception JavaDoc {
162         URLFinder.findURLs(this, "this is a line with a <a HREF=index.html>url</a> in it");
163         int expected = 1;
164         assertEquals("actual nr of urls found differs from expected", expected, count);
165         assertEquals("malformed URLs reported", 0, malformed);
166     }
167
168     public void testStringBadlyQuoted1 ( ) throws Exception JavaDoc {
169         URLFinder.findURLs(this, "this is a line with a <a HREF=\"index.html>url</a> in it");
170         int expected = 1;
171         assertEquals("actual nr of urls found differs from expected", expected, count);
172         assertEquals("url found wasn't the one expected", new URL JavaDoc("http://j-spider.sourceforge.net/index.html"), lastURL);
173         assertEquals("malformed URLs reported", 0, malformed);
174     }
175
176     public void testStringBadlyQuoted2 ( ) throws Exception JavaDoc {
177         URLFinder.findURLs(this, "this is a line with a <a HREF='index.html>url</a> in it");
178         int expected = 1;
179         assertEquals("actual nr of urls found differs from expected", expected, count);
180         assertEquals("url found wasn't the one expected", new URL JavaDoc("http://j-spider.sourceforge.net/index.html"), lastURL);
181         assertEquals("malformed URLs reported", 0, malformed);
182     }
183
184     public void testStringBadlyQuoted3 ( ) throws Exception JavaDoc {
185         URLFinder.findURLs(this, "this is a line with a <a HREF=index.html'>url</a> in it");
186         int expected = 1;
187         assertEquals("actual nr of urls found differs from expected", expected, count);
188         assertEquals("url found wasn't the one expected", new URL JavaDoc("http://j-spider.sourceforge.net/index.html"), lastURL);
189         assertEquals("malformed URLs reported", 0, malformed);
190     }
191
192     public void testStringBadlyQuoted4 ( ) throws Exception JavaDoc {
193         URLFinder.findURLs(this, "this is a line with a <a HREF=index.html\">url</a> in it");
194         int expected = 1;
195         assertEquals("actual nr of urls found differs from expected", expected, count);
196         assertEquals("url found wasn't the one expected", new URL JavaDoc("http://j-spider.sourceforge.net/index.html"), lastURL);
197         assertEquals("malformed URLs reported", 0, malformed);
198     }
199
200     public void testStringBadlyQuoted5 ( ) throws Exception JavaDoc {
201         URLFinder.findURLs(this, "this is a line with a <a HREF='index.html\">url</a> in it");
202         int expected = 1;
203         assertEquals("actual nr of urls found differs from expected", expected, count);
204         assertEquals("url found wasn't the one expected", new URL JavaDoc("http://j-spider.sourceforge.net/index.html"), lastURL);
205         assertEquals("malformed URLs reported", 0, malformed);
206     }
207
208     public void testStringBadlyQuoted6 ( ) throws Exception JavaDoc {
209         URLFinder.findURLs(this, "this is a line with a <a HREF=\"index.html'>url</a> in it");
210         int expected = 1;
211         assertEquals("actual nr of urls found differs from expected", expected, count);
212         assertEquals("url found wasn't the one expected", new URL JavaDoc("http://j-spider.sourceforge.net/index.html"), lastURL);
213         assertEquals("malformed URLs reported", 0, malformed);
214     }
215
216     public void testMalformed ( ) throws Exception JavaDoc {
217         URLFinder.findURLs(this, "this is a line with a <a HREF=\"someprotocol:index.html'>url</a> in it");
218         int expected = 0;
219         int malformedExpected = 1;
220         assertEquals("actual nr of urls found differs from expected", expected, count);
221         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
222     }
223
224     public void testTwoMalformed ( ) throws Exception JavaDoc {
225         URLFinder.findURLs(this, "this is a line with two malformed <a HREF=\"someprotocol:index.html'>urls</a><a HREF='test:'/> in it");
226         int expected = 0;
227         int malformedExpected = 2;
228         assertEquals("actual nr of urls found differs from expected", expected, count);
229         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
230     }
231
232     public void testWellformedAndMalformed ( ) throws Exception JavaDoc {
233         URLFinder.findURLs(this, "this is a line with one malformed <a HREF=\"someprotocol:index.html'>urls</a><a HREF='index.html'/> in it, and one wellformed");
234         int expected = 1;
235         int malformedExpected = 1;
236         assertEquals("actual nr of urls found differs from expected", expected, count);
237         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
238     }
239
240     public void testWellformedAndMalformedWithBaseURL ( ) throws Exception JavaDoc {
241         URLFinder.findURLs(this, "this is a line <base HREF='http://www.somehost.com/folder/test.html'/>with one malformed <a HREF=\"someprotocol:index.html'>urls</a><a HREF='index.html'/> in it, and one wellformed");
242         int expected = 2;
243         int malformedExpected = 1;
244         assertEquals("actual nr of urls found differs from expected", expected, count);
245         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
246         assertEquals("baseURL is wrong", "http://www.somehost.com/folder/test.html", contextURL.toString());
247         assertEquals("lastURL is wrong", "http://www.somehost.com/folder/index.html", lastURL.toString());
248     }
249
250     public void testWithBaseURL ( ) throws Exception JavaDoc {
251         URLFinder.findURLs(this, "this is a line <base HREF='http://www.somehost.com/folder/subfolder/test.html'/>with one malformed <a HREF=\"someprotocol:index.html'>urls</a><a HREF='../index.html'/> in it, and one wellformed");
252         int expected = 2;
253         int malformedExpected = 1;
254         assertEquals("actual nr of urls found differs from expected", expected, count);
255         assertEquals("number of malformed URLs reported differs from expected", malformedExpected, malformed);
256         assertEquals("baseURL is wrong", "http://www.somehost.com/folder/subfolder/test.html", contextURL.toString());
257         assertEquals("lastURL is wrong", "http://www.somehost.com/folder/index.html", lastURL.toString());
258     }
259
260 }
261
Popular Tags