1   package servletunit;
2   
3   //  StrutsTestCase - a JUnit extension for testing Struts actions
4   //  within the context of the ActionServlet.
5   //  Copyright (C) 2002 Deryl Seale
6   //
7   //  This library is free software; you can redistribute it and/or
8   //  modify it under the terms of the Apache Software License as
9   //  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  {
27      private Object dispatchedResource;
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      {
39          this.dispatchedResource = dispatchedResource;
40      }
41      /**
42       * Simulates the forward method of the <code>javax.servlet.RequestDispatcher</code> interface
43       */
44      public void forward( ServletRequest request, ServletResponse response ) throws ServletException, IOException
45      {
46          if( dispatchedResource instanceof Servlet )
47              ((Servlet)dispatchedResource).service( request, response );
48      }
49      public void include( ServletRequest request, ServletResponse response ) throws ServletException, IOException
50      {
51          System.out.println( dispatchedResource.toString() );
52      }
53  
54      public String getForward() {
55          if (dispatchedResource instanceof String)
56              return (String) dispatchedResource;
57          else
58              return dispatchedResource.getClass().toString();
59      }
60  }