Java Reference
In-Depth Information
14.3.1
Presenting the Administration application
The goal of this sample Administration application is to let administrators perform
database queries on a relational database. Suppose the application it administers
already exists. Administrators can perform queries such as listing all the transactions
that took place during a given time interval, listing the transactions that were out of
service level agreement (
SLA
), and so forth. We set up a typical web application archi-
tecture (see figure 14.2) to demonstrate how to unit test each type of component (fil-
ter, servlet,
JSP
, and
EJB
).
The application first receives from the user an
HTTP
request containing the
SQL
query to execute. The request is caught by a security filter that checks whether the
SQL
query is a
SELECT
query (to prevent modifying the database). If not, the user
is redirected to an error page. If the query is a
SELECT
, the
AdminServlet
servlet is
called. The servlet performs the requested database query and forwards the results to
a
JSP
page, which displays the results. The page uses
JSP
tags to iterate over the
returned results and to display them in
HTML
tables.
JSP
tags are used for all the
presentation logic code. The
JSP
s contain only layout/style tags (no Java code in
scriptlets). We start by unit testing the
AdminServlet
servlet. Then, in the following
subsections, we test the other components of the Administration application.
14.3.2
Writing servlet tests with Cactus
In this section, we focus on using Cactus to unit test the
AdminServlet
servlet (see fig-
ure 14.2) from the Administration application.
Let's test
AdminServlet
by writing the tests before we write the servlet code. This
strategy is called test-driven development, or test first, and it's efficient for designing
extensible and flexible code and making sure the unit test suite is as complete as pos-
sible. (See chapter 4 for an introduction to
TDD
.)
Figure 14.2 The sample Administration application. We use it as a base sample in this
chapter to see how to unit test servlets, filters, JSPs, taglibs, and database applications.