01: package servletunit;
02:
03: // StrutsTestCase - a JUnit extension for testing Struts actions
04: // within the context of the ActionServlet.
05: // Copyright (C) 2002 Deryl Seale
06: //
07: // This library is free software; you can redistribute it and/or
08: // modify it under the terms of the Apache Software License as
09: // published by the Apache Software Foundation; either version 1.1
10: // of the License, or (at your option) any later version.
11: //
12: // This library is distributed in the hope that it will be useful,
13: // but WITHOUT ANY WARRANTY; without even the implied warranty of
14: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: // Apache Software Foundation Licens for more details.
16: //
17: // You may view the full text here: http://www.apache.org/LICENSE.txt
18:
19: import javax.servlet.*;
20: import java.io.IOException;
21:
22: /**
23: * Simulates a <code>javax.servlet.RequestDispatcher</code> object.
24: */
25: public class RequestDispatcherSimulator implements RequestDispatcher {
26: private Object dispatchedResource;
27:
28: /**
29: *@param dispatchedResource The <code>dispatchedResource</code> object represents the resource that
30: * <code>this</code> <code>javax.servlet.RequestDispatcher</code> is tied to.
31: * Currently this class only supports <code>javax.servlet.Servlet</code> objects
32: * and <code>java.lang.String</code> objects. If the parameter passed in is not
33: * a <code>javax.servlet.Servlet</code> object when forward or include is called
34: * the parameter's toString method is called and sent to <code>System.out</code>.
35: * Otherwise, the appropriate service method is called.
36: */
37: public RequestDispatcherSimulator(Object dispatchedResource) {
38: this .dispatchedResource = dispatchedResource;
39: }
40:
41: /**
42: * Simulates the forward method of the <code>javax.servlet.RequestDispatcher</code> interface
43: */
44: public void forward(ServletRequest request, ServletResponse response)
45: throws ServletException, IOException {
46: if (dispatchedResource instanceof Servlet)
47: ((Servlet) dispatchedResource).service(request, response);
48: }
49:
50: public void include(ServletRequest request, ServletResponse response)
51: throws ServletException, IOException {
52: System.out.println(dispatchedResource.toString());
53: }
54:
55: public String getForward() {
56: if (dispatchedResource instanceof String)
57: return (String) dispatchedResource;
58: else
59: return dispatchedResource.getClass().toString();
60: }
61: }
|