KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.jaspersoft.jasperserver.war;
22
23 import com.meterware.httpunit.Button;
24 import com.meterware.httpunit.SubmitButton;
25 import com.meterware.httpunit.WebForm;
26 import com.meterware.httpunit.WebResponse;
27
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
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 Edit User Role Page
37  **/

38 public class HttpUnitEditRoleTest
39     extends HttpUnitBaseTestCase {
40
41     private static String JavaDoc editRoleUserUrl = JasperServerConstants.BASE_URL +
42                                                     "/jasperserver/editRoleSelectingUsers.html";
43     private static String JavaDoc editRolePageText1 = "Only Users with this role?";
44     private static String JavaDoc editRolePageText2 = "EDIT ROLE - USERS";
45     private static String JavaDoc createEditPageText1 = "CREATE / EDIT USER";
46     private static String JavaDoc createEditPageText2 = "Test_User";
47     private static String JavaDoc pageText = "Users for Role";
48     private static String JavaDoc viewEditBtnText = "btEdView";
49     private static String JavaDoc cancelButtonText = "btcancsave";
50     private static String JavaDoc submitButtonText = "btSaveUser";
51     private static String JavaDoc textFieldText = "username";
52     private static String JavaDoc chkBoxText = "enabled";
53     private static String JavaDoc editRoleFormNameText = "fmRolSelUsr";
54     private static String JavaDoc editUserFormNameText = "fmCreEdUsr";
55     private static String JavaDoc editPgeText = "User";
56
57     private static WebForm editRoleForm;
58     private static WebForm editUserForm;
59     private static SubmitButton editRoleSubmit;
60
61     /**
62      * Constructor
63      *
64      * @param s
65      **/

66     public HttpUnitEditRoleTest(String JavaDoc s) {
67         super(s);
68     }
69
70     /**
71      * Checks through commonLoginFunction for loggin in at first time
72      *
73      * @throws Exception if fails
74      **/

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

85
86     /**
87      * This test case method is for Edit Role User Page of JS application
88      * This test case checks for the text, button, textField & check box elements on the Page
89      * On a proper wResponse - test is success else - failure
90      *
91      * @throws Exception if fails
92      **/

93     public void testEditDetailsPage()
94       throws Exception JavaDoc {
95
96         WebResponse editDetailResponse = this.getWebConversation().getCurrentPage();
97         assertNotNull("User searsh page is null", editDetailResponse);
98
99         String JavaDoc str = editDetailResponse.getText();
100         assertNotNull("page is null", str);
101
102         if(str == null ||str.trim().length()==0)
103             fail("There is no text on response");
104         assertTrue(str.indexOf(editRolePageText1)!= -1 && str.indexOf(editRolePageText2)!= -1);
105
106         //checking if the View/Edit button exists on the page
107
assertNotNull("The View/Edit button Element is not present",
108                 editDetailResponse.getElementsWithName(viewEditBtnText));
109
110         //checking if the Username textfield exists on the page
111
assertNotNull(textFieldText+"textfield Element is not present",
112                 editDetailResponse.getElementsWithName(textFieldText));
113
114         //checking if the CheckBox exists on the page
115
assertNotNull(chkBoxText+"Element is not present",
116                 editDetailResponse.getElementsWithName(chkBoxText));
117     }
118
119     /**
120      * This test case method is for Edit Role User Page of JS application
121      * This test case checks if on click View/Edit Button the page goes to Create/Edit Page
122      * Than on submitting cancel button it comes back to the Edit Role Page.
123      *
124      * On a proper wResponse - test is success else - failure
125      *
126      * @throws Exception if fails
127      **/

128     public void testSimpleEditBtnFlow()throws Exception JavaDoc {
129
130         WebResponse editBtnResponse = this.getWebConversation().getCurrentPage();
131
132         editRoleForm = editBtnResponse.getFormWithName(editRoleFormNameText);
133         assertNotNull("Form is null", editRoleForm);
134
135         editRoleSubmit = editRoleForm.getSubmitButton(viewEditBtnText);
136         assertNotNull("Button is null", editRoleSubmit);
137
138         editRoleForm.submit(editRoleSubmit);
139
140
141         WebResponse editUserPage = this.getWebConversation().getCurrentPage();
142
143         String JavaDoc str = editUserPage.getText();
144         assertNotNull("Text is null", str);
145         if(str == null || str.trim().length()==0)
146             fail("There is no text on response");
147
148         assertTrue(str.indexOf(editPgeText)!= -1);
149
150         editUserForm = editUserPage.getFormWithName(editUserFormNameText);
151         assertNotNull("Form is null", editRoleForm);
152
153         SubmitButton cancelButton = editUserForm.getSubmitButton(cancelButtonText);
154         assertNotNull("Button is null", editRoleSubmit);
155
156         editUserForm.submit(cancelButton);
157
158         WebResponse bckEditRolePage = this.getWebConversation().getCurrentPage();
159
160         String JavaDoc string = bckEditRolePage.getText();
161         assertNotNull("Page text is null", string);
162         if(string == null || string.trim().length()==0)
163             fail("There is no text on response");
164
165         assertTrue(string.indexOf(editRolePageText2)!= -1);
166     }
167
168     /**
169      * This test case method is for the Edit Role User Page,
170      * It checks through View/Edit button we can go to Create/Edit Page
171      * and edit user details and again come back to Edit Role Page,
172      * now again on clicking the View/Edit button it shows the corrosponding changes on Create/Edit Page
173      *
174      * @throws Exception if fails
175      */

176     public void testEditBtnFunctionality() throws Exception JavaDoc {
177
178         WebResponse editRolePage = this.getWebConversation().getCurrentPage();
179
180         editRoleForm = editRolePage.getFormWithName(editRoleFormNameText);
181         assertNotNull("Form is null", editRoleForm);
182
183         editRoleSubmit = editRoleForm.getSubmitButton(viewEditBtnText);
184         assertNotNull("Button is null", editRoleSubmit);
185
186         editRoleForm.getScriptableObject().setParameterValue("userselected","TestUser");
187         editRoleForm.getScriptableObject().setParameterValue("editroleuseraction","viewedituser");
188
189         editRoleForm.submit(editRoleSubmit);
190
191         //checking on the Create/Edit User Page
192
WebResponse editUserPage = this.getWebConversation().getCurrentPage();
193
194         String JavaDoc string = editUserPage.getText();
195         assertNotNull("page text is null", string);
196
197         if(string==null || string.trim().length()==0)
198             fail("No text found on the page on response");
199
200         assertTrue(string.indexOf(editPgeText)!= -1);
201
202         //editing the Full Name and again submitting
203
WebResponse response = this.getWebConversation().getCurrentPage();
204
205         editUserForm = response.getFormWithName(editUserFormNameText);
206         assertNotNull("Form is null", editUserForm);
207
208         /*
209         SubmitButton editUserButton = editUserForm.getSubmitButton(submitButtonText);
210         assertNotNull("Button is null", editUserButton);
211         editUserForm.setParameter("fullName","Test_User");
212         editUserForm.submit(editUserButton);
213         */

214
215         //START - CODE CHANGE - NEW for SAVE BUTTON
216
Button buttons[] = editUserForm.getButtons();
217         Button saveButton = null;
218         boolean found = false;
219
220         if(buttons != null){
221             for(int i=0; i<buttons.length; i++){
222                 saveButton = buttons[i];
223                 if(saveButton.getName().equalsIgnoreCase(submitButtonText)){
224                 found = true;
225                 break;
226                 }
227             }
228         }
229
230         assertTrue("Button is null", found);
231
232         editUserForm.setParameter("username", "TestUser");
233         editUserForm.setParameter("fullName", "Test_User");
234         editUserForm.setParameter("password", "newPassword");
235         editUserForm.setParameter("repassword", "newPassword");
236
237         saveButton.click();
238         //FINISH - CODE CHANGE - NEW for SAVE BUTTON
239

240
241         //checking on the Create/Edit User Page
242
WebResponse bkEditRolePage = this.getWebConversation().getCurrentPage();
243
244         String JavaDoc str = bkEditRolePage.getText();
245         assertNotNull("page text is null", str);
246
247         if(str==null || str.trim().length()==0)
248             fail("No text found on the page on response");
249
250         assertTrue(str.indexOf(pageText)!= -1);
251
252         //again submitting through view/edit button to go to create/edit page
253
editRoleForm.getScriptableObject().setParameterValue("userselected","TestUser");
254         editRoleForm.getScriptableObject().setParameterValue("editroleuseraction","viewedituser");
255
256         editRoleForm.submit(editRoleSubmit);
257
258         //checking on the Create/Edit User Page
259
WebResponse bkEditUserPage = this.getWebConversation().getCurrentPage();
260
261         String JavaDoc page = bkEditUserPage.getText();
262         assertNotNull("page text is null", page);
263
264         if(page==null || page.trim().length()==0)
265             fail("No text found on the page on response");
266
267         assertTrue(page.indexOf(createEditPageText1)!= -1 && page.indexOf(createEditPageText2)!= -1);
268
269     }
270
271
272     //****--------------------------------------------------------------------------*****/
273
//* Base class method implementaion */
274
//****--------------------------------------------------------------------------*****/
275

276
277
278     /* (non-Javadoc)
279      * @see com.jaspersoft.jasperserver.war.HttpUnitBaseTestCase#getloginCredentials()
280      */

281     protected String JavaDoc[] getloginCredentials() {
282         return new String JavaDoc[] { USERNAME, PASSWORD };
283     }
284
285     //****--------------------------------------------------------------------------*****/
286
//* HttpUnit framework methods */
287
//****--------------------------------------------------------------------------*****/
288

289
290     /**
291      * the main method for calling all the test cases whichever is being added into the suite.
292      *
293      * @param args
294      **/

295     public static void main(String JavaDoc[] args) {
296         try {
297             junit.textui.TestRunner.run(suite());
298         } catch (Exception JavaDoc _ex) {
299             _ex.printStackTrace();
300         }
301     }
302
303     /**
304      * this method is for adding which all test case/s method/s need to be tested
305      *
306      * @return Test
307      *
308      * @throws Exception if fails
309      **/

310     public static Test suite()
311       throws Exception JavaDoc {
312         TestSuite suite = new TestSuite();
313
314         TestCase test1 = new HttpUnitEditRoleTest("testEditDetailsPage");
315         TestCase test2 = new HttpUnitEditRoleTest("testSimpleEditBtnFlow");
316         TestCase test3 = new HttpUnitEditRoleTest("testEditBtnFunctionality");
317
318         suite.addTest(test1);
319         suite.addTest(test2);
320         //TODO to be done after the "search" button functionality on Edit Role -Users
321
suite.addTest(test3);
322
323         return suite;
324     }
325
326 }
Popular Tags