KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/scannersTests/BodyScannerTest.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 junit.framework.TestSuite;
36
37 import org.htmlparser.scanners.BodyScanner;
38 import org.htmlparser.tags.BodyTag;
39 import org.htmlparser.tests.ParserTestCase;
40 import org.htmlparser.util.ParserException;
41
42 public class BodyScannerTest extends ParserTestCase
43 {
44
45     public BodyScannerTest(String JavaDoc name)
46     {
47         super(name);
48     }
49
50     public void testSimpleBody() throws ParserException
51     {
52         createParser("<html><head><title>Test 1</title></head><body>This is a body tag</body></html>");
53         parser.registerScanners();
54         BodyScanner bodyScanner = new BodyScanner("-b");
55         parser.addScanner(bodyScanner);
56         parseAndAssertNodeCount(6);
57         assertTrue(node[4] instanceof BodyTag);
58         // check the body node
59
BodyTag bodyTag = (BodyTag) node[4];
60         assertEquals("Body", "This is a body tag", bodyTag.getBody());
61         assertEquals(
62             "Body",
63             "<BODY>This is a body tag</BODY>",
64             bodyTag.toHtml());
65         assertEquals("Body Scanner", bodyScanner, bodyTag.getThisScanner());
66     }
67
68     public void testBodywithJsp() throws ParserException
69     {
70         createParser("<html><head><title>Test 1</title></head><body><%=BodyValue%></body></html>");
71         parser.registerScanners();
72         BodyScanner bodyScanner = new BodyScanner("-b");
73         parser.addScanner(bodyScanner);
74         parseAndAssertNodeCount(6);
75         assertTrue(node[4] instanceof BodyTag);
76         // check the body node
77
BodyTag bodyTag = (BodyTag) node[4];
78         assertStringEquals(
79             "Body",
80             "<BODY><%=BodyValue%></BODY>",
81             bodyTag.toHtml());
82         assertEquals("Body Scanner", bodyScanner, bodyTag.getThisScanner());
83     }
84
85     public void testBodyMixed() throws ParserException
86     {
87         createParser("<html><head><title>Test 1</title></head><body>before jsp<%=BodyValue%>after jsp</body></html>");
88         parser.registerScanners();
89         BodyScanner bodyScanner = new BodyScanner("-b");
90         parser.addScanner(bodyScanner);
91         parseAndAssertNodeCount(6);
92         assertTrue(node[4] instanceof BodyTag);
93         // check the body node
94
BodyTag bodyTag = (BodyTag) node[4];
95         assertEquals(
96             "Body",
97             "<BODY>before jsp<%=BodyValue%>after jsp</BODY>",
98             bodyTag.toHtml());
99         assertEquals("Body Scanner", bodyScanner, bodyTag.getThisScanner());
100     }
101
102     public void testBodyEnding() throws ParserException
103     {
104         createParser("<html><body>before jsp<%=BodyValue%>after jsp</html>");
105         parser.registerScanners();
106         BodyScanner bodyScanner = new BodyScanner("-b");
107         parser.addScanner(bodyScanner);
108         parseAndAssertNodeCount(3);
109         assertTrue(node[1] instanceof BodyTag);
110         // check the body node
111
BodyTag bodyTag = (BodyTag) node[1];
112         assertEquals(
113             "Body",
114             "<BODY>before jsp<%=BodyValue%>after jsp</BODY>",
115             bodyTag.toHtml());
116         assertEquals("Body Scanner", bodyScanner, bodyTag.getThisScanner());
117     }
118
119     public static TestSuite suite()
120     {
121         return new TestSuite(BodyScannerTest.class);
122     }
123
124     public static void main(String JavaDoc[] args)
125     {
126         new junit.awtui.TestRunner().start(
127             new String JavaDoc[] { BodyScannerTest.class.getName()});
128     }
129
130 }
131
Popular Tags