01: // StrutsTestCase - a JUnit extension for testing Struts actions
02: // within the context of the ActionServlet.
03: // Copyright (C) 2002 Deryl Seale
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the Apache Software License as
07: // published by the Apache Software Foundation; either version 1.1
08: // of the License, or (at your option) any later version.
09: //
10: // This library is distributed in the hope that it will be useful,
11: // but WITHOUT ANY WARRANTY; without even the implied warranty of
12: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: // Apache Software Foundation Licens for more details.
14: //
15: // You may view the full text here: http://www.apache.org/LICENSE.txt
16:
17: package servletunit.tests;
18:
19: import junit.framework.TestCase;
20: import servletunit.HttpServletRequestSimulator;
21:
22: import javax.servlet.http.Cookie;
23:
24: public class TestCookies extends TestCase {
25:
26: HttpServletRequestSimulator request;
27:
28: public TestCookies(String testName) {
29: super (testName);
30: }
31:
32: public void setUp() {
33: this .request = new HttpServletRequestSimulator(null);
34: }
35:
36: public void testNoCookies() {
37: assertNull(request.getCookies());
38: }
39:
40: public void testAddCookie() {
41: request.addCookie(new Cookie("test", "testValue"));
42: Cookie[] cookies = request.getCookies();
43: boolean assertion = false;
44: for (int i = 0; i < cookies.length; i++) {
45: if ((cookies[i].getName().equals("test"))
46: && (cookies[i].getValue().equals("testValue")))
47: assertion = true;
48: }
49: assertTrue(assertion);
50: }
51:
52: public void testSetCookies() {
53: Cookie[] cookies = new Cookie[2];
54: cookies[0] = new Cookie("test", "testValue");
55: cookies[1] = new Cookie("test2", "testValue2");
56: boolean assert1 = false;
57: boolean assert2 = false;
58: request.setCookies(cookies);
59: Cookie[] resultCookies = request.getCookies();
60: for (int i = 0; i < resultCookies.length; i++) {
61: if ((resultCookies[i].getName().equals("test"))
62: && (resultCookies[i].getValue().equals("testValue")))
63: assert1 = true;
64: if ((resultCookies[i].getName().equals("test2"))
65: && (resultCookies[i].getValue()
66: .equals("testValue2")))
67: assert2 = true;
68:
69: }
70: assertTrue(assert1 && assert2);
71: }
72:
73: public void testCheckForWrongCookie() {
74: request.addCookie(new Cookie("test", "testValue"));
75: Cookie[] cookies = request.getCookies();
76: boolean assertion = true;
77: for (int i = 0; i < cookies.length; i++) {
78: if ((cookies[i].getName().equals("badValue"))
79: && (cookies[i].getValue().equals("dummyValue")))
80: assertion = false;
81: }
82: assertTrue(assertion);
83: }
84:
85: }
|