Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] - نسخه متنی

Larne Pekowsky

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







6.1. An Introduction to Cactus


When testing a Web application with HTTPUnit the testing code sits entirely outside the server being tested. The only interaction between the code doing the testing and the code being tested is the HTTP request and response, as shown in Figure 6.1.


Figure 6.1. Simple HTTP testing.

By contrast Cactus provides an "in-container strategy," meaning a portion of the code doing the testing resides within the application. This portion is a servlet called the

redirector, and it acts as the central point of all Cactus activity.

The fact that the redirector is a servlet has two immediate consequences. First, there is a simple and obvious way in which the redirector is placed within the application server; it is installed like any other servlet. This can be accomplished with the following entries in web.xml, the application's configuration file:Chapter 17.


   
<servlet>
<servlet-name>
ServletRedirector
</servlet-name>
<servlet-class>
org.apache.cactus.server.ServletTestRedirector
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletRedirector</servlet-name>
<url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>

Note that the servlet mapping must place the redirector at the top level of the application, or it would not be possible to map the servlet to "/chapter06/ServletRedirector." The servlet to be tested, however, may reside anywhere.

The second implication of having the redirector be a servlet is that it provides a natural way for an external program to invoke a test. The external program can make a regular HTTP request to the appropriate URL/ServletRedirector, in this caseand on receiving such a request the redirector will run the tests. The broad outline of this process is illustrated in Figure 6.2, which will be refined shortly. Figure 6.2 gives a sense of how Cactus works, but the picture is not yet complete. So far nothing has been said about the client. Since JUnit provides such a useful and flexible framework, the Cactus client will be constructed on top of it. This means that Cactus test cases will look much like JUnit and HTTPUnit test cases; they will be individual Java classes with specially named methods that a test runner will dynamically discover and invoke.


Figure 6.2. Internal testing.

There are a number of ways in which the client could know which server and which URL to contact in order to invoke the redirector. Cactus looks for this information in a file called cactus.properties, which must be placed somewhere in the CLASSPATH on the client side. The cactus.properties file for this example contains the following:


cactus.contextURL = http://localhost:8080/toolbook
cactus.servletRedirectorName = ServletRedirector

Moving right in Figure 6.2, the next point of clarification is the communication channel between the client and the redirector. When HTTPUnit wants to test a page, it sends a request containing the URL, query string, POST data, and other elements intended for the page to be tested. When Cactus wants to test a page, the URL will be the URL for the redirector, not for the page or servlet to be tested. There must, therefore, be a mechanism for the client to tell the redirector how to run the test. This is done through an auxiliary class called a

WebRequest.

The full process by which a Cactus test runs is listed here, and it will be illustrated shortly in the context of an actual test case.


1.

The JUnit TestRunner will construct an instance of the test class, just as it would do for any other test case. This instance will be referred to as the

client instance.

2.

Cactus will look for a method in the test class whose name starts with the prefix test, just as with any JUnit-based test. For the purpose of what follows, assume Cactus finds a method called testTest1.

3.

Cactus will then look for a corresponding method whose name starts with beginin this case, that would be beginTest1. This method will be invoked with a WebRequest object. The WebRequest is purely a client-side entity. It contains information about the request that will be sent to the server, including the base URL, parameters, cookies, and so forth. It should not be confused with server-side classes such as HttpServletRequest. The beginTest1 method may set parameters in the WebRequest that will appear in the HttpRequest of the class being tested.

4.

Information in the cactus.properties file is used to construct a URL to the redirector on the server.

5.

An HTTP request is made to this URL, including all the parameters and other information loaded into the WebRequest by the beginTest1 method. In this way the parameters set in the WebRequest on the client side are transparently placed in the HttpServletRequest on the server side.

6.

The redirector constructs a

second instance of the test class, which will be referred to as the

server instance.

7.

The redirector will set various fields in the server instance that are defined in the test class. Most importantly, the redirector will set the request variable to be the current HttpServletRequest.

8.

Like any JUnit test, the setup() method of the server instance is invoked if it is defined.

9.

The redirector invokes the testTest1 method of the server instance. Note that if there are multiple test methods, only the one found in step (1) will be invoked at this time. Tests can use the request and other servlet-related objects that were set in step (6).

10.

The teardown() method is called on the server instance, if it exists.

11.

The redirector bundles up information about the response.

12.

The test result is sent back to the client along with the response information built in step (11). This information is used to construct a WebResponse object, and if the client instance has an endTest1 method corresponding to the testTest1 method, it will be called with this object. The endTest1 method can be used to check status codes, cookies, or anything else the server instance might have set in the response.


Items 2 through 12 are repeated once for each test, so if a test class defines testTest2 and testTest3 methods, then there will be three requests to the server, three server instances constructed, setup() will be called three times, and so on. This process is illustrated in Figure 6.3.


Figure 6.3. Testing with Cactus.


/ 207