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