1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.form;
16
17 import org.figure8.join.control.JoinForm;
18 import org.figure8.join.control.action.UserActions;
19 import org.figure8.join.control.action.CreateUserAction;
20 import org.figure8.join.businessobjects.security.User;
21 import org.figure8.join.util.JoinFormTestCase;
22
23 import org.apache.commons.beanutils.PropertyUtils;
24 import org.apache.struts.action.ActionErrors;
25 /**
26 * This is a test case for testing UserForm validation and setter methods.
27 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28 * @version $Revision: 1.1 $
29 */
30 public class UserFormTest extends JoinFormTestCase{
31
32
33
34 /** The UserForm to test */
35 protected UserForm form = new UserForm();
36
37
38
39
40 /** Test form sucessfull validation */
41 public void testValidationSuccess(){
42
43 setOperation(UserActions.LOAD_OP);
44 form.setLogin("login");
45 ActionErrors errors = validateForm();
46 assertTrue("Load operation validation successfull", errors.isEmpty());
47
48 setOperation(CreateUserAction.CREATE_OP);
49 form.setLogin("login");
50 form.setPassword("password");
51 form.setPassword2("password");
52 form.setLastname("lastname");
53 form.setFirstname("firstname");
54 errors = validateForm();
55 assertTrue("Create operation validation sucessfull", errors.isEmpty());
56 }
57
58 /** Test different form validation failure reason */
59 public void testValidationFailures(){
60
61 setOperation(UserActions.LOAD_OP);
62 ActionErrors errors = validateForm();
63 assertEquals("Load op. validation fails with no params", 1, errors.size());
64 assertEquals("Load op. validation fails with required login", "errors.required[label.user.login]", errors.get().next().toString());
65
66 setOperation(CreateUserAction.CREATE_OP);
67 errors = validateForm();
68 assertEquals("Create op. validation fails with no params", 5, errors.size());
69 assertEquals("Create op. validation fails with required login", "errors.required[label.user.login]", errors.get().next().toString());
70
71 form.setLogin("login");
72 form.setPassword("password");
73 form.setLastname("lastname");
74 form.setFirstname("firstname");
75 form.setPassword2("pwd");
76 errors = validateForm();
77 assertEquals("Create op. validation fails with different confirmation", 1, errors.size());
78 assertEquals("Create op. validation fails with different confirmation", "errors.different[label.user.cpassword, label.user.password]", errors.get().next().toString());
79 }
80
81 /** Test the copy of properties from domain object to form */
82 public void testPropertiesCopy(){
83
84 User user = new User("login", "password", "lastname", "firstname");
85 try{
86
87 PropertyUtils.copyProperties(form, user);
88 }
89 catch (Exception e){
90 fail("The properties copy failed with: " + e.getMessage());
91 }
92
93 assertEquals("Login of the form is the same as User", "login", form.getLogin());
94 assertEquals("Password of the form is the same as User", "password", form.getPassword());
95 assertEquals("Lastname of the form is the same as User", "lastname", form.getLastname());
96 assertEquals("Firstname of the form is the same as User", "firstname", form.getFirstname());
97 }
98
99
100
101
102 /** @return A {@link UserForm} instance */
103 public JoinForm getFormToValidate(){
104 return form;
105 }
106 }