1 20 package org.apache.cactus.sample.servlet; 21 22 import java.io.IOException ; 23 24 import java.net.URLDecoder ; 25 26 import java.util.Hashtable ; 27 28 import org.apache.cactus.Cookie; 29 import org.apache.cactus.ServletTestCase; 30 import org.apache.cactus.WebRequest; 31 import org.apache.cactus.WebResponse; 32 33 38 public class TestSampleServlet extends ServletTestCase 39 { 40 45 public void testReadServletOutputStream() throws IOException 46 { 47 SampleServlet servlet = new SampleServlet(); 48 49 servlet.doGet(request, response); 50 } 51 52 59 public void endReadServletOutputStream(WebResponse theResponse) 60 throws IOException 61 { 62 String expected = "<html><head/><body>A GET request</body></html>"; 63 String result = theResponse.getText(); 64 65 assertEquals(expected, result); 66 } 67 68 70 77 public void beginPostMethod(WebRequest theRequest) 78 { 79 theRequest.addParameter("param", "value", WebRequest.POST_METHOD); 80 } 81 82 87 public void testPostMethod() 88 { 89 SampleServlet servlet = new SampleServlet(); 90 91 assertEquals("POST", servlet.checkMethod(request)); 92 assertEquals("value", request.getParameter("param")); 93 } 94 95 97 101 public void testGetMethod() 102 { 103 SampleServlet servlet = new SampleServlet(); 104 105 assertEquals("GET", servlet.checkMethod(request)); 106 } 107 108 110 114 public void testSetAttribute() 115 { 116 SampleServlet servlet = new SampleServlet(); 117 118 servlet.setSessionVariable(request); 119 120 assertNotNull(session); 121 assertEquals("value_setSessionVariable", 122 session.getAttribute("name_setSessionVariable")); 123 } 124 125 128 public void testSetRequestAttribute() 129 { 130 SampleServlet servlet = new SampleServlet(); 131 132 servlet.setRequestAttribute(request); 133 134 assertEquals("value_setRequestAttribute", 135 request.getAttribute("name_setRequestAttribute")); 136 } 137 138 140 146 public void beginSendParams(WebRequest theRequest) 147 { 148 theRequest.addParameter("param1", "value1"); 149 theRequest.addParameter("param2", "value2"); 150 } 151 152 155 public void testSendParams() 156 { 157 SampleServlet servlet = new SampleServlet(); 158 Hashtable params = servlet.getRequestParameters(request); 159 160 assertNotNull(params.get("param1")); 161 assertNotNull(params.get("param2")); 162 assertEquals("value1", params.get("param1")); 163 assertEquals("value2", params.get("param2")); 164 } 165 166 168 174 public void beginSendHeader(WebRequest theRequest) 175 { 176 theRequest.addHeader("testheader", "this is a header test"); 177 } 178 179 182 public void testSendHeader() 183 { 184 SampleServlet servlet = new SampleServlet(); 185 String headerValue = servlet.getRequestHeader(request); 186 187 assertEquals("this is a header test", headerValue); 188 } 189 190 192 198 public void beginSendCookie(WebRequest theRequest) 199 { 200 theRequest.addCookie("testcookie", "thisisacookie"); 205 } 206 207 210 public void testSendCookie() 211 { 212 SampleServlet servlet = new SampleServlet(); 213 Hashtable cookies = servlet.getRequestCookies(request); 214 215 assertNotNull("Cannot find [testcookie] cookie in request", 216 cookies.get("testcookie")); 217 assertEquals("thisisacookie", cookies.get("testcookie")); 218 } 219 220 226 public void beginSendMultipleCookies(WebRequest theRequest) 227 { 228 theRequest.addCookie("testcookie1", "cookie1"); 229 theRequest.addCookie("testcookie2", "cookie2"); 230 } 231 232 235 public void testSendMultipleCookies() 236 { 237 SampleServlet servlet = new SampleServlet(); 238 Hashtable cookies = servlet.getRequestCookies(request); 239 240 assertNotNull(cookies.get("testcookie1")); 241 assertEquals("cookie1", cookies.get("testcookie1")); 242 243 assertNotNull(cookies.get("testcookie2")); 244 assertEquals("cookie2", cookies.get("testcookie2")); 245 } 246 247 249 253 public void testReceiveHeader() 254 { 255 SampleServlet servlet = new SampleServlet(); 256 257 servlet.setResponseHeader(response); 258 } 259 260 266 public void endReceiveHeader(WebResponse theResponse) 267 { 268 assertEquals("this is a response header", 269 theResponse.getConnection().getHeaderField("responseheader")); 270 } 271 272 274 281 public void beginReceiveCookie(WebRequest theRequest) 282 { 283 theRequest.setURL("jakarta.apache.org", null, null, null, null); 292 } 293 294 298 public void testReceiveCookie() 299 { 300 SampleServlet servlet = new SampleServlet(); 301 302 servlet.setResponseCookie(response); 303 } 304 305 314 public void endReceiveCookie(WebResponse theResponse) throws Exception 315 { 316 Cookie cookie = theResponse.getCookie("responsecookie"); 317 318 assertNotNull("Cannot find [responsecookie]", cookie); 319 assertEquals("responsecookie", cookie.getName()); 320 321 322 assertEquals("this is a response cookie", 327 URLDecoder.decode(cookie.getValue())); 328 329 assertEquals("jakarta.apache.org", cookie.getDomain()); 330 } 331 332 334 341 public void testRequestDispatcherForward() throws Exception 342 { 343 SampleServlet servlet = new SampleServlet(); 344 345 servlet.doForward(request, response, config); 346 } 347 348 357 public void endRequestDispatcherForward(WebResponse theResponse) 358 throws IOException 359 { 360 assertTrue("Text missing 'Hello !' : [" + theResponse.getText() + "]", 365 theResponse.getText().indexOf("Hello !") > 0); 366 } 367 368 370 377 public void testRequestDispatcherInclude() throws Exception 378 { 379 SampleServlet servlet = new SampleServlet(); 380 381 servlet.doInclude(request, response, config); 382 } 383 384 393 public void endRequestDispatcherInclude(WebResponse theResponse) 394 throws IOException 395 { 396 assertTrue("Text missing 'Hello !' : [" + theResponse.getText() + "]", 401 theResponse.getText().indexOf("Hello !") > 0); 402 } 403 } 404 | Popular Tags |