KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > HttpUnitBaseTestCase


1
2 /*
3  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed WITHOUT ANY WARRANTY; and without the
11  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
16  * or write to:
17  *
18  * Free Software Foundation, Inc.,
19  * 59 Temple Place - Suite 330,
20  * Boston, MA USA 02111-1307
21  */

22 package com.jaspersoft.jasperserver.war;
23
24 import com.meterware.httpunit.*;
25
26 import java.net.*;
27
28 import junit.framework.*;
29
30 /**
31  * The test cases are for: -
32  * Connecting the Jasper Server Startup Page - checking the Jasper Server Home Page - checking the
33  * Jasper Server List Reports Page - checking the Jasper Server View Report Page
34  **/

35 public abstract class HttpUnitBaseTestCase
36     extends TestCase
37     implements JasperServerConstants {
38     private String JavaDoc username = "j_username";
39     private String JavaDoc password = "j_password";
40     protected static WebResponse wResponse;
41     private WebConversation webConversation;
42
43     /**
44      * Creates a new HttpUnitBaseTestCase object.
45      **/

46     public HttpUnitBaseTestCase() {
47         super();
48     }
49
50     /**
51      * Creates a new HttpUnitBaseTestCase object.
52      *
53      * @param arg0 arg0
54      **/

55     public HttpUnitBaseTestCase(String JavaDoc arg0) {
56         super(arg0);
57     }
58
59     /**
60      * Common Login functionality for each individual URL-s in the
61      * JS application. Throws exception if login is not successfull or link is not found
62      *
63      * @param url application url
64      *
65      * @return - void
66      *
67      * @throws Exception if fails
68      **/

69     protected WebResponse commonLoginFunction(String JavaDoc url)
70       throws Exception JavaDoc {
71         WebResponse response = null;
72         WebConversation wcon = new WebConversation();
73         WebRequest wreq = new GetMethodWebRequest(new URL(url), "");
74         response = wcon.getResponse(wreq);
75
76         WebForm form = response.getForms()[0];
77         assertEquals("Form Action", "j_acegi_security_check", form.getAction());
78         wreq = form.getRequest();
79         wreq.setParameter(username, getloginCredentials()[0]);
80         wreq.setParameter(password, getloginCredentials()[1]);
81         response = wcon.getResponse(wreq);
82
83         this.setWebConversation(wcon);
84         return response;
85     }
86
87     /**
88      * This method is for getting the response from the site to be tested
89      * TODO : need to revisit this method implementation
90      *
91      * @param url Url to be navigated
92      *
93      * @return WebResponse
94      *
95      * @throws Exception if fails
96      **/

97     protected WebResponse getURLResponse(String JavaDoc url)
98       throws Exception JavaDoc {
99         WebResponse response = null;
100         URL serverUrl = new URL(url);
101         WebConversation conversation = new WebConversation();
102         WebRequest request = new GetMethodWebRequest(serverUrl, "");
103         response = conversation.getResponse(request);
104         return response;
105     }
106
107     /**
108      * Subclass would provide implementation for this method
109      *
110      * @return username and password
111      **/

112     protected abstract String JavaDoc[] getloginCredentials();
113
114     /**
115      * Returns the associated webConversation object reference.
116      *
117      * @return the associated webConversation object reference.
118      **/

119     public WebConversation getWebConversation() {
120         return webConversation;
121     }
122
123     /**
124      * Sets the WebConversation object.
125      *
126      * @param webConversation
127      **/

128     public void setWebConversation(WebConversation webConversation) {
129         this.webConversation = webConversation;
130     }
131 }
132
Popular Tags