001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: import javax.servlet.http.Cookie;
021:
022: import org.apache.tapestry.internal.test.TestableRequestImpl;
023: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
024: import org.testng.Assert;
025: import org.testng.annotations.Test;
026:
027: /**
028: * Tests for {@link org.apache.tapestry.services.impl.CookiesImpl}.
029: */
030: @Test
031: public class CookiesImplTest extends Assert {
032: private static class ComparableCookie extends Cookie {
033: public ComparableCookie(String name, String value, int maxAge) {
034: super (name, value);
035: setMaxAge(maxAge);
036: }
037:
038: @Override
039: public boolean equals(Object obj) {
040: Cookie c = (Cookie) obj;
041:
042: return equals(getName(), c.getName())
043: && equals(getValue(), c.getValue())
044: && equals(getPath(), c.getPath())
045: && getMaxAge() == c.getMaxAge();
046: }
047:
048: private boolean equals(Object value, Object other) {
049: return value == other
050: || (value != null && value.equals(other));
051: }
052: }
053:
054: private CookieSource newCookieSource(final String[] nameValues) {
055: return new CookieSource() {
056: public Cookie[] getCookies() {
057:
058: Cookie[] cookies = null;
059:
060: if (nameValues != null) {
061:
062: List<Cookie> l = new ArrayList<Cookie>();
063:
064: for (int i = 0; i < nameValues.length; i += 2) {
065: String name = nameValues[i];
066: String value = nameValues[i + 1];
067:
068: Cookie c = new Cookie(name, value);
069:
070: l.add(c);
071: }
072:
073: cookies = l.toArray(new Cookie[l.size()]);
074: }
075: return cookies;
076: }
077: };
078: }
079:
080: private void attempt(String name, String expected,
081: String[] nameValues) {
082: // In seconds
083: final int ONE_WEEK = 7 * 24 * 60 * 60;
084: CookiesImpl cs = new CookiesImpl(null,
085: newCookieSource(nameValues), null, ONE_WEEK);
086: String actual = cs.readCookieValue(name);
087: assertEquals(actual, expected);
088: }
089:
090: public void test_No_Cookies() {
091: attempt("foo", null, null);
092: }
093:
094: public void test_Match() {
095: attempt("fred", "flintstone", new String[] { "barney",
096: "rubble", "fred", "flintstone" });
097: }
098:
099: public void test_No_Match() {
100: attempt("foo", null, new String[] { "bar", "baz" });
101: }
102:
103: public void test_Write_Cookie_Domain() {
104: List<Cookie> cookies = CollectionFactory.newList();
105: CookiesImpl cs = createCookiesFixture("/context", cookies);
106:
107: cs.writeDomainCookieValue("foo", "bar", "fobar.com", 1234);
108: Cookie expectedCookie = new ComparableCookie("foo", "bar", 1234);
109: expectedCookie.setPath("/context/");
110: expectedCookie.setDomain("fobar.com");
111: assertEquals(cookies.size(), 1);
112: assertEquals(cookies.get(0), expectedCookie);
113: }
114:
115: private CookiesImpl createCookiesFixture(String contextPath,
116: final List<Cookie> cookies) {
117: return new CookiesImpl(new TestableRequestImpl(contextPath),
118: null, new CookieSink() {
119: public void addCookie(Cookie cookie) {
120: cookies.add(cookie);
121: }
122:
123: }, 1000);
124: }
125:
126: public void test_Write_Cookie_With_Max_Age() {
127: final List<Cookie> cookies = CollectionFactory.newList();
128: CookiesImpl cs = createCookiesFixture("/ctx", cookies);
129:
130: cs.writeCookieValue("foo", "bar", -1);
131: Cookie expectedCookie = new ComparableCookie("foo", "bar", -1);
132: expectedCookie.setPath("/ctx/");
133: assertEquals(cookies.size(), 1);
134: assertEquals(cookies.get(0), expectedCookie);
135: }
136:
137: public void test_Write_Cookie() {
138: final List<Cookie> cookies = CollectionFactory.newList();
139: CookiesImpl cs = createCookiesFixture("/ctx", cookies);
140:
141: cs.writeCookieValue("foo", "bar");
142: Cookie expectedCookie = new ComparableCookie("foo", "bar", 1000);
143: expectedCookie.setPath("/ctx/");
144: assertEquals(cookies.size(), 1);
145: assertEquals(cookies.get(0), expectedCookie);
146: }
147:
148: public void test_Remove_Cookie() {
149: final List<Cookie> cookies = CollectionFactory.newList();
150: CookiesImpl cs = createCookiesFixture("/ctx", cookies);
151:
152: cs.removeCookieValue("foo");
153: Cookie expectedCookie = new ComparableCookie("foo", null, 0);
154: expectedCookie.setPath("/ctx/");
155: assertEquals(cookies.size(), 1);
156: assertEquals(cookies.get(0), expectedCookie);
157: }
158: }
|