KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ServletDispatcherResultTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import com.mockobjects.dynamic.C;
8 import com.mockobjects.dynamic.Mock;
9 import com.opensymphony.webwork.ServletActionContext;
10 import com.opensymphony.webwork.WebWorkStatics;
11 import com.opensymphony.xwork.ActionContext;
12 import junit.framework.TestCase;
13 import ognl.Ognl;
14
15 import javax.servlet.RequestDispatcher JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19
20 /**
21  * DOCUMENT ME!
22  *
23  * @author $author$
24  * @version $Revision: 1.5 $
25  */

26 public class ServletDispatcherResultTest extends TestCase implements WebWorkStatics {
27     //~ Methods ////////////////////////////////////////////////////////////////
28

29     public void testInclude() {
30         ServletDispatcherResult view = new ServletDispatcherResult();
31         view.setLocation("foo.jsp");
32
33         Mock dispatcherMock = new Mock(RequestDispatcher JavaDoc.class);
34         dispatcherMock.expect("include", C.ANY_ARGS);
35
36         Mock requestMock = new Mock(HttpServletRequest JavaDoc.class);
37         requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy());
38
39         Mock responseMock = new Mock(HttpServletResponse JavaDoc.class);
40         responseMock.expectAndReturn("isCommitted", Boolean.TRUE);
41
42         ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
43         ActionContext.setContext(ac);
44         ServletActionContext.setRequest((HttpServletRequest JavaDoc) requestMock.proxy());
45         ServletActionContext.setResponse((HttpServletResponse JavaDoc) responseMock.proxy());
46
47         try {
48             view.execute(null);
49         } catch (Exception JavaDoc e) {
50             e.printStackTrace();
51             fail();
52         }
53
54         dispatcherMock.verify();
55         requestMock.verify();
56         dispatcherMock.verify();
57     }
58
59     public void testSimple() {
60         ServletDispatcherResult view = new ServletDispatcherResult();
61         view.setLocation("foo.jsp");
62
63         Mock dispatcherMock = new Mock(RequestDispatcher JavaDoc.class);
64         dispatcherMock.expect("forward", C.ANY_ARGS);
65
66         Mock requestMock = new Mock(HttpServletRequest JavaDoc.class);
67         requestMock.expectAndReturn("getAttribute", "javax.servlet.include.servlet_path", null);
68         requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy());
69         requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
70
requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
71
requestMock.matchAndReturn("getRequestURI", "foo.jsp");
72
73         Mock responseMock = new Mock(HttpServletResponse JavaDoc.class);
74         responseMock.expectAndReturn("isCommitted", Boolean.FALSE);
75
76         ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
77         ActionContext.setContext(ac);
78         ServletActionContext.setRequest((HttpServletRequest JavaDoc) requestMock.proxy());
79         ServletActionContext.setResponse((HttpServletResponse JavaDoc) responseMock.proxy());
80
81         try {
82             view.execute(null);
83         } catch (Exception JavaDoc e) {
84             e.printStackTrace();
85             fail();
86         }
87
88         dispatcherMock.verify();
89         requestMock.verify();
90         dispatcherMock.verify();
91     }
92 }
93
Popular Tags