1 64 65 package com.jcorporate.expresso.services.test; 66 67 import com.jcorporate.expresso.core.ExpressoConstants; 68 import com.jcorporate.expresso.core.controller.Controller; 69 import com.jcorporate.expresso.core.controller.ControllerException; 70 import com.jcorporate.expresso.core.controller.ControllerResponse; 71 import com.jcorporate.expresso.core.db.DBConnectionPool; 72 import com.jcorporate.expresso.core.misc.ConfigManager; 73 import com.jcorporate.expresso.core.misc.CookieUtil; 74 import com.jcorporate.expresso.core.misc.StringDOMParser; 75 import org.apache.cactus.ServletTestCase; 76 import org.apache.cactus.WebRequest; 77 import org.apache.cactus.WebResponse; 78 import org.apache.log4j.Category; 79 import org.apache.log4j.Logger; 80 import org.apache.struts.action.ActionForm; 81 import org.apache.struts.action.ActionForward; 82 import org.apache.struts.action.ActionMapping; 83 import org.apache.struts.action.ActionServlet; 84 import org.apache.struts.config.ActionConfig; 85 import org.apache.struts.util.RequestUtils; 86 import org.apache.xpath.XPathAPI; 87 import org.w3c.dom.Document ; 88 import org.w3c.dom.NodeList ; 89 90 import javax.servlet.ServletException ; 91 import javax.servlet.http.HttpSession ; 92 import java.io.IOException ; 93 import java.net.HttpURLConnection ; 94 95 96 103 public class ControllerTestCase 104 extends ServletTestCase { 105 private String theClass = null; 106 private Logger log = Logger.getLogger(ControllerTestCase.class); 107 private String theState = null; 108 boolean isInitialized = false; 109 110 111 119 public ControllerTestCase(String testName, String controllerName) 120 throws Exception { 121 super(testName); 122 TestSystemInitializer.setUp(); 123 theClass = controllerName; 124 125 Class.forName(controllerName); 129 } 130 131 137 public ControllerTestCase(String testName, Class controllerClass) 138 throws Exception { 139 super(testName); 140 TestSystemInitializer.setUp(); 141 theClass = controllerClass.getName(); 142 } 143 144 150 protected Category getLog() { 151 return log; 152 } 153 154 159 public void logIn(WebRequest theRequest) 160 throws Exception { 161 this.logIn(theRequest, "Admin", ""); 162 } 163 164 168 public void logIn(WebRequest theRequest, String userName, String password) 169 throws Exception { 170 String encryptedPassword; 171 172 if (password == null || password.length() == 0) { 173 encryptedPassword = "NONE"; 174 } else { 175 encryptedPassword = CookieUtil.cookieEncode(password); 176 } 177 178 String encryptedUserName = CookieUtil.cookieEncode(userName); 179 theRequest.addCookie("UserName", encryptedUserName); 180 theRequest.addCookie("Password", encryptedPassword); 181 theRequest.addCookie("db", CookieUtil.cookieEncode(TestSystemInitializer.getTestContext())); 182 } 183 184 189 public void setupParameters(String state, WebRequest request) { 190 theState = state; 191 request.addParameter("style", "xml"); 192 request.addParameter("xsl", "none"); 193 request.addParameter(Controller.STATE_PARAM_KEY, state); 194 } 195 196 201 public Document parseResponse(WebResponse response) 202 throws Exception { 203 try { 204 int responseCode = response.getConnection().getResponseCode(); 205 206 if (responseCode != HttpURLConnection.HTTP_OK) { 207 fail("Failed to get OK Response code. Got " + 208 Integer.toString(response.getConnection().getResponseCode()) + 209 " instead"); 210 } 211 212 String s = response.getText(); 213 assertTrue("Some Real Content Returned", 214 response.getConnection().getContentLength() > 4); 215 216 StringDOMParser sdom = new StringDOMParser(); 217 Document d = sdom.parseString(s); 218 assertTrue("Reponse Returned Valid XML Document", d != null); 219 220 return d; 221 } catch (java.io.IOException ioe) { 222 log.error("IOException parsing response", ioe); 223 fail("IOException Parsing Reponse"); 224 225 return null; 226 } 227 } 228 229 235 public ControllerResponse buildControllerResponse(Document d) { 236 ControllerResponse cr = null; 237 238 try { 239 cr = ControllerResponse.fromXML(d); 240 } catch (ControllerException ce) { 241 log.error("ControllerException parsing response", ce); 242 } 243 244 assertTrue("Built a valid controller response object", cr != null); 245 246 return cr; 247 } 248 249 255 public ControllerResponse buildControllerResponse(WebResponse theConnection) 256 throws Exception { 257 Document d = parseResponse(theConnection); 258 assertTrue("XML Document Returned", d != null); 259 260 NodeList nl = getNodes(d, "controller-response"); 261 assertTrue("controller-response node exists", nl.getLength() > 0); 262 263 return buildControllerResponse(d); 264 } 265 266 273 public ControllerResponse controllerProcess() 274 throws ServletException , IOException , 275 Exception { 276 try { 277 ActionServlet servlet = new ActionServlet(); 278 servlet.init(config); 279 280 ActionConfig actionConfig = ConfigManager.getActionConfig(theClass, theState); 281 ActionForm form = RequestUtils.createActionForm(request, (ActionMapping) actionConfig, 282 actionConfig.getModuleConfig(), servlet); 283 if (form != null) { 284 if ("request".equals(actionConfig.getScope())) { 285 request.setAttribute(actionConfig.getAttribute(), form); 286 } else { 287 HttpSession session = request.getSession(); 288 session.setAttribute(actionConfig.getAttribute(), form); 289 } 290 } 291 292 Controller controller = null; 293 294 try { 295 controller = ConfigManager.getControllerFactory().getController(theClass); 296 } catch (ControllerException ce) { 297 log.error("ControllerException " + theClass, ce); 298 throw new ServletException (ce); 299 } 300 try { 301 if (log.isDebugEnabled()) { 302 log.debug("forwarding to class: " + 303 controller.getClass().getName()); 304 } 305 306 ActionForward forward = controller.execute((ActionMapping) actionConfig, form, 307 request, response); 308 309 if (forward == null) { 312 return null; 313 } 314 315 assertTrue("ERROR: Redirected To Error Page. ", 316 !(forward.getName().equals("error"))); 317 318 ControllerResponse res = null; 319 320 try { 321 res = (ControllerResponse) request.getAttribute(ExpressoConstants.CONTROLLER_RESPONSE_KEY); 322 } catch (ClassCastException cce) { 323 fail("Request Attribute Controller.RESPONSE_KEY was not a ControllerResponse Class"); 324 } 325 if (res == null) { 326 fail("Error: Controller returned a null controller response"); 327 } 328 329 return res; 330 } catch (NullPointerException npe) { 331 log.error("Null Pointer Exception", npe); 332 throw npe; 333 } 334 } finally { 335 336 DBConnectionPool dbcp = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext()); 339 dbcp.disconnectAll(); 340 } 341 } 342 343 349 public NodeList getNodes(Document d, String xpath) { 350 NodeList nl = null; 351 352 try { 353 nl = XPathAPI.selectNodeList(d, xpath); 354 } catch (javax.xml.transform.TransformerException trex) { 355 log.error("Error performing transformation", trex); 356 } 357 358 return nl; 359 } 360 } 361 362 | Popular Tags |