Testing, Debugging, Logging, Performance Tuning Struts Applications
Testing, Debugging, Logging, Performance Tuning Struts Applications
Testing, Debugging,
Logging,
Performance tuning
Struts Applications
1
07/21/2005
Sang Shin
[email protected]
www.javapassion.com
Java™ Technology Evangelist
Sun Microsystems, Inc.
2
2
07/21/2005
3
07/21/2005
Revision History
? 12/01/2003: version 1: created by Sang Shin
? Things to do
– Contents on performance tuning, debugging still
need to be added
4
07/21/2005
Topics
? StrutsTestCase Unit testing
? Logging
This is the topics we will discuss in this session. First, we will talk about
logging, then unit testing and debugging, and performance tuning and best
practice guidelines.
5
07/21/2005
Unit Testing
6
07/21/2005
7
07/21/2005
StrutsTestCase
? Extends JUnit framework to allow testing the
Action class
? Because StrutsTestCase uses the ActionServlet
controller to test your code, you can test not only
the implementation of your Action objects, but
also your mappings, form beans, and forwards
declarations
? Offers two approaches for testing
– Mock Object approach
– In-container testing approach
StrutsTestCase for JUnit is an extension of the standard JUnit TestCase class that
provides facilities for testing code based on the Struts framework. StrutsTestCase
provides both a Mock Object approach and a Cactus approach to actually run the
Struts ActionServlet, allowing you to test your Struts code with or without a
running servlet engine. Because StrutsTestCase uses the ActionServlet controller
to test your code, you can test not only the implementation of your Action objects,
but also your mappings, form beans, and forwards declarations. And because
StrutsTestCase already provides validation methods, it's quick and easy to write
unit test cases.
8
07/21/2005
There are two popular approaches to testing server-side classes: mock objects,
which test classes by simulating the server container, and in-container testing,
which tests classes running in the actual server container. StrutsTestCase for JUnit
allows you to use either approach, with very minimal impact on your actual unit
test code. In fact, because the StrutsTestCase setup and validation methods are
exactly the same for both approaches, choosing one approach over the other
simply effects which base class you use!
9
07/21/2005
Base Classes
? Mock objects
– MockStrutsTestCase uses a set of HttpServlet
mock objects to simulate the container
environment without requiring a running servlet
engine
? In-container
– CactusStrutsTestCase uses the Cactus testing
framework to test Struts classes in the actual
server container
10
StrutsTestCase for JUnit provides two base classes, both of which are extensions
of the standard JUnit TestCase. MockStrutsTestCase uses a set of HttpServlet
mock objects to simulate the container environment without requiring a running
servlet engine. CactusStrutsTestCase uses the Cactus testing framework to test
Struts classes in the actual server container, allowing for a testing environment
more in line with the actual deployment environment.
Please note that while the following examples use the MockStrutsTestCase
approach, you could choose to use the Cactus approach by simply subclassing
from CactusStrutsTestCase without changing another line of code!
How does it work?
10
07/21/2005
Example
public class LoginAction extends Action {
if ((!username.equals("deryl")) || (!password.equals("radar")))
errors.add("password",new ActionError("error.password.mismatch"));
if (!errors.empty()) {
saveErrors(request,errors);
return mapping.findForward("login");
}
11
So, what are we doing here? Well, we receive an ActionForm bean which should
contain login information. First, we try to get the username and password
information, and then check to see if it is valid. If there is a mismatch in the
username or password values, we then create an ActionError message with a key
to a message catalogue somewhere, and then try to forward to the login screen so
we can log in again. If the username and password match, however, we store some
authentication information in the session, and we try to forward to the next page.
11
07/21/2005
Example
// store authentication info on the session
HttpSession session = request.getSession();
session.setAttribute("authentication", username);
12
12
07/21/2005
13
StrutsTestCase gives you the ability to test all of these conditions within the
familiar JUnit framework. All of the Struts setup -- which really amounts to
starting up the ActionServlet -- is taken care of for you.
13
07/21/2005
14
So, how do we actually do it? Let's start by creating an empty test case, which we
extend from the base StrutsTestCase class.
If you choose to override the setUp() method, you must explicitly call
super.setUp(). This method performs some important initialization routines, and
StrutsTestCase will not work if it is not called.
14
07/21/2005
15
The first thing we need to do is to tell Struts which mapping to use in this test. To
do so, we specify a path that is associated with a Struts mapping; this is the same
mechanism that the Struts tag library method uses.
15
07/21/2005
setRequestPathInfo("/login");
addRequestParameter("username","deryl");
addRequestParameter("password","radar");
16
Finally, we need to get the Action to do its thing, which just involves executing
the actionPerform method:
16
07/21/2005
setRequestPathInfo("/login");
addRequestParameter("username","deryl");
addRequestParameter("password","radar");
actionPerform();
17
That's all you have to do to get the ActionServlet to process your request, and if
all goes well, then nothing will happen. But we're not done yet -- we still need to
verify that everything happened as we expected it to. First, we want to make sure
we got to the right page:
It's worth noting here that when you verify which page you ended up at, you can
use the Struts forward mapping. You don't have to hard code filenames -- the
StrutsTestCase framework takes care of this for you. Thus, if you were to change
where "success" pointed to, your tests would still work correctly. All in the spirit
of Struts.
17
07/21/2005
setRequestPathInfo("/login");
addRequestParameter("username","deryl");
addRequestParameter("password","radar");
actionPerform();
verifyForward("success");
}
}
18
Next, we want to make sure that authentication information was stored properly:
Here we're getting the session object from the request, and checking to see if it has
the proper attribute and value. You could just as easily place an object on the
session that your Action object expects to find. All of the servlet classes available
in the StrutsTestCase base classes are fully functioning objects.
18
07/21/2005
setRequestPathInfo("/login");
addRequestParameter("username","deryl");
addRequestParameter("password","radar");
actionPerform();
verifyForward("success");
assertEquals("deryl",(String) getSession().getAttribute("authentication"));
}
}
19
Finally, we want to make sure that no ActionError messages were sent along. We
can use a built in method to make sure of this condition:
19
07/21/2005
addRequestParameter("username","deryl");
addRequestParameter("password","express");
setRequestPathInfo("/login");
actionPerform();
}
}
20
So, now that we've written one test case, it's easy to write another. For example,
we'd probably want to test the case where a user supplies incorrect login
information. We'd write such a test case like the following:
Now, this looks quite similar to our first test case, except that we're passing
incorrect information. Also, we're checking to make sure we used a different
forward, namely one that takes us back to the login page, and that the
authentication information is not on the session.
We're also verifying that the correct error messages were sent. Note that we used
the symbolic name, not the actual text. Because the verifyActionErrors() method
takes a String array, we can verify more than one error message, and
StrutsTestCase will make sure there is an exact match. If the test produced more
error messages than we were expecting, it will fail; if it produced fewer, it will
also fail. Only an exact match in name and number will pass.
It's that easy! As you can see, StrutsTestCase not only tests the implementation of
your Action objects, but also the mappings that execute them, the ActionForm
beans that are passed as arguments, and the error messages and forward
statements that result from execution. It's the whole enchilada!
20
07/21/2005
StrutsTestCase
Testing Tiles
21
21
07/21/2005
verifyTilesForward("success","success.tiles.def");
}
}
22
The Tiles framework, which is integrated into Struts 1.2, is a flexible templating
mechanism designed to easily re-use common user experience elements.
StrutsTestCase now provides support for testing applications that use Tiles,
allowing any test case to verify that an Action object uses the correct Tiles
definition. Tiles testing is similar to calling verifyActionForward, with a twist:
This is similar to our previous test cases, except that we're additionally passing in
a definition name to verify that this Action uses a given Tiles definition when
resolving the expected forward. If it uses a different Tiles definition, or if the
expected definition does not exist, then this test will fail.
22
07/21/2005
23
23
07/21/2005
Logging
24
24
07/21/2005
25
In Struts 1.0, the logging functionality was fairly limited. You could set a
debugging detail level with a servlet initialization parameter, and all log
messages were written to wherever ServletContext.log() output is sent by your
servlet container. WIth Struts 1.1, however, all logging messages written by
Struts itself, as well as the commons librarires that it utilizes, flow through an
abstract wrapper called Commons Logging, which can be used as a wrapper
around any logging implementation. The most common implementations used
are simple logging to System.err, the Apache Log4J package, or the built-in
logging capabilities of JDK 1.4 or later in the java.util.logging package.
25
07/21/2005
26
In Struts 1.0, the logging functionality was fairly limited. You could set a
debugging detail level with a servlet initialization parameter, and all log
messages were written to wherever ServletContext.log() output is sent by your
servlet container. WIth Struts 1.1, however, all logging messages written by
Struts itself, as well as the commons librarires that it utilizes, flow through an
abstract wrapper called Commons Logging, which can be used as a wrapper
around any logging implementation. The most common implementations used
are simple logging to System.err, the Apache Log4J package, or the built-in
logging capabilities of JDK 1.4 or later in the java.util.logging package.
26
07/21/2005
In Struts 1.0, the logging functionality was fairly limited. You could set a
debugging detail level with a servlet initialization parameter, and all log
messages were written to wherever ServletContext.log() output is sent by your
servlet container. WIth Struts 1.1, however, all logging messages written by
Struts itself, as well as the commons librarires that it utilizes, flow through an
abstract wrapper called Commons Logging, which can be used as a wrapper
around any logging implementation. The most common implementations used
are simple logging to System.err, the Apache Log4J package, or the built-in
logging capabilities of JDK 1.4 or later in the java.util.logging package.
27
07/21/2005
In Struts 1.0, the logging functionality was fairly limited. You could set a
debugging detail level with a servlet initialization parameter, and all log
messages were written to wherever ServletContext.log() output is sent by your
servlet container. WIth Struts 1.1, however, all logging messages written by
Struts itself, as well as the commons librarires that it utilizes, flow through an
abstract wrapper called Commons Logging, which can be used as a wrapper
around any logging implementation. The most common implementations used
are simple logging to System.err, the Apache Log4J package, or the built-in
logging capabilities of JDK 1.4 or later in the java.util.logging package.
28
07/21/2005
Using commons-logging in your own code is very simple - all you need are two
imports and a declaration for a logger. Let's take a look:
The general idea is to instantiate a single logger per class and to use a name for
the logger which reflects where it's being used. The example is constructed with
the class itself. This gives the logger the name of
“org.apache.struts.webapp.Example”. Doing things this way lets you easily see
where the output is coming from, so you can quickly pin-point problem areas. In
addition, you are able to enable/disable logging in a very fine-grained way.
29
07/21/2005
Using commons-logging in your own code is very simple - all you need are two
imports and a declaration for a logger. Let's take a look:
The general idea is to instantiate a single logger per class and to use a name for
the logger which reflects where it's being used. The example is constructed with
the class itself. This gives the logger the name of
“org.apache.struts.webapp.Example”. Doing things this way lets you easily see
where the output is coming from, so you can quickly pin-point problem areas. In
addition, you are able to enable/disable logging in a very fine-grained way.
30
07/21/2005
31
This is another example of how logging API is used. This is the init() method of
MemoryDatabasePlugin in struts-example1 sample code. Here we have a single
INFO line that will be logged.
31
07/21/2005
32
Because the init() method of the plugin should be called when the application
gets installed and loaded, I did “ant install”.
32
07/21/2005
<jwsdp-install>/logs/launcher.server.log
33
This is theout from launcher.server.log file. As you see the line that is pointed
by the arrow, the information that we saw in the previous slide is wrtten to a log
file here.
33
07/21/2005
Passion!
34
34