001 package com.mockrunner.servlet; 002 003 import javax.servlet.Filter; 004 import javax.servlet.ServletRequest; 005 import javax.servlet.ServletResponse; 006 import javax.servlet.http.HttpServlet; 007 008 import com.mockrunner.base.BasicHTMLOutputTestCase; 009 import com.mockrunner.base.HTMLOutputModule; 010 import com.mockrunner.mock.web.WebMockObjectFactory; 011 012 /** 013 * Delegator for {@link com.mockrunner.servlet.ServletTestModule}. You can 014 * subclass this adapter or use {@link com.mockrunner.servlet.ServletTestModule} 015 * directly (so your test case can use another base class). 016 * This basic adapter can be used if you don't need any other modules. It 017 * does not extend {@link com.mockrunner.base.BaseTestCase}. If you want 018 * to use several modules in conjunction, consider subclassing 019 * {@link com.mockrunner.servlet.ServletTestCaseAdapter}. 020 * <b>This class is generated from the {@link com.mockrunner.servlet.ServletTestModule} 021 * and should not be edited directly</b>. 022 */ 023 public abstract class BasicServletTestCaseAdapter extends BasicHTMLOutputTestCase 024 { 025 private ServletTestModule servletTestModule; 026 private WebMockObjectFactory webMockObjectFactory; 027 028 public BasicServletTestCaseAdapter() 029 { 030 031 } 032 033 public BasicServletTestCaseAdapter(String name) 034 { 035 super(name); 036 } 037 038 protected void tearDown() throws Exception 039 { 040 super.tearDown(); 041 servletTestModule = null; 042 webMockObjectFactory = null; 043 } 044 045 /** 046 * Creates the {@link com.mockrunner.servlet.ServletTestModule}. If you 047 * overwrite this method, you must call <code>super.setUp()</code>. 048 */ 049 protected void setUp() throws Exception 050 { 051 super.setUp(); 052 servletTestModule = createServletTestModule(getWebMockObjectFactory()); 053 } 054 055 /** 056 * Creates a {@link com.mockrunner.mock.web.WebMockObjectFactory}. 057 * @return the created {@link com.mockrunner.mock.web.WebMockObjectFactory} 058 */ 059 protected WebMockObjectFactory createWebMockObjectFactory() 060 { 061 return new WebMockObjectFactory(); 062 } 063 064 /** 065 * Same as <code>createWebMockObjectFactory(otherFactory, true)</code>. 066 */ 067 protected WebMockObjectFactory createWebMockObjectFactory(WebMockObjectFactory otherFactory) 068 { 069 return new WebMockObjectFactory(otherFactory); 070 } 071 072 /** 073 * Creates a {@link com.mockrunner.mock.web.WebMockObjectFactory} based on another 074 * {@link com.mockrunner.mock.web.WebMockObjectFactory}. 075 * The created {@link com.mockrunner.mock.web.WebMockObjectFactory} will have its own 076 * request and response objects. If you set <i>createNewSession</i> 077 * to <code>true</code> it will also have its own session object. 078 * The two factories will share one <code>ServletContext</code>. 079 * Especially important for multithreading tests. 080 * If you set <i>createNewSession</i> to false, the two factories 081 * will share one session. This setting simulates multiple requests 082 * from the same client. 083 * @param otherFactory the other factory 084 * @param createNewSession create a new session for the new factory 085 * @return the created {@link com.mockrunner.mock.web.WebMockObjectFactory} 086 */ 087 protected WebMockObjectFactory createWebMockObjectFactory(WebMockObjectFactory otherFactory, boolean createNewSession) 088 { 089 return new WebMockObjectFactory(otherFactory, createNewSession); 090 } 091 092 /** 093 * Gets the {@link com.mockrunner.mock.web.WebMockObjectFactory}. 094 * @return the {@link com.mockrunner.mock.web.WebMockObjectFactory} 095 */ 096 protected WebMockObjectFactory getWebMockObjectFactory() 097 { 098 synchronized(WebMockObjectFactory.class) 099 { 100 if(webMockObjectFactory == null) 101 { 102 webMockObjectFactory = createWebMockObjectFactory(); 103 } 104 } 105 return webMockObjectFactory; 106 } 107 108 /** 109 * Sets the {@link com.mockrunner.mock.web.WebMockObjectFactory}. 110 * @param webMockObjectFactory the {@link com.mockrunner.mock.web.WebMockObjectFactory} 111 */ 112 protected void setWebMockObjectFactory(WebMockObjectFactory webMockObjectFactory) 113 { 114 this.webMockObjectFactory = webMockObjectFactory; 115 } 116 117 /** 118 * Creates a {@link com.mockrunner.servlet.ServletTestModule} based on the current 119 * {@link com.mockrunner.mock.web.WebMockObjectFactory}. 120 * Same as <code>createServletTestModule(getWebMockObjectFactory())</code>. 121 * @return the created {@link com.mockrunner.servlet.ServletTestModule} 122 */ 123 protected ServletTestModule createServletTestModule() 124 { 125 return new ServletTestModule(getWebMockObjectFactory()); 126 } 127 128 /** 129 * Creates a {@link com.mockrunner.servlet.ServletTestModule} with the specified 130 * {@link com.mockrunner.mock.web.WebMockObjectFactory}. 131 * @return the created {@link com.mockrunner.servlet.ServletTestModule} 132 */ 133 protected ServletTestModule createServletTestModule(WebMockObjectFactory mockFactory) 134 { 135 return new ServletTestModule(mockFactory); 136 } 137 138 /** 139 * Returns the {@link com.mockrunner.servlet.ServletTestModule} as 140 * {@link com.mockrunner.base.HTMLOutputModule}. 141 * @return the {@link com.mockrunner.base.HTMLOutputModule} 142 */ 143 protected HTMLOutputModule getHTMLOutputModule() 144 { 145 return servletTestModule; 146 } 147 148 /** 149 * Gets the {@link com.mockrunner.servlet.ServletTestModule}. 150 * @return the {@link com.mockrunner.servlet.ServletTestModule} 151 */ 152 protected ServletTestModule getServletTestModule() 153 { 154 return servletTestModule; 155 } 156 157 /** 158 * Sets the {@link com.mockrunner.servlet.ServletTestModule}. 159 * @param servletTestModule the {@link com.mockrunner.servlet.ServletTestModule} 160 */ 161 protected void setServletTestModule(ServletTestModule servletTestModule) 162 { 163 this.servletTestModule = servletTestModule; 164 } 165 166 /** 167 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#setServlet(HttpServlet)} 168 */ 169 protected void setServlet(HttpServlet servlet) 170 { 171 servletTestModule.setServlet(servlet); 172 } 173 174 /** 175 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#setServlet(HttpServlet, boolean)} 176 */ 177 protected void setServlet(HttpServlet servlet, boolean doInit) 178 { 179 servletTestModule.setServlet(servlet, doInit); 180 } 181 182 /** 183 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#doGet} 184 */ 185 protected void doGet() 186 { 187 servletTestModule.doGet(); 188 } 189 190 /** 191 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#doPost} 192 */ 193 protected void doPost() 194 { 195 servletTestModule.doPost(); 196 } 197 198 /** 199 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#doHead} 200 */ 201 protected void doHead() 202 { 203 servletTestModule.doHead(); 204 } 205 206 /** 207 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#doPut} 208 */ 209 protected void doPut() 210 { 211 servletTestModule.doPut(); 212 } 213 214 /** 215 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#doDelete} 216 */ 217 protected void doDelete() 218 { 219 servletTestModule.doDelete(); 220 } 221 222 /** 223 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#doOptions} 224 */ 225 protected void doOptions() 226 { 227 servletTestModule.doOptions(); 228 } 229 230 /** 231 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#doTrace} 232 */ 233 protected void doTrace() 234 { 235 servletTestModule.doTrace(); 236 } 237 238 /** 239 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#getServlet} 240 */ 241 protected HttpServlet getServlet() 242 { 243 return servletTestModule.getServlet(); 244 } 245 246 /** 247 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#createServlet(Class)} 248 */ 249 protected HttpServlet createServlet(Class servletClass) 250 { 251 return servletTestModule.createServlet(servletClass); 252 } 253 254 /** 255 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#createFilter(Class)} 256 */ 257 protected Filter createFilter(Class filterClass) 258 { 259 return servletTestModule.createFilter(filterClass); 260 } 261 262 /** 263 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#addFilter(Filter)} 264 */ 265 protected void addFilter(Filter filter) 266 { 267 servletTestModule.addFilter(filter); 268 } 269 270 /** 271 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#addFilter(Filter, boolean)} 272 */ 273 protected void addFilter(Filter filter, boolean doInit) 274 { 275 servletTestModule.addFilter(filter, doInit); 276 } 277 278 /** 279 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#releaseFilters} 280 */ 281 protected void releaseFilters() 282 { 283 servletTestModule.releaseFilters(); 284 } 285 286 /** 287 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#setDoChain(boolean)} 288 */ 289 protected void setDoChain(boolean doChain) 290 { 291 servletTestModule.setDoChain(doChain); 292 } 293 294 /** 295 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#doFilter} 296 */ 297 protected void doFilter() 298 { 299 servletTestModule.doFilter(); 300 } 301 302 /** 303 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#getFilteredRequest} 304 */ 305 protected ServletRequest getFilteredRequest() 306 { 307 return servletTestModule.getFilteredRequest(); 308 } 309 310 /** 311 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#getFilteredResponse} 312 */ 313 protected ServletResponse getFilteredResponse() 314 { 315 return servletTestModule.getFilteredResponse(); 316 } 317 318 /** 319 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#clearOutput} 320 */ 321 protected void clearOutput() 322 { 323 servletTestModule.clearOutput(); 324 } 325 326 /** 327 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#init} 328 */ 329 protected void init() 330 { 331 servletTestModule.init(); 332 } 333 334 /** 335 * Delegates to {@link com.mockrunner.servlet.ServletTestModule#service} 336 */ 337 protected void service() 338 { 339 servletTestModule.service(); 340 } 341 }