1 package com.tonbeller.tbutils.httpunit; 2 3 import java.io.IOException ; 4 import java.io.UnsupportedEncodingException ; 5 import java.net.MalformedURLException ; 6 import java.net.URLEncoder ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import javax.xml.transform.TransformerException ; 11 12 import junit.framework.TestCase; 13 14 import org.apache.log4j.Logger; 15 import org.jaxen.JaxenException; 16 import org.jaxen.XPath; 17 import org.jaxen.dom.DOMXPath; 18 import org.w3c.dom.Document ; 19 import org.w3c.dom.Node ; 20 import org.xml.sax.SAXException ; 21 22 import com.meterware.httpunit.GetMethodWebRequest; 23 import com.meterware.httpunit.HttpUnitOptions; 24 import com.meterware.httpunit.SubmitButton; 25 import com.meterware.httpunit.TableCell; 26 import com.meterware.httpunit.WebConversation; 27 import com.meterware.httpunit.WebForm; 28 import com.meterware.httpunit.WebRequest; 29 import com.meterware.httpunit.WebResponse; 30 import com.meterware.httpunit.WebTable; 31 32 public abstract class HttpUnitTestBase extends TestCase { 33 private static Logger logger = Logger.getLogger(HttpUnitTestBase.class); 34 35 private String servletUrl; 36 private String jspName; 37 protected WebConversation wc; 38 protected HttpUnitUtils utils; 39 40 protected String formID = "form01"; 41 private boolean checkHtmlErrors; 42 43 public HttpUnitTestBase(String name, String jspName) { 44 super(name); 45 this.jspName = jspName; 46 } 47 48 protected String getJspUrl() { 49 return servletUrl + "/" + jspName; 50 } 51 52 protected String getJspUrl(String paramName, String paramValue) { 53 if (paramName == null) 54 paramName = ""; 55 if (paramValue == null) 56 paramValue = ""; 57 try { 58 StringBuffer result = new StringBuffer (); 59 result.append(servletUrl); 60 result.append("/"); 61 result.append(jspName); 62 result.append("?"); 63 result.append(paramName); 64 result.append("="); 65 result.append(URLEncoder.encode(paramValue, "ISO-8859-1")); 66 67 return result.toString(); 68 } catch (UnsupportedEncodingException e) { 69 logger.error("exception caught", e); 70 fail(e.toString()); 71 return null; 72 } 73 } 74 75 protected WebResponse submitForm(String buttonName) throws IOException , SAXException { 76 WebForm form = getForm(); 77 SubmitButton sb = form.getSubmitButton(buttonName); 78 assertNotNull("submit button not found", sb); 79 form.submit(sb); 80 return wc.getCurrentPage(); 81 } 82 83 protected WebForm getForm() throws SAXException { 84 return wc.getCurrentPage().getFormWithID(formID); 85 } 86 87 protected WebForm getForm(String formId) throws SAXException { 88 return wc.getCurrentPage().getFormWithID(formId); 89 } 90 91 protected void assertTableSize(String tableID, int rows, int cols) throws SAXException { 92 WebTable table = wc.getCurrentPage().getTableWithID(tableID); 93 assertEquals("Table RowCount", rows, table.getRowCount()); 94 assertEquals("Table ColumnCount", cols, table.getColumnCount()); 95 } 96 97 protected void assertCellValue(String tableID, int row, int col, String value) 98 throws SAXException { 99 WebTable table = wc.getCurrentPage().getTableWithID(tableID); 100 TableCell cell = table.getTableCell(row, col); 101 assertEquals("cellValue", value, cell.asText()); 102 } 103 104 121 protected void check(String fileName, String xslName, String id) throws IOException , 122 TransformerException , SAXException { 123 utils.saveHTML(fileName); 124 utils.saveXML(fileName, xslName, id, getCurrentDOM(fileName)); 125 assertTrue("XML Files \"" + fileName + "\" differ", utils.equalsXML(fileName)); 126 } 127 128 protected Document getCurrentDOM(String message) throws SAXException { 129 HtmlListener hl = HtmlListener.instance(); 130 try { 131 hl.setCheckErrors(checkHtmlErrors); 132 Document dom = wc.getCurrentPage().getDOM(); 133 hl.failOnError(message); 134 return dom; 135 } finally { 136 hl.reset(); 137 } 138 } 139 140 protected void tearDown() throws Exception { 141 super.tearDown(); 142 } 143 144 protected void setUp() throws Exception { 145 super.setUp(); 146 147 HttpUnitOptions.setScriptingEnabled(false); 148 servletUrl = System.getProperty("httpunit.url"); 149 if (servletUrl == null) 150 throw new RuntimeException ( 151 "missing JVM Parameter httpunit.url, e.g. -Dhttpunit.url=http://localhost:8080/appname"); 152 wc = new WebConversation(); 153 wc.setHeaderField("accept-language", getLanguage()); 154 setAuthorization(wc); 155 156 WebRequest wreq = getStartRequest(); 157 if (wreq != null) 158 wc.sendRequest(wreq); 159 160 utils = new HttpUnitUtils(wc); 161 } 162 163 protected void setAuthorization(WebConversation wc) { 164 wc.setAuthorization("tdiuser", "tdiuser"); 165 } 166 167 170 protected abstract WebRequest getStartRequest(); 171 172 173 protected String getLanguage() { 174 return "en-US"; 175 } 176 177 180 public String getJspName() { 181 return jspName; 182 } 183 184 188 public void setJspName(String jspName) { 189 this.jspName = jspName; 190 } 191 192 195 public String getFormID() { 196 return formID; 197 } 198 199 202 public void setFormID(String string) { 203 formID = string; 204 } 205 206 209 public String getServletUrl() { 210 return servletUrl; 211 } 212 213 216 public WebConversation getWc() { 217 return wc; 218 } 219 220 223 public void setServletUrl(String string) { 224 servletUrl = string; 225 } 226 227 230 public void setWc(WebConversation conversation) { 231 wc = conversation; 232 } 233 234 244 public void xpath(String msg, String xpathExpr, int nodeCount) throws SAXException , 245 JaxenException { 246 Document dom = getCurrentDOM(msg); 247 XPath xpath = new DOMXPath(xpathExpr); 248 List list = xpath.selectNodes(dom); 249 if (list.size() != nodeCount) { 250 StringBuffer sb = new StringBuffer (); 251 sb.append("\n").append(msg).append("\n"); 252 sb.append(" Node count for expression '").append(xpathExpr).append(" does not match"); 253 if (list.size() > 0) { 254 sb.append("\nFound Nodes: "); 255 for (Iterator it = list.iterator(); it.hasNext();) { 256 Node node = (Node ) it.next(); 257 sb.append(node.getNodeName()); 258 if (it.hasNext()) 259 sb.append(", "); 260 } 261 sb.append("\n"); 262 } 263 assertEquals(sb.toString(), nodeCount, list.size()); 264 } 265 } 266 267 public int countNodes(String xpathExpr) throws JaxenException, SAXException { 268 DOMXPath dx = new DOMXPath(xpathExpr); 269 return dx.selectNodes(getCurrentDOM(xpathExpr)).size(); 270 } 271 272 281 public void xpath(String msg, String xpathExpr, String nodeValue) throws SAXException , 282 JaxenException { 283 xpath(msg, xpathExpr, 1); 284 Document dom = getCurrentDOM(msg); 285 XPath xpath = new DOMXPath(xpathExpr); 286 Node n = (Node ) xpath.selectSingleNode(dom); 287 assertEquals(msg, nodeValue, n.getNodeValue()); 288 } 289 290 public void xpath(String msg, String xpathExpr) throws SAXException , JaxenException { 291 xpath(msg, xpathExpr, 1); 292 } 293 294 protected WebResponse showPage(String page) throws MalformedURLException , IOException , SAXException { 295 if (!page.startsWith("/")) 297 page = "/" + page; 298 WebRequest req = new GetMethodWebRequest(getServletUrl() + page); 299 return wc.sendRequest(req); 300 } 301 302 protected WebResponse followLink(String text, int nth) throws JaxenException, IOException , SAXException { 303 return utils.followXPath("//a[contains(., '" + text + "')]", nth); 304 } 305 306 protected WebResponse followLink(String text) throws JaxenException, IOException , SAXException { 307 return followLink(text, 0); 308 } 309 310 public boolean isCheckHtmlErrors() { 311 return checkHtmlErrors; 312 } 313 314 public void setCheckHtmlErrors(boolean checkErros) { 315 this.checkHtmlErrors = checkErros; 316 } 317 318 } 319 | Popular Tags |