001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: import java.util.Collection;
021: import java.util.Date;
022:
023: /**
024: * Test Case for {@link Cookies}.
025: */
026: public class CookieTest extends GWTTestCase {
027:
028: public String getModuleName() {
029: return "com.google.gwt.user.User";
030: }
031:
032: public void test() {
033: // Make the cookie expire in one minute, so that they don't hang around
034: // past the end of this test.
035: Date expires = new Date(new Date().getTime() + (60 * 1000));
036:
037: // Test setting a simple cookie.
038: Cookies.setCookie("foo", "bar", expires);
039: assertEquals("bar", Cookies.getCookie("foo"));
040:
041: // Make sure that parsing cookies with embedded '=' works correctly.
042: Cookies.setCookie("foo1", "foo=bar", expires);
043: assertEquals("foo=bar", Cookies.getCookie("foo1"));
044:
045: // Make sure that setting the second cookie doesn't clobber the first.
046: assertEquals("bar", Cookies.getCookie("foo"));
047:
048: // Make sure embedded ';' works as well.
049: Cookies.setCookie("foo2", "foo;bar", expires);
050:
051: // Differentiate null cookie from '' cookie.
052: Cookies.setCookie("novalue", "", expires);
053: assertEquals(Cookies.getCookie("novalue"), "");
054: assertEquals(Cookies.getCookie("notpresent"), null);
055: }
056:
057: /*
058: * Test that the cookie will expire correctly after a set amount of time,
059: * but does not expire before that time.
060: */
061: public void testExpires() {
062: // Test that the cookie expires in 5 seconds
063: Date expiresEarly = new Date(new Date().getTime() + (5 * 1000));
064: Date expiresLate = new Date(new Date().getTime() + (60 * 1000));
065: Cookies.setCookie("shouldExpireEarly", "early", expiresEarly);
066: Cookies.setCookie("shouldExpireLate", "late", expiresLate);
067: Cookies.setCookie("shouldNotExpire", "forever", null);
068:
069: // Wait until the cookie expires before checking it
070: Timer timer = new Timer() {
071: public void run() {
072: // Verify that the early expiring cookie does NOT exist
073: assertNull(Cookies.getCookie("shouldExpireEarly"));
074:
075: // Verify that the late expiring cookie does exist
076: assertEquals(Cookies.getCookie("shouldExpireLate"),
077: "late");
078:
079: // Verify the session cookie doesn't expire
080: assertEquals(Cookies.getCookie("shouldNotExpire"),
081: "forever");
082: Cookies.removeCookie("shouldNotExpire");
083: assertNull(Cookies.getCookie("shouldNotExpire"));
084:
085: // Finish the test
086: finishTest();
087: }
088: };
089: timer.schedule(5010);
090: delayTestFinish(6 * 1000);
091: }
092:
093: /**
094: * Test that removing cookies works correctly.
095: */
096: public void testRemoveCookie() {
097: // First clear all cookies
098: clearCookies();
099:
100: // Set a few cookies
101: Cookies.setCookie("test1", "value1");
102: Cookies.setCookie("test2", "value2");
103: Cookies.setCookie("test3", "value3");
104: Collection<String> cookies = Cookies.getCookieNames();
105: assertEquals(3, cookies.size());
106:
107: // Remove a cookie
108: Cookies.removeCookie("test2");
109: assertEquals("value1", Cookies.getCookie("test1"));
110: assertEquals(null, Cookies.getCookie("test2"));
111: assertEquals("value3", Cookies.getCookie("test3"));
112:
113: // Remove another cookie
114: Cookies.removeCookie("test1");
115: assertEquals(null, Cookies.getCookie("test1"));
116: assertEquals(null, Cookies.getCookie("test2"));
117: assertEquals("value3", Cookies.getCookie("test3"));
118:
119: // Remove last cookie
120: Cookies.removeCookie("test3");
121: assertEquals(null, Cookies.getCookie("test1"));
122: assertEquals(null, Cookies.getCookie("test2"));
123: assertEquals(null, Cookies.getCookie("test3"));
124: cookies = Cookies.getCookieNames();
125: assertEquals(0, cookies.size());
126: }
127:
128: /**
129: * Clear out all existing cookies.
130: */
131: private void clearCookies() {
132: Collection<String> cookies = Cookies.getCookieNames();
133: for (String cookie : cookies) {
134: Cookies.removeCookie(cookie);
135: }
136: }
137: }
|