1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.test; |
16 |
| |
17 |
| import java.util.HashMap; |
18 |
| import java.util.Iterator; |
19 |
| import java.util.Map; |
20 |
| |
21 |
| import javax.servlet.http.HttpServlet; |
22 |
| |
23 |
| import org.apache.hivemind.ApplicationRuntimeException; |
24 |
| import org.apache.tapestry.test.mock.InitParameterHolder; |
25 |
| import org.apache.tapestry.test.mock.MockContext; |
26 |
| import org.apache.tapestry.test.mock.MockRequest; |
27 |
| import org.apache.tapestry.test.mock.MockResponse; |
28 |
| import org.apache.tapestry.test.mock.MockServletConfig; |
29 |
| import org.apache.tapestry.util.RegexpMatcher; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class ScriptedTestSession |
39 |
| { |
40 |
| private ScriptDescriptor _scriptDescriptor; |
41 |
| private MockContext _context; |
42 |
| private MockRequest _request; |
43 |
| private MockResponse _response; |
44 |
| private RegexpMatcher _matcher = new RegexpMatcher(); |
45 |
| |
46 |
11
| public ScriptedTestSession(ScriptDescriptor descriptor)
|
47 |
| { |
48 |
11
| _scriptDescriptor = descriptor;
|
49 |
| } |
50 |
| |
51 |
0
| public void execute()
|
52 |
| { |
53 |
0
| setupForExecute();
|
54 |
| |
55 |
0
| Iterator i = _scriptDescriptor.getRequestDescriptors().iterator();
|
56 |
| |
57 |
0
| while (i.hasNext())
|
58 |
| { |
59 |
0
| RequestDescriptor rd = (RequestDescriptor) i.next();
|
60 |
| |
61 |
0
| executeRequest(rd);
|
62 |
| } |
63 |
| } |
64 |
| |
65 |
0
| private void setupForExecute()
|
66 |
| { |
67 |
0
| _context = new MockContext(_scriptDescriptor.getRootDirectory());
|
68 |
0
| _context.setServletContextName(_scriptDescriptor.getContextName());
|
69 |
| } |
70 |
| |
71 |
0
| private void executeRequest(RequestDescriptor rd)
|
72 |
| { |
73 |
0
| HttpServlet s = getServlet(rd.getServletName());
|
74 |
| |
75 |
0
| _request = new MockRequest(_context, rd.getServletPath());
|
76 |
| |
77 |
0
| loadParameters(_request, rd);
|
78 |
| |
79 |
0
| _response = new MockResponse(_request);
|
80 |
| |
81 |
0
| try
|
82 |
| { |
83 |
0
| s.service(_request, _response);
|
84 |
| } |
85 |
| catch (Exception ex) |
86 |
| { |
87 |
0
| throw new ApplicationRuntimeException(
|
88 |
| "Error executing request: " + ex.getMessage(), |
89 |
| rd.getLocation(), |
90 |
| ex); |
91 |
| } |
92 |
| |
93 |
0
| rd.executeAssertions(this);
|
94 |
| } |
95 |
| |
96 |
0
| private void loadParameters(MockRequest request, RequestDescriptor rd)
|
97 |
| { |
98 |
0
| String[] names = rd.getParameterNames();
|
99 |
| |
100 |
0
| for (int i = 0; i < names.length; i++)
|
101 |
| { |
102 |
0
| String[] values = rd.getParameterValues(names[i]);
|
103 |
| |
104 |
0
| request.setParameter(names[i], values);
|
105 |
| } |
106 |
| } |
107 |
| |
108 |
| private Map _servlets = new HashMap(); |
109 |
| |
110 |
0
| private HttpServlet getServlet(String name)
|
111 |
| { |
112 |
0
| if (name == null)
|
113 |
0
| name = _scriptDescriptor.getDefaultServletDescriptor().getName();
|
114 |
| |
115 |
0
| HttpServlet result = (HttpServlet) _servlets.get(name);
|
116 |
| |
117 |
0
| if (result == null)
|
118 |
| { |
119 |
0
| result = readyServlet(name);
|
120 |
0
| _servlets.put(name, result);
|
121 |
| } |
122 |
| |
123 |
0
| return result;
|
124 |
| } |
125 |
| |
126 |
0
| private HttpServlet readyServlet(String name)
|
127 |
| { |
128 |
0
| ServletDescriptor sd = _scriptDescriptor.getServletDescriptor(name);
|
129 |
| |
130 |
0
| HttpServlet result = instantiateServlet(sd);
|
131 |
| |
132 |
0
| MockServletConfig config = new MockServletConfig(name, _context);
|
133 |
| |
134 |
0
| loadInitParameters(config, sd.getInitParameters());
|
135 |
| |
136 |
0
| try
|
137 |
| { |
138 |
0
| result.init(config);
|
139 |
| } |
140 |
| catch (Exception ex) |
141 |
| { |
142 |
0
| throw new ApplicationRuntimeException(
|
143 |
| "Unable to initialize servlet '" + name + "': " + ex.getMessage(), |
144 |
| sd.getLocation(), |
145 |
| ex); |
146 |
| } |
147 |
| |
148 |
0
| return result;
|
149 |
| } |
150 |
| |
151 |
0
| private void loadInitParameters(InitParameterHolder holder, Map parameters)
|
152 |
| { |
153 |
0
| Iterator i = parameters.entrySet().iterator();
|
154 |
0
| while (i.hasNext())
|
155 |
| { |
156 |
0
| Map.Entry e = (Map.Entry) i.next();
|
157 |
| |
158 |
0
| String name = (String) e.getKey();
|
159 |
0
| String value = (String) e.getValue();
|
160 |
| |
161 |
0
| holder.setInitParameter(name, value);
|
162 |
| } |
163 |
| } |
164 |
| |
165 |
0
| private HttpServlet instantiateServlet(ServletDescriptor sd)
|
166 |
| { |
167 |
0
| String className = sd.getClassName();
|
168 |
0
| ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
169 |
| |
170 |
0
| try
|
171 |
| { |
172 |
0
| Class servletClass = Class.forName(className, true, loader);
|
173 |
| |
174 |
0
| return (HttpServlet) servletClass.newInstance();
|
175 |
| } |
176 |
| catch (Exception ex) |
177 |
| { |
178 |
0
| throw new ApplicationRuntimeException(
|
179 |
| "Unable to instantiate servlet '" + sd.getName() + "': " + ex.getMessage(), |
180 |
| sd.getLocation(), |
181 |
| ex); |
182 |
| } |
183 |
| } |
184 |
| |
185 |
| |
186 |
| |
187 |
11
| void setResponse(MockResponse response)
|
188 |
| { |
189 |
11
| _response = response;
|
190 |
| } |
191 |
| |
192 |
22
| public MockResponse getResponse()
|
193 |
| { |
194 |
22
| return _response;
|
195 |
| } |
196 |
| |
197 |
8
| public RegexpMatcher getMatcher()
|
198 |
| { |
199 |
8
| return _matcher;
|
200 |
| } |
201 |
| |
202 |
| } |