Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #4299
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!postnews.google.com!y27g2000prb.googlegroups.com!not-for-mail |
|---|---|
| From | jimgardener <jimgardener@gmail.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | cactus and servlet redirection |
| Date | Thu, 19 May 2011 07:52:19 -0700 (PDT) |
| Organization | http://groups.google.com |
| Lines | 148 |
| Message-ID | <33ded26e-3777-49ed-8eb8-1853927340de@y27g2000prb.googlegroups.com> (permalink) |
| NNTP-Posting-Host | 115.184.221.249 |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Trace | posting.google.com 1305816742 32106 127.0.0.1 (19 May 2011 14:52:22 GMT) |
| X-Complaints-To | groups-abuse@google.com |
| NNTP-Posting-Date | Thu, 19 May 2011 14:52:22 +0000 (UTC) |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | y27g2000prb.googlegroups.com; posting-host=115.184.221.249; posting-account=ClVFQQoAAAApZ31UMTiDeArQN-Srmoi9 |
| User-Agent | G2/1.0 |
| X-HTTP-UserAgent | Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13,gzip(gfe) |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4299 |
Show key headers only | View raw
hi,
I have a servlet for adding books to a booksdb and another for listing
books.I am trying to use cactus for testing.
In the beginPostMethod(..) of testcase ,I created parameters needed
for creating a Book and a Publisher,and added them to WebRequest
instance.
testPostMethod() of testcase merely creates a BookAddServlet and calls
doPost()on it.
In doPost(..) of BookAddServlet,after a Book and a Publisher are
created ,the response.sendRedirect() is called to invoke
BookListServlet's doGet() method.
BookListServlet's doGet() method gets a list of books from booksdb and
adds them to request.Then using RequestDispatcher
forwards to booklist.jsp.
It is the endPostMethod(..) where I am having a problem.
I want to test if after creating a book ,the booklist jsp page is
properly displayed.
I am using endPostMethod(com.meterware.httpunit.WebResponse response)
to make use of httpunit's WebResponse class.
Here ,I am getting response.getResponseCode() as 302.I don't get the
title string of booklist.jsp(which is "allBooks") when
I try response.getTitle().I only get an empty string.
Is this because the doGet() of BookList servlet is invoked in between?
Can someone comment on it?How do I test whether the response is
properly redirected to the correct jsp page?
thanks in advance
jim
web.xml maps the servlets as below
..
<servlet>
<servlet-name>booklistservlet</servlet-name>
<servlet-class>bookshop.servlets.BookListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>booklistservlet</servlet-name>
<url-pattern>/booklistservlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>addbookservlet</servlet-name>
<servlet-class>bookshop.servlets.BookAddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addbookservlet</servlet-name>
<url-pattern>/createbook</url-pattern>
</servlet-mapping>
...
BookAddServlet.java
package bookshop.servlets;
...
public class BookAddServlet extends HttpServlet {
private BookDaoImpl bookdaoimpl =BookDaoImpl.getInstance();
private PublisherDaoImpl pubdaoimpl =PublisherDaoImpl.getInstance();
public void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException {
RequestDispatcher dispatcher =
request.getRequestDispatcher("bookadd.jsp");
List<Publisher> publishers = pubdaoimpl.findAllPublishers();
request.setAttribute("publishers",publishers);
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException {
String isbn = request.getParameter("isbn");
String publisherName = request.getParameter("publishername");
Book book = new Book();
book.setIsbn(Long.parseLong(isbn));
Publisher publisher = new Publisher();
publisher.setName(publisherName);
book.setPublisher(publisher);
bookdaoimpl.saveOrUpdateBook(book);//publisher will be saved since
it is so configured in hibernate
response.sendRedirect("booklistservlet");//which calls get method of
BookListServlet
}
private Date parseDate(String publishDate) {
try {
Date dt = new SimpleDateFormat("yyyy/MM/dd").parse(publishDate);
return dt;
} catch (ParseException e) {
return null;
}
}
}
BookListServlet.java
package bookshop.servlets;
...
public class BookListServlet extends HttpServlet {
private BookDaoImpl bookdaoimpl =BookDaoImpl.getInstance();
public void doGet(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException {
List books = bookdaoimpl.findAllBooks();
request.setAttribute("books", books);
RequestDispatcher dispatcher =
request.getRequestDispatcher("booklist.jsp");
dispatcher.forward(request, response);
}
}
Now I want to test using cactus and httpunit these two servlets
package bookshop.test.cactus.servlet;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
public class BookAddServletTests extends ServletTestCase{
private BookAddServlet bookaddservlet;
public BookAddServletTests(String theName) {
super(theName);
}
public void beginPostMethod(WebRequest theRequest){
String isbn = "1111";
String publisherName = "amazon";
theRequest.addParameter("isbn", isbn);
theRequest.addParameter("publishername", publisherName);
}
public void testPostMethod() throws ServletException, IOException{
BookAddServlet bookaddservlet = new BookAddServlet();
bookaddservlet.doPost(this.request, this.response);
}
public void endPostMethod(com.meterware.httpunit.WebResponse
response) throws SAXException{
assertEquals("response should be redirected",
302,response.getResponseCode());
String responseTitle = response.getTitle();
//this is where I have doubts..response should be redirected finally
to booklist.jsp which has a title "allBooks"
assertEquals("response title should be
allBooks","allBooks",responseTitle);
}
}
Back to comp.lang.java.programmer | Previous | Next | Find similar
cactus and servlet redirection jimgardener <jimgardener@gmail.com> - 2011-05-19 07:52 -0700
csiph-web