001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestEngineStateSession.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import java.io.File;
011:
012: import com.meterware.httpunit.*;
013: import com.uwyn.rife.TestCaseServerside;
014: import com.uwyn.rife.tools.FileUtils;
015:
016: public class TestEngineStateSession extends TestCaseServerside {
017: public TestEngineStateSession(int siteType, String name) {
018: super (siteType, name);
019: }
020:
021: public void testSubmissionInputsPreserved() throws Exception {
022: setupSite("site/statesession.xml");
023:
024: WebConversation conversation = new WebConversation();
025: WebRequest request = null;
026: WebResponse response = null;
027: WebLink submission_link = null;
028: WebForm submission_form = null;
029:
030: request = new GetMethodWebRequest(
031: "http://localhost:8181/submissions/inputspreserved");
032: request.setParameter("input1", "submission1input1value");
033: request.setParameter("input4", "submission1input4value");
034: try {
035: response = conversation.getResponse(request);
036: } catch (Throwable e) {
037: getLogSink().getInternalException().printStackTrace();
038: }
039: assertTrue(isSessionResponse(response));
040: submission_link = response.getLinkWith("submission1");
041: assertTrue(isSessionLink(submission_link));
042: submission_link.click();
043: response = conversation.getCurrentPage();
044:
045: assertEquals("thevalue" + "submission1input1value" + "null"
046: + "input3default", response.getText());
047:
048: request = new GetMethodWebRequest(
049: "http://localhost:8181/submissions/inputspreserved");
050: request.setParameter("input1", "submission2input1value");
051: request.setParameter("input4", "submission2input4value");
052: response = conversation.getResponse(request);
053: submission_form = response.getForms()[0];
054: assertTrue(isSessionForm(submission_form));
055: submission_form.setParameter("login", "gbevin2");
056: response = submission_form.submit();
057: submission_form = response.getForms()[0];
058: assertTrue(isSessionForm(submission_form));
059: submission_form.setParameter("password", "oncemoremypassword");
060: response = submission_form.submit();
061:
062: assertEquals("gbevin2,oncemoremypassword"
063: + "submission2input1value" + "null" + "input3default",
064: response.getText());
065:
066: request = new GetMethodWebRequest(
067: "http://localhost:8181/submissions/inputspreserved");
068: request.setParameter("input1", "submission3input1value");
069: request.setParameter("input4", "submission3input4value");
070: response = conversation.getResponse(request);
071: submission_form = response.getForms()[1];
072: assertTrue(isSessionForm(submission_form));
073: response = submission_form.submit();
074: submission_form = response.getForms()[1];
075: assertTrue(isSessionForm(submission_form));
076: submission_form.setParameter("login", "me");
077: response = submission_form.submit();
078:
079: assertEquals("me,it is" + "submission3input1value" + "null"
080: + "input3default", response.getText());
081:
082: request = new GetMethodWebRequest(
083: "http://localhost:8181/submissions/inputspreserved");
084: request.setParameter("input1", "submission4input1value");
085: request.setParameter("input4", "submission4input4value");
086: response = conversation.getResponse(request);
087: submission_link = response.getLinkWith("submission4");
088: assertTrue(isSessionLink(submission_link));
089: submission_link.click();
090: response = conversation.getCurrentPage();
091:
092: assertEquals("submission4" + "submission4input1value" + "null"
093: + "input3default", response.getText());
094: }
095:
096: public void testFileUploadSingleParamMultipleFiles()
097: throws Exception {
098: setupSite("site/statesession.xml");
099:
100: WebConversation conversation = new WebConversation();
101: WebRequest request = null;
102: WebResponse response = null;
103: WebForm form = null;
104:
105: request = new GetMethodWebRequest(
106: "http://localhost:8181/fileupload/simple");
107: response = conversation.getResponse(request);
108: assertTrue(isSessionResponse(response));
109: form = response.getForms()[0];
110: assertTrue(isSessionForm(form));
111:
112: String upload1_content = "abcdefghijklmnopqrstuvwxyz";
113: File upload1 = File.createTempFile("rifetest", ".tmp");
114: String upload2_content = "oiuezroizehfkjsdgfhgizeugfizuhfksjdhfiuzhfiuzehfizeuhfziuh";
115: File upload2 = File.createTempFile("rifetest", ".tmp");
116: upload1.deleteOnExit();
117: FileUtils.writeString(upload1_content, upload1);
118: upload2.deleteOnExit();
119: FileUtils.writeString(upload2_content, upload2);
120:
121: form.setParameter("purpose", "it will serve you well");
122: UploadFileSpec upload_spec1 = new UploadFileSpec(upload1);
123: UploadFileSpec upload_spec2 = new UploadFileSpec(upload2);
124: form.setParameter("doc1", new UploadFileSpec[] { upload_spec1,
125: upload_spec2 });
126:
127: response = form.submit();
128: assertEquals(upload1_content + "," + upload2_content
129: + ";no file 2;it will serve you well", response
130: .getText());
131:
132: upload1.delete();
133: upload2.delete();
134: }
135:
136: public void testExistsDirectlink() throws Exception {
137: setupSite("site/statesession.xml");
138: WebConversation conversation = new WebConversation();
139: WebRequest request = new GetMethodWebRequest(
140: "http://localhost:8181/exits/directlink/source");
141: WebResponse response = conversation.getResponse(request);
142: assertTrue(isSessionResponse(response));
143: WebLink direct_link = response.getLinkWith("direct link");
144: assertTrue(isSessionLink(direct_link));
145: direct_link.click();
146: response = conversation.getCurrentPage();
147: assertEquals("this isgreat", response.getText());
148: }
149:
150: public void testExitsGeneratedUrl() throws Exception {
151: setupSite("site/statesession.xml");
152:
153: WebConversation conversation = new WebConversation();
154: WebRequest request = null;
155: WebResponse response = null;
156:
157: request = new GetMethodWebRequest(
158: "http://localhost:8181/exits/generatedurl/source");
159: response = conversation.getResponse(request);
160: assertTrue(isSessionResponse(response));
161: WebLink exit1_link = response.getLinkWith("exit1");
162: assertTrue(isSessionLink(exit1_link));
163: response = exit1_link.click();
164: assertEquals("the first,the second", response.getText());
165:
166: request = new GetMethodWebRequest(
167: "http://localhost:8181/exits/generatedurl/source");
168: response = conversation.getResponse(request);
169: WebLink exit2_link = response.getLinkWith("exit2");
170: assertTrue(isSessionLink(exit2_link));
171: response = exit2_link.click();
172: assertEquals("the second,the third", response.getText());
173:
174: request = new GetMethodWebRequest(
175: "http://localhost:8181/exits/generatedurl/source?switch=overridden");
176: response = conversation.getResponse(request);
177: WebLink exit1_link_overridden = response.getLinkWith("exit1");
178: assertTrue(isSessionLink(exit1_link_overridden));
179: response = exit1_link_overridden.click();
180: assertEquals("the overridden first,the second", response
181: .getText());
182:
183: request = new GetMethodWebRequest(
184: "http://localhost:8181/exits/generatedurl/source?switch=overridden");
185: response = conversation.getResponse(request);
186: WebLink exit2_link_overridden = response.getLinkWith("exit2");
187: assertTrue(isSessionLink(exit2_link_overridden));
188: response = exit2_link_overridden.click();
189: assertEquals("the second,the overridden third", response
190: .getText());
191: }
192:
193: public void testExistsDirectlinkQuerystate() throws Exception {
194: setupSite("site/statesession.xml");
195: WebConversation conversation = new WebConversation();
196: WebRequest request = new GetMethodWebRequest(
197: "http://localhost:8181/exits/directlink/source/querystate");
198: WebResponse response = conversation.getResponse(request);
199: assertTrue(isSessionResponse(response));
200: WebLink direct_link = response.getLinkWith("direct link");
201: assertFalse(isSessionLink(direct_link));
202: direct_link.click();
203: response = conversation.getCurrentPage();
204: assertEquals("this isgreat", response.getText());
205: }
206:
207: public void testSubmissionSubsite() throws Exception {
208: setupSite("site/statesession.xml");
209:
210: WebConversation conversation = new WebConversation();
211: WebRequest request = new GetMethodWebRequest(
212: "http://localhost:8181/subsite/submission");
213: WebResponse response = conversation.getResponse(request);
214: assertTrue(isSessionResponse(response));
215: WebForm submission_form = response.getForms()[0];
216: assertTrue(isSessionForm(submission_form));
217: submission_form.setParameter("login", "gbevin2");
218: submission_form.setParameter("password", "oncemoremypassword");
219: response = submission_form.submit();
220:
221: assertEquals("gbevin2,oncemoremypassword", response.getText());
222: }
223:
224: private boolean isSessionResponse(WebResponse response) {
225: return 1 == response.getNewCookieNames().length
226: && "JSESSIONID".equals(response.getNewCookieNames()[0]);
227: }
228:
229: private boolean isSessionLink(WebLink link) {
230: return link.getURLString().indexOf(ReservedParameters.STATEID) != -1;
231: }
232:
233: private boolean isSessionForm(WebForm form) {
234: return form.hasParameterNamed(ReservedParameters.STATEID);
235: }
236: }
|