KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > ScriptedTestSession


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

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