KickJava   Java API By Example, From Geeks To Geeks.

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


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

39 public class HttpUnitCreateUserTest
40     extends HttpUnitBaseTestCase {
41     private static String JavaDoc userMgmtPageUrl = JasperServerConstants.BASE_URL +
42                                             "/jasperserver/usersearch.html";
43     private static String JavaDoc objSecurityLink = "Object Security for User";
44     private static String JavaDoc usrSearchFormText = "fmUsrSrch";
45     private static String JavaDoc editUserFormText = "fmCreEdUsr";
46     private static String JavaDoc newUsrButtonText = "newuser";
47     private static String JavaDoc submitButtonText = "btSaveUser";
48     private static String JavaDoc cancelButtonText = "btcancsave";
49     private static String JavaDoc userNameText = "username";
50     private static String JavaDoc usersText = "Selected Roles";
51     private static String JavaDoc errMsg1Text = "Property username threw exception;";
52     private static String JavaDoc errMsg2Text = "FullName can't be empty";
53     private static String JavaDoc usersSearchText = "Enabled User?";
54     
55     private static SubmitButton cancelButton;
56     private static WebForm createUsrForm;
57     private static SubmitButton submitButton;
58     private static WebForm usrSearchForm;
59     private static SubmitButton newUsrButton;
60     
61     /**
62      * Creates a new HttpUnitCreateUserTest object.
63      **/

64     public HttpUnitCreateUserTest() {
65         super();
66     }
67
68     /**
69      * Constructor
70      *
71      * @param s
72      **/

73     public HttpUnitCreateUserTest(String JavaDoc s) {
74         super(s);
75     }
76
77     /**
78      * Checks through commonLoginFunction for logging in in at first time
79      *
80      * @throws Exception if fails
81      **/

82     public void setUp()
83       throws Exception JavaDoc {
84           commonLoginFunction(userMgmtPageUrl);
85     }
86     
87     
88     //****--------------------------------------------------------------------------*****/
89
//* HttpUnit test cases */
90
//****--------------------------------------------------------------------------*****/
91

92     
93     /**
94      * This test case method is for Create/Edit User page of JS application.
95      * This page take User information as input parameter and on Submit goes to User Search Page
96      * on proper response test success else fail
97      *
98      * @throws Exception if fails
99      **/

100     public void testCreateUserPage()
101       throws Exception JavaDoc {
102         User user = new User();
103         user.setUserName("Test_HttpUnit");
104         user.setFullName("Test_HttpUnit");
105         user.setPassWord("123");
106         user.setRePassWord("123");
107         user.setEnable(true);
108
109         this.createNewUser(user);
110         WebResponse usrSearchPage = this.getWebConversation().getCurrentPage();
111         
112         String JavaDoc searchPage = usrSearchPage.getText();
113         assertNotNull("User searsh page is null", searchPage);
114         
115         if ((searchPage == null) || (searchPage.trim().length() == 0)) {
116             fail("No text found on response");
117         }
118         
119         assertTrue(searchPage.indexOf(usersSearchText) != -1);
120     }
121     
122     /**
123      * This test case method is for Create/Edit User page of JS application.
124      * This page take empty user info as parameter and on Submit remains on same Page with Error message
125      * On proper response test success else failure
126      *
127      * @throws Exception if fails
128      **/

129     public void testCreateNoUserPage()
130       throws Exception JavaDoc {
131          User user = new User(); //sending empty user info
132
this.createNewUser(user);
133
134         WebResponse resultPage = this.getWebConversation().getCurrentPage();
135         assertNotNull("Result page is null", resultPage);
136         String JavaDoc createUserPage = resultPage.getText();
137         
138         if ((createUserPage == null) || (createUserPage.trim().length() == 0)) {
139         fail("No text found on response");
140         }
141         
142         assertTrue(createUserPage.indexOf(errMsg1Text) != -1 && createUserPage.indexOf(errMsg2Text) != -1);
143     }
144     
145     /**
146      * This test case method is for Create/Edit User page of JS application.
147      * On click to Cancel Button it goes to User Search Page
148      * On proper response test success else failure
149      *
150      * @throws Exception if fails
151      **/

152     public void testUserCancelPage()
153       throws Exception JavaDoc {
154
155         WebResponse usrSearchPage = this.getWebConversation().getCurrentPage();
156         assertNotNull("User searsh page is null", usrSearchPage);
157             
158         usrSearchForm = usrSearchPage.getFormWithName(usrSearchFormText);
159         assertNotNull("Form is null", usrSearchForm);
160         
161         newUsrButton = usrSearchForm.getSubmitButton(newUsrButtonText);
162         assertNotNull("Button is null", newUsrButton);
163         
164         usrSearchForm.submit(newUsrButton);
165         
166         WebResponse createUsrPage = this.getWebConversation().getCurrentPage();
167         createUsrForm = createUsrPage.getFormWithName(editUserFormText);
168         assertNotNull("Form is null", createUsrForm);
169         
170         cancelButton = createUsrForm.getSubmitButton(cancelButtonText);
171         assertNotNull("Button is null", cancelButton);
172         
173         createUsrPage = createUsrForm.submit(cancelButton);
174         
175         //check if in response it come back to the User Search page
176
WebResponse bckUsrSearchPage = this.getWebConversation().getCurrentPage();
177         
178         String JavaDoc str = bckUsrSearchPage.getText();
179         assertNotNull("Text is null", str);
180         if(str == null || str.trim().length()== 0)
181             fail("There was no text in response");
182         
183         assertTrue(str.indexOf(usersSearchText)!= -1);
184     }
185     
186     /**
187      * This method checks for the User Search Page and New User Button on the page
188      * also checks for the text field, text and link on the page.
189      *
190      * @param user to be created
191      *
192      * @throws Exception if fails
193      **/

194     protected WebResponse createNewUser(User user)
195       throws Exception JavaDoc {
196             
197         WebResponse userSearchPage = this.getWebConversation().getCurrentPage();
198
199         //navigate to create user page by clicking new user button
200
usrSearchForm = userSearchPage.getFormWithName(usrSearchFormText);
201         newUsrButton = usrSearchForm.getSubmitButton(newUsrButtonText);
202         usrSearchForm.submit(newUsrButton);
203         
204         //checking for UserName Text field on the Create/Edit page
205
WebResponse createSearchPage = this.getWebConversation().getCurrentPage();
206         assertNotNull("Text Field Element on Create/Edit Page is not present",
207                 createSearchPage.getElementsWithName(userNameText));
208         
209         //checking for Text on the Create/Edit page
210
assertNotNull("Text Field Element on Create/Edit Page is not present",
211                 createSearchPage.getElementsWithName(usersText));
212         
213         //checking for the link on the page
214
WebLink link = createSearchPage.getLinkWith(objSecurityLink);
215         assertNotNull(objSecurityLink + "link is null",link);
216         
217
218         WebResponse createUserPage = this.getWebConversation().getCurrentPage();
219
220         WebResponse resultPage = this.saveUser(createUserPage, user);
221         
222         return resultPage;
223         
224         
225     }
226
227     /**
228      * Saves a User .
229      *
230      * @param webResponse page reference
231      * @param user to be added
232      *
233      * @return Result response
234      *
235      * @throws Exception if fails
236      **/

237     protected WebResponse saveUser(WebResponse webResponse,
238                                    User user)
239       throws Exception JavaDoc {
240         
241         createUsrForm = webResponse.getFormWithName(editUserFormText);
242         submitButton = createUsrForm.getSubmitButton(submitButtonText);
243         createUsrForm.setParameter("username", user.getUserName());
244         createUsrForm.setParameter("password", user.getPassWord());
245         createUsrForm.setParameter("fullName", user.getFullName());
246         createUsrForm.setParameter("repassword", user.getRePassWord());
247         createUsrForm.setCheckbox("enabled", user.isEnable());
248
249         WebResponse wbResponse = createUsrForm.submit(submitButton);
250         assertNotNull("The response is Null", wbResponse);
251                 
252         return wbResponse;
253     }
254     
255     
256     //****--------------------------------------------------------------------------*****/
257
//* Base class method implementaion */
258
//****--------------------------------------------------------------------------*****/
259

260     
261     /* (non-Javadoc)
262      * @see com.jaspersoft.jasperserver.war.HttpUnitBaseTestCase#getloginCredentials()
263      */

264     protected String JavaDoc[] getloginCredentials() {
265         return new String JavaDoc[] { USERNAME, PASSWORD };
266     }
267     
268     
269     //****--------------------------------------------------------------------------*****/
270
//* HttpUnit framework methods */
271
//****--------------------------------------------------------------------------*****/
272

273     
274     public class User {
275         private String JavaDoc userName;
276         private String JavaDoc passWord;
277         private String JavaDoc fullName;
278         private String JavaDoc rePassWord;
279         private boolean enable;
280
281         public User() {
282             super();
283         }
284
285         public User(String JavaDoc userName,
286                     String JavaDoc passwd,
287                     String JavaDoc fullName,
288                     String JavaDoc rePassWord,
289                     boolean enable) {
290             super();
291             this.fullName = fullName;
292             this.passWord = passwd;
293             this.userName = userName;
294             this.rePassWord = rePassWord;
295             this.enable = enable;
296         }
297
298         public boolean isEnable() {
299             return enable;
300         }
301
302         public void setEnable(boolean enable) {
303             this.enable = enable;
304         }
305
306         public String JavaDoc getFullName() {
307             return fullName;
308         }
309
310         public void setFullName(String JavaDoc fullName) {
311             this.fullName = fullName;
312         }
313
314         public String JavaDoc getPassWord() {
315             return passWord;
316         }
317
318         public void setPassWord(String JavaDoc passWord) {
319             this.passWord = passWord;
320         }
321
322         public String JavaDoc getRePassWord() {
323             return rePassWord;
324         }
325
326         public void setRePassWord(String JavaDoc rePassWord) {
327             this.rePassWord = rePassWord;
328         }
329
330         public String JavaDoc getUserName() {
331             return userName;
332         }
333
334         public void setUserName(String JavaDoc userName) {
335             this.userName = userName;
336         }
337     }
338     
339     /**
340      * the main method for calling all the test cases whichever is being added into the suite.
341      *
342      * @param args
343      **/

344     public static void main(String JavaDoc[] args) {
345         try {
346             junit.textui.TestRunner.run(suite());
347         } catch (Exception JavaDoc _ex) {
348             _ex.printStackTrace();
349         }
350     }
351
352     /**
353      * this method is for adding which all test case/s method/s need to be tested
354      *
355      * @return Test
356      *
357      * @throws Exception if fails
358      **/

359     public static Test suite()
360       throws Exception JavaDoc {
361         TestSuite suite = new TestSuite();
362         
363         TestCase test1 = new HttpUnitCreateUserTest("testCreateUserPage");
364         TestCase test2 = new HttpUnitCreateUserTest("testCreateNoUserPage");
365         TestCase test3 = new HttpUnitCreateUserTest("testUserCancelPage");
366         
367         suite.addTest(test1);
368         suite.addTest(test2);
369         suite.addTest(test3);
370         return suite;
371     }
372
373 }
374
Popular Tags