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