001: /*
002: * All content copyright (c) 2003-2007 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.WebConversation;
008: import com.meterware.httpunit.WebResponse;
009: import com.tc.test.server.appserver.AppServerFactory;
010: import com.tc.test.server.appserver.deployment.AbstractDeploymentTest;
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.SessionConfigServlet;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import junit.framework.Test;
025:
026: /**
027: * Test session-descriptor setting http://edocs.bea.com/wls/docs81/webapp/weblogic_xml.html#1038173
028: * http://edocs.bea.com/wls/docs90/webapp/weblogic_xml.html#1071982
029: *
030: * @author hhuynh
031: */
032: public class SessionConfigTest extends AbstractDeploymentTest {
033: private static final String RESOURCE_ROOT = "/com/tctest/server/appserver/unit/sessionconfigtest";
034: private static final String CONTEXT = "SessionConfigTest";
035: private static final String MAPPING = "app";
036:
037: private DeploymentBuilder builder;
038: private TcConfigBuilder tcConfigBuilder;
039: private WebApplicationServer server;
040: private Map descriptors = new HashMap();
041: private Map extraServerJvmArgs = new HashMap();
042: private boolean weblogicOrWebsphere;
043:
044: public SessionConfigTest() {
045: boolean weblogicOrWebsphere = AppServerFactory
046: .getCurrentAppServerId() == AppServerFactory.WEBLOGIC
047: || AppServerFactory.getCurrentAppServerId() == AppServerFactory.WEBSPHERE;
048: }
049:
050: public static Test suite() {
051: return new ServerTestSetup(SessionConfigTest.class);
052: }
053:
054: // CookieEnabled = false
055: public void testCookieDisabled() throws Exception {
056: if (!weblogicOrWebsphere)
057: return;
058: descriptors.put("wl81", "weblogic81a.xml");
059: descriptors.put("wl92", "weblogic92a.xml");
060: descriptors.put("was61", "websphere61a.xml");
061: init();
062:
063: WebConversation conversation = new WebConversation();
064:
065: WebResponse response = request(server,
066: "testcase=testCookieDisabled&hit=0", conversation);
067: assertEquals("OK", response.getText().trim());
068:
069: response = request(server, "testcase=testCookieDisabled&hit=1",
070: conversation);
071: assertEquals("OK", response.getText().trim());
072: }
073:
074: // CookieEnabled = false, URLRewritingEnabled = false
075: public void testUrlRewritingDisabled() throws Exception {
076: if (!weblogicOrWebsphere)
077: return;
078: descriptors.put("wl81", "weblogic81b.xml");
079: descriptors.put("wl92", "weblogic92b.xml");
080: descriptors.put("was61", "websphere61b.xml");
081: init();
082:
083: WebResponse response = request(server,
084: "testcase=testUrlRewritingDisabled",
085: new WebConversation());
086: System.out.println("Response: " + response.getText());
087: assertEquals("OK", response.getText().trim());
088: }
089:
090: // TrackingEnabled = false -- only applicable to Weblogic
091: public void testTrackingDisabled() throws Exception {
092: if (!weblogicOrWebsphere)
093: return;
094: if (AppServerFactory.getCurrentAppServerId() != AppServerFactory.WEBLOGIC) {
095: return;
096: }
097: descriptors.put("wl81", "weblogic81c.xml");
098: descriptors.put("wl92", "weblogic92c.xml");
099: init();
100:
101: WebConversation wc = new WebConversation();
102: WebResponse response = request(server,
103: "testcase=testTrackingDisabled&hit=0", wc);
104: System.out.println("Response: " + response.getText());
105: assertEquals("OK", response.getText().trim());
106:
107: response = request(server,
108: "testcase=testTrackingDisabled&hit=1", wc);
109: System.out.println("Response: " + response.getText());
110: assertEquals("OK", response.getText().trim());
111: }
112:
113: // IDLength = 69 -- only applicable to Weblogic
114: public void testIdLength() throws Exception {
115: if (AppServerFactory.getCurrentAppServerId() != AppServerFactory.WEBLOGIC) {
116: return;
117: }
118: descriptors.put("wl81", "weblogic81d.xml");
119: descriptors.put("wl92", "weblogic92d.xml");
120: init();
121:
122: WebConversation wc = new WebConversation();
123: WebResponse response = request(server, "", wc);
124: assertEquals("OK", response.getText().trim());
125: int idLength = wc.getCookieValue("JSESSIONID").indexOf("!");
126: System.out.println(wc.getCookieValue("JSESSIONID")
127: + ", length = " + idLength);
128: assertEquals(69, idLength);
129: }
130:
131: // test session-timeout field in web.xml
132: public void testSessionTimeOutNegative() throws Exception {
133: initWithSessionTimeout(-1);
134: WebConversation wc = new WebConversation();
135: WebResponse response = request(server,
136: "testcase=testSessionTimeOutNegative&hit=0", wc);
137: long actual = Long.parseLong(response.getText().replaceAll(
138: "[^\\d+-]", ""));
139: System.out.println("actual: " + actual);
140: assertTrue(actual < 0);
141:
142: // even though negative number should dictate the session never timeouts
143: // we only test it for 1 minute to save time.
144: Thread.sleep(60 * 1000);
145: response = request(server,
146: "testcase=testSessionTimeOutNegative&hit=1", wc);
147: assertEquals("OK", response.getText().trim());
148: }
149:
150: // test session-timeout field in web.xml
151: public void testSessionTimeOutArbitrary() throws Exception {
152: int someBigValue = Integer.MAX_VALUE / 60;
153: initWithSessionTimeout(someBigValue);
154: WebConversation wc = new WebConversation();
155: WebResponse response = request(server,
156: "testcase=testSessionTimeOutArbitrary", wc);
157: int actual = Integer.parseInt(response.getText().replaceAll(
158: "[^\\d+]", ""));
159: assertEquals(someBigValue * 60, actual);
160: }
161:
162: public void testSessionTimeOutFromTCProperties() throws Exception {
163: extraServerJvmArgs.put("com.tc.session.maxidle.seconds", String
164: .valueOf(Integer.MAX_VALUE));
165: init();
166: WebConversation wc = new WebConversation();
167: WebResponse response = request(server,
168: "testcase=testSessionTimeOutArbitrary", wc);
169: int actual = Integer.parseInt(response.getText().replaceAll(
170: "[^\\d+]", ""));
171: assertEquals(Integer.MAX_VALUE, actual);
172: }
173:
174: private void initWithSessionTimeout(int timeOutInMinutes)
175: throws Exception {
176: createTestDeployment();
177: builder.addSessionConfig("session-timeout", String
178: .valueOf(timeOutInMinutes));
179: createAndStartAppServer();
180: }
181:
182: private WebResponse request(WebApplicationServer appserver,
183: String params, WebConversation con) throws Exception {
184: return appserver.ping("/" + CONTEXT + "/" + MAPPING + "?"
185: + params, con);
186: }
187:
188: private DeploymentBuilder createTestDeployment() {
189: tcConfigBuilder = new TcConfigBuilder();
190: tcConfigBuilder.addWebApplication(CONTEXT);
191:
192: // prepare test war
193: builder = makeDeploymentBuilder(CONTEXT + ".war");
194: builder.addServlet("SessionConfigServlet",
195: "/" + MAPPING + "/*", SessionConfigServlet.class, null,
196: false);
197:
198: return builder;
199: }
200:
201: private void createAndStartAppServer() throws Exception {
202: server = makeWebApplicationServer(tcConfigBuilder);
203: addSessionDescriptor();
204: server.addWarDeployment(builder.makeDeployment(), CONTEXT);
205: if (extraServerJvmArgs.size() > 0) {
206: for (Iterator it = extraServerJvmArgs.entrySet().iterator(); it
207: .hasNext();) {
208: Map.Entry e = (Map.Entry) it.next();
209: server.getServerParameters().appendSysProp(
210: (String) e.getKey(), (String) e.getValue());
211: }
212: }
213: server.start();
214: }
215:
216: private void init() throws Exception {
217: createTestDeployment();
218: createAndStartAppServer();
219: }
220:
221: private void addSessionDescriptor() throws Exception {
222: if (descriptors.size() == 0)
223: return;
224:
225: switch (AppServerFactory.getCurrentAppServerId()) {
226: case AppServerFactory.WEBLOGIC:
227: if (AppServerFactory.getCurrentAppServerMajorVersion()
228: .equals("8")) {
229: builder.addResourceFullpath(RESOURCE_ROOT,
230: (String) descriptors.get("wl81"),
231: "WEB-INF/weblogic.xml");
232: }
233: if (AppServerFactory.getCurrentAppServerMajorVersion()
234: .equals("9")) {
235: builder.addResourceFullpath(RESOURCE_ROOT,
236: (String) descriptors.get("wl92"),
237: "WEB-INF/weblogic.xml");
238: }
239: break;
240: case AppServerFactory.WEBSPHERE:
241: Was6xAppServer wasServer = (Was6xAppServer) ((GenericServer) server)
242: .getAppServer();
243: wasServer.setExtraScript(TCFileUtils
244: .getResourceFile(RESOURCE_ROOT + "/"
245: + (String) descriptors.get("was61")));
246: break;
247: default:
248: break;
249: }
250: }
251:
252: }
|