1 7 package com.inversoft.junit; 8 9 10 import javax.servlet.ServletContext ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 import junit.framework.TestCase; 15 16 import org.apache.log4j.Logger; 17 18 import com.inversoft.junit.internal.Mediator; 19 import com.inversoft.junit.internal.MediatorFactory; 20 import com.inversoft.junit.internal.http.MockHttpServletRequest; 21 import com.inversoft.junit.internal.http.MockHttpServletResponse; 22 import com.inversoft.junit.internal.http.MockServletConfig; 23 import com.inversoft.junit.internal.http.MockServletContext; 24 25 26 155 public class WebTestCase extends TestCase { 156 157 160 private static final Logger logger = Logger.getLogger(WebTestCase.class); 161 162 166 public static final Class [] CONSTRUCTOR_PARAM_TYPES = new Class [] {String .class}; 167 168 169 172 private boolean local; 173 174 177 protected HttpServletRequest request; 178 179 182 protected HttpServletResponse response; 183 184 187 private HttpServletRequest origRequest; 188 189 192 private HttpServletResponse origResponse; 193 194 197 protected ServletContext context; 198 199 200 204 public WebTestCase(String name) { 205 super(name); 206 if (logger.isDebugEnabled()) { 207 logger.debug("The name of the test is " + name); 208 } 209 } 210 211 212 217 public boolean isLocal() { 218 return local; 219 } 220 221 226 public void setLocal(boolean local) { 227 this.local = local; 228 } 229 230 231 238 public final void runBare() throws Throwable { 239 240 setUp(); 243 244 try { 245 Mediator mediator = MediatorFactory.buildMediator(local); 246 mediator.mediate(this); 247 } catch (Throwable t) { 248 if (logger.isDebugEnabled()) { 249 logger.debug("The throwable being thrown from the AbstactTestCase is: " + 250 t.toString()); 251 } 252 throw t; 253 } 254 255 tearDown(); 258 } 259 260 261 265 266 271 public MockHttpServletRequest getRequest() { 272 if (!isLocal()) { 273 throw new IllegalStateException ("You can only call getRequest when" + 274 " running tests locally"); 275 } 276 277 return (MockHttpServletRequest) request; 278 } 279 280 283 public void setRequest(HttpServletRequest request) { 284 this.request = request; 285 } 286 287 292 public MockHttpServletResponse getResponse() { 293 if (!isLocal()) { 294 throw new IllegalStateException ("You can only call getRequest when" + 295 " running tests locally"); 296 } 297 return (MockHttpServletResponse) response; 298 } 299 300 303 public void setResponse(HttpServletResponse response) { 304 this.response = response; 305 } 306 307 314 public HttpServletRequest getOrigRequest() { 315 return origRequest; 316 } 317 318 325 public void setOrigRequest(HttpServletRequest origRequest) { 326 this.origRequest = origRequest; 327 } 328 329 335 public HttpServletResponse getOrigResponse() { 336 return origResponse; 337 } 338 339 345 public void setOrigResponse(HttpServletResponse origResponse) { 346 this.origResponse = origResponse; 347 } 348 349 354 public MockServletContext getContext() { 355 if (!isLocal()) { 356 throw new IllegalStateException ("You can only call getRequest when" + 357 " running tests locally"); 358 } 359 return (MockServletContext) context; 360 } 361 362 365 public void setContext(ServletContext context) { 366 this.context = context; 367 } 368 369 376 public MockServletConfig createConfig() { 377 return new MockServletConfig(context); 378 } 379 380 389 public MockServletConfig createConfig(String servletName) { 390 return new MockServletConfig(servletName, context); 391 } 392 } | Popular Tags |