1 16 package org.apache.cocoon; 17 18 import com.gargoylesoftware.htmlunit.Page; 19 import com.gargoylesoftware.htmlunit.SubmitMethod; 20 import com.gargoylesoftware.htmlunit.WebClient; 21 import com.gargoylesoftware.htmlunit.WebResponse; 22 import com.gargoylesoftware.htmlunit.html.DomNode; 23 import com.gargoylesoftware.htmlunit.html.HtmlPage; 24 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath; 25 import com.gargoylesoftware.htmlunit.xml.XmlPage; 26 27 import junit.framework.TestCase; 28 29 import org.apache.avalon.framework.logger.ConsoleLogger; 30 import org.apache.avalon.framework.logger.Logger; 31 32 import org.apache.commons.httpclient.methods.DeleteMethod; 33 import org.apache.commons.httpclient.methods.PutMethod; 34 import org.apache.commons.httpclient.methods.StringRequestEntity; 35 36 import org.apache.commons.io.FileUtils; 37 38 import org.jaxen.SimpleNamespaceContext; 39 import org.jaxen.XPath; 40 import org.jaxen.dom.DOMXPath; 41 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Node ; 44 45 import java.io.File ; 46 import java.net.URL ; 47 import java.util.ArrayList ; 48 import java.util.HashMap ; 49 import java.util.Map ; 50 51 81 public abstract class HtmlUnitTestCase 82 extends TestCase 83 { 84 89 protected Logger logger; 90 91 95 protected URL baseURL; 96 97 100 protected WebClient webClient; 101 102 105 protected WebResponse response; 106 107 111 protected Object document; 112 113 116 protected Map namespaces = new HashMap (); 117 118 121 protected void setUp() 122 throws Exception 123 { 124 super.setUp(); 125 126 String level = System.getProperty("junit.test.loglevel", "" + ConsoleLogger.LEVEL_WARN); 127 this.logger = new ConsoleLogger(Integer.parseInt(level)); 128 129 String baseurl = System.getProperty("htmlunit.test.baseurl"); 130 this.baseURL = new URL (baseurl); 131 this.webClient = new WebClient(); 132 this.webClient.setRedirectEnabled(false); 133 this.namespaces.clear(); 134 } 135 136 139 protected void tearDown() 140 throws Exception 141 { 142 this.response = null; 143 this.document = null; 144 145 super.tearDown(); 146 } 147 148 151 protected void loadResponse(String pageURL) 152 throws Exception 153 { 154 URL url = new URL (baseURL, pageURL); 155 this.response = webClient.loadWebResponse(url, SubmitMethod.GET, new ArrayList (0)); 156 } 157 158 161 protected void loadDeleteResponse(String pageURL) 162 throws Exception 163 { 164 URL url = new URL (baseURL, pageURL); 165 DeleteMethod method = new DeleteMethod(url.toExternalForm()); 166 this.response = new HttpClientResponse(url, method); 167 } 168 169 172 protected void loadPutResponse(String pageURL, String content) 173 throws Exception 174 { 175 URL url = new URL (baseURL, pageURL); 176 PutMethod method = new PutMethod(url.toExternalForm()); 177 method.setRequestEntity(new StringRequestEntity(content)); 178 this.response = new HttpClientResponse(url, method); 179 } 180 181 184 protected void loadHtmlPage(String pageURL) 185 throws Exception 186 { 187 URL url = new URL (baseURL, pageURL); 188 Page page = webClient.getPage(url); 189 this.response = page.getWebResponse(); 190 assertTrue("Response should be an HTML page", page instanceof HtmlPage); 191 this.document = page; 192 assertNotNull("Response contains invalid HTML", this.document); 193 } 194 195 198 protected void loadXmlPage(String pageURL) 199 throws Exception 200 { 201 URL url = new URL (baseURL, pageURL); 202 Page page = webClient.getPage(url); 203 this.response = page.getWebResponse(); 204 assertTrue("Response should be an XML page", page instanceof XmlPage); 205 XmlPage xmlPage = (XmlPage)page; 206 this.document = xmlPage.getXmlDocument(); 207 assertNotNull("Response contains invalid XML", this.document); 208 } 209 210 218 protected String evalXPath(String xpathExpr) 219 throws Exception 220 { 221 XPath xpath = null; 222 if( document == null ) 223 return null; 224 else if( document instanceof HtmlPage ) 225 xpath = new HtmlUnitXPath(xpathExpr); 226 else if( document instanceof Document ) 227 xpath = new DOMXPath(xpathExpr); 228 else 229 fail("Document type "+document.getClass().getName()); 230 231 xpath.setNamespaceContext(new SimpleNamespaceContext(namespaces)); 232 233 return xpath.stringValueOf(document); 234 } 235 236 239 protected void addNamespace(String prefix, String uri) 240 throws Exception 241 { 242 namespaces.put(prefix, uri); 243 } 244 245 248 protected void assertXPath(String xpathExpr, String expected) 249 throws Exception 250 { 251 assertEquals(xpathExpr, expected, evalXPath(xpathExpr)); 252 } 253 254 263 protected void copyWebappFile(String filename, String param, String value) 264 throws Exception 265 { 266 String srcdir = System.getProperty("htmlunit.test.source-dir"); 267 String dstdir = System.getProperty("htmlunit.test.deploy-dir"); 268 File srcfile = new File (srcdir+"/"+filename); 269 File dstfile = new File (dstdir+"/"+filename); 270 271 final String encoding = "ISO-8859-1"; 272 StringBuffer content = new StringBuffer (FileUtils.readFileToString(srcfile, encoding)); 273 274 int index = content.indexOf(param); 275 while( index != -1 ) { 276 content.replace(index, index+param.length(), value); 277 index = content.indexOf(param, index+1); 278 } 279 280 FileUtils.writeStringToFile(dstfile, content.toString(), encoding); 281 282 284 Thread.sleep(1000); 285 } 286 } 287 | Popular Tags |