KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > scannersTests > BaseHREFScannerTest


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/scannersTests/BaseHREFScannerTest.java,v 1.2 2004/02/11 02:16:58 woolfel Exp $
2
/*
3  * ====================================================================
4  * Copyright 2002-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20 // The developers of JMeter and Apache are greatful to the developers
21
// of HTMLParser for giving Apache Software Foundation a non-exclusive
22
// license. The performance benefits of HTMLParser are clear and the
23
// users of JMeter will benefit from the hard work the HTMLParser
24
// team. For detailed information about HTMLParser, the project is
25
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
26
//
27
// HTMLParser was originally created by Somik Raha in 2000. Since then
28
// a healthy community of users has formed and helped refine the
29
// design so that it is able to tackle the difficult task of parsing
30
// dirty HTML. Derrick Oswald is the current lead developer and was kind
31
// enough to assist JMeter.
32

33 package org.htmlparser.tests.scannersTests;
34
35 import org.htmlparser.scanners.BaseHrefScanner;
36 import org.htmlparser.scanners.LinkScanner;
37 import org.htmlparser.scanners.TitleScanner;
38 import org.htmlparser.tags.BaseHrefTag;
39 import org.htmlparser.tests.ParserTestCase;
40 import org.htmlparser.util.LinkProcessor;
41 import org.htmlparser.util.ParserException;
42
43 public class BaseHREFScannerTest extends ParserTestCase
44 {
45
46     private BaseHrefScanner scanner;
47
48     public BaseHREFScannerTest(String JavaDoc arg0)
49     {
50         super(arg0);
51     }
52
53     protected void setUp()
54     {
55         scanner = new BaseHrefScanner();
56     }
57
58     public void testRemoveLastSlash()
59     {
60         String JavaDoc url1 = "http://www.yahoo.com/";
61         String JavaDoc url2 = "http://www.google.com";
62         String JavaDoc modifiedUrl1 = LinkProcessor.removeLastSlash(url1);
63         String JavaDoc modifiedUrl2 = LinkProcessor.removeLastSlash(url2);
64         assertEquals("Url1", "http://www.yahoo.com", modifiedUrl1);
65         assertEquals("Url2", "http://www.google.com", modifiedUrl2);
66     }
67
68     public void testEvaluate()
69     {
70         String JavaDoc testData1 = "BASE HREF=\"http://www.abc.com/\"";
71         assertTrue(
72             "Data 1 Should have evaluated true",
73             scanner.evaluate(testData1, null));
74         String JavaDoc testData2 = "Base HREF=\"http://www.abc.com/\"";
75         assertTrue(
76             "Data 2 Should have evaluated true",
77             scanner.evaluate(testData2, null));
78     }
79
80     public void testScan() throws ParserException
81     {
82         createParser(
83             "<html><head><TITLE>test page</TITLE><BASE HREF=\"http://www.abc.com/\"><a HREF=\"home.cfm\">Home</a>...</html>",
84             "http://www.google.com/test/index.html");
85         LinkScanner linkScanner = new LinkScanner("-l");
86         parser.addScanner(linkScanner);
87         parser.addScanner(new TitleScanner("-t"));
88         parser.addScanner(linkScanner.createBaseHREFScanner("-b"));
89         parseAndAssertNodeCount(7);
90         //Base href tag should be the 4th tag
91
assertTrue(node[3] instanceof BaseHrefTag);
92         BaseHrefTag baseRefTag = (BaseHrefTag) node[3];
93         assertEquals(
94             "Base HREF Url",
95             "http://www.abc.com",
96             baseRefTag.getBaseUrl());
97     }
98
99 }
100
Popular Tags