KickJava   Java API By Example, From Geeks To Geeks.

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


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/ScriptTagTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/09/02 02:28:14 $
10
// $Revision: 1.46 $
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 org.htmlparser.PrototypicalNodeFactory;
30 import org.htmlparser.tags.ScriptTag;
31 import org.htmlparser.tests.ParserTestCase;
32 import org.htmlparser.util.ParserException;
33
34 public class ScriptTagTest extends ParserTestCase{
35
36     static
37     {
38         System.setProperty ("org.htmlparser.tests.tagTests.ScriptTagTest", "ScriptTagTest");
39     }
40
41     public ScriptTagTest(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     public void testCreation() throws ParserException
47     {
48         String JavaDoc testHtml = "<SCRIPT>Script Code</SCRIPT>";
49         createParser(testHtml,"http://localhost/index.html");
50         parseAndAssertNodeCount(1);
51         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
52         ScriptTag scriptTag = (ScriptTag)node[0];
53         assertEquals("Script Tag Begin",0,scriptTag.getStartPosition ());
54         assertEquals("Script Tag End",28,scriptTag.getEndTag ().getEndPosition ());
55         assertEquals("Script Tag Code","Script Code",scriptTag.getScriptCode());
56     }
57
58     public void testToHTML() throws ParserException {
59         createParser("<SCRIPT>document.write(d+\".com\")</SCRIPT>");
60         parseAndAssertNodeCount(1);
61         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
62         // Check the data in the applet tag
63
ScriptTag scriptTag = (ScriptTag)node[0];
64         assertEquals("Expected Raw String","<SCRIPT>document.write(d+\".com\")</SCRIPT>",scriptTag.toHtml());
65     }
66
67     /**
68      * Test raw string.
69      * Bug check by Wolfgang Germund 2002-06-02
70      * Upon parsing :
71      * &lt;script language="javascript"&gt;
72      * if(navigator.appName.indexOf("Netscape") != -1)
73      * document.write ('xxx');
74      * else
75      * document.write ('yyy');
76      * &lt;/script&gt;
77      * check toRawString().
78      */

79     public void testToHTMLWG() throws ParserException
80     {
81         StringBuffer JavaDoc sb2 = new StringBuffer JavaDoc();
82         sb2.append("<script language=\"javascript\">\r\n");
83         sb2.append("if(navigator.appName.indexOf(\"Netscape\") != -1)\r\n");
84         sb2.append(" document.write ('xxx');\r\n");
85         sb2.append("else\r\n");
86         sb2.append(" document.write ('yyy');\r\n");
87         sb2.append("</script>");
88         String JavaDoc expectedHTML = sb2.toString();
89
90         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
91         sb1.append("<body>");
92         sb1.append(expectedHTML);
93         sb1.append("\r\n");
94         String JavaDoc testHTML1 = sb1.toString();
95
96         createParser(testHTML1);
97         parser.setNodeFactory (new PrototypicalNodeFactory (new ScriptTag ()));
98         parseAndAssertNodeCount(3);
99         assertTrue("Node should be a script tag",node[1]
100         instanceof ScriptTag);
101         // Check the data in the script tag
102
ScriptTag scriptTag = (ScriptTag)node[1];
103         assertStringEquals("Expected Script Code",expectedHTML,scriptTag.toHtml());
104     }
105
106     public void testParamExtraction() throws ParserException {
107         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
108         sb1.append("<script SRC=\"/adb.js\" language=\"javascript\">\r\n");
109         sb1.append("if(navigator.appName.indexOf(\"Netscape\") != -1)\r\n");
110         sb1.append(" document.write ('xxx');\r\n");
111         sb1.append("else\r\n");
112         sb1.append(" document.write ('yyy');\r\n");
113         sb1.append("</script>\r\n");
114         createParser(sb1.toString());
115         parseAndAssertNodeCount(2);
116         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
117         ScriptTag scriptTag = (ScriptTag)node[0];
118         assertEquals("Script Src","/adb.js",scriptTag.getAttribute("src"));
119         assertEquals("Script Language","javascript",scriptTag.getAttribute("language"));
120     }
121
122     public void testVariableDeclarations() throws ParserException {
123         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
124         sb1.append("<script language=\"javascript\">\n");
125         sb1.append("var lower = '<%=lowerValue%>';\n");
126         sb1.append("</script>\n");
127         createParser(sb1.toString());
128         parseAndAssertNodeCount(2);
129         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
130         ScriptTag scriptTag = (ScriptTag)node[0];
131         assertStringEquals("Script toHTML()","<script language=\"javascript\">\nvar lower = '<%=lowerValue%>';\n</script>",scriptTag.toHtml());
132     }
133
134     public void testSingleApostropheParsingBug() throws ParserException {
135         String JavaDoc script = "<script SRC='<%=sourceFileName%>'></script>";
136         createParser(script);
137         parseAndAssertNodeCount(1);
138         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
139         ScriptTag scriptTag = (ScriptTag)node[0];
140         assertStringEquals("Script toHTML()",script,scriptTag.toHtml());
141     }
142
143 }
144
Popular Tags