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: TestEngineMocks.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.test;
009:
010: import com.uwyn.rife.config.Config;
011: import com.uwyn.rife.config.RifeConfig;
012: import com.uwyn.rife.engine.ReservedParameters;
013: import com.uwyn.rife.engine.SiteBuilder;
014: import com.uwyn.rife.engine.exceptions.MultipartFileTooBigException;
015: import com.uwyn.rife.resources.ResourceFinderClasspath;
016: import com.uwyn.rife.test.MockConversation;
017: import com.uwyn.rife.test.MockResponse;
018: import com.uwyn.rife.tools.Base64;
019: import com.uwyn.rife.tools.FileUtils;
020: import com.uwyn.rife.tools.StringUtils;
021: import com.uwyn.rife.tools.SwallowingLogFormatter;
022: import java.io.File;
023: import java.net.InetAddress;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.logging.Formatter;
027: import java.util.logging.Handler;
028: import java.util.logging.Logger;
029: import junit.framework.TestCase;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032:
033: // These are just a collection of different kind of engine tests that
034: // are already done in the engine testsuite within the embedded servlet
035: // container. By using the same site-structures, we can check that the
036: // out-of-container tests yield the same results.
037: public class TestEngineMocks extends TestCase {
038: public TestEngineMocks(String name) {
039: super (name);
040: }
041:
042: public void testSimpleHtml() throws Exception {
043: SiteBuilder builder = new SiteBuilder("test",
044: ResourceFinderClasspath.getInstance()).enterElement(
045: "element/engine/simple_html.xml")
046: .setUrl("/simple/html").leaveElement();
047: MockConversation conversation = new MockConversation(builder
048: .getSite());
049:
050: MockResponse response = conversation
051: .doRequest("http://localhost/simple/html");
052: assertEquals(200, response.getStatus());
053:
054: // Get the host name
055: String hostname = InetAddress.getByName("127.0.0.1")
056: .getHostName();
057:
058: assertEquals("text/html; charset=UTF-8", response
059: .getContentType());
060: assertEquals("Just some text 127.0.0.1:" + hostname
061: + ":.simple_html:", response.getText());
062:
063: assertEquals(".simple_html", response.getLastElementId());
064: }
065:
066: public void testWrongServerRootUrl() throws Exception {
067: SiteBuilder builder = new SiteBuilder("test",
068: ResourceFinderClasspath.getInstance()).enterElement(
069: "element/engine/simple_html.xml")
070: .setUrl("/simple/html").leaveElement();
071: MockConversation conversation = new MockConversation(builder
072: .getSite());
073:
074: assertNull(conversation
075: .doRequest("http://10.0.0.1/simple/html"));
076: }
077:
078: public void testContinuationsConditional() throws Exception {
079: SiteBuilder builder = new SiteBuilder("site/continuations.xml",
080: ResourceFinderClasspath.getInstance());
081: MockConversation conversation = new MockConversation(builder
082: .getSite());
083: MockForm form;
084: ParsedHtml parsed;
085:
086: MockResponse response = conversation.doRequest("/conditional");
087: assertEquals(200, response.getStatus());
088: parsed = response.getParsedHtml();
089:
090: assertEquals("printing", parsed.getTitle());
091: form = parsed.getFormWithName("pause");
092: assertNotNull(form);
093: form.setParameter("answer", "1");
094: response = form.submit();
095: assertEquals(".conditional", response.getLastElementId());
096: parsed = response.getParsedHtml();
097:
098: assertEquals("pauzing", parsed.getTitle());
099: form = parsed.getFormWithName("pause");
100: assertNotNull(form);
101: form.setParameter("answer", "1"); // will not be checked
102: response = form.submit();
103: assertEquals(".conditional", response.getLastElementId());
104: parsed = response.getParsedHtml();
105:
106: assertEquals("pauzingprinting", parsed.getTitle());
107: form = parsed.getFormWithName("pause");
108: assertNotNull(form);
109: form.setParameter("answer", "0");
110: response = form.submit();
111: assertEquals(".conditional", response.getLastElementId());
112: parsed = response.getParsedHtml();
113:
114: assertEquals("printing", parsed.getTitle());
115: form = parsed.getFormWithName("pause");
116: assertNotNull(form);
117: response = conversation.doRequest("/conditional",
118: new MockRequest());
119: assertEquals(".conditional", response.getLastElementId());
120: parsed = response.getParsedHtml();
121:
122: assertEquals("printing", parsed.getTitle());
123: form = parsed.getFormWithName("pause");
124: assertNotNull(form);
125: form.setParameter("stop", "1");
126: response = form.submit();
127: assertEquals(".conditional", response.getLastElementId());
128: parsed = response.getParsedHtml();
129:
130: assertEquals("stopping", parsed.getTitle());
131: }
132:
133: public void testCookies() throws Exception {
134: SiteBuilder builder = new SiteBuilder("site/cookies.xml",
135: ResourceFinderClasspath.getInstance());
136: MockConversation conversation = new MockConversation(builder
137: .getSite()).cookie("cookie1",
138: "this is the first cookie").cookie("cookie2",
139: "this is the second cookie").cookie("cookie3",
140: "this is the third cookie");
141:
142: MockResponse response = conversation
143: .doRequest("/cookies/valid/source");
144:
145: // check if the correct cookies were returned
146: assertEquals(conversation.getCookie("cookie3").getValue(),
147: "this is the first cookie");
148: assertEquals(conversation.getCookie("cookie4").getValue(),
149: "this is the second cookie");
150:
151: // new page with cookie context
152: conversation.cookie("cookie4", "this is the fourth cookie");
153: response = conversation.doRequest("/cookies/valid/destination");
154: assertEquals(".COOKIES_VALID_DESTINATION", response
155: .getLastElementId());
156: assertEquals(
157: "this is the second cookie,this is the first cookie,this is the fourth cookie",
158: response.getText());
159: }
160:
161: public void testEmbeddingCookies() throws Exception {
162: SiteBuilder builder = new SiteBuilder("site/embedding.xml",
163: ResourceFinderClasspath.getInstance());
164: MockConversation conversation = new MockConversation(builder
165: .getSite());
166:
167: MockResponse response = conversation.doRequest("/cookies");
168: assertEquals(
169: "value 1,"
170: + "value 2,"
171: + "value 3,"
172: + "value 4,"
173: + "<!--V 'OUTCOOKIE:cookie5'/-->,"
174: + "<form action=\"/cookies\"><input name=\"submission\" type=\"hidden\" value=\"submission\" /><input name=\"submissioncontext\" type=\"hidden\" value=\""
175: + Base64.encodeToString(
176: ".COOKIES::engine_embedding_cookies:.COOKIES_EMBEDDED^.COOKIES_EMBEDDED"
177: .getBytes("UTF-8"), false)
178: + "\" /><input type=\"submit\" /></form>\n",
179: response.getText());
180:
181: ParsedHtml parsed = response.getParsedHtml();
182: MockForm form = parsed.getForms().get(0);
183: response = form.submit();
184:
185: assertEquals(".COOKIES", response.getLastElementId());
186: assertEquals("embedded value 1," + "embedded value 2,"
187: + "value 3," + "embedded value 4,"
188: + "<!--V 'OUTCOOKIE:cookie5'/-->,submitted\n", response
189: .getText());
190: }
191:
192: public void testEmbeddingPriorities() throws Exception {
193: SiteBuilder builder = new SiteBuilder("site/embedding.xml",
194: ResourceFinderClasspath.getInstance());
195: MockConversation conversation = new MockConversation(builder
196: .getSite());
197:
198: MockResponse response = conversation.doRequest("/priorities");
199: assertEquals("These elements are embedded:\n"
200: + ".PRIORITIES_EMBEDDED_LATE\n"
201: + ".PRIORITIES_EMBEDDED_NORMAL\n"
202: + ".PRIORITIES_EMBEDDED_EARLY\n" + "done.\n"
203: + ".PRIORITIES_EMBEDDED_EARLY\n"
204: + ".PRIORITIES_EMBEDDED_NORMAL\n" + ".PRIORITIES\n"
205: + ".PRIORITIES_EMBEDDED_LATE\n", response.getText());
206:
207: List<MockResponse> embedded_reponses = response
208: .getEmbeddedResponses();
209: assertEquals(3, embedded_reponses.size());
210: assertEquals(".PRIORITIES_EMBEDDED_EARLY", embedded_reponses
211: .get(0).getLastElementId());
212: assertEquals(0, embedded_reponses.get(0).getEmbeddedResponses()
213: .size());
214: assertEquals(".PRIORITIES_EMBEDDED_NORMAL", embedded_reponses
215: .get(1).getLastElementId());
216: assertEquals(0, embedded_reponses.get(1).getEmbeddedResponses()
217: .size());
218: assertEquals(".PRIORITIES_EMBEDDED_LATE", embedded_reponses
219: .get(2).getLastElementId());
220: assertEquals(0, embedded_reponses.get(2).getEmbeddedResponses()
221: .size());
222:
223: assertEquals(".PRIORITIES_EMBEDDED_EARLY", response
224: .getEmbeddedResponse("-PRIORITIES_EMBEDDED_EARLY")
225: .getLastElementId());
226: assertEquals(".PRIORITIES_EMBEDDED_NORMAL", response
227: .getEmbeddedResponse("PRIORITIES_EMBEDDED_NORMAL")
228: .getLastElementId());
229: assertEquals(".PRIORITIES_EMBEDDED_LATE", response
230: .getEmbeddedResponse("+PRIORITIES_EMBEDDED_LATE")
231: .getLastElementId());
232: }
233:
234: public void testEmbeddingSuccessiveGlobalcookie() throws Exception {
235: SiteBuilder builder = new SiteBuilder("site/embedding.xml",
236: ResourceFinderClasspath.getInstance());
237: MockConversation conversation = new MockConversation(builder
238: .getSite());
239:
240: MockResponse response = conversation
241: .doRequest("/successive_globalcookie");
242: assertEquals(".SUCCESSIVE_GLOBALCOOKIE", response
243: .getLastElementId());
244: assertEquals(
245: "embedded value 1,embedded value 2,<!--V 'OUTCOOKIE:cookie3'/-->,<!--V 'OUTCOOKIE:cookie4'/-->,<!--V 'OUTCOOKIE:cookie5'/-->\n"
246: + "first: embedded\n"
247: + "second: embedded value 1embedded value 2no cookie3\n",
248: response.getText());
249: }
250:
251: public void testXPath() throws Exception {
252: SiteBuilder builder = new SiteBuilder("site/exits.xml",
253: ResourceFinderClasspath.getInstance());
254: MockConversation conversation = new MockConversation(builder
255: .getSite());
256:
257: MockResponse response = null;
258:
259: response = conversation.doRequest("/exits/generatedurl/source");
260:
261: NodeList links = response.xpathNodeSet("//a");
262: assertNotNull(links);
263: assertEquals(2, links.getLength());
264: assertEquals("exit1", links.item(0).getTextContent());
265: assertEquals("exit2", links.item(1).getTextContent());
266: assertEquals(
267: "/exits/generatedurl/destination1?input1=the+first&input2=the+second",
268: links.item(0).getAttributes().getNamedItem("href")
269: .getTextContent());
270: assertEquals(
271: "/exits/generatedurl/destination2?input1=the+second&input2=the+third",
272: links.item(1).getAttributes().getNamedItem("href")
273: .getTextContent());
274:
275: Node body = response.xpathNode("/html/body");
276: NodeList body_children = body.getChildNodes();
277: assertEquals(7, body_children.getLength());
278: assertEquals(Node.TEXT_NODE, body_children.item(0)
279: .getNodeType());
280: assertEquals("", body_children.item(0).getTextContent().trim());
281: assertEquals(Node.ELEMENT_NODE, body_children.item(1)
282: .getNodeType());
283: assertEquals("h1", body_children.item(1).getNodeName());
284: assertEquals(Node.TEXT_NODE, body_children.item(2)
285: .getNodeType());
286: assertEquals("", body_children.item(2).getTextContent().trim());
287: assertEquals(Node.ELEMENT_NODE, body_children.item(3)
288: .getNodeType());
289: assertEquals("a", body_children.item(3).getNodeName());
290: assertEquals(Node.TEXT_NODE, body_children.item(4)
291: .getNodeType());
292: assertEquals("", body_children.item(4).getTextContent().trim());
293: assertEquals(Node.ELEMENT_NODE, body_children.item(5)
294: .getNodeType());
295: assertEquals("a", body_children.item(5).getNodeName());
296: assertEquals(Node.TEXT_NODE, body_children.item(6)
297: .getNodeType());
298: assertEquals("", body_children.item(6).getTextContent().trim());
299:
300: assertEquals(
301: "This is a template where the exit urls are generated.",
302: response.xpathString("//h1/text()"));
303:
304: assertEquals(2, response.xpathNumber("count(//a)").intValue());
305:
306: assertTrue(response
307: .xpathBoolean("contains(//a[1]/text(), 'exit1')"));
308: assertFalse(response
309: .xpathBoolean("contains(//a[2]/text(), 'exit1')"));
310: assertEquals(true, response.xpathBoolean(
311: "contains(a[1]/text(), 'exit1')", body).booleanValue());
312:
313: assertEquals(true, response.xpathBoolean(
314: "contains(a[1]/text(), 'exit1')", body).booleanValue());
315: assertEquals(false, response.xpathBoolean(
316: "contains(a[2]/text(), 'exit1')", body).booleanValue());
317: assertEquals(
318: "/exits/generatedurl/destination1?input1=the+first&input2=the+second",
319: response.xpathNode("a[1]", body).getAttributes()
320: .getNamedItem("href").getTextContent());
321: assertEquals(2, response.xpathNodeSet("a", body).getLength());
322: assertEquals(2, response.xpathNumber("count(a)", body)
323: .intValue());
324: assertEquals(
325: "This is a template where the exit urls are generated.",
326: response.xpathString("h1", body));
327: }
328:
329: public void testExitsGeneratedUrl() throws Exception {
330: SiteBuilder builder = new SiteBuilder("site/exits.xml",
331: ResourceFinderClasspath.getInstance());
332: MockConversation conversation = new MockConversation(builder
333: .getSite());
334: ParsedHtml parsed;
335:
336: MockResponse response = null;
337:
338: response = conversation.doRequest("/exits/generatedurl/source");
339: parsed = response.getParsedHtml();
340: MockLink exit1_link = parsed.getLinkWithText("exit1");
341: response = exit1_link.click();
342: assertEquals(".GENERATEDURL_DESTINATION1", response
343: .getLastElementId());
344: assertEquals("the first,the second", response.getText());
345:
346: response = conversation.doRequest("/exits/generatedurl/source");
347: parsed = response.getParsedHtml();
348: MockLink exit2_link = parsed.getLinkWithText("exit2");
349: response = exit2_link.click();
350: assertEquals(".GENERATEDURL_DESTINATION2", response
351: .getLastElementId());
352: assertEquals("the second,the third", response.getText());
353:
354: response = conversation
355: .doRequest("/exits/generatedurl/source?switch=overridden");
356: parsed = response.getParsedHtml();
357: MockLink exit1_link_overridden = parsed
358: .getLinkWithText("exit1");
359: response = exit1_link_overridden.click();
360: assertEquals(".GENERATEDURL_DESTINATION1", response
361: .getLastElementId());
362: assertEquals("the overridden first,the second", response
363: .getText());
364:
365: response = conversation
366: .doRequest("/exits/generatedurl/source?switch=overridden");
367: parsed = response.getParsedHtml();
368: MockLink exit2_link_overridden = parsed
369: .getLinkWithText("exit2");
370: response = exit2_link_overridden.click();
371: assertEquals(".GENERATEDURL_DESTINATION2", response
372: .getLastElementId());
373: assertEquals("the second,the overridden third", response
374: .getText());
375: }
376:
377: public void testExitSelective() throws Exception {
378: SiteBuilder builder = new SiteBuilder("site/exits.xml",
379: ResourceFinderClasspath.getInstance());
380: MockConversation conversation = new MockConversation(builder
381: .getSite());
382:
383: MockResponse response1 = conversation.doRequest(
384: "/exits/selective/source", new MockRequest().parameter(
385: "switch", "1"));
386: assertEquals(".SELECTIVE_DESTINATION1", response1
387: .getLastElementId());
388: assertEquals("destination1", response1.getText());
389:
390: MockResponse response2 = conversation.doRequest(
391: "/exits/selective/source", new MockRequest().parameter(
392: "switch", "2"));
393: assertEquals(".SELECTIVE_DESTINATION2", response2
394: .getLastElementId());
395: assertEquals("destination2", response2.getText());
396:
397: MockResponse response3 = conversation.doRequest(
398: "/exits/selective/source", new MockRequest().parameter(
399: "switch", "3"));
400: assertEquals(".SELECTIVE_DESTINATION3", response3
401: .getLastElementId());
402: assertEquals("destination3", response3.getText());
403: }
404:
405: public void testExitsGeneratedUrlOverflow() throws Exception {
406: SiteBuilder builder = new SiteBuilder("site/exits.xml",
407: ResourceFinderClasspath.getInstance());
408: MockConversation conversation = new MockConversation(builder
409: .getSite());
410:
411: // setup swallowing log formatters
412: HashMap<Handler, Formatter> formatters = new HashMap<Handler, Formatter>();
413: SwallowingLogFormatter formatter = new SwallowingLogFormatter();
414: Logger logger = Logger.getLogger("");
415: for (Handler handler : logger.getHandlers()) {
416: formatters.put(handler, handler.getFormatter());
417: handler.setFormatter(formatter);
418: }
419:
420: try {
421: MockResponse response = conversation
422: .doRequest("/exits/generatedurloverflow/source");
423: assertEquals(".GENERATEDURLOVERFLOW_SOURCE", response
424: .getLastElementId());
425:
426: ParsedHtml parsed = response.getParsedHtml();
427: MockLink exit1_link = parsed.getLinkWithText("exit1");
428: assertTrue(exit1_link
429: .getHref()
430: .startsWith(
431: "/exits/generatedurloverflow/destination;jsessionid="));
432: assertTrue(exit1_link.getHref().indexOf("?stateid=") != -1);
433: response = exit1_link.click();
434: assertEquals(".GENERATEDURLOVERFLOW_DESTINATION", response
435: .getLastElementId());
436: assertEquals("the first,"
437: + StringUtils.repeat("abcdefghijklmnopqrstuvwxyz",
438: 76) + "012345678", response.getText());
439:
440: assertEquals(1, formatter.getRecords().size());
441: assertEquals(
442: "The exit 'exit1' of element '.GENERATEDURLOVERFLOW_SOURCE' generated an URL whose length of 2049 exceeds the maximum length of 2048 bytes, using session state store instead. The generated URL was '/exits/generatedurloverflow/destination?input1=the+first&input2="
443: + StringUtils.repeat(
444: "abcdefghijklmnopqrstuvwxyz", 76)
445: + "012345678" + "'.", formatter
446: .getRecords().get(0).getMessage());
447: } finally {
448: // restore the previous formatters
449: for (Handler handler : logger.getHandlers()) {
450: handler.setFormatter(formatters.get(handler));
451: }
452: }
453: }
454:
455: public void testExpressionInputsGroovy() throws Exception {
456: SiteBuilder builder = new SiteBuilder(
457: "site/expressionelement.xml", ResourceFinderClasspath
458: .getInstance());
459: MockConversation conversation = new MockConversation(builder
460: .getSite());
461:
462: MockResponse response;
463:
464: response = conversation
465: .doRequest("/inputs_groovy?input1=value1");
466: assertEquals(".INPUTS_GROOVY", response.getLastElementId());
467: assertEquals("this is value 1\n\n\n", response.getText());
468:
469: response = conversation
470: .doRequest("/inputs_groovy?input1=value2");
471: assertEquals(".INPUTS_GROOVY", response.getLastElementId());
472: assertEquals("this is value 2\n\n\n", response.getText());
473:
474: response = conversation
475: .doRequest("/inputs_groovy?input1=value3");
476: assertEquals(".INPUTS_GROOVY", response.getLastElementId());
477: assertEquals("<!--V 'GROOVY:value'/-->\n\n\n", response
478: .getText());
479: }
480:
481: public void testInheritanceExits() throws Exception {
482: SiteBuilder builder = new SiteBuilder("site/inheritance.xml",
483: ResourceFinderClasspath.getInstance());
484: MockConversation conversation = new MockConversation(builder
485: .getSite());
486:
487: MockResponse response;
488: MockForm form;
489: ParsedHtml parsed;
490:
491: response = conversation.doRequest("/inheritance/exits",
492: new MockRequest().parameter("childinput1", "will this")
493: .parameter("childinput2", "arrive too"));
494: assertEquals(".EXITS_PARENT2", response.getLastElementId());
495: parsed = response.getParsedHtml();
496:
497: form = parsed.getFormWithName("formparent2");
498: response = form.parameter("activationparent2",
499: "don't go to child").submit();
500: assertEquals(".EXITS_PARENT2", response.getLastElementId());
501: parsed = response.getParsedHtml();
502:
503: form = parsed.getFormWithName("formparent2");
504: response = form.parameter("activationparent2", "go to child")
505: .submit();
506: assertEquals(".EXITS_PARENT1", response.getLastElementId());
507: parsed = response.getParsedHtml();
508:
509: form = parsed.getFormWithName("formparent1");
510: response = form.parameter("activationparent1",
511: "don't go to exit").submit();
512: assertEquals(".EXITS_PARENT1", response.getLastElementId());
513: parsed = response.getParsedHtml();
514:
515: form = parsed.getFormWithName("formparent1");
516: response = form.parameter("activationparent1", "go to exit")
517: .submit();
518: assertEquals(".EXITS_PARENT1_EXIT1", response
519: .getLastElementId());
520: parsed = response.getParsedHtml();
521:
522: form = parsed.getFormWithName("formparent1exit1");
523: response = form.parameter("activationparent1exit1",
524: "don't go to exit").submit();
525: assertEquals(".EXITS_PARENT1_EXIT1", response
526: .getLastElementId());
527: parsed = response.getParsedHtml();
528:
529: form = parsed.getFormWithName("formparent1exit1");
530: response = form.parameter("activationparent1exit1",
531: "go to exit").submit();
532: assertEquals(".EXITS_PARENT1_EXIT2", response
533: .getLastElementId());
534: parsed = response.getParsedHtml();
535:
536: form = parsed.getFormWithName("formparent1exit2");
537: response = form.parameter("activationparent1exit2",
538: "don't go to child").submit();
539: assertEquals(".EXITS_PARENT1_EXIT2", response
540: .getLastElementId());
541: parsed = response.getParsedHtml();
542:
543: form = parsed.getFormWithName("formparent1exit2");
544: response = form.parameter("activationparent1exit2",
545: "go to child").submit();
546: assertEquals(".EXITS_PARENT2", response.getLastElementId());
547: parsed = response.getParsedHtml();
548:
549: form = parsed.getFormWithName("formparent2");
550: response = form.parameter("activationparent2",
551: "don't go to child").submit();
552: assertEquals(".EXITS_PARENT2", response.getLastElementId());
553: parsed = response.getParsedHtml();
554:
555: form = parsed.getFormWithName("formparent2");
556: response = form.parameter("activationparent2", "go to child")
557: .submit();
558: assertEquals(".EXITS_CHILD", response.getLastElementId());
559:
560: assertEquals("will this,arrive too", response.getText());
561: }
562:
563: public void testInputsNamedBeanPrefix() throws Exception {
564: SiteBuilder builder = new SiteBuilder("site/inputs.xml",
565: ResourceFinderClasspath.getInstance());
566: MockConversation conversation = new MockConversation(builder
567: .getSite());
568:
569: MockResponse response;
570: response = conversation.doRequest("/inputs/named_bean/prefix",
571: new MockRequest().parameter("prefix_string",
572: "the string").parameter("prefix_boolean", "y")
573: .parameter("prefix_string", "the string")
574: .parameter("prefix_stringbuffer",
575: "the stringbuffer").parameter(
576: "prefix_int", "23154").parameter(
577: "prefix_integer", "893749").parameter(
578: "prefix_char", "u").parameter(
579: "prefix_character", "R").parameter(
580: "prefix_boolean", "y").parameter(
581: "prefix_booleanObject", "no")
582: .parameter("prefix_byte", "120").parameter(
583: "prefix_byteObject", "21").parameter(
584: "prefix_double", "34878.34").parameter(
585: "prefix_doubleObject", "25435.98")
586: .parameter("prefix_float", "3434.76")
587: .parameter("prefix_floatObject", "6534.8")
588: .parameter("prefix_long", "34347897")
589: .parameter("prefix_longObject", "2335454")
590: .parameter("prefix_short", "32").parameter(
591: "prefix_shortObject", "12"));
592: assertEquals(".NAMED_BEAN_PREFIX", response.getLastElementId());
593: assertEquals(
594: "the string,the stringbuffer,23154,893749,u,null,true,false,0,21,34878.34,25435.98,3434.76,6534.8,34347897,2335454,32,12",
595: response.getText());
596:
597: response = conversation.doRequest("/inputs/named_bean/prefix",
598: new MockRequest().parameter("prefix_string",
599: "the string").parameter("prefix_boolean", "y")
600: .parameter("prefix_string", "the string")
601: .parameter("prefix_stringbuffer",
602: "the stringbuffer").parameter(
603: "prefix_int", "23fd33").parameter(
604: "prefix_char", "u").parameter(
605: "prefix_character", "R").parameter(
606: "prefix_boolean", "y").parameter(
607: "prefix_booleanObject", "no")
608: .parameter("prefix_byte", "120").parameter(
609: "prefix_byteObject", "21").parameter(
610: "prefix_double", "zef.34").parameter(
611: "prefix_doubleObject", "25435.98")
612: .parameter("prefix_float", "3434.76")
613: .parameter("prefix_floatObject", "6534.8")
614: .parameter("prefix_long", "34347897")
615: .parameter("prefix_longObject", "233f5454")
616: .parameter("prefix_short", "32").parameter(
617: "prefix_shortObject", ""));
618: assertEquals(".NAMED_BEAN_PREFIX", response.getLastElementId());
619: assertEquals(
620: "NOTNUMERIC : int\nNOTNUMERIC : double\nNOTNUMERIC : longObject\nthe string,the stringbuffer,0,null,u,null,true,false,0,21,0.0,25435.98,3434.76,6534.8,34347897,null,32,null",
621: response.getText());
622: }
623:
624: public void testInputsGenerated() throws Exception {
625: SiteBuilder builder = new SiteBuilder("site/inputs.xml",
626: ResourceFinderClasspath.getInstance());
627: MockConversation conversation = new MockConversation(builder
628: .getSite());
629:
630: MockResponse response;
631: response = conversation.doRequest("/inputs/generated",
632: new MockRequest().parameter("wantsupdates", "on")
633: .parameter(
634: "colors",
635: new String[] { "orange", "blue",
636: "green" }).parameter(
637: "firstname", "Geert").parameter(
638: "lastname", "Bevin"));
639: assertEquals(".GENERATED", response.getLastElementId());
640: assertEquals(
641: "Geert, Bevin\n"
642: + "<input type=\"checkbox\" name=\"wantsupdates\" checked=\"checked\"> I want updates<br />\n"
643: + "<input type=\"checkbox\" name=\"colors\" value=\"orange\" checked=\"checked\">orange<br />\n"
644: + "<input type=\"checkbox\" name=\"colors\" value=\"blue\" checked=\"checked\">blue<br />\n"
645: + "<input type=\"checkbox\" name=\"colors\" value=\"red\">red<br />\n"
646: + "<input type=\"radio\" name=\"firstname\" checked=\"checked\"> Geert\n"
647: + "<input type=\"radio\" name=\"firstname\"> Nathalie\n"
648: + "<select name=\"lastname\">\n"
649: + "\t<option value=\"Bevin\" selected=\"selected\">Bevin</option>\n"
650: + "\t<option value=\"Mafessoni\">Mafessoni</option>\n"
651: + "</select>\n", response.getText());
652:
653: response = conversation.doRequest("/inputs/generated");
654: assertEquals(".GENERATED", response.getLastElementId());
655: assertEquals(
656: "<!--V 'INPUT:firstname'/-->, <!--V 'INPUT:lastname'/-->\n"
657: + "<input type=\"checkbox\" name=\"wantsupdates\"> I want updates<br />\n"
658: + "<input type=\"checkbox\" name=\"colors\" value=\"orange\">orange<br />\n"
659: + "<input type=\"checkbox\" name=\"colors\" value=\"blue\">blue<br />\n"
660: + "<input type=\"checkbox\" name=\"colors\" value=\"red\">red<br />\n"
661: + "<input type=\"radio\" name=\"firstname\"> Geert\n"
662: + "<input type=\"radio\" name=\"firstname\"> Nathalie\n"
663: + "<select name=\"lastname\">\n"
664: + "\t<option value=\"Bevin\">Bevin</option>\n"
665: + "\t<option value=\"Mafessoni\">Mafessoni</option>\n"
666: + "</select>\n", response.getText());
667: }
668:
669: public void testFileUploadSingleParam() throws Exception {
670: SiteBuilder builder = new SiteBuilder("site/submissions.xml",
671: ResourceFinderClasspath.getInstance());
672: MockConversation conversation = new MockConversation(builder
673: .getSite());
674:
675: MockResponse response;
676: MockForm form;
677: ParsedHtml parsed;
678:
679: response = conversation.doRequest("/fileupload/simple");
680: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
681: parsed = response.getParsedHtml();
682: form = parsed.getForms().get(0);
683:
684: String upload_content = "abcdefghijklmnopqrstuvwxyz";
685: File upload = File.createTempFile("rifetest", ".tmp");
686: upload.deleteOnExit();
687: FileUtils.writeString(upload_content, upload);
688:
689: form.setParameter("purpose", "it will serve you well");
690: MockFileUpload file_upload = new MockFileUpload(upload);
691: form.setFile("doc1", file_upload);
692:
693: response = form.submit();
694: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
695: assertEquals(upload_content
696: + ";no file 2;it will serve you well", response
697: .getText());
698:
699: upload.delete();
700: }
701:
702: public void testFileUploadMultipleParams() throws Exception {
703: SiteBuilder builder = new SiteBuilder("site/submissions.xml",
704: ResourceFinderClasspath.getInstance());
705: MockConversation conversation = new MockConversation(builder
706: .getSite());
707:
708: MockResponse response;
709: MockForm form;
710: ParsedHtml parsed;
711:
712: response = conversation.doRequest("/fileupload/simple");
713: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
714: parsed = response.getParsedHtml();
715: form = parsed.getForms().get(0);
716:
717: String upload1_content = "abcdefghijklmnopqrstuvwxyz";
718: File upload1 = File.createTempFile("rifetest", ".tmp");
719: String upload2_content = "oiuezroizehfkjsdgfhgizeugfizuhfksjdhfiuzhfiuzehfizeuhfziuh";
720: File upload2 = File.createTempFile("rifetest", ".tmp");
721: upload1.deleteOnExit();
722: FileUtils.writeString(upload1_content, upload1);
723: upload2.deleteOnExit();
724: FileUtils.writeString(upload2_content, upload2);
725:
726: form.setParameter("purpose", "it will serve you well");
727: MockFileUpload file_upload1 = new MockFileUpload(upload1);
728: MockFileUpload file_upload2 = new MockFileUpload(upload2);
729: form.setFile("doc1", file_upload1);
730: form.setFile("doc2", file_upload2);
731:
732: response = form.submit();
733: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
734: assertEquals(upload1_content + ";" + upload2_content
735: + ";it will serve you well", response.getText());
736:
737: upload1.delete();
738: upload2.delete();
739: }
740:
741: public void testFileUploadSingleParamMultipleFiles()
742: throws Exception {
743: SiteBuilder builder = new SiteBuilder("site/submissions.xml",
744: ResourceFinderClasspath.getInstance());
745: MockConversation conversation = new MockConversation(builder
746: .getSite());
747:
748: MockResponse response;
749: MockForm form;
750: ParsedHtml parsed;
751:
752: response = conversation.doRequest("/fileupload/simple");
753: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
754: parsed = response.getParsedHtml();
755: form = parsed.getForms().get(0);
756:
757: String upload1_content = "abcdefghijklmnopqrstuvwxyz";
758: File upload1 = File.createTempFile("rifetest", ".tmp");
759: String upload2_content = "oiuezroizehfkjsdgfhgizeugfizuhfksjdhfiuzhfiuzehfizeuhfziuh";
760: File upload2 = File.createTempFile("rifetest", ".tmp");
761: upload1.deleteOnExit();
762: FileUtils.writeString(upload1_content, upload1);
763: upload2.deleteOnExit();
764: FileUtils.writeString(upload2_content, upload2);
765:
766: form.setParameter("purpose", "it will serve you well");
767: MockFileUpload file_upload1 = new MockFileUpload(upload1);
768: MockFileUpload file_upload2 = new MockFileUpload(upload2);
769: form.setFiles("doc1", new MockFileUpload[] { file_upload1,
770: file_upload2 });
771:
772: response = form.submit();
773: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
774: assertEquals(upload1_content + "," + upload2_content
775: + ";no file 2;it will serve you well", response
776: .getText());
777:
778: upload1.delete();
779: upload2.delete();
780: }
781:
782: public void testFileUploadSizeLimit() throws Exception {
783: SiteBuilder builder = new SiteBuilder("site/submissions.xml",
784: ResourceFinderClasspath.getInstance());
785: MockConversation conversation = new MockConversation(builder
786: .getSite());
787:
788: MockResponse response;
789: MockForm form;
790: ParsedHtml parsed;
791:
792: String upload_content_part = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567"; // 128 bytes
793: StringBuffer upload_content = new StringBuffer();
794: File upload = File.createTempFile("rifetest", ".tmp");
795: upload.deleteOnExit();
796: for (int i = 0; i < 4; i++) {
797: for (int j = 0; j < 8; j++) // 1KB
798: {
799: upload_content.append(upload_content_part);
800: }
801: }
802: FileUtils.writeString(upload_content.toString(), upload);
803: MockFileUpload file_upload = null;
804:
805: // exactly the same size as the limit
806: Config.getRepInstance().setParameter(
807: RifeConfig.Engine.PARAM_FILEUPLOAD_SIZE_LIMIT,
808: "" + upload_content.length());
809: response = conversation.doRequest("/fileupload/simple");
810: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
811: parsed = response.getParsedHtml();
812: form = parsed.getForms().get(0);
813:
814: form.setParameter("purpose", "it will serve you well");
815: file_upload = new MockFileUpload(upload);
816: form.setFile("doc1", file_upload);
817:
818: response = form.submit();
819: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
820: assertEquals(upload_content.toString()
821: + ";no file 2;it will serve you well", response
822: .getText());
823:
824: // exceeding the size by 1
825: Config.getRepInstance().setParameter(
826: RifeConfig.Engine.PARAM_FILEUPLOAD_SIZE_LIMIT,
827: "" + (upload_content.length() - 1));
828: response = conversation.doRequest("/fileupload/simple");
829: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
830: parsed = response.getParsedHtml();
831: form = parsed.getForms().get(0);
832:
833: // throw no exception when size is exceeded, but don't provide the uploaded file
834: Config.getRepInstance().setParameter(
835: RifeConfig.Engine.PARAM_FILEUPLOAD_SIZE_EXCEPTION,
836: false);
837: form.setParameter("purpose", "it will serve you well");
838: file_upload = new MockFileUpload(upload);
839: form.setFile("doc1", file_upload);
840: response = form.submit();
841: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
842: assertEquals(
843: "file 1 size exceeded;no file 2;it will serve you well",
844: response.getText());
845:
846: // throw exception when size is exceeded
847: Config
848: .getRepInstance()
849: .setParameter(
850: RifeConfig.Engine.PARAM_FILEUPLOAD_SIZE_EXCEPTION,
851: true);
852: form.setParameter("purpose", "it will serve you well");
853: file_upload = new MockFileUpload(upload);
854: form.setFile("doc1", file_upload);
855:
856: try {
857: response = form.submit();
858: fail();
859: } catch (MultipartFileTooBigException e) {
860: assertEquals("doc1", e.getFileName());
861: assertEquals(upload_content.length() - 1, e.getSizeLimit());
862: }
863:
864: // disable size limit
865: Config.getRepInstance().setParameter(
866: RifeConfig.Engine.PARAM_FILEUPLOAD_SIZE_CHECK, false);
867: response = conversation.doRequest("/fileupload/simple");
868: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
869: parsed = response.getParsedHtml();
870: form = parsed.getForms().get(0);
871:
872: form.setParameter("purpose", "it will serve you well");
873: file_upload = new MockFileUpload(upload);
874: form.setFile("doc1", file_upload);
875:
876: response = form.submit();
877: assertEquals(".FILEUPLOAD_SIMPLE", response.getLastElementId());
878:
879: upload.delete();
880:
881: Config.getRepInstance().setParameter(
882: RifeConfig.Engine.PARAM_FILEUPLOAD_SIZE_CHECK, "true");
883: }
884:
885: private boolean isSessionResponse(MockResponse response) {
886: return 1 == response.getNewCookieNames().size()
887: && "JSESSIONID".equals(response.getNewCookieNames()
888: .get(0));
889: }
890:
891: private boolean isSessionLink(MockLink link) {
892: return link.getHref().indexOf(ReservedParameters.STATEID) != -1;
893: }
894:
895: private boolean isSessionForm(MockForm form) {
896: return form.hasParameter(ReservedParameters.STATEID);
897: }
898:
899: public void testSessionSubmissionInputsPreserved() throws Exception {
900: SiteBuilder builder = new SiteBuilder("site/statesession.xml",
901: ResourceFinderClasspath.getInstance());
902: MockConversation conversation = new MockConversation(builder
903: .getSite());
904:
905: MockResponse response;
906: MockLink submission_link;
907: MockForm submission_form;
908: ParsedHtml parsed;
909:
910: response = conversation.doRequest(
911: "/submissions/inputspreserved", new MockRequest()
912: .parameter("input1", "submission1input1value")
913: .parameter("input4", "submission1input4value"));
914: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
915: parsed = response.getParsedHtml();
916: assertTrue(isSessionResponse(response));
917: submission_link = parsed.getLinkWithText("submission1");
918: assertTrue(isSessionLink(submission_link));
919: response = submission_link.click();
920: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
921:
922: assertEquals("thevalue" + "submission1input1value" + "null"
923: + "input3default", response.getText());
924:
925: response = conversation.doRequest(
926: "/submissions/inputspreserved", new MockRequest()
927: .parameter("input1", "submission2input1value")
928: .parameter("input4", "submission2input4value"));
929: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
930: parsed = response.getParsedHtml();
931: submission_form = parsed.getForms().get(0);
932: assertTrue(isSessionForm(submission_form));
933: submission_form.setParameter("login", "gbevin2");
934: response = submission_form.submit();
935: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
936:
937: submission_form = parsed.getForms().get(0);
938: assertTrue(isSessionForm(submission_form));
939: submission_form.setParameter("password", "oncemoremypassword");
940: response = submission_form.submit();
941: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
942:
943: assertEquals("gbevin2,oncemoremypassword"
944: + "submission2input1value" + "null" + "input3default",
945: response.getText());
946:
947: response = conversation.doRequest(
948: "/submissions/inputspreserved", new MockRequest()
949: .parameter("input1", "submission3input1value")
950: .parameter("input4", "submission3input4value"));
951: parsed = response.getParsedHtml();
952: submission_form = parsed.getForms().get(1);
953: assertTrue(isSessionForm(submission_form));
954: response = submission_form.submit();
955: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
956:
957: submission_form = parsed.getForms().get(1);
958: assertTrue(isSessionForm(submission_form));
959: submission_form.setParameter("login", "me");
960: response = submission_form.submit();
961: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
962:
963: assertEquals("me,it is" + "submission3input1value" + "null"
964: + "input3default", response.getText());
965:
966: response = conversation.doRequest(
967: "/submissions/inputspreserved", new MockRequest()
968: .parameter("input1", "submission4input1value")
969: .parameter("input4", "submission4input4value"));
970: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
971: parsed = response.getParsedHtml();
972: submission_link = parsed.getLinkWithText("submission4");
973: assertTrue(isSessionLink(submission_link));
974: response = submission_link.click();
975: assertEquals(".INPUTSPRESERVED", response.getLastElementId());
976:
977: assertEquals("submission4" + "submission4input1value" + "null"
978: + "input3default", response.getText());
979: }
980: }
|