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