1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.views.jsp;
22
23 import java.io.File;
24 import java.io.StringWriter;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.jsp.JspWriter;
30
31 import org.apache.struts2.ServletActionContext;
32 import org.apache.struts2.StrutsTestCase;
33 import org.apache.struts2.TestAction;
34 import org.apache.struts2.dispatcher.ApplicationMap;
35 import org.apache.struts2.dispatcher.Dispatcher;
36 import org.apache.struts2.dispatcher.RequestMap;
37 import org.apache.struts2.dispatcher.SessionMap;
38
39 import com.mockobjects.dynamic.Mock;
40 import com.opensymphony.xwork2.Action;
41 import com.opensymphony.xwork2.ActionContext;
42 import com.opensymphony.xwork2.inject.Container;
43 import com.opensymphony.xwork2.util.ValueStack;
44 import com.opensymphony.xwork2.util.ValueStackFactory;
45
46
47 /***
48 * Base class to extend for unit testing UI Tags.
49 *
50 */
51 public abstract class AbstractTagTest extends StrutsTestCase {
52 protected Action action;
53 protected Map context;
54 protected Map session;
55 protected ValueStack stack;
56
57 /***
58 * contains the buffer that our unit test will write to. we can later verify this buffer for correctness.
59 */
60 protected StringWriter writer;
61 protected StrutsMockHttpServletRequest request;
62 protected StrutsMockPageContext pageContext;
63 protected HttpServletResponse response;
64 protected StrutsMockServletContext servletContext;
65
66 protected Mock mockContainer;
67
68 /***
69 * Constructs the action that we're going to test against. For most UI tests, this default action should be enough.
70 * However, simply override getAction to return a custom Action if you need something more sophisticated.
71 *
72 * @return the Action to be added to the ValueStack as part of the unit test
73 */
74 public Action getAction() {
75 return new TestAction();
76 }
77
78 protected void setUp() throws Exception {
79 super.setUp();
80
81 /***
82 * create our standard mock objects
83 */
84 action = this.getAction();
85 stack = ValueStackFactory.getFactory().createValueStack();
86 context = stack.getContext();
87 stack.push(action);
88
89 request = new StrutsMockHttpServletRequest();
90 request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
91 response = new StrutsMockHttpServletResponse();
92 request.setSession(new StrutsMockHttpSession());
93 request.setupGetServletPath("/");
94
95 writer = new StringWriter();
96
97 JspWriter jspWriter = new StrutsMockJspWriter(writer);
98
99 servletContext = new StrutsMockServletContext();
100 servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
101 servletContext.setServletInfo("Resin");
102
103 pageContext = new StrutsMockPageContext();
104 pageContext.setRequest(request);
105 pageContext.setResponse(response);
106 pageContext.setJspWriter(jspWriter);
107 pageContext.setServletContext(servletContext);
108
109 mockContainer = new Mock(Container.class);
110 Dispatcher du = new Dispatcher(pageContext.getServletContext(), new HashMap());
111 du.init();
112 Dispatcher.setInstance(du);
113 du.setConfigurationManager(configurationManager);
114 session = new SessionMap(request);
115 Map extraContext = du.createContextMap(new RequestMap(request),
116 request.getParameterMap(),
117 session,
118 new ApplicationMap(pageContext.getServletContext()),
119 request,
120 response,
121 pageContext.getServletContext());
122
123
124 extraContext.remove(ActionContext.LOCALE);
125 stack.getContext().putAll(extraContext);
126
127 context.put(ServletActionContext.HTTP_REQUEST, request);
128 context.put(ServletActionContext.HTTP_RESPONSE, response);
129 context.put(ServletActionContext.SERVLET_CONTEXT, servletContext);
130
131 ActionContext.setContext(new ActionContext(context));
132 }
133
134 protected void tearDown() throws Exception {
135 pageContext.verify();
136 request.verify();
137 }
138 }