1 7 package com.inversoft.junit.internal; 8 9 10 import java.io.IOException ; 11 import java.lang.reflect.Constructor ; 12 import javax.servlet.ServletException ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import javax.servlet.jsp.PageContext ; 16 17 import org.apache.log4j.Logger; 18 19 import org.jdom.Document; 20 import org.jdom.output.XMLOutputter; 21 22 import com.inversoft.junit.JspTestCase; 23 import com.inversoft.junit.Request; 24 import com.inversoft.junit.Response; 25 import com.inversoft.junit.Result; 26 import com.inversoft.junit.URL; 27 import com.inversoft.junit.WebTestCase; 28 import com.inversoft.junit.internal.http.HttpServletRequestWrapper; 29 import com.inversoft.junit.internal.http.HttpServletResponseWrapper; 30 import com.inversoft.junit.internal.http.ServletContextWrapper; 31 32 33 40 public class RemoteMediator implements Mediator { 41 42 46 private static final Logger logger = Logger.getLogger(RemoteMediator.class); 47 48 49 55 public void mediate(WebTestCase testCase) throws Throwable { 56 57 Request request = new Request (); 60 ClientCaller client = new ClientCaller(); 61 62 client.callBeginMethod(testCase, request); 63 64 HttpCaller caller = new HttpCaller(); 65 Response response = caller.runTest(testCase, request); 67 client.callEndMethod(testCase, response); 68 } 69 70 73 public void mediateServer(HttpServletRequest request, HttpServletResponse response) 74 throws Throwable { 75 76 String type = request.getParameter(Constants.URL_REQUEST_TYPE_PARAM); 77 if (type == null) { 78 throw new ServletException ("FATAL-No type specified!"); 79 } 80 81 if (type.equals(Constants.REQUEST_TYPE_TEST)) { 83 WebTestCase testCase; 84 85 try { 86 testCase = getTestCaseInstance(request); 87 } catch (ClassNotFoundException cnfe) { 88 Result result = new Result(cnfe); 89 request.getSession().getServletContext().setAttribute(Constants.RESULT_PARAM, result); 90 91 throw new ServletException (cnfe); 92 } 93 94 setupTestCase(testCase, request, response); 97 98 LocationCaller location = new LocationCaller(); 99 Result result; 100 101 try { 102 location.callTestMethod(testCase); 103 result = new Result(); 104 } catch (Throwable t) { 105 result = new Result(t); 106 logger.debug("Test threw exception", t); 107 } 108 109 logger.debug("Ran the test [" + testCase.getName() + "] storing result"); 111 request.getSession().getServletContext().setAttribute(Constants.RESULT_PARAM, result); 112 113 } else if (type.equals(Constants.REQUEST_TYPE_RESULT)) { 114 115 Result result = 116 (Result) request.getSession().getServletContext().getAttribute(Constants.RESULT_PARAM); 117 118 if (result == null) { 119 throw new ServletException ("FATAL ERROR ON SERVER - Result object does not exist"); 120 } 121 122 outputResult(result, response); 123 124 } else { 125 ServletException se = 126 new ServletException ("FATAL ERROR ON SERVER - Invalid type: " + type); 127 Result result = new Result(se); 128 request.getSession().getServletContext().setAttribute(Constants.RESULT_PARAM, result); 129 throw se; 130 } 131 } 132 133 140 protected WebTestCase getTestCaseInstance(HttpServletRequest request) 141 throws ClassNotFoundException { 142 String className = request.getParameter(Constants.URL_CLASS_PARAM); 143 String methodName = request.getParameter(Constants.URL_METHOD_PARAM); 144 145 if (className == null) { 146 throw new ClassNotFoundException ("FATAL-No class name parameter in request"); 147 } 148 149 if (methodName == null) { 150 throw new ClassNotFoundException ("FATAL-No method name parameter in request"); 151 } 152 153 Class theClass = Class.forName(className); 154 155 try { 156 Constructor constructor = 157 theClass.getConstructor(WebTestCase.CONSTRUCTOR_PARAM_TYPES); 158 159 return (WebTestCase) constructor.newInstance(new Object [] {methodName}); 160 } catch (Exception e) { 161 throw new ClassNotFoundException ("Error instantiating the TestCase: " + e.toString()); 162 } 163 } 164 165 169 protected void setupTestCase(WebTestCase testCase, HttpServletRequest request, 170 HttpServletResponse response) { 171 URL url = null; 172 String protocol = request.getParameter(Constants.URL_PROTOCOL_PARAM); 173 String server = request.getParameter(Constants.URL_SERVERNAME_PARAM); 174 String servlet = request.getParameter(Constants.URL_SERVLETPATH_PARAM); 175 String context = request.getParameter(Constants.URL_CONTEXT_PARAM); 176 String path = request.getParameter(Constants.URL_PATHINFO_PARAM); 177 String query = request.getParameter(Constants.URL_QUERYSTRING_PARAM); 178 179 if (protocol != null || server != null || servlet != null || 180 context != null || path != null || query != null) { 181 url = new URL(request, context, path, protocol, query, server, servlet); 182 } 183 184 testCase.setOrigRequest(request); 185 testCase.setOrigResponse(response); 186 testCase.setRequest(new HttpServletRequestWrapper(request, url)); 187 testCase.setResponse(new HttpServletResponseWrapper(response)); 188 testCase.setContext(new ServletContextWrapper( 189 request.getSession().getServletContext())); 190 191 PageContext pc = (PageContext ) request.getAttribute(Constants.PAGE_CONTEXT_KEY); 192 if (context != null && testCase instanceof JspTestCase) { 193 ((JspTestCase) testCase).pageContext = pc; 194 } 195 } 196 197 204 protected void outputResult(Result result, HttpServletResponse response) 205 throws IOException { 206 Document xml = result.toXML(); 207 XMLOutputter xmlOut = new XMLOutputter(" ", true); 208 209 xmlOut.output(xml, response.getOutputStream()); 210 } 211 } | Popular Tags |