1 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 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 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 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 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 [] names = rd.getParameterNames(); 99 100 for (int i = 0; i < names.length; i++) 101 { 102 String [] values = rd.getParameterValues(names[i]); 103 104 request.setParameter(names[i], values); 105 } 106 } 107 108 private Map _servlets = new HashMap (); 109 110 private HttpServlet getServlet(String name) 111 { 112 if (name == null) 113 name = _scriptDescriptor.getDefaultServletDescriptor().getName(); 114 115 HttpServlet result = (HttpServlet ) _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 readyServlet(String name) 127 { 128 ServletDescriptor sd = _scriptDescriptor.getServletDescriptor(name); 129 130 HttpServlet 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 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 parameters) 152 { 153 Iterator i = parameters.entrySet().iterator(); 154 while (i.hasNext()) 155 { 156 Map.Entry e = (Map.Entry ) i.next(); 157 158 String name = (String ) e.getKey(); 159 String value = (String ) e.getValue(); 160 161 holder.setInitParameter(name, value); 162 } 163 } 164 165 private HttpServlet instantiateServlet(ServletDescriptor sd) 166 { 167 String className = sd.getClassName(); 168 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 169 170 try 171 { 172 Class servletClass = Class.forName(className, true, loader); 173 174 return (HttpServlet ) servletClass.newInstance(); 175 } 176 catch (Exception ex) 177 { 178 throw new ApplicationRuntimeException( 179 "Unable to instantiate servlet '" + sd.getName() + "': " + ex.getMessage(), 180 sd.getLocation(), 181 ex); 182 } 183 } 184 185 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 |