1 16 17 package test.httpunit; 18 19 import junit.framework.TestCase; 20 import com.meterware.httpunit.*; 21 22 import java.io.*; 23 import java.net.MalformedURLException ; 24 25 import org.xml.sax.SAXException ; 26 27 30 public class HttpUnitTestBase extends TestCase { 31 35 protected String url; 36 37 public HttpUnitTestBase(String s) { 38 super(s); 39 } 40 41 private static String URL_PROPERTY="test.functional.webapp.url"; 42 46 public void setUp() throws Exception { 47 url=System.getProperty(URL_PROPERTY); 48 assertNotNull(URL_PROPERTY+" not set",url); 49 HttpUnitOptions.setExceptionsThrownOnErrorStatus(true); 50 HttpUnitOptions.setMatchesIgnoreCase(true); 51 HttpUnitOptions.setParserWarningsEnabled(true); 52 } 53 54 61 public void assertStringInBody(WebResponse response,String searchfor, String url) 62 throws IOException { 63 String body=response.getText(); 64 boolean found=body.indexOf(searchfor)>=0; 65 if(!found) { 66 String message; 67 message="failed to find ["+searchfor+"] at "+url; 68 fail(message); 69 } 70 } 71 72 80 protected void assertStringInBody( WebRequest request, 81 String searchfor 82 ) 83 throws IOException, org.xml.sax.SAXException { 84 WebResponse response = makeRequest(request); 85 assertStringInBody(response,searchfor,request.getURL().toString()); 86 } 87 88 95 protected WebResponse makeRequest(WebRequest request) throws IOException, SAXException { 96 WebConversation session = new WebConversation(); 97 WebResponse response=session.getResponse(request); 98 return response; 99 } 100 101 108 protected void assertStringNotInBody(WebResponse response, 109 String searchfor, 110 String url) 111 throws IOException { 112 String body=response.getText(); 113 boolean found=body.indexOf(searchfor)>=0; 114 if(found) { 115 String message; 116 message="unexpectedly found "+searchfor+" at "+url; 117 fail(message); 118 } 119 120 } 121 122 129 protected void assertStringNotInBody( WebRequest request, 130 String searchfor) 131 throws IOException, org.xml.sax.SAXException { 132 WebConversation session = new WebConversation(); 133 WebResponse response=session.getResponse(request); 134 assertStringNotInBody(response,searchfor, 135 request.getURL().toString()); 136 } 137 138 150 protected void expectErrorCode(WebRequest request, 151 int errorCode, String errorText) 152 throws MalformedURLException , IOException, SAXException { 153 WebConversation session = new WebConversation(); 154 String failureText="Expected error "+errorCode+" from "+request.getURL(); 155 try { 156 session.getResponse(request); 157 fail(errorText+" -got success instead"); 158 } catch (HttpException e) { 159 assertEquals(failureText,errorCode,e.getResponseCode()); 160 167 } 168 } 169 } 170 | Popular Tags |