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