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.core.messaging.persistence;
16  
17  import org.figure8.join.util.SpringTestCase;
18  import org.figure8.join.core.InvalidParameterException;
19  import org.figure8.join.core.messaging.JMSConsumerBeanInfo;
20  import org.figure8.join.core.messaging.EchoJMSConsumerBean;
21  import org.figure8.join.core.messaging.JMSConsumerBeanParameterInfo;
22  /**
23   * JUnit test case for testing JMSConsumerBeanInfo dao implementation.
24   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
25   * @version $Revision: 1.2 $
26   */
27  public class JMSConsumerBeanInfoDaoTest extends SpringTestCase{
28  
29     // Static -------------------------------------------------------------------
30  
31     /** Spring configuration files */
32     private String[] configLocations = new String[]{"classpath:/org/figure8/join/core/messaging/persistence/spring.xml"};
33  
34  
35     // Attributes ---------------------------------------------------------------
36  
37     /** The JMSConsumerBeanInfo dao to test. */
38     protected JMSConsumerBeanInfoDao dao = null;
39  
40  
41     // Override of SpringTestCase -----------------------------------------------
42  
43     /** Retrieve JMSConsumerBeanInfo dao after having initialized context */
44     public void setUp(){
45        super.setUp();
46        // Get JMSConsumerBeanInfo dao.
47        dao = (JMSConsumerBeanInfoDao)context.getBean("jmsConsumerDao");
48     }
49  
50  
51     // Public -------------------------------------------------------------------
52  
53     /** Test consumer info creation */
54     public void testConsumerInfoCreation(){
55        try{
56           // Save a new consumer info.
57           JMSConsumerBeanInfo info = new JMSConsumerBeanInfo("creation", null,
58                   "myDestination", "org.figure8.join.core.messaging.EchoJMSConsumerBean");
59           int size = getConsumerInfoListSize();
60           dao.save(info);
61           // Assert there's one more consumer info.
62           assertEquals("JMSConsumerBeanInfo is successfully created", size + 1, dao.findAll().size());
63        }
64        catch (InvalidParameterException e){
65           fail("No InvalidParameterException should have been thrown ...");
66        }
67     }
68  
69     /** Test the getByName() method */
70     public void testFindByName(){
71        try{
72           // Save a bunch of new consumer infos.
73           JMSConsumerBeanInfo info1 = new JMSConsumerBeanInfo("name1", null,
74                   "myDestination1", "org.figure8.join.core.messaging.EchoJMSConsumerBean");
75           JMSConsumerBeanInfo info2 = new JMSConsumerBeanInfo("name2", null,
76                   "myDestination2", "org.figure8.join.core.messaging.EchoJMSConsumerBean");
77           JMSConsumerBeanParameterInfo param1 = new JMSConsumerBeanParameterInfo("name1", "value1");
78           JMSConsumerBeanParameterInfo param2 = new JMSConsumerBeanParameterInfo("name2", "value2");
79           info2.addConsumerParameterInfo(param1);
80           info2.addConsumerParameterInfo(param2);
81           int size = getConsumerInfoListSize();
82           dao.save(info1);
83           dao.save(info2);
84           // Assert there's two more consumer infos.
85           assertEquals("JMSConsumerBeanInfo are successfully created", size + 2, dao.findAll().size());
86           // Retrieve info using name.
87           JMSConsumerBeanInfo foundInfo = dao.getJMSConsumerBeanInfo("name2");
88           assertNotNull("Found an info", foundInfo);
89           assertEquals("Found an info with correct name", "name2", foundInfo.getName());
90           assertEquals("Found an info with correct selector", null, foundInfo.getSelector());
91           assertEquals("Found an info with correct destination", "myDestination2", foundInfo.getDestination());
92           assertTrue("Found an info with correct consumer", foundInfo.getJMSConsumerBean() instanceof EchoJMSConsumerBean);
93           assertEquals("Found an info with correct consumer params", 2, foundInfo.getConsumerParameterInfos().size());
94        }
95        catch (InvalidParameterException e){
96           fail("No InvalidParameterException should have been thrown ...");
97        }
98     }
99  
100    /** Test consumer info removal */
101    public void testConsumerInfoRemoval(){
102       try{
103          // Save a new consumer info.
104          JMSConsumerBeanInfo info = new JMSConsumerBeanInfo("removal", null,
105                  "myDestination", "org.figure8.join.core.messaging.EchoJMSConsumerBean");
106          int size = getConsumerInfoListSize();
107          dao.save(info);
108          // Assert there's one more consumer info.
109          assertEquals("JMSConsumerBeanInfo is successfully created", size + 1, dao.findAll().size());
110          // Remove and assert.
111          dao.remove(info);
112          assertEquals("JMSConsumerBeanInfo is successfully removed", size, dao.findAll().size());
113       }
114       catch (InvalidParameterException e){
115          fail("No InvalidParameterException should have been thrown ...");
116       }
117    }
118 
119 
120    // Protected ----------------------------------------------------------------
121 
122    /** Test the findAll on consumer */
123    protected int getConsumerInfoListSize(){
124       return dao.findAll().size();
125    }
126 
127 
128    // Implementation of SpringTestCase -----------------------------------------
129 
130    /** @return configLocations inner field */
131    public String[] getConfigLocations(){
132       return configLocations;
133    }
134 }