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.PermissionActions;
19 import org.figure8.join.businessobjects.security.Role;
20 import org.figure8.join.util.JoinFormTestCase;
21
22 import org.apache.commons.beanutils.PropertyUtils;
23 import org.apache.struts.action.ActionErrors;
24 /**
25 * This is a test case for testing PermissionForm validation and setter methods.
26 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
27 * @version $Revision: 1.1 $
28 */
29 public class PermissionFormTest extends JoinFormTestCase{
30
31
32
33 /** The PermissionForm to test. */
34 protected PermissionForm form = new PermissionForm();
35
36
37
38
39 /** Test form sucessfull validation */
40 public void testValidationSuccess(){
41
42 setOperation(PermissionActions.LOAD_ROLE_OP);
43 form.setIdStr("123456");
44 ActionErrors errors = validateForm();
45 assertTrue("Load operation validation sucessfull", errors.isEmpty());
46
47 setOperation(PermissionActions.SAVE_ROLE_OP);
48 form.setName("name");
49 form.setDescription("description");
50 form.setPermissionResourceResolverClass("");
51 errors = validateForm();
52 assertTrue("Save operation validation sucessfull", errors.isEmpty());
53
54 form.setName("name");
55 form.setDescription("description");
56 form.setPermissionResourceResolverClass("org.figure8.join.services.security.PermissionResourceResolverStub");
57 errors = validateForm();
58 assertTrue("Save operation validation sucessfull", errors.isEmpty());
59
60
61 setOperation(PermissionActions.LOAD_PERMISSIONS_OP);
62 form.setUserId("userId");
63 errors = validateForm();
64 assertTrue("Load permissions op. validation successfull", errors.isEmpty());
65 }
66
67 /** Test different form validation failure reason */
68 public void testValidationFailures(){
69
70 setOperation(PermissionActions.LOAD_ROLE_OP);
71 ActionErrors errors = validateForm();
72 assertEquals("Load op. validation fails with no id", 1, errors.size());
73
74 setOperation(PermissionActions.SAVE_ROLE_OP);
75 errors = validateForm();
76 assertEquals("Save op. validation fails with no params", 2, errors.size());
77
78
79 setOperation(PermissionActions.LOAD_PERMISSIONS_OP);
80 errors = validateForm();
81 assertEquals("Load op. validation fails with no params", 1, errors.size());
82 }
83
84 /** Test the copy of properties from domain object to form */
85 public void testPropertiesCopy(){
86
87 Role role = new Role("name", "description");
88 try{
89 role.setPermissionResourceResolverClass("org.figure8.join.services.security.PermissionResourceResolverStub");
90
91 PropertyUtils.copyProperties(form, role);
92 }
93 catch (Exception e){
94 fail("The properties copy failed with: " + e.getMessage());
95 }
96
97 assertEquals("Name of form is the same as Role", role.getName(), form.getName());
98 assertEquals("Description of form is the same as Role", role.getDescription(), form.getDescription());
99 assertEquals("Resolver of form is the same as Role", role.getPermissionResourceResolverClass(), form.getPermissionResourceResolverClass());
100 }
101
102
103
104
105 /**
106 * Return an instance of PermissionForm.
107 * @return The JoinForm instance for testing validation
108 */
109 public JoinForm getFormToValidate(){
110 return form;
111 }
112 }