KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > tagTests > BodyTagTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/BodyTagTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/02 00:49:31 $
10
// $Revision: 1.21 $
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.tagTests;
28
29 import java.util.Hashtable JavaDoc;
30 import junit.framework.TestSuite;
31 import org.htmlparser.Node;
32
33 import org.htmlparser.PrototypicalNodeFactory;
34 import org.htmlparser.Tag;
35 import org.htmlparser.tags.BodyTag;
36 import org.htmlparser.tags.Html;
37 import org.htmlparser.tags.TitleTag;
38 import org.htmlparser.tests.ParserTestCase;
39 import org.htmlparser.util.NodeIterator;
40 import org.htmlparser.util.ParserException;
41
42 public class BodyTagTest extends ParserTestCase {
43
44     static
45     {
46         System.setProperty ("org.htmlparser.tests.tagTests.BodyTagTest", "BodyTagTest");
47     }
48
49     private BodyTag bodyTag;
50     private String JavaDoc html = "<body>Yahoo!</body>";
51
52     public BodyTagTest(String JavaDoc name) {
53         super(name);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         createParser("<html><head><title>body tag test</title></head>" + html + "</html>");
59         parseAndAssertNodeCount(1);
60         assertTrue("Only node should be an HTML node",node[0] instanceof Html);
61         Html html = (Html)node[0];
62         assertTrue("HTML node should have two children",2 == html.getChildCount ());
63         assertTrue("Second node should be an BODY tag",html.getChild(1) instanceof BodyTag);
64         bodyTag = (BodyTag)html.getChild(1);
65     }
66
67     public void testToPlainTextString() throws ParserException {
68         // check the label node
69
assertEquals("Body","Yahoo!",bodyTag.toPlainTextString());
70     }
71
72     public void testToHTML() throws ParserException {
73         assertStringEquals("Raw String", html, bodyTag.toHtml());
74     }
75
76     public void testToString() throws ParserException {
77         assertEquals("Body","BODY: Yahoo!",bodyTag.toString());
78     }
79
80     public void testAttributes ()
81     {
82         NodeIterator iterator;
83         Node node;
84         Hashtable JavaDoc attributes;
85
86         try
87         {
88             createParser("<body style=\"margin-top:4px; margin-left:20px;\" title=\"body\">");
89             parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ()));
90             iterator = parser.elements ();
91             node = null;
92             while (iterator.hasMoreNodes ())
93             {
94                 node = iterator.nextNode ();
95                 if (node instanceof BodyTag)
96                 {
97                     attributes = ((BodyTag)node).getAttributes ();
98                     assertTrue ("no style attribute", attributes.containsKey ("STYLE"));
99                     assertTrue ("no title attribute", attributes.containsKey ("TITLE"));
100                 }
101                 else
102                     fail ("not a body tag");
103                 assertTrue ("more than one node", !iterator.hasMoreNodes ());
104             }
105             assertNotNull ("no elements", node);
106         }
107         catch (ParserException pe)
108         {
109             fail ("exception thrown " + pe.getMessage ());
110         }
111     }
112
113     public void testSimpleBody() throws ParserException {
114         createParser("<html><head><title>Test 1</title></head><body>This is a body tag</body></html>");
115         parser.setNodeFactory (
116             new PrototypicalNodeFactory (
117                 new Tag[]
118                 {
119                     new BodyTag (),
120                     new TitleTag (),
121                 }));
122         parseAndAssertNodeCount(6);
123         assertTrue(node[4] instanceof BodyTag);
124         // check the body node
125
BodyTag bodyTag = (BodyTag) node[4];
126         assertEquals("Body","This is a body tag",bodyTag.getBody());
127         assertEquals("Body","<body>This is a body tag</body>",bodyTag.toHtml());
128     }
129
130     public void testBodywithJsp() throws ParserException {
131         String JavaDoc body = "<body><%=BodyValue%></body>";
132         createParser("<html><head><title>Test 1</title></head>" + body + "</html>");
133         parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ()));
134         parseAndAssertNodeCount(8);
135         assertTrue(node[6] instanceof BodyTag);
136         // check the body node
137
BodyTag bodyTag = (BodyTag) node[6];
138         assertStringEquals("Body",body,bodyTag.toHtml());
139     }
140
141     public void testBodyMixed() throws ParserException {
142         String JavaDoc body = "<body>before jsp<%=BodyValue%>after jsp</body>";
143         createParser("<html><head><title>Test 1</title></head>" + body + "</html>");
144         parser.setNodeFactory (
145             new PrototypicalNodeFactory (
146                 new Tag[]
147                 {
148                     new BodyTag (),
149                     new TitleTag (),
150                 }));
151         parseAndAssertNodeCount(6);
152         assertTrue(node[4] instanceof BodyTag);
153         // check the body node
154
BodyTag bodyTag = (BodyTag) node[4];
155         assertEquals("Body",body,bodyTag.toHtml());
156     }
157
158     public void testBodyEnding() throws ParserException {
159         String JavaDoc body = "<body>before jsp<%=BodyValue%>after jsp";
160         createParser("<html>" + body + "</html>");
161         parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ()));
162         parseAndAssertNodeCount(3);
163         assertTrue(node[1] instanceof BodyTag);
164         // check the body node
165
BodyTag bodyTag = (BodyTag) node[1];
166         assertEquals("Body",body + "</body>",bodyTag.toHtml());
167     }
168
169     public static TestSuite suite()
170     {
171         return new TestSuite(BodyTagTest.class);
172     }
173 }
174
Popular Tags