jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Parmley, Scott C." <sparm...@p2es.com>
Subject RequestDispatcher question
Date Tue, 01 Jul 2003 17:20:46 GMT
Hello everyone,

I have a question reguarding testing XSP pages.  My test scenerio is this:

1. Submit some login data to an XSP page whose URI is "/myapp/login".
2. Test that a login was successful by looking for an attribute called "LoginConfig" in HttpSession.
3. Fail/Succeed test if session attribute is present.


The test I have written extends ServletTestCase is recommended by the Cactus documentation.
 The problem I am running into is it seems as though "request" is not populated with the request
data the redirectory proxy recieved via WebRequest.  The forward from RequestDispatcher doesn't
seem to work here.  The "request" object being passed into the dispatcher is empty from what
I can tell, and in addition, I am not convinced that RequestDispatcher is actually forwarding
to the given URI.  Am I missing something obvious here?  I have included a sample of code
below.

Thanks in advance,
-Scott


[...]
public class LoginTest extends ServletTestCase {
  private RequestDispatcher rdis;
  
  public LoginTest(String testName) {
    super(testName);
  }

  public static Test suite() {
    return new TestSuite(LoginTest.class);
  }

  public static void main(String[] args) {
    TestRunner.main(new String[]{LoginTest.class.getName()});
  }

  public void beginLogin(WebRequest theRequest) {	
    theRequest.addParameter("host","Demo");
    theRequest.addParameter("username","someuser");
    theRequest.addParameter("password","XXXX");
    System.out.println("Completed pre-login phase.");
  }

  public void testLogin() {
    try {
      if(config == null) {
        System.out.println("Servlet not initialized! config(null)");
      }
      if(config.getServletContext() == null) {
        System.out.println("Context is null!");
      }
      if(config.getServletContext().getRequestDispatcher("/myapp/login") == null) {
        System.out.println("RequestDispatcher is null or not valid!");
      }

      rdis = config.getServletContext().getRequestDispatcher("/myapp/login");
      System.out.println("RequestDispatcher setup complete.");

      System.out.println("Reading request.");
	  Enumeration rattributes = request.getAttributeNames();
	  while(rattributes.hasMoreElements()){
	  	System.out.println("Request Attribute: "+rattributes.nextElement());
	  }
	

	//request is empty!//
      rdis.forward(request, response);
      System.out.println("Forward finished.");

      System.out.println("Testing assertion.");
      System.out.println("SESSION ID: "+session.getId());
      
      assertNotNull(session.getAttribute("LoginConfig"));
    }
    catch(IOException e) {
      e.printStackTrace();
    }
    catch(ServletException e) {
      e.printStackTrace();
    }
  }
}

Mime
View raw message