1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.mc.test.core;
20
21 import java.io.IOException;
22
23 import javax.faces.application.Application;
24 import javax.faces.component.UICommand;
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.UIInput;
27 import javax.faces.context.ExternalContext;
28 import javax.faces.context.FacesContext;
29
30 import org.apache.myfaces.test.mock.MockHttpServletRequest;
31 import org.apache.myfaces.test.mock.MockHttpServletResponse;
32 import org.apache.myfaces.test.mock.MockHttpSession;
33
34
35
36
37
38
39
40
41
42
43 public abstract class AbstractMyFacesRequestTestCase extends AbstractMyFacesTestCase
44 {
45
46 @Override
47 public void setUp() throws Exception
48 {
49 super.setUp();
50
51 }
52
53 @Override
54 public void tearDown() throws Exception
55 {
56 tearDownRequest();
57 session = null;
58 if (client != null)
59 {
60 client.setTestCase(null);
61 }
62 client = null;
63 super.tearDown();
64 }
65
66 protected void setupRequest() throws Exception
67 {
68 setupRequest(null);
69 }
70
71 protected void setupRequest(String pathInfo) throws Exception
72 {
73 if (pathInfo == null)
74 {
75 setupRequest(null, null);
76 }
77 else
78 {
79 int queryIndex = pathInfo.indexOf("?");
80 if (queryIndex >= 0)
81 {
82 setupRequest(pathInfo.substring(0,queryIndex), pathInfo.substring(queryIndex+1));
83 }
84 else
85 {
86 setupRequest(pathInfo, null);
87 }
88 }
89 }
90
91 protected void setupRequest(String pathInfo, String query) throws Exception
92 {
93 session = (session == null) ? new MockHttpSession() : session;
94 session.setServletContext(servletContext);
95 request = new MockHttpServletRequest(session);
96 request.setServletContext(servletContext);
97 response = new MockHttpServletResponse();
98
99 request.setPathElements(getContextPath(), getServletPath(), pathInfo, query);
100
101 facesContext = facesContextFactory.getFacesContext(servletContext, request, response, lifecycle);
102 externalContext = facesContext.getExternalContext();
103 application = facesContext.getApplication();
104 if (client != null)
105 {
106 client.apply(request);
107 }
108
109 client = createClient();
110 }
111
112 protected MockMyFacesClient createClient()
113 {
114 return new MockMyFacesClient(facesContext, this);
115 }
116
117 protected void tearDownRequest()
118 {
119 if (facesContext != null)
120 {
121 facesContext.release();
122 }
123 facesContext = null;
124 externalContext = null;
125 application = null;
126
127 response = null;
128 request = null;
129
130 }
131
132 protected String getContextPath()
133 {
134 return "/test";
135 }
136
137 protected String getServletPath()
138 {
139 return "/faces";
140 }
141
142 protected void processLifecycleExecute() throws Exception
143 {
144 processLifecycleExecute(facesContext);
145 }
146
147 protected void processLifecycleExecuteAndRender() throws Exception
148 {
149 processLifecycleExecute();
150 processRender();
151 }
152
153 protected void processRestoreViewPhase() throws Exception
154 {
155 processRestoreViewPhase(facesContext);
156 }
157
158 protected void processApplyRequestValuesPhase() throws Exception
159 {
160 processApplyRequestValuesPhase(facesContext);
161 }
162
163 protected void processValidationsPhase() throws Exception
164 {
165 processValidationsPhase(facesContext);
166 }
167
168 protected void processUpdateModelPhase() throws Exception
169 {
170 processUpdateModelPhase(facesContext);
171
172 }
173
174 protected void processInvokeApplicationPhase() throws Exception
175 {
176 processInvokeApplicationPhase(facesContext);
177 }
178
179 protected void processRender() throws Exception
180 {
181 processRender(facesContext);
182 }
183
184 protected void processRemainingExecutePhases() throws Exception
185 {
186 processRemainingExecutePhases(facesContext);
187 }
188
189 protected void processRemainingPhases() throws Exception
190 {
191 processRemainingPhases(facesContext);
192 }
193
194 protected String getRenderedContent() throws IOException
195 {
196 return getRenderedContent(facesContext);
197 }
198
199 protected void inputText(UIComponent input, String text)
200 {
201 client.inputText((UIInput)input, text);
202 }
203
204
205
206
207
208
209
210
211
212 protected void submit(UIComponent component) throws Exception
213 {
214 client.submit(component);
215
216
217
218
219
220
221
222 }
223
224 protected MockMyFacesClient client = null;
225
226
227 protected MockHttpServletRequest request = null;
228 protected MockHttpServletResponse response = null;
229 protected MockHttpSession session = null;
230
231 protected Application application = null;
232 protected ExternalContext externalContext = null;
233 protected FacesContext facesContext = null;
234
235 }