KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > webtest > UserManagementTest


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.webtest;
17
18 import com.blandware.atleap.common.util.RegExUtil;
19 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
20 import com.gargoylesoftware.htmlunit.html.HtmlForm;
21 import com.gargoylesoftware.htmlunit.html.HtmlOption;
22 import com.gargoylesoftware.htmlunit.html.HtmlPage;
23 import com.gargoylesoftware.htmlunit.html.HtmlSelect;
24 import com.gargoylesoftware.htmlunit.html.HtmlTable;
25 import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
26 import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
27 import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
28 import org.jaxen.JaxenException;
29
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * <p>This class is used to test operations, related to user management</p>
35  * <p><a HREF="UserManagementTest.java.htm"><i>View Source</i></a></p>
36  *
37  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
38  * @version $Revision: 1.3 $ $Date: 2006/03/11 14:06:34 $
39  */

40 public class UserManagementTest extends BaseWebTestCase {
41
42     /**
43      * Creates new instance of UserManagementTest
44      */

45     public UserManagementTest() {
46     }
47
48     /**
49      * Creates new instance of UserManagementTest
50      *
51      * @param name Name of test case to execute on test run
52      */

53     public UserManagementTest(String JavaDoc name) {
54         super(name);
55     }
56
57     /**
58      * Verifies, that page with list of users has been returned in last response
59      */

60     protected void verifyListPage() throws JaxenException {
61         assertXPathResultNotNull("//table[@id='userList']");
62         Boolean JavaDoc hasRows = (Boolean JavaDoc) evaluateXPath("count(//table[@id='userList']/tbody/tr) > 0", lastResponse);
63         assertNotNull(hasRows);
64         assertTrue(hasRows.booleanValue());
65     }
66
67     /**
68      * Verifies, that page with form for updating user has been returned in last response
69      */

70     protected void verifyUpdatePage() throws Exception JavaDoc {
71         // assert, we're on the Update user page
72
String JavaDoc title = lastResponse.getTitleText();
73         assertMatches(title, ".+" + RegExUtil.escapeMetasymbols(getMessage("core.user.callUpdate")));
74     }
75
76     /**
77      * Verifies, that page with form for creating user has been returned in last response
78      */

79     protected void verifyCreatePage() throws Exception JavaDoc {
80         // assert, we're on the Create user page
81
String JavaDoc title = lastResponse.getTitleText();
82         assertMatches(title, ".+" + RegExUtil.escapeMetasymbols(getMessage("core.user.create")));
83     }
84
85     /**
86      * Verifies, that page with form for assigning roles to user has been returned in last response
87      */

88     protected void verifyAssignRolesAndGroupsPage() throws Exception JavaDoc {
89         // assert, we're on the Assign Roles and Groups page
90
String JavaDoc title = lastResponse.getTitleText();
91         assertMatches(title, ".+" + RegExUtil.escapeMetasymbols(getMessage("core.user.assignRolesAndGroups")));
92     }
93
94     // ~ Test methods =================================================================================================
95

96     public void testListUsers() throws Exception JavaDoc {
97
98         // log into the application
99
login();
100
101         // request list of users
102
invoke("/core/user/list.do");
103         verifyListPage();
104     }
105
106     public void testViewProfile() throws Exception JavaDoc {
107
108         // log into the application
109
login();
110
111         // request user profile page
112
invoke("/core/user/view.do");
113
114         // assert, we're on the View Profile page
115
String JavaDoc title = lastResponse.getTitleText();
116         assertMatches(title, ".+" + RegExUtil.escapeMetasymbols(getMessage("core.user.view")));
117
118         // assert, that information about 'admin' user is shown on the page
119
String JavaDoc userNameXPathExpr = "string(//table[@id='userTable']//tr/th[text() = '" + getMessage("core.user.form.name") + "']/following-sibling::td)";
120         assertXPathResultEquals(userNameXPathExpr, "admin");
121
122         // click Show list button
123
clickButtonWithLabel(null, getMessage("core.commons.buttons.showList"));
124
125         // assert, we're on the list
126
verifyListPage();
127
128         // request user profile page once again
129
invoke("/core/user/view.do");
130
131         // click Edit button
132
clickButtonWithLabel(null, getMessage("core.commons.buttons.callUpdate"));
133
134         // assert, we're on update page
135
verifyUpdatePage();
136     }
137
138     public void testCreateAndDelete() throws Exception JavaDoc {
139
140         // log into the application
141
login();
142
143         // request page with list of users and verify we've got it
144
invoke("/core/user/list.do");
145         verifyListPage();
146
147         // click Create button
148
clickButtonWithLabel(null, getMessage("core.commons.buttons.create"));
149
150         // verify we're on create page
151
verifyCreatePage();
152
153         HtmlForm userForm = getFormByName("userForm");
154         getInputByName(userForm, "name", INPUT_TYPE_TEXT).setValueAttribute("test");
155         getInputByName(userForm, "password", INPUT_TYPE_PASSWORD).setValueAttribute("testpass");
156         getInputByName(userForm, "confirmPassword", INPUT_TYPE_PASSWORD).setValueAttribute("testpass");
157         getInputByName(userForm, "firstName", INPUT_TYPE_TEXT).setValueAttribute("Test");
158         getInputByName(userForm, "lastName", INPUT_TYPE_TEXT).setValueAttribute("User");
159         getInputByName(userForm, "city", INPUT_TYPE_TEXT).setValueAttribute("Izhevsk");
160         getInputByName(userForm, "province", INPUT_TYPE_TEXT).setValueAttribute("Udmurtia");
161         getInputByName(userForm, "country", INPUT_TYPE_TEXT).setValueAttribute("Russia");
162         getInputByName(userForm, "postalCode", INPUT_TYPE_TEXT).setValueAttribute("426000");
163
164         // Post an invalid email address
165
getInputByName(userForm, "email", INPUT_TYPE_TEXT).setValueAttribute("test.user@.somewhere.net");
166         getInputByName(userForm, "website", INPUT_TYPE_TEXT).setValueAttribute("http://site.somewhere.net");
167
168         // submit form. We should see this page back with error message
169
clickButtonWithLabel(userForm, getMessage("core.commons.buttons.signup"));
170
171         // verify, we're on create page again
172
verifyCreatePage();
173
174         // verify, we've got an error message for 'email' field
175
String JavaDoc errorFieldXPathExpr = "string(//form[@name='userForm']//label[@class='error']/@for)";
176         assertXPathResultEquals(errorFieldXPathExpr, "email");
177
178         // post correct data
179
userForm = getFormByName("userForm");
180         getInputByName(userForm, "name", INPUT_TYPE_TEXT).setValueAttribute("test");
181         getInputByName(userForm, "password", INPUT_TYPE_PASSWORD).setValueAttribute("testpass");
182         getInputByName(userForm, "confirmPassword", INPUT_TYPE_PASSWORD).setValueAttribute("testpass");
183         getInputByName(userForm, "firstName", INPUT_TYPE_TEXT).setValueAttribute("Test");
184         getInputByName(userForm, "lastName", INPUT_TYPE_TEXT).setValueAttribute("User");
185         getInputByName(userForm, "city", INPUT_TYPE_TEXT).setValueAttribute("Izhevsk");
186         getInputByName(userForm, "province", INPUT_TYPE_TEXT).setValueAttribute("Udmurtia");
187         getInputByName(userForm, "country", INPUT_TYPE_TEXT).setValueAttribute("Russia");
188         getInputByName(userForm, "postalCode", INPUT_TYPE_TEXT).setValueAttribute("426000");
189         getInputByName(userForm, "email", INPUT_TYPE_TEXT).setValueAttribute("test.user@somewhere.net");
190         getInputByName(userForm, "website", INPUT_TYPE_TEXT).setValueAttribute("http://site.somewhere.net");
191
192         // submit form
193
clickButtonWithLabel(userForm, getMessage("core.commons.buttons.signup"));
194
195         // verify, we've got an Assign roles and groups page
196
verifyAssignRolesAndGroupsPage();
197
198         // cancel role/group assignment and verify, we're on the page with list
199
clickButtonWithLabel(null, getMessage("core.commons.buttons.cancel"));
200         verifyListPage();
201
202         // logout and login as newly created user
203
logout();
204         login("test", "testpass");
205
206         // logout again and login as admin
207
logout();
208         login();
209
210         // request page with list of users and verify we've got it
211
invoke("/core/user/list.do");
212         verifyListPage();
213
214         // following code is only valid, when there is only one page in the list of users
215
// it is one of the reasons, why testing should be performed on specially prepared data
216
// if you run this test in the production mode, beware: unexpected errors can occur
217

218         HtmlTable userTable = (HtmlTable) evaluateXPath("//table[@id='userList']", lastResponse);
219
220         // get all anchors, contained in table
221
List JavaDoc anchors = userTable.getHtmlElementsByTagName("a");
222
223         // search for one, which can be used to
224
String JavaDoc deleteUri = "/core/user/delete";
225         String JavaDoc userNameParam = "name=test";
226         Iterator JavaDoc anchorsIterator = anchors.iterator();
227         HtmlAnchor deleteAnchor = null;
228         while ( anchorsIterator.hasNext() && deleteAnchor == null ) {
229             HtmlAnchor anchor = (HtmlAnchor) anchorsIterator.next();
230             String JavaDoc href = anchor.getHrefAttribute();
231             if ( href.indexOf(deleteUri) >= 0 ) {
232                 if ( href.indexOf(userNameParam) > 0 ) {
233                     deleteAnchor = anchor;
234                 }
235             }
236         }
237
238         assertNotNull("No link could be found to delete user 'test'", deleteAnchor);
239
240         // click 'delete' link
241
setLastResponse((HtmlPage) deleteAnchor.click());
242
243         // verify, we're on the list page
244
verifyListPage();
245
246         // ensure, that user has been deleted
247
HtmlTableCell userCell = (HtmlTableCell) evaluateXPath("//table[@id='userList']/tbody//td[text()='test']", lastResponse);
248         assertNull("User 'test' was not deleted", userCell);
249
250     }
251
252     public void testAssignRolesAndGroups() throws Exception JavaDoc {
253
254         // log into the application
255
login();
256
257         // request page with list of users and verify we've got it
258
invoke("/core/user/list.do");
259         verifyListPage();
260
261         // following code is only valid, when there is only one page in the list of users
262
// it is one of the reasons, why testing should be performed on specially prepared data
263
// if you run this test in the production mode, beware: unexpected errors can occur
264

265         HtmlTable userTable = (HtmlTable) evaluateXPath("//table[@id='userList']", lastResponse);
266
267         // get all anchors, contained in table
268
List JavaDoc anchors = userTable.getHtmlElementsByTagName("a");
269
270         // search for one, which can be used to
271
String JavaDoc assignRolesUri = "/core/user/callAssignRolesAndGroups";
272         String JavaDoc userNameParam = "userName=user";
273         Iterator JavaDoc anchorsIterator = anchors.iterator();
274         HtmlAnchor assignRolesAndGroupsAnchor = null;
275         while ( anchorsIterator.hasNext() && assignRolesAndGroupsAnchor == null ) {
276             HtmlAnchor anchor = (HtmlAnchor) anchorsIterator.next();
277             String JavaDoc href = anchor.getHrefAttribute();
278             if ( href.indexOf(assignRolesUri) >= 0 ) {
279                 if ( href.indexOf(userNameParam) > 0 ) {
280                     assignRolesAndGroupsAnchor = anchor;
281                 }
282             }
283         }
284
285         assertNotNull("No link could be found to assign roles/groups to user 'user'", assignRolesAndGroupsAnchor);
286
287         // go to assign roles/groups page and verify, we've got it
288
setLastResponse((HtmlPage) assignRolesAndGroupsAnchor.click());
289         verifyAssignRolesAndGroupsPage();
290
291         HtmlForm selectRolesAndGroupsForm = getFormByName("selectRolesAndGroupsForm");
292
293         // get select elements
294
HtmlSelect availableGroups = getSelectByName(selectRolesAndGroupsForm, "availableGroups");
295         HtmlSelect selectedGroups = getSelectByName(selectRolesAndGroupsForm, "selectedGroups");
296
297         // move 'managers' group
298
// iterate through options in available groups list
299
int numberOfOptions = availableGroups.getOptionSize();
300         for ( int i = 0; i < numberOfOptions; i++ ) {
301             HtmlOption option = availableGroups.getOption(i);
302             if ( option.getValueAttribute().equals("managers") ) {
303                 availableGroups.removeOption(i);
304                 selectedGroups.appendOption(option);
305                 continue;
306             }
307
308             // select option
309
option.setSelected(true);
310         }
311
312         // select all options in the 'selectedGroups' list
313
numberOfOptions = selectedGroups.getOptionSize();
314         for ( int i = 0; i < numberOfOptions; i++ ) {
315             HtmlOption option = selectedGroups.getOption(i);
316             option.setSelected(true);
317         }
318
319         // submit form
320
clickButtonWithLabel(selectRolesAndGroupsForm, getMessage("core.commons.buttons.assign"));
321
322         // verify we're on the list page
323
verifyListPage();
324
325         // ensure, that roles have been assigned
326
userTable = (HtmlTable) evaluateXPath("//table[@id='userList']", lastResponse);
327         Iterator JavaDoc rowsIterator = userTable.getRows().iterator();
328         while ( rowsIterator.hasNext() ) {
329             HtmlTableRow row = (HtmlTableRow) rowsIterator.next();
330             if ( row.getCells().size() > 0 ) {
331                 HtmlTableCell cell = row.getCell(0);
332                 if ( cell.getFirstChild().getNodeValue().equals("user") ) {
333                     // sixth cell should contain Users and Managers substrings
334
cell = row.getCell(5);
335                     String JavaDoc text = cell.getFirstChild().getNodeValue();
336                     assertTrue("User 'user' must be a member of both Users and Managers groups", text.indexOf("Users") >= 0 && text.indexOf("Managers") >= 0);
337                     break;
338                 }
339             }
340         }
341     }
342
343
344     public void testUpdateUser() throws Exception JavaDoc {
345         // log into the application
346
login();
347
348         // request page with list of users and verify we've got it
349
invoke("/core/user/list.do");
350         verifyListPage();
351
352         // following code is only valid, when there is only one page in the list of users
353
// it is one of the reasons, why testing should be performed on specially prepared data
354
// if you run this test in the production mode, beware: unexpected errors can occur
355

356         HtmlTable userTable = (HtmlTable) evaluateXPath("//table[@id='userList']", lastResponse);
357
358         // get all anchors, contained in table
359
List JavaDoc anchors = userTable.getHtmlElementsByTagName("a");
360
361         // search for one, which can be used to
362
String JavaDoc updateUri = "/core/user/callUpdate";
363         String JavaDoc userNameParam = "name=user";
364         Iterator JavaDoc anchorsIterator = anchors.iterator();
365         HtmlAnchor updateAnchor = null;
366         while ( anchorsIterator.hasNext() && updateAnchor == null ) {
367             HtmlAnchor anchor = (HtmlAnchor) anchorsIterator.next();
368             String JavaDoc href = anchor.getHrefAttribute();
369             if ( href.indexOf(updateUri) >= 0 ) {
370                 if ( href.indexOf(userNameParam) > 0 ) {
371                     updateAnchor = anchor;
372                 }
373             }
374         }
375
376         assertNotNull("No link could be found to get page with form for updating user 'user'", updateAnchor);
377
378         // go to update page and verify, we've got it
379
setLastResponse((HtmlPage) updateAnchor.click());
380         verifyUpdatePage();
381
382         String JavaDoc newFirstName = "I'm a Guest";
383
384         HtmlForm updateForm = getFormByName("userForm");
385         HtmlTextInput firstNameInput = (HtmlTextInput) getInputByName(updateForm, "firstName", INPUT_TYPE_TEXT);
386         firstNameInput.setValueAttribute(newFirstName);
387
388         // submit form
389
clickButtonWithLabel(updateForm, getMessage("core.commons.buttons.update"));
390
391         // verify we're on the list
392
verifyListPage();
393
394         // ensure, that update was successfull
395
userTable = (HtmlTable) evaluateXPath("//table[@id='userList']", lastResponse);
396         Iterator JavaDoc rowsIterator = userTable.getRows().iterator();
397         while ( rowsIterator.hasNext() ) {
398             HtmlTableRow row = (HtmlTableRow) rowsIterator.next();
399             if ( row.getCells().size() > 0 ) {
400                 HtmlTableCell cell = row.getCell(0);
401                 if ( cell.getFirstChild().getNodeValue().equals("user") ) {
402                     // second cell should contain updated first name
403
cell = row.getCell(1);
404                     String JavaDoc text = cell.getFirstChild().getNodeValue();
405                     assertTrue("First name of user 'user' must be equal to '" + newFirstName + "'", text.equals(newFirstName));
406                     break;
407                 }
408             }
409         }
410     }
411
412 }
413
Popular Tags