KickJava   Java API By Example, From Geeks To Geeks.

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


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/JspTagTest.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.Tag;
31 import org.htmlparser.tags.JspTag;
32 import org.htmlparser.tests.ParserTestCase;
33 import org.htmlparser.util.ParserException;
34
35 public class JspTagTest extends ParserTestCase
36 {
37     static
38     {
39         System.setProperty ("org.htmlparser.tests.tagTests.JspTagTest", "JspTagTest");
40     }
41
42     private static final boolean JSP_TESTS_ENABLED = false;
43
44     public JspTagTest(String JavaDoc name)
45     {
46         super(name);
47     }
48
49     /**
50      * Check if the JSP Tag is being correctly recognized.
51      * Our test html is : <BR>
52      * &lt;%@ taglib uri="/WEB-INF/struts.tld" prefix="struts" %&gt;<BR>
53      * &lt;jsp:useBean id="transfer" scope="session" class="com.bank.PageBean"/&gt;<BR>
54      * &lt;%<BR>
55      * org.apache.struts.util.BeanUtils.populate(transfer, request);<BR>
56      * if(request.getParameter("marker") == null)<BR>
57      * // initialize a pseudo-property<BR>
58      * transfer.set("days", java.util.Arrays.asList(<BR>
59      * new String[] {"1", "2", "3", "4", "31"}));<BR>
60      * else <BR>
61      * if(transfer.validate(request))<BR>
62      * %&gt;&lt;jsp:forward page="transferConfirm.jsp"/&gt;&lt;%
63      * %&gt;
64      * Creation date: (6/17/2001 4:01:06 PM)
65      */

66     public void testJspTag() throws ParserException
67     {
68         String JavaDoc contents = "jsp:useBean id=\"transfer\" scope=\"session\" class=\"com.bank.PageBean\"/";
69         String JavaDoc jsp = "<" + contents + ">";
70         String JavaDoc contents2 = "%\n"+
71             " org.apache.struts.util.BeanUtils.populate(transfer, request);\n"+
72             " if(request.getParameter(\"marker\") == null)\n"+
73             " // initialize a pseudo-property\n"+
74             " transfer.set(\"days\", java.util.Arrays.asList(\n"+
75             " new String[] {\"1\", \"2\", \"3\", \"4\", \"31\"}));\n"+
76             " else \n"+
77             " if(transfer.validate(request))\n"+
78             " %";
79         createParser(
80             "<%@ taglib uri=\"/WEB-INF/struts.tld\" prefix=\"struts\" %>\n"+
81             jsp + "\n" +
82             "<" + contents2 + ">\n<jsp:forward page=\"transferConfirm.jsp\"/><%\n"+
83             "%>");
84         parser.setNodeFactory (new PrototypicalNodeFactory (new JspTag ()));
85         parseAndAssertNodeCount(8);
86         // The first node should be a JspTag
87
assertTrue("Node 1 should be a JspTag",node[0] instanceof JspTag);
88         JspTag tag = (JspTag)node[0];
89         assertStringEquals("Contents of the tag","%@ taglib uri=\"/WEB-INF/struts.tld\" prefix=\"struts\" %",tag.getText());
90
91         // The second node should be a normal tag
92
assertTrue("Node 3 should be a normal Tag",node[2] instanceof Tag);
93         Tag htag = (Tag)node[2];
94         assertStringEquals("Contents of the tag",contents,htag.getText());
95         assertStringEquals("html",jsp,htag.toHtml());
96         // The third node should be an JspTag
97
assertTrue("Node 5 should be an JspTag",node[4] instanceof JspTag);
98         JspTag tag2 = (JspTag)node[4];
99         assertStringEquals("Contents of the tag",contents2,tag2.getText());
100     }
101
102     /**
103      * Check if the JSP Tag is being correctly recognized.
104      * Our test html is : <BR>
105      * &lt;%@ taglib uri="/WEB-INF/struts.tld" prefix="struts" %&gt;<BR>
106      * &lt;jsp:useBean id="transfer" scope="session" class="com.bank.PageBean"/&gt;<BR>
107      * &lt;%<BR>
108      * org.apache.struts.util.BeanUtils.populate(transfer, request);<BR>
109      * if(request.getParameter("marker") == null)<BR>
110      * // initialize a pseudo-property<BR>
111      * transfer.set("days", java.util.Arrays.asList(<BR>
112      * new String[] {"1", "2", "3", "4", "31"}));<BR>
113      * else <BR>
114      * if(transfer.validate(request))<BR>
115      * %&gt;&lt;jsp:forward page="transferConfirm.jsp"/&gt;&lt;%
116      * %&gt;
117      * Creation date: (6/17/2001 4:01:06 PM)
118      */

119     public void testToHtml () throws ParserException
120     {
121         String JavaDoc guts = "\n"+
122             " org.apache.struts.util.BeanUtils.populate(transfer, request);\n"+
123             " if(request.getParameter(\"marker\") == null)\n"+
124             " // initialize a pseudo-property\n"+
125             " transfer.set(\"days\", java.util.Arrays.asList(\n"+
126             " new String[] {\"1\", \"2\", \"3\", \"4\", \"31\"}));\n"+
127             " else \n"+
128             " if(transfer.validate(request))\n"+
129             " ";
130         createParser(
131             "<%@ taglib uri=\"/WEB-INF/struts.tld\" prefix=\"struts\" %>\n"+
132             "<jsp:useBean id=\"transfer\" scope=\"session\" class=\"com.bank.PageBean\"/>\n"+
133             "<%" +
134             guts
135             + "%><jsp:forward page=\"transferConfirm.jsp\"/><%\n"+
136             "%>\n");
137         parser.setNodeFactory (new PrototypicalNodeFactory (new JspTag ()));
138         parseAndAssertNodeCount(8);
139         // The first node should be a JspTag
140
assertTrue("Node 1 should be a JspTag",node[0] instanceof JspTag);
141         JspTag tag = (JspTag)node[0];
142         assertEquals("Raw String of the first JSP tag","<%@ taglib uri=\"/WEB-INF/struts.tld\" prefix=\"struts\" %>",tag.toHtml());
143
144
145         // The fifth node should be a JspTag
146
assertTrue("Node 5 should be a JspTag",node[4] instanceof JspTag);
147         JspTag tag2 = (JspTag)node[4];
148         String JavaDoc expected = "<%" + guts + "%>";
149         assertEquals("Raw String of the second JSP tag",expected,tag2.toHtml());
150         assertTrue("Node 7 should be a JspTag",node[6] instanceof JspTag);
151         JspTag tag4 = (JspTag)node[6];
152         expected = "<%\n%>";
153         assertEquals("Raw String of the fourth JSP tag",expected,tag4.toHtml());
154     }
155
156     public void testSpecialCharacters() throws ParserException {
157         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
158         sb1.append("<% for (i=0;i<j;i++);%>");
159         createParser(sb1.toString());
160         parser.setNodeFactory (new PrototypicalNodeFactory (new JspTag ()));
161         parseAndAssertNodeCount(1);
162         //assertTrue("Node should be a jsp tag",node[1] instanceof HTMLJspTag);
163
JspTag jspTag = (JspTag)node[0];
164         assertEquals("jsp toHTML()","<% for (i=0;i<j;i++);%>",jspTag.toHtml());
165     }
166
167
168     /**
169      * See bug #772700 Jsp Tags are not parsed correctly when in quoted attributes.
170      */

171     public void testJspTagsInUnQuotedAttribes() throws ParserException
172     {
173         // this test should pass when none of the attibutes are quoted
174
if (JSP_TESTS_ENABLED)
175             testJspTagsInAttributes("<img alt=<%=altText1%> SRC=<%=imgUrl1%> border=<%=borderToggle%>>");
176     }
177
178     /**
179      * See bug #772700 Jsp Tags are not parsed correctly when in quoted attributes.
180      */

181     public void testJspTagsInQuotedAttribes() throws ParserException
182     {
183         // this test seems to mess up....
184
testJspTagsInAttributes("<img alt=\"<%=altText1%>\" SRC=\"<%=imgUrl1%>\" border=\"<%=borderToggle%>\">");
185     }
186
187     private void testJspTagsInAttributes(String JavaDoc html) throws ParserException
188     {
189         createParser (html);
190         parser.setNodeFactory (new PrototypicalNodeFactory (new JspTag ()));
191         if (JSP_TESTS_ENABLED)
192         {
193             parseAndAssertNodeCount (7);
194
195             assertTrue ("Should be a Jsp tag but was " + node[1].getClass().getName(), node[1] instanceof JspTag);
196             assertTrue ("Should be a Jsp tag but was " + node[3].getClass().getName(), node[3] instanceof JspTag);
197             assertTrue ("Should be a Jsp tag but was " + node[5].getClass().getName(), node[5] instanceof JspTag);
198
199             assertTrue ("Text Should be '<%=altText1%>'but was '" + node[1].toHtml() + "'" , node[1].toHtml().equals("<%=altText1%>"));
200             assertTrue ("Text Should be '<%=imgUrl1%>' but was '" + node[3].toHtml() + "'" , node[3].toHtml().equals("<%=imgUrl1%>"));
201             assertTrue ("Text Should be '<%=borderToggle%>' but was '" + node[5].toHtml() + "'" , node[5].toHtml().equals("<%=borderToggle%>"));
202         }
203         else
204             parseAndAssertNodeCount (1);
205     }
206 }
207
Popular Tags