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.util.JoinFormTestCase;
18  import org.figure8.join.businessobjects.environment.Machine;
19  import org.figure8.join.control.JoinForm;
20  import org.figure8.join.control.action.MachineActions;
21  
22  import org.apache.commons.beanutils.PropertyUtils;
23  import org.apache.struts.action.ActionErrors;
24  /**
25   * JUnit test case for testing MachineForm 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 MachineFormTest extends JoinFormTestCase{
30  
31     // Attributes ---------------------------------------------------------------
32  
33     /** The MachineForm to test. */
34     protected MachineForm form = new MachineForm();
35  
36  
37     // Public -------------------------------------------------------------------
38  
39     /** Test form sucessfull validation */
40     public void testValidationSuccess(){
41        // Load machine operation.
42        setOperation(MachineActions.LOAD_OP);
43        form.setId(123456);
44        ActionErrors errors = validateForm();
45        assertTrue("Load operation validation sucessfull", errors.isEmpty());
46        // Save machine operation.
47        setOperation(MachineActions.SAVE_OP);
48        form.setName("localhost");
49        form.setIpAddress("127.0.0.1");
50        form.setCpuInfo("AMD Athlon XP");
51        form.setOsInfo("KUbuntu Linux 5.0.4");
52        form.setRamAmountStr("1024");
53        form.setRomAmountStr("80");
54        errors = validateForm();
55        assertTrue("Save operation validation sucessfull", errors.isEmpty());
56        assertEquals("Save operation converts RAM amount", 1024, form.getRamAmount());
57        assertEquals("Save operation converts ROM amount", 80, form.getRomAmount());
58     }
59  
60     /** Test different form validation failure reason */
61     public void testValidationFailures(){
62        // Load machine operation.
63        setOperation(MachineActions.LOAD_OP);
64        ActionErrors errors = validateForm();
65        assertEquals("Load op. validation fails with no id", 1, errors.size());
66        assertEquals("Load op. validation fails with required id", "errors.required[label.global.id]", errors.get().next().toString());
67        // Save machine operation.
68        setOperation(MachineActions.SAVE_OP);
69        errors = validateForm();
70        assertEquals("Save op. validation fails with no param", 2, errors.size());
71        // Test string number.
72        form.setName("localhost");
73        form.setIpAddress("127.0.0.1");
74        form.setRamAmountStr("abc");
75        errors = validateForm();
76        assertEquals("Save op. validation fails with no RAM amount int", "errors.integer[label.machine.ramamount]", errors.get().next().toString());
77        // Test string number.
78        form.setRamAmountStr("1024");
79        form.setRomAmountStr("def");
80        errors = validateForm();
81        assertEquals("Save op. validation fails with no ROM amount int", "errors.integer[label.machine.romamount]", errors.get().next().toString());
82     }
83  
84     /** Test the copy of properties from domain object to form */
85     public void testPropertiesCopy(){
86        // Create a new Machine.
87        Machine machine = new Machine("localhost", "127.0.0.1");
88        machine.setCpuInfo("AMD Athlon XP");
89        machine.setOsInfo("KUbuntu Linux 5.0.4");
90        machine.setRamAmount(1024);
91        machine.setRomAmount(80);
92        try{
93           // Copy into form the step properties.
94           PropertyUtils.copyProperties(form, machine);
95        }
96        catch (Exception e){
97           fail("The properties copy failed with: " + e.getMessage());
98        }
99        // Assert.
100       assertEquals("Name of form is the same as Machine", "localhost", form.getName());
101       assertEquals("IP of form is the same as Machine", "127.0.0.1", form.getIpAddress());
102    }
103 
104 
105    // Implementation of JoinFormTestCase ---------------------------------------
106 
107    /** @return A {@link MachineForm} instance */
108    public JoinForm getFormToValidate(){
109       return form;
110    }
111 }