1 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 name) 45 { 46 super(name); 47 } 48 49 66 public void testJspTag() throws ParserException 67 { 68 String contents = "jsp:useBean id=\"transfer\" scope=\"session\" class=\"com.bank.PageBean\"/"; 69 String jsp = "<" + contents + ">"; 70 String 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 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 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 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 119 public void testToHtml () throws ParserException 120 { 121 String 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 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 assertTrue("Node 5 should be a JspTag",node[4] instanceof JspTag); 147 JspTag tag2 = (JspTag)node[4]; 148 String 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 sb1 = new StringBuffer (); 158 sb1.append("<% for (i=0;i<j;i++);%>"); 159 createParser(sb1.toString()); 160 parser.setNodeFactory (new PrototypicalNodeFactory (new JspTag ())); 161 parseAndAssertNodeCount(1); 162 JspTag jspTag = (JspTag)node[0]; 164 assertEquals("jsp toHTML()","<% for (i=0;i<j;i++);%>",jspTag.toHtml()); 165 } 166 167 168 171 public void testJspTagsInUnQuotedAttribes() throws ParserException 172 { 173 if (JSP_TESTS_ENABLED) 175 testJspTagsInAttributes("<img alt=<%=altText1%> SRC=<%=imgUrl1%> border=<%=borderToggle%>>"); 176 } 177 178 181 public void testJspTagsInQuotedAttribes() throws ParserException 182 { 183 testJspTagsInAttributes("<img alt=\"<%=altText1%>\" SRC=\"<%=imgUrl1%>\" border=\"<%=borderToggle%>\">"); 185 } 186 187 private void testJspTagsInAttributes(String 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
|