KickJava   Java API By Example, From Geeks To Geeks.

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


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

21
22 package com.jaspersoft.jasperserver.war;
23
24 import com.meterware.httpunit.SubmitButton;
25 import com.meterware.httpunit.WebForm;
26 import com.meterware.httpunit.WebLink;
27 import com.meterware.httpunit.WebResponse;
28
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 /**
34  * The test cases are for: -
35  * Connecting the Jasper Server Startup Page - checking the Jasper Server Home Page - checking the
36  * Jasper Server Role Management Page
37  **/

38 public class HttpUnitRoleMgmtTest
39     extends HttpUnitBaseTestCase {
40     private static String JavaDoc editRolePageUrl = BASE_URL +"/jasperserver/editrole.html";
41     private static String JavaDoc createUserText = "Role Management";
42     private static String JavaDoc roleNameText = "Role Name:";
43     private static String JavaDoc userRolePageText = "Users for Role";
44     private static String JavaDoc editUserPageText = "TestUser";
45     private static String JavaDoc saveRoleBtnText = "btsubmit";
46     private static String JavaDoc roleNameFldText = "roleName";
47     private static String JavaDoc formNameText = "fmCreEdRole";
48     private static String JavaDoc roleSelFormText = "fmRolSelUsr";
49     private static String JavaDoc editPageFormText = "fmCreEdUsr";
50     private static String JavaDoc cancelBtnText = "btcancsave";
51     private static String JavaDoc editBtnText = "btEdView";
52     private static String JavaDoc saveBtnText = "btSaveUser";
53     private static String JavaDoc usrRoleLink = "Users for Role";
54     private static String JavaDoc roleMgmtLink = "Role Management";
55     private static String JavaDoc adminStatusText = "Role is saved successfully into the Database";
56     
57     private static WebForm form;
58     
59     /**
60      * Constructor
61      *
62      * @param s
63      **/

64     public HttpUnitRoleMgmtTest(String JavaDoc s) {
65         super(s);
66     }
67     
68     /**
69      * Checks through commonLoginFunction for loggin in at first time
70      *
71      * @throws Exception
72      **/

73     public void setUp()
74       throws Exception JavaDoc {
75             wResponse = commonLoginFunction(editRolePageUrl);
76         }
77     
78
79     //****--------------------------------------------------------------------------*****/
80
//* HttpUnit test cases */
81
//****--------------------------------------------------------------------------*****/
82

83     
84     /**
85      * This test case method is for EditRole Page of JS application
86      * This checks for the text field,button & link on the page
87      * For a perticular Role it goes to Admin Status Page,
88      * and clicking on the link on Admin Status Page it comes back to the Create/Edit Page
89      * It also checks if the added Role exists in the User Management Page
90      * On a proper wResponse - test is success else - failure
91      *
92      * @throws Exception if fails
93      **/

94     public void testEditRolePage()
95       throws Exception JavaDoc {
96                 
97         WebResponse roleMgmtPage = this.getWebConversation().getCurrentPage();
98         assertNotNull("User search page is null", roleMgmtPage);
99         
100         String JavaDoc string = roleMgmtPage.getText();
101         assertNotNull("Role Mgmt page is null", string);
102         
103         if ((string == null) || (string.trim().length() == 0))
104             fail("Text is not found on wResponse");
105         
106         assertTrue(string.indexOf(roleNameText) != -1);
107         
108         //checking if the Save Role button exist on the page
109
assertNotNull("The Save Role Element is not present",
110                 roleMgmtPage.getElementsWithName(saveRoleBtnText));
111         
112         //checking if the Role Name text field on the page
113
assertNotNull("The Role Name Element is not present",
114                 roleMgmtPage.getElementsWithName(roleNameFldText));
115
116         //checking for the function of the Save Role button
117
WebForm form = roleMgmtPage.getFormWithName(formNameText);
118         SubmitButton submitBtn = form.getSubmitButton(saveRoleBtnText);
119         
120         form.setParameter("roleName", "Tom");
121         
122         form.submit(submitBtn);
123         
124         //checking if on submit it goes to Admin Status Page
125
WebResponse adminStatusPage = this.getWebConversation().getCurrentPage();
126         
127         String JavaDoc str = adminStatusPage.getText();
128         assertNotNull("text page is null", str);
129         
130         if(str == null ||str.trim().length()==0)
131             fail("There is no text on response");
132         
133         assertTrue(str.indexOf(adminStatusText)!= -1);
134         
135         WebLink adminPageLink = adminStatusPage.getLinkWith(roleMgmtLink);
136         assertNotNull("The link is Null", adminPageLink);
137         
138         adminStatusPage = adminPageLink.click();
139         
140         //check if link takes back to the Role Management Page
141
WebResponse bckCreatePage = this.getWebConversation().getCurrentPage();
142         
143         String JavaDoc page = bckCreatePage.getText();
144         assertNotNull("text on page is Null", page);
145         
146         if(page == null || page.trim().length()==0)
147             fail("There is no text on response");
148         
149         assertTrue(page.indexOf(createUserText)!= -1);
150     }
151     
152     /**
153      * This test case method is for the Link on the EditRole page
154      * Setting the parameter and than clicking the link takes to the Edit User Role page
155      *
156      * On a proper response - test is success else - failure
157      *
158      * @throws Exception if fails
159      **/

160     public void testEditPageLink() throws Exception JavaDoc{
161         
162         WebResponse roleMgmtPage = this.getWebConversation().getCurrentPage();
163         
164         WebLink link = roleMgmtPage.getLinkWith(usrRoleLink);
165         assertNotNull("Link is Null", link);
166         
167         form = roleMgmtPage.getFormWithName(formNameText);
168         form.setParameter("roleName", "Tom");
169         
170         roleMgmtPage = link.click();
171         
172         assertNotNull("User Role Link is Null", roleMgmtPage);
173         
174         WebResponse editRolePage = this.getWebConversation().getCurrentPage();
175         
176         String JavaDoc role = editRolePage.getText();
177         assertNotNull("page text is Null", role);
178         
179         if((role == null)||(role.trim().length()==0))
180             fail("There is no text in response");
181         
182         assertTrue(role.indexOf(userRolePageText)!= -1);
183         
184         WebForm userRoleForm = editRolePage.getFormWithName(roleSelFormText);
185         assertNotNull("Form is Null", userRoleForm);
186         
187         SubmitButton editButton = userRoleForm.getSubmitButton(editBtnText);
188         assertNotNull("Button is Null", editButton);
189         
190         userRoleForm.getScriptableObject().setParameterValue("userselected","TestUser");
191         
192         userRoleForm.submit(editButton);
193         
194         WebResponse editUserPage = this.getWebConversation().getCurrentPage();
195         
196         String JavaDoc string = editUserPage.getText();
197         assertNotNull("page text is Null", string);
198         
199         if((string == null)||(string.trim().length()==0))
200             fail("There is no text in response");
201         
202         assertTrue(string.indexOf(editUserPageText)!= -1);
203     
204         //checking if the Save Role button exist on the page
205
assertNotNull("The Save Role Element is not present",
206                 editUserPage.getElementsWithName(saveBtnText));
207         
208         WebForm editForm = editUserPage.getFormWithName(editPageFormText);
209         assertNotNull("Form is Null", editForm);
210         
211         SubmitButton cancelBtn = editForm.getSubmitButton(cancelBtnText);
212         assertNotNull("Button is Null", cancelBtn);
213         
214         editForm.submit(cancelBtn);
215         
216         WebResponse bckUserRolePage = this.getWebConversation().getCurrentPage();
217         
218         String JavaDoc str = bckUserRolePage.getText();
219         assertNotNull("page text is Null", str);
220         
221         if(str == null || str.trim().length()==0)
222             fail("There is no text on the page");
223         
224         assertTrue(str.indexOf(userRolePageText)!= -1);
225     }
226     
227     
228     /**
229      * This test case method is for the Link on the EditRole page
230      * Setting the invalid parameter and than clicking the link throws error
231      *
232      * On a proper response - test is success else - failure
233      *
234      * @throws Exception if fails
235      **/

236     public void testErrorLink() throws Exception JavaDoc{
237         
238         WebResponse roleMgmtPage = this.getWebConversation().getCurrentPage();
239         WebLink errorLink = roleMgmtPage.getLinkWith(usrRoleLink);
240         
241         form.setParameter("roleName", "test");
242         roleMgmtPage = errorLink.click();
243         
244     }
245     
246     
247     //****--------------------------------------------------------------------------*****/
248
//* Base class method implementaion */
249
//****--------------------------------------------------------------------------*****/
250

251
252     /* (non-Javadoc)
253      * @see com.jaspersoft.jasperserver.war.HttpUnitBaseTestCase#getloginCredentials()
254      */

255     protected String JavaDoc[] getloginCredentials() {
256         return new String JavaDoc[] { USERNAME, PASSWORD };
257     }
258
259
260     //****--------------------------------------------------------------------------*****/
261
//* HttpUnit framework methods */
262
//****--------------------------------------------------------------------------*****/
263

264     
265     /**
266      * the main method for calling all the test cases whichever is being added into the suite.
267      *
268      * @param args
269      **/

270     public static void main(String JavaDoc[] args) {
271         try {
272             junit.textui.TestRunner.run(suite());
273         } catch (Exception JavaDoc _ex) {
274             _ex.printStackTrace();
275         }
276     }
277     /**
278      * This method is for adding which all test case/s method/s need to be
279      *
280      * @return Test
281      *
282      * @throws Exception if fails
283      **/

284     public static Test suite()
285       throws Exception JavaDoc {
286         TestSuite suite = new TestSuite();
287         
288         TestCase test1 = new HttpUnitRoleMgmtTest("testEditRolePage");
289         TestCase test2 = new HttpUnitRoleMgmtTest("testEditPageLink");
290         
291         suite.addTest(test1);
292         suite.addTest(test2);
293         return suite;
294     }
295 }
Popular Tags