1 18 19 package org.apache.strutsel.taglib.html; 20 21 import java.util.HashMap ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.jsp.JspException ; 27 import junit.framework.Test; 28 import junit.framework.TestSuite; 29 30 import org.apache.strutsel.taglib.utils.DOMHelper; 31 import org.apache.strutsel.taglib.utils.JspTagTestCase; 32 import org.apache.strutsel.taglib.utils.TestHelper; 33 import org.w3c.dom.Element ; 34 35 36 public class TestELBaseTag 37 extends JspTagTestCase { 38 protected static final String REQUIRED_HREF_VALUE_KEY = 39 "RequiredHRefValue"; 40 protected static final String REQUIRED_TARGET_VALUE_KEY = 41 "RequiredTargetValue"; 42 protected ELBaseTag elBaseTag = null; 43 44 public TestELBaseTag(String theName) { 45 super(theName); 46 } 47 48 public static void main(String [] args) { 49 junit.awtui.TestRunner.main( 50 new String [] { TestELBaseTag.class.getName() }); 51 } 52 53 public static Test suite() { 54 return new TestSuite(TestELBaseTag.class); 55 } 56 57 public void setUp() { 58 elBaseTag = new ELBaseTag(); 59 elBaseTag.setPageContext(pageContext); 60 } 61 62 public void tearDown() { 63 elBaseTag = null; 64 } 65 66 72 private String getRequiredHrefValue(HttpServletRequest request) { 73 StringBuffer sb = new StringBuffer (); 76 sb.append(request.getScheme()); 77 sb.append("://"); 78 sb.append(request.getServerName()); 79 80 if ((request.getScheme().equals("http") && 81 (request.getServerPort() != 80)) || 82 (request.getScheme().equals("https") && 83 (request.getServerPort() != 443))) { 84 sb.append(":"); 85 sb.append(request.getServerPort()); 86 } 87 88 sb.append(request.getRequestURI()); 89 90 String requiredHrefValue = sb.toString(); 91 92 return (requiredHrefValue); 93 } 94 95 99 public void testHref() 100 throws ServletException , JspException { 101 HttpServletResponse response = (HttpServletResponse )pageContext.getResponse(); 102 String requiredHrefValue = getRequiredHrefValue(request); 103 System.out.println("requiredHrefValue[" + requiredHrefValue + "]"); 104 response.addHeader(REQUIRED_HREF_VALUE_KEY, requiredHrefValue); 105 response.addHeader("abc", "def"); 106 response.addHeader("ghi", "jkl"); 107 response.addHeader("mno", "pqr"); 108 response.addHeader("stuvwx", "abc"); 110 111 int startTagReturn = elBaseTag.doStartTag(); 112 } 113 114 public void endHref(com.meterware.httpunit.WebResponse testResponse) { 115 try { 116 TestHelper.printResponse(testResponse); 117 118 Element docElement = testResponse.getDOM().getDocumentElement(); 119 DOMHelper.printNode(docElement); 120 121 HashMap attrMap = new HashMap (); 122 DOMHelper.recordFoundAttributes(testResponse.getDOM(), 123 "/html/head/base", attrMap); 124 DOMHelper.verifyAttributesPresent(attrMap, new String [] { "href" }, 125 false); 126 String header = 127 testResponse.getHeaderField(REQUIRED_HREF_VALUE_KEY); 128 System.out.println("[header[" + header + "]]"); 129 checkAttrValue(attrMap, testResponse, REQUIRED_HREF_VALUE_KEY, 130 "base", "href"); 131 } catch (Exception ex) { 132 ex.printStackTrace(); 133 fail(); 134 } 135 } 136 137 141 public void testTarget() 142 throws ServletException , JspException { 143 HttpServletResponse response = (HttpServletResponse )pageContext.getResponse(); 144 String requiredTargetValue = "flork"; 145 response.addHeader(REQUIRED_TARGET_VALUE_KEY, requiredTargetValue); 146 147 String varName = "targetVar"; 148 pageContext.setAttribute(varName, requiredTargetValue); 149 elBaseTag.setTargetExpr("${" + varName + "}"); 150 151 int startTagReturn = elBaseTag.doStartTag(); 152 } 153 154 public void endTarget(com.meterware.httpunit.WebResponse testResponse) { 155 try { 156 TestHelper.printResponse(testResponse); 157 158 Element docElement = testResponse.getDOM().getDocumentElement(); 159 DOMHelper.printNode(docElement); 160 161 HashMap attrMap = new HashMap (); 162 DOMHelper.recordFoundAttributes(testResponse.getDOM(), 163 "/html/head/base", attrMap); 164 DOMHelper.verifyAttributesPresent(attrMap, 165 new String [] { "href", "target" }, 166 false); 167 checkAttrValue(attrMap, testResponse, REQUIRED_TARGET_VALUE_KEY, 168 "base", "target"); 169 } catch (Exception ex) { 170 ex.printStackTrace(); 171 fail(); 172 } 173 } 174 175 180 public void testNonexistentVariable() 181 throws ServletException , JspException { 182 HttpServletResponse response = (HttpServletResponse )pageContext.getResponse(); 183 String requiredTargetValue = ""; 184 response.addHeader(REQUIRED_TARGET_VALUE_KEY, requiredTargetValue); 185 186 String varName = "targetVar"; 187 pageContext.setAttribute(varName, "flork"); 188 elBaseTag.setTargetExpr("${" + varName + "x" + "}"); 189 190 int startTagReturn = elBaseTag.doStartTag(); 191 } 192 193 public void endNonexistentVariable(com.meterware.httpunit.WebResponse testResponse) { 194 try { 195 TestHelper.printResponse(testResponse); 196 197 Element docElement = testResponse.getDOM().getDocumentElement(); 198 DOMHelper.printNode(docElement); 199 200 HashMap attrMap = new HashMap (); 201 DOMHelper.recordFoundAttributes(testResponse.getDOM(), 202 "/html/head/base", attrMap); 203 DOMHelper.verifyAttributesPresent(attrMap, 204 new String [] { "href", "target" }, 205 false); 206 checkAttrValue(attrMap, testResponse, REQUIRED_TARGET_VALUE_KEY, 207 "base", "target"); 208 } catch (Exception ex) { 209 ex.printStackTrace(); 210 fail(); 211 } 212 } 213 } 214 | Popular Tags |