StrutsTestCase Simplifies the Development Process
By Sunil Patil
2005-04-28
Sample Application
I'll start by walking you through the creation of a sample Struts application, which will be the basis for our testing. You can use struts-blank.war, which is shipped with Struts, or your favorite IDE to create it. The sample application will have a login page on which a user will enter his user name and password. If the login is successful, the user will be redirected to a success page. If the login fails, he will be redirected to the login page.
The source code that accompanies this article is available by selecting the
Code icon at the top or bottom of this article.
The Login.jsp page
Create the login page, as shown in Listing 1:
Listing 1. Login.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:html>
<HEAD>
<%@ page language="java"contentType="text/html;
charset=ISO-8859-1"pageEncoding="ISO-8859-1" %>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<TITLE>Login.jsp</TITLE>
</HEAD>
<BODY>
<html:form action="/login">
<html:errors/>
<H3>Login</H3>
<TABLE border="0">
<TBODY>
<TR>
<TH>User Name</TH>
<TD><html:text property='userName' value='' /></TD>
<TR>
<TR>
<TH>Password</TH>
<TD><html:text property='password' value='' /></TD>
</TR>
<TR>
<TD><html:submit property="submit" value="Submit" /></TD>
<TD><html:reset /></TD>
</TR>
</TBODY>
</TABLE>
</html:form>
</BODY>
</html:html>
The LoginActionForm.java class
Create the LoginActionForm.java class, as shown in Listing 2:
Listing 2. LoginActionForm.java
public class LoginActionForm extends ActionForm {
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (userName == null || userName.length() == 0)
errors.add("userName", new ActionError("username.required"));
if (password == null || password.length() == 0)
errors.add("password", new ActionError("password.required"));
if( isUserDisabled(userName))
errors.add("userName",new ActionError("user.disabled"));
return errors;
}
//Query USERDISABLED table to check if user account is disabled
public boolean isUserDisabled(String userName) {
//SQL logic to check if user account is disabled
}
}In the validate() method, you want to check that the user enters a user name and password because those fields are mandatory. Also, you want to query the USERDISABLED table to verify that the user account is not disabled.
The LoginAction.java class
Next, create the LoginAction.java class, as shown in Listing 3:
Listing 3. The LoginAction.java class
public class LoginAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (isValidUser(loginForm.getUserName(), loginForm.getPassword())) {
request.getSession().setAttribute(
"userName",
loginForm.getUserName());
return mapping.findForward("success");
} else {
ActionErrors errors = new ActionErrors();
errors.add("userName", new ActionError("invalid.login"));
saveErrors(request, errors);
return new ActionForward(mapping.getInput());
}
}
//Query User Table to find out if userName and password combination is right.
public boolean isValidUser(String userName, String password) {
//SQL Logic to check if username password combination is right
}
}Here, the execute() method verifies that the user name and password are valid. The sample application uses the USER table to store the user name and password. If the user's credentials are valid, the user name is saved in the request scope, and he is forwarded the success page (Success.jsp).
The struts-config.xml file
Create the struts-config.xml file, as shown in Listing 4:
Listing 4. The struts-config.xml file
<action-mappings>
<action path="/login" type="com.sample.login.LoginAction"
name="loginForm" scope="request" input="Login.jsp">
<forward name="success" path="/Success.jsp"/>
</action>
</action-mappings>
If the login is unsuccessful, we redirect the user to the login page.
The Success.jsp page
Create the Success.jsp page, as shown in Listing 5:
Listing 5. The Success.jsp page
<HTML>
<HEAD>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page language="java" contentType="text/html; %>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<TITLE>Success.jsp</TITLE>
</HEAD>
<BODY>
<%
String userName = (String)session.getAttribute("userName");
%>
Login Successful<br/>
<P>Welcome: <%=userName%> .</P>
</BODY>
<HTML>
Here, the userName attribute is read from the request scope and is used to greet users who have logged in.
Tutorial Pages:
»
Save Time by Using STC's Mock and Cactus Testing Approaches
» Sample Application
»
Using the Mock Object Approach
»
Advantages and Disadvantages of Mocking
»
Cactus Approach
»
Cactus Pros and Cons
»
Conclusion
»
Resources
First published by IBM DeveloperWorks
|
