001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.server.appserver.unit;
006:
007: import com.meterware.httpunit.WebResponse;
008: import com.tc.test.server.appserver.AppServerFactory;
009: import com.tc.test.server.appserver.deployment.AbstractDeploymentTest;
010: import com.tc.test.server.appserver.deployment.Deployment;
011: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
012: import com.tc.test.server.appserver.deployment.GenericServer;
013: import com.tc.test.server.appserver.deployment.ServerTestSetup;
014: import com.tc.test.server.appserver.deployment.WebApplicationServer;
015: import com.tc.test.server.appserver.was6x.Was6xAppServer;
016: import com.tc.test.server.util.TcConfigBuilder;
017: import com.tc.util.io.TCFileUtils;
018: import com.tctest.webapp.servlets.OkServlet;
019:
020: import java.io.File;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import junit.framework.Test;
029:
030: /**
031: * @author hhuynh
032: */
033: public class CookieSettingTest extends AbstractDeploymentTest {
034: private static final String CONTEXT = "CookieSettingTest";
035: private static Deployment deployment;
036: private static TcConfigBuilder tcConfigBuilder;
037: private static WebApplicationServer server0;
038: private static WebApplicationServer server1;
039:
040: public CookieSettingTest() {
041: //
042: }
043:
044: public static Test suite() {
045: return new ServerTestSetup(CookieSettingTest.class);
046: }
047:
048: protected boolean shouldKillAppServersEachRun() {
049: return false;
050: }
051:
052: public void setUp() throws Exception {
053: super .setUp();
054:
055: if (deployment == null) {
056: tcConfigBuilder = new TcConfigBuilder();
057: tcConfigBuilder.addWebApplication(CONTEXT);
058:
059: deployment = makeDeployment();
060:
061: // server0 is NOT enabled with DSO
062: GenericServer.setDsoEnabled(false);
063: server0 = makeWebApplicationServer(tcConfigBuilder);
064: server0.addWarDeployment(deployment, CONTEXT);
065: setCookieForWebsphere(server0);
066: server0.start();
067:
068: // server1 is enabled with DSO
069: GenericServer.setDsoEnabled(true);
070: server1 = makeWebApplicationServer(tcConfigBuilder);
071: server1.addWarDeployment(deployment, CONTEXT);
072: setCookieForWebsphere(server1);
073: server1.start();
074: }
075: }
076:
077: public void testCookieSetting() throws Exception {
078: System.out.println("Hitting server0...");
079: WebResponse response0 = server0.ping("/" + CONTEXT + "/ok");
080: System.out.println("Cookie from server0 w/o DSO: "
081: + response0.getHeaderField("Set-Cookie"));
082:
083: System.out.println("Hitting server1...");
084: WebResponse response1 = server1.ping("/" + CONTEXT + "/ok");
085: System.out.println("Cookie from server1 w/ DSO: "
086: + response1.getHeaderField("Set-Cookie"));
087:
088: assertCookie(response0.getHeaderField("Set-Cookie"), response1
089: .getHeaderField("Set-Cookie"));
090: }
091:
092: private Deployment makeDeployment() throws Exception {
093: DeploymentBuilder builder = makeDeploymentBuilder(CONTEXT
094: + ".war");
095: builder.addServlet("OkServlet", "/ok/*", OkServlet.class, null,
096: false);
097: builder.addSessionConfig("session-timeout", "69");
098:
099: // add container specific descriptor
100: if (AppServerFactory.getCurrentAppServerId() == AppServerFactory.WEBLOGIC) {
101: if (AppServerFactory.getCurrentAppServerMajorVersion()
102: .equals("8")) {
103: builder
104: .addResourceFullpath(
105: "/com/tctest/server/appserver/unit/cookiesettingtest",
106: "weblogic81.xml",
107: "WEB-INF/weblogic.xml");
108: }
109: if (AppServerFactory.getCurrentAppServerMajorVersion()
110: .equals("9")) {
111: builder
112: .addResourceFullpath(
113: "/com/tctest/server/appserver/unit/cookiesettingtest",
114: "weblogic92.xml",
115: "WEB-INF/weblogic.xml");
116: }
117: }
118:
119: return builder.makeDeployment();
120: }
121:
122: private void setCookieForWebsphere(WebApplicationServer server)
123: throws Exception {
124: if (AppServerFactory.getCurrentAppServerId() == AppServerFactory.WEBSPHERE) {
125: System.out.println("Setting cookie for websphere...");
126: File cookieSettingsScript = TCFileUtils
127: .getResourceFile("/com/tctest/server/appserver/unit/cookiesettingtest/cookiesettings.py");
128: Was6xAppServer wasServer = (Was6xAppServer) ((GenericServer) server)
129: .getAppServer();
130: wasServer.setExtraScript(cookieSettingsScript);
131: }
132: }
133:
134: private void assertCookie(String expected, String actual)
135: throws Exception {
136: SimpleDateFormat formatter = new SimpleDateFormat(
137: "EEE, dd MMM yyyy HH:mm:ss z");
138:
139: Map expectedCookie = toHash(expected);
140: Map actualCookie = toHash(actual);
141:
142: // all keys are matched
143: assertEquals(expectedCookie.keySet(), actualCookie.keySet());
144:
145: // all content are matched exception [J]SESSIONID
146: Set keys = expectedCookie.keySet();
147: for (Iterator it = keys.iterator(); it.hasNext();) {
148: String key = (String) it.next();
149:
150: // expires time need to match appromixately within 15s of each other
151: if (key.equalsIgnoreCase("expires")) {
152: // websphere doesn't use "-" between date when the other webapps do
153: // strip them out to be consistent
154: Date expectedDate = formatter
155: .parse(((String) expectedCookie.get(key))
156: .replace('-', ' '));
157: Date actualDate = formatter
158: .parse(((String) actualCookie.get(key))
159: .replace('-', ' '));
160: assertTrue(Math.abs(actualDate.getTime()
161: - expectedDate.getTime()) < 15 * 1000);
162: continue;
163: }
164:
165: if (key.toUpperCase().endsWith("SESSIONID")) {
166: switch (AppServerFactory.getCurrentAppServerId()) {
167: case AppServerFactory.WEBLOGIC:
168: case AppServerFactory.WEBSPHERE:
169: assertEquals(key.toUpperCase(), "CUSTOMSESSIONID");
170: break;
171: default:
172: assertEquals(key.toUpperCase(), "JSESSIONID");
173: }
174: } else {
175: assertEquals(expectedCookie.get(key), actualCookie
176: .get(key));
177: }
178: }
179:
180: }
181:
182: private Map toHash(String cookieString) {
183: Map map = new HashMap();
184: String[] tokens = cookieString.split(";");
185: for (int i = 0; i < tokens.length; i++) {
186: String[] name_value = tokens[i].trim().split("=");
187: if (name_value.length == 2) {
188: map.put(name_value[0], name_value[1]);
189: } else {
190: map.put(name_value[0], "");
191: }
192: }
193: return map;
194: }
195: }
|