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.StepActions;
19 import org.figure8.join.businessobjects.commons.Step;
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 StepForm 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 StepFormTest extends JoinFormTestCase{
30
31
32
33 /** The StepForm to test */
34 protected StepForm form = new StepForm();
35
36
37
38
39 /** Test form sucessfull validation */
40 public void testValidationSuccess(){
41
42 setOperation(StepActions.LOAD_OP);
43 form.setIdStr("1");
44 ActionErrors errors = validateForm();
45 assertTrue("Load operation validation sucessfull", errors.isEmpty());
46 assertEquals("Validation convert string to long", 1, form.getId());
47
48 setOperation(StepActions.SAVE_OP);
49 form.setLabel("label");
50 form.setPositionStr("1");
51 errors = validateForm();
52 assertTrue("Save operation validation sucessfull", errors.isEmpty());
53 assertEquals("Validation convert string to int", 1, form.getPosition());
54 }
55
56 /** Test different form validation failure reason */
57 public void testValidationFailures(){
58
59 setOperation(StepActions.LOAD_OP);
60 ActionErrors errors = validateForm();
61 assertEquals("Load op. validation fails with no id", 1, errors.size());
62 assertEquals("Load op. validation fails with required id", "errors.required[label.global.id]", errors.get().next().toString());
63
64 setOperation(StepActions.SAVE_OP);
65 form.setLabel("label");
66 errors = validateForm();
67 assertEquals("Save op. validation fails with no position", 1, errors.size());
68 assertEquals("Save op. validation fails with required position", "errors.required[label.step.position]", errors.get().next().toString());
69
70 form.setPositionStr("pos");
71 errors = validateForm();
72 assertEquals("Save op. validation fails with no int position", 2, errors.size());
73 assertEquals("Save op. validation fails with no int position", "errors.integer[label.step.position]", errors.get().next().toString());
74
75 form.setPositionStr("-2");
76 errors = validateForm();
77 assertEquals("Save op. validation fails with negative position", 1, errors.size());
78 assertTrue("Save op. validation fails with negative position", errors.get().next().toString().indexOf("errors.range[label.step.position, 0") != -1);
79 }
80
81 /** Test the copy of properties from domain object to form */
82 public void testPropertiesCopy(){
83
84 Step step = new Step("label", 1);
85 try{
86
87 PropertyUtils.copyProperties(form, step);
88 }
89 catch (Exception e){
90 fail("The properties copy failed with: " + e.getMessage());
91 }
92
93 assertEquals("Label of form is the same as Step", "label", form.getLabel());
94 assertEquals("Position of form is the same as Step", 1, form.getPosition());
95 assertEquals("Position string of form is the same as Step", "1", form.getPositionStr());
96 }
97
98
99
100
101 /** @return A {@link StepForm} instance */
102 public JoinForm getFormToValidate(){
103 return form;
104 }
105 }