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.DeliverableActions;
19  import org.figure8.join.businessobjects.artifact.Deliverable;
20  import org.figure8.join.businessobjects.artifact.DeliverableType;
21  import org.figure8.join.businessobjects.commons.Release;
22  import org.figure8.join.util.JoinFormTestCase;
23  
24  import org.apache.struts.action.ActionErrors;
25  import org.apache.commons.beanutils.PropertyUtils;
26  
27  import java.util.Date;
28  /**
29   * This is a test case for testing DeliverableForm validation and setter methods.
30   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31   * @version $Revision: 1.1 $
32   */
33  public class DeliverableFormTest extends JoinFormTestCase{
34  
35     // Attributes ---------------------------------------------------------------
36  
37     /** The DeliverableForm to test */
38     protected DeliverableForm form = new DeliverableForm();
39  
40  
41     // Public -------------------------------------------------------------------
42  
43     /** Test form sucessfull validation */
44     public void testValidationSuccess(){
45        // Load deliverable operation.
46        setOperation(DeliverableActions.LOAD_OP);
47        form.setKey("key");
48        ActionErrors errors = validateForm();
49        assertTrue("Load operation validation successfull", errors.isEmpty());
50        // Prepare form operation.
51        setOperation(DeliverableActions.FORM_OP);
52        form.setTypeKey("typeKey");
53        errors = validateForm();
54        assertTrue("Prepare form operation validation successfull", errors.isEmpty());
55        // Save deliverable operation.
56        setOperation(DeliverableActions.SAVE_OP);
57        form.setTypeKey("typeKey");
58        form.setComments("comments");
59        form.setVersionInfo("version");
60        form.setReleaseName("releaseName");
61        errors = validateForm();
62        assertTrue("Save operation validation successfull", errors.isEmpty());
63     }
64  
65     /** Test different form validation failure reason */
66     public void testValidationFailures(){
67        // Load deliverable operation.
68        setOperation(DeliverableActions.LOAD_OP);
69        ActionErrors errors = validateForm();
70        assertEquals("Load op. validation fails with no params", 1, errors.size());
71        assertEquals("Load op. validation fails with required key", "errors.required[label.deliverable.key]", errors.get().next().toString());
72        // Prepare form operation.
73        setOperation(DeliverableActions.FORM_OP);
74        errors = validateForm();
75        assertEquals("Prepare form op. validation fails with no params", 1, errors.size());
76        assertEquals("Prepare form op. validation fails with required type", "errors.required[label.deliverable.type]", errors.get().next().toString());
77        // Save deliverable operation.
78        setOperation(DeliverableActions.SAVE_OP);
79        errors = validateForm();
80        assertEquals("Save op. validation fails with no params", 4, errors.size());
81     }
82  
83     /** Test the copy of properties from domain object to form */
84     public void testPropertiesCopy(){
85        // Create a new Deliverable.
86        Release release = new Release("1.0", 1, 0, new Date());
87        DeliverableType type = new DeliverableType("key", "label", "key-r{0}-v{1}", true, true);
88        Deliverable deliverable = new Deliverable("01", "comments", "supplierId", release, type);
89        try{
90           // Copy into form the consumer properties.
91           PropertyUtils.copyProperties(form, deliverable);
92        }
93        catch (Exception e){
94           fail("The properties copy failed with: " + e.getMessage());
95        }
96        // Assert.
97        assertEquals("Key of form is the same as deliverable", deliverable.getKey(), form.getKey());
98        assertEquals("Version of form is the same as deliverable", deliverable.getVersionInfo(), form.getVersionInfo());
99        assertEquals("Comments of form is the same as deliverable", deliverable.getComments(), form.getComments());
100       assertEquals("Suuplier of form is the same as deliverable", deliverable.getSupplierId(), form.getSupplierId());
101    }
102 
103 
104    // Implementation of JoinFormTestCase ---------------------------------------
105 
106    /** @return A {@link DeliverableTypeForm} instance */
107    public JoinForm getFormToValidate(){
108       return form;
109    }
110 }