KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > DefaultCookieWriterTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.terracotta.session.util;
6
7 import com.tc.object.bytecode.Manager;
8 import com.tc.properties.TCPropertiesImpl;
9 import com.terracotta.session.SessionId;
10
11 import javax.servlet.http.Cookie JavaDoc;
12 import javax.servlet.http.MockHttpServletRequest JavaDoc;
13 import javax.servlet.http.MockHttpServletResponse JavaDoc;
14
15 import junit.framework.TestCase;
16
17 public class DefaultCookieWriterTest extends TestCase {
18
19   private final String JavaDoc cookieName = "SomeCookieName";
20   private final String JavaDoc idValue = "SomeSessionId";
21
22   private DefaultCookieWriter writer;
23   private MockHttpServletRequest JavaDoc req;
24   private MockHttpServletResponse JavaDoc res;
25   private SessionId id;
26
27   public final void setUp() {
28     writer = new DefaultCookieWriter(true, true, true, cookieName, null, ConfigProperties.defaultCookiePath, null, -1,
29                                      false);
30     req = new MockHttpServletRequest JavaDoc();
31     res = new MockHttpServletResponse JavaDoc();
32     id = new DefaultSessionId(idValue, idValue, idValue, Manager.LOCK_TYPE_WRITE);
33   }
34
35   public final void testConstructor() {
36     // test default c-tor
37
DefaultCookieWriter w = DefaultCookieWriter.makeInstance(new ConfigProperties(null, TCPropertiesImpl
38         .getProperties()));
39     assertEquals(ConfigProperties.defaultCookieName, w.cookieName);
40
41   }
42
43   public final void testCreateCookie() {
44     final Cookie JavaDoc c = writer.createCookie(req, res, id);
45     checkCookie(cookieName, idValue, req.getContextPath(), c);
46   }
47
48   public final void testWriteCookie() {
49     writer.writeCookie(req, res, id);
50     Cookie JavaDoc[] cookies = res.getCookies();
51     assertNotNull(cookies);
52     assertEquals(1, cookies.length);
53     checkCookie(cookieName, idValue, req.getContextPath(), cookies[0]);
54   }
55
56   public final void testGetCookiePath() {
57     // when path specified in c-tor is ConfigProperties.defaultCookiePath, request.getContextPath should be returned
58
assertEquals(req.getContextPath(), writer.getCookiePath(req));
59     // in case an override is specified it should be used instead
60
final String JavaDoc pathOverride = "/SomePath";
61     writer = new DefaultCookieWriter(true, true, true, cookieName, null, pathOverride, null, -1, false);
62     assertEquals(pathOverride, writer.getCookiePath(req));
63   }
64
65   public final void testUrlRewrite() {
66     final String JavaDoc requestUrl = "http://localhost:8080/some_page.jsp";
67     req.setRequestUrl(requestUrl);
68     req.setRequestedSessionId(id.getExternalId());
69     req.setSidFromCookie(false);
70     req.setSidValid(true);
71     req.setScheme("http");
72     req.setServerName("localhost");
73     req.setServerPort(8080);
74     final String JavaDoc actual = writer.encodeRedirectURL("/", req);
75     final String JavaDoc expected = "/;" + this.cookieName.toLowerCase() + "=" + id.getExternalId();
76     assertEquals(expected, actual);
77   }
78
79   private final void checkCookie(final String JavaDoc cName, final String JavaDoc cVal, final String JavaDoc path, Cookie JavaDoc c) {
80     assertEquals(cName, c.getName());
81     assertEquals(cVal, c.getValue());
82     assertEquals(path, c.getPath());
83   }
84
85 }
86
Popular Tags