Cactus Architecture

Last update : September 25 2001

About
  • What is Cactus ?
  • Features/Status
  • Goals
  • News/Changes
  • Roadmap/Todo
  • Contributors
  • Contributing
  • License
  • Feedback


  • Downloads
  • Downloads


  • Documentation
  • How it works ?
  • Getting Started
  • Mock vs Container
  • API Reference


  • Howto Guides
  • Config Howto
  • TestCase Howto
  • Ant Howto
  • HttpUnit Howto
  • Sample Howto
  • EJB Howto
  • IDE Howto


  • Support
  • Bug database
  • Mailing list
  • FAQ


  • Misc.
  • Why the name ?
  • Resources


  • Developers
  • CVS
  • Coding Conventions


  • How it works

    Cactus Architecture

    Note Cactus provides several TestCase classes that extends the JUnit Testcase and it also provides several kind of redirectors (Servlet Redirector, JSP Redirector, ...). The diagram above is a generic diagram which serves to explain the principles. You'll find details for a specific redirector proxy in the next section.

    Let's go through the different steps to understand how it works. Here is what happens for each testXXX() method in your test case class (derived from YYYTestCase) :

    1. The JUnit Test Runner calls a YYYTestCase.runTest() method. This method looks for a beginXXX(ServletTestRequest) method and executes it if one is found. This is executed on the client side (i.e. not in a server engine). The ServletTestRequest parameter passed to the beginXXX() method is used to set the HTTP headers, the HTTP parameters, ... that will be sent in step 2 to the Redirector proxy.
    2. The YYYTestCase.runTest() method then opens an HTTP connection to the Redirector proxy. All the parameters set up in the beginXXX() method are sent in the HTTP request (HTTP headers, HTTP parameters, ...)
    3. The Redirector proxy acts as a proxy on the server side for your test case class. It means that your test case class is instantiated twice : once on the client side (by the JUnit Test Runner) and once on the server side (by the Redirector proxy). The client side instance is used for executing the beginXXX() and endXXX() methods (see steps 1 and 8) and the server side instance is used for executing the testXXX() methods (see step 4). The Redirector proxy does the following :
      • creates an instance of your test class using reflection. It then sets specific implicit objects (which are class variables of YYYTestCase). These implicit objects depend on the redirector proxy (see next section).
      • creates instances of Cactus wrappers for some server objects (HttpServletRequest, ServletConfig, ServletContext, ...). This is to be able to to override some methods in order to return simulated values. For example, the Cactus framework can simulate an URI (i.e. act as if this URI was called instead of the Redirector proxy URI). Thus, the getServerName(), getServerPort(), getRequestURI(), ... methods return values based on the simulated URI (if there is any defined by the user).
      • creates an HTTP Session if the user has expressed the wish (using the ServletTestRequest.setAutomaticSession(boolean) code in the beginXXX() method. By default a session is always created) and the redirector fills by reflection the session implicit object.
    4. The setUp(), testXXX() and tearDown() methods of your test class are executed (in that order). They are called by the Redirector proxy using reflection. Of course, setUp() and tearDown() are optional (same as in JUnit).
    5. Your testXXX() method calls your server side code to test, executing the test and using the JUnit assert API to assert the result (assert(), assertEquals(), fail(), ...)
    6. If the test fails, your testXXX() methods throws exceptions which are caught by the Redirector proxy.
    7. If an exception has been raised, the Redirector proxy returns the information about the exception (it's name, class, stack trace) back to the client side. Information about the exception will then be printed by JUnit in it's Test Runner console.
    8. If no exception occurred, the YYYTestCase.runTest() method looks for an endXXX(HttpURLConnection) method and executes it if found. At this stage, you have the opportunity to check returned HTTP headers, Cookies and the servlet output stream in the endXXX() method, again using JUnit asserts and helper utility classes provided by Cactus.
    Redirector Proxies

    Cactus provides 2 implementation for the Redirector Proxy :

    • A Servlet Redirector. This redirector is a servlet that should be used for unit testing servlet methods or any java class that uses servlet objects. It provides the following implicit objects : request, response, session and config.
    • A JSP Redirector. This redirector is a JSP page that should be used for unit testing server code that need access to the following objects : pageContext and out. These objects are provided in addition to all the objects provided by the Servlet Redirector. The JSP Redirector should be used to unit test custom Tag libraries.

    Note Testing custom JSP Tag libraries is not finalized yet and only tag libraries that do not use the bodyContent can currently be tested.

    Servlet Redirector Proxy

    Servlet Redirector Architecture

    The client side opens 2 HTTP connections to the Servlet redirector. Once to execute the tests and retrieve the servlet output stream and a second time to get the test result. This is to be able to get the exception data (message, stack trace, ...) if the test failed. The test results are stored in a variable which is put in the ServletContext and then retrieved on the second HTTP connection.


    JSP Redirector Proxy

    JSP Redirector Architecture

    The client side opens 2 HTTP connections. Once to the Redirector JSP to execute the tests and retrieve the JSP output stream and a second time to the Servlet Redirector to get the test result. This is to be able to get the exception data (message, stack trace, ...) if the test failed. The test results are stored in a variable which is put in the ServletContext and then retrieved on the second HTTP connection.






    Copyright © 2000-2001 The Apache Software Foundation. All Rights Reserved.