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.ConsumerActions;
19  import org.figure8.join.core.messaging.JMSConsumerBeanInfo;
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 ConsumerForm 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 ConsumerFormTest extends JoinFormTestCase{
30  
31     // Attributes ---------------------------------------------------------------
32  
33     /** The ConsumerForm to test. */
34     protected ConsumerForm form = new ConsumerForm();
35  
36  
37     // Public -------------------------------------------------------------------
38  
39     /** Test form sucessfull validation */
40     public void testValidationSuccess(){
41        // Load consumer operation.
42        setOperation(ConsumerActions.LOAD_OP);
43        form.setName("name");
44        ActionErrors errors = validateForm();
45        assertTrue("Load operation validation successfull", errors.isEmpty());
46        // Save consumer operation.
47        setOperation(ConsumerActions.SAVE_OP);
48        form.setName("name");
49        form.setSelector("selector");
50        form.setDestination("destination");
51        form.setConsumerBeanClass("org.figure8.join.core.messaging.EchoJMSConsumerBean");
52        form.setActive(true);
53        form.setThreadSafe(false);
54        errors = validateForm();
55        assertTrue("Save operation validation successfull", errors.isEmpty());
56     }
57  
58     /** Test different form validation failure reason */
59     public void testValidationFailures(){
60        // Load consumer operation.
61        setOperation(ConsumerActions.LOAD_OP);
62        ActionErrors errors = validateForm();
63        assertEquals("Load op. validation fails with no params", 1, errors.size());
64        assertEquals("Load op. validation fails with required name", "errors.required[label.consumer.name]", errors.get().next().toString());
65        // Save consumer operation.
66        setOperation(ConsumerActions.SAVE_OP);
67        errors = validateForm();
68        assertEquals("Save op. validation fails with no params", 3, errors.size());
69        assertEquals("Save op. validation fails with required name", "errors.required[label.consumer.name]", errors.get().next().toString());
70        // Test with name.
71        form.setName("name");
72        errors = validateForm();
73        assertEquals("Save op. validation fails with name only", 2, errors.size());
74        assertEquals("Save op. validation fails with required destination", "errors.required[label.consumer.destination]", errors.get().next().toString());
75        // Test with destination.
76        form.setDestination("destination");
77        errors = validateForm();
78        assertEquals("Save op. validation fails with destination", 1, errors.size());
79        assertEquals("Save op. validation fails with required consumer class", "errors.required[label.consumer.consumerBeanClass]", errors.get().next().toString());
80        // Test with bad consumer class.
81        form.setConsumerBeanClass("org.figure8.join.core.EntityObject");
82        errors = validateForm();
83        assertEquals("Save op. validation fails with bad implementation", "errors.badimplementation[label.consumer.consumerBeanClass]", errors.get().next().toString());
84        // Test with nof found consumer class.
85        form.setConsumerBeanClass("org.myorg.join.messaging.MyJMSConsumerBean");
86        errors = validateForm();
87        assertEquals("Save op. validation fails with class not found", "errors.classnotfound[label.consumer.consumerBeanClass]", errors.get().next().toString());
88     }
89  
90     /** Test the copy of properties from domain object to form */
91     public void testPropertiesCopy(){
92        // Create a new JMSConsumerBeanInfo.
93        JMSConsumerBeanInfo info = null;
94        try{
95           info = new JMSConsumerBeanInfo("name", "selector", "destination",
96                "org.figure8.join.core.messaging.EchoJMSConsumerBean");
97           info.setThreadSafe(true);
98           // Copy into form the consumer properties.
99           PropertyUtils.copyProperties(form, info);
100       }
101       catch (Exception e){
102          fail("The properties copy failed with: " + e.getMessage());
103       }
104       // Assert.
105       assertEquals("Name of form is the same as consumer", "name", form.getName());
106       assertEquals("Selector of form is the same as consumer", "selector", form.getSelector());
107       assertEquals("Destination of form is the same as consumer", "destination", form.getDestination());
108       assertEquals("ConsumerBeanClass of form is the same as consumer",
109               "org.figure8.join.core.messaging.EchoJMSConsumerBean", form.getConsumerBeanClass());
110       assertEquals("Active flag of form is the same as consumer", true, form.isActive());
111       assertEquals("ThreadSafe flag of form is the same as consumer", true, form.isThreadSafe());
112    }
113 
114 
115    // Implementation of JoinFormTestCase ---------------------------------------
116 
117    /** @return A {@link ConsumerForm} instance */
118    public JoinForm getFormToValidate(){
119       return form;
120    }
121 }