KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > tests > TestCookies


1 // StrutsTestCase - a JUnit extension for testing Struts actions
2
// within the context of the ActionServlet.
3
// Copyright (C) 2002 Deryl Seale
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the Apache Software License as
7
// published by the Apache Software Foundation; either version 1.1
8
// of the License, or (at your option) any later version.
9
//
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 JavaDoc;
23
24 public class TestCookies extends TestCase {
25
26     HttpServletRequestSimulator request;
27
28     public TestCookies(String JavaDoc 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 JavaDoc("test","testValue"));
42     Cookie JavaDoc[] cookies = request.getCookies();
43     boolean assertion = false;
44     for (int i = 0; i < cookies.length; i++) {
45         if ((cookies[i].getName().equals("test")) && (cookies[i].getValue().equals("testValue")))
46         assertion = true;
47     }
48     assertTrue(assertion);
49     }
50
51     public void testSetCookies() {
52     Cookie JavaDoc[] cookies = new Cookie JavaDoc[2];
53     cookies[0] = new Cookie JavaDoc("test","testValue");
54     cookies[1] = new Cookie JavaDoc("test2","testValue2");
55     boolean assert1 = false;
56     boolean assert2 = false;
57     request.setCookies(cookies);
58     Cookie JavaDoc[] resultCookies = request.getCookies();
59     for (int i = 0; i < resultCookies.length; i++) {
60         if ((resultCookies[i].getName().equals("test")) && (resultCookies[i].getValue().equals("testValue")))
61         assert1 = true;
62         if ((resultCookies[i].getName().equals("test2")) && (resultCookies[i].getValue().equals("testValue2")))
63         assert2 = true;
64
65     }
66     assertTrue(assert1 && assert2);
67     }
68
69     public void testCheckForWrongCookie() {
70     request.addCookie(new Cookie JavaDoc("test","testValue"));
71     Cookie JavaDoc[] cookies = request.getCookies();
72     boolean assertion = true;
73     for (int i = 0; i < cookies.length; i++) {
74         if ((cookies[i].getName().equals("badValue")) && (cookies[i].getValue().equals("dummyValue")))
75         assertion = false;
76     }
77     assertTrue(assertion);
78     }
79
80
81
82
83 }
84
Popular Tags