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.control.JoinForm;
19  import org.figure8.join.control.action.ReleaseActions;
20  import org.figure8.join.businessobjects.commons.Release;
21  
22  import org.apache.commons.beanutils.PropertyUtils;
23  import org.apache.struts.action.ActionErrors;
24  
25  import java.util.Date;
26  import java.text.SimpleDateFormat;
27  /**
28   * This is a test case for testing ReleaseForm validation and setter methods.
29   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
30   * @version $Revision: 1.1 $
31   */
32  public class ReleaseFormTest extends JoinFormTestCase{
33  
34     // Attributes ---------------------------------------------------------------
35  
36     /** The ReleaseForm to test */
37     protected ReleaseForm form = new ReleaseForm();
38  
39  
40     // Public -------------------------------------------------------------------
41  
42     /** Test form sucessfull validation */
43     public void testValidationSuccess(){
44        // Load release operation.
45        setOperation(ReleaseActions.LOAD_OP);
46        form.setName("name");
47        ActionErrors errors = validateForm();
48        assertTrue("Load operation validation sucessfull", errors.isEmpty());
49        // Save release operation.
50        setOperation(ReleaseActions.SAVE_OP);
51        form.setName("name");
52        form.setMajorStr("1");
53        form.setMinorStr("0");
54        form.setShippingDateStr("19/07/2005");
55        errors = validateForm();
56        assertTrue("Save operation validation successfull", errors.isEmpty());
57     }
58  
59     /** Test different form validation failure reason */
60     public void testValidationFailures(){
61        // Load release operation.
62        setOperation(ReleaseActions.LOAD_OP);
63        ActionErrors errors = validateForm();
64        assertEquals("Load op. validation fails with no name", 1, errors.size());
65        assertEquals("Load op. validation fails with required name", "errors.required[label.release.name]", errors.get().next().toString());
66        // Save release operation.
67        setOperation(ReleaseActions.SAVE_OP);
68        errors = validateForm();
69        assertEquals("Save op. validation fails with no params", 4, errors.size());
70        // Test string numbers.
71        form.setName("name");
72        form.setMinorStr("0");
73        form.setMajorStr("majorStr");
74        errors = validateForm();
75        assertEquals("Save op. validation fails with no int major", "errors.integer[label.release.major]", errors.get().next().toString());
76     }
77  
78     /** Test the copy of properties from domain object to form */
79     public void testPropertiesCopy(){
80        Date today = new Date();
81        // Create a new release.
82        Release release = new Release("name", 1, 0, today);
83        try{
84           // Copy into form the step properties.
85           PropertyUtils.copyProperties(form, release);
86        }
87        catch (Exception e){
88           fail("The properties copy failed with: " + e.getMessage());
89        }
90        // Assert.
91        assertEquals("Name of form is the same as Release", "name", form.getName());
92        assertEquals("Major of form is the same as Release", 1, form.getMajor());
93        assertEquals("Minor of form is the same as Release", 0, form.getMinor());
94        assertEquals("Date of form is the same as Release", today.getTime(), form.getShippingDate().getTime());
95        assertEquals("Major string of form is the same as Release", "1", form.getMajorStr());
96        assertEquals("Minor string of form is the same as Release", "0", form.getMinorStr());
97        assertEquals("Date string of form is the same as Release", new SimpleDateFormat("dd/MM/yyyy").format(today), form.getShippingDateStr());
98     }
99  
100 
101    // Implementation of JoinFormTestCase ---------------------------------------
102 
103    /** @return A {@link ReleaseForm} instance */
104    public JoinForm getFormToValidate(){
105       return form;
106    }
107 }