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.TargetActions;
19  import org.figure8.join.businessobjects.commons.Target;
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 TargetForm 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 TargetFormTest extends JoinFormTestCase{
30  
31     // Attributes ---------------------------------------------------------------
32  
33     /** The TargetForm to test. */
34     protected TargetForm form = new TargetForm();
35  
36  
37     // Public -------------------------------------------------------------------
38  
39     /** Test form sucessfull validation */
40     public void testValidationSuccess(){
41        // Load target operation.
42        setOperation(TargetActions.LOAD_OP);
43        form.setName("name");
44        ActionErrors errors = validateForm();
45        assertTrue("Load operation validation sucessfull", errors.isEmpty());
46        // Save target operation.
47        setOperation(TargetActions.SAVE_OP);
48        form.setName("name");
49        form.setDescription("description");
50        errors = validateForm();
51        assertTrue("Save operation validation sucessfull", errors.isEmpty());
52     }
53  
54     /** Test different form validation failure reason */
55     public void testValidationFailures(){
56        // Load target operation.
57        setOperation(TargetActions.LOAD_OP);
58        ActionErrors errors = validateForm();
59        assertEquals("Load op. validation fails with no name", 1, errors.size());
60        assertEquals("Load op. validation fails with required name", "errors.required[label.target.name]", errors.get().next().toString());
61        // Save target operation.
62        setOperation(TargetActions.SAVE_OP);
63        errors = validateForm();
64        assertEquals("Load op. validation fails with no param", 2, errors.size());
65        // Test with name.
66        form.setName("name");
67        errors = validateForm();
68        assertEquals("Load op. validation fails with no description", 1, errors.size());
69        assertEquals("Load op. validation fails with required name", "errors.required[label.target.description]", errors.get().next().toString());
70        // Test with description.
71        form.setName(null);
72        form.setDescription("description");
73        errors = validateForm();
74        assertEquals("Load op. validation fails with no name", 1, errors.size());
75        assertEquals("Load op. validation fails with required name", "errors.required[label.target.name]", errors.get().next().toString());
76     }
77  
78     /** Test the copy of properties from domain object to form */
79     public void testPropertiesCopy(){
80        // Create a new Target.
81        Target target = new Target("name", "description");
82        try{
83           // Copy into form the target properties.
84           PropertyUtils.copyProperties(form, target);
85        }
86        catch (Exception e){
87           fail("The properties copy failed with: " + e.getMessage());
88        }
89        // Assert.
90        assertEquals("Name of form is the same as Target", "name", form.getName());
91        assertEquals("Description of form is the same as Target", "description", form.getDescription());
92     }
93  
94  
95     // Implementation of JoinFormTestCase ---------------------------------------
96  
97     /** @return A {@link TargetForm} instance */
98     public JoinForm getFormToValidate(){
99        return form;
100    }
101 }