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;
16  
17  import junit.framework.Test;
18  import junit.framework.TestCase;
19  import junit.framework.TestSuite;
20  
21  import org.jencks.TargetSourceMessageListener;
22  import org.apache.commons.pool.PoolableObjectFactory;
23  
24  import javax.jms.MessageListener;
25  import javax.resource.spi.UnavailableException;
26  /**
27   * JUnit test case for the custom JCA message endpoint factory.
28   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
29   * @version $Revision: 1.2 $
30   */
31  public class JMSConsumerEndpointFactoryTest extends TestCase{
32  
33     // Attributes ---------------------------------------------------------------
34  
35     /** The bean wrapping JMS consumer info used in tests. */
36     protected JMSConsumerBeanInfo consumerInfo = null;
37  
38  
39     // Constructors -------------------------------------------------------------
40  
41     /** Default constructor. Build a test case using a name. */
42     public JMSConsumerEndpointFactoryTest(String testName){
43        super(testName);
44     }
45  
46  
47     // Override of TestCase -----------------------------------------------------
48  
49     /**
50      * Make this class a TestSuite.
51      * @return A TestSuite containing this TestCase.
52      */
53     public static Test suite(){
54        TestSuite suite = new TestSuite(JMSConsumerEndpointFactoryTest.class);
55        return suite;
56     }
57     
58     
59     // Public -------------------------------------------------------------------
60  
61     /** Setup test fixtures. */
62     public void setUp() throws Exception{
63        // Initialize bean info.
64        consumerInfo = new JMSConsumerBeanInfo();
65        consumerInfo.setName("test");
66        consumerInfo.setDestination("testTopic");
67        consumerInfo.setConsumerBeanClass("org.figure8.join.core.messaging.EchoJMSConsumerBean");
68     }
69  
70     /** Test the creation of a MessageListener for thread safe consumer. */
71     public void testThreadSafeMessageListenerCreation(){
72        // Set safe property on consumer info.
73        consumerInfo.setThreadSafe(true);
74        // Build an endpoint factory for creating listener.
75        JMSConsumerEndpointFactory factory = new JMSConsumerEndpointFactory(consumerInfo);
76        MessageListener listener = null;
77        // Create the message listener.
78        try {listener = factory.createMessageListener();}
79        catch (UnavailableException ue){
80           fail("Cannot create listener: " + ue.getMessage());
81        }
82        // Assert.
83        assertTrue("TS listener is created as is", listener instanceof EchoJMSConsumerBean);
84        assertFalse("TS listener is not wrapped into a TargetSource", listener instanceof TargetSourceMessageListener);
85     }
86  
87     /** Test the creation of a MessageListener for non thread safe consumer. */
88     public void testNonThreadSafeMessageListenerCreation(){
89        // Set safe property on consumer info.
90        consumerInfo.setThreadSafe(false);
91        // Build an endpoint factory for creating listener.
92        JMSConsumerEndpointFactory factory = new JMSConsumerEndpointFactory(consumerInfo);
93        MessageListener listener = null;
94        // Create the message listener.
95        try {listener = factory.createMessageListener();}
96        catch (UnavailableException ue){
97           fail("Cannot create listener: " + ue.getMessage());
98        }
99        // Assert.
100       assertTrue("NTS listener is wrapped into a TargetSource", listener instanceof TargetSourceMessageListener);
101       assertTrue("TargetSource is a poolable object factory", ((TargetSourceMessageListener)listener).getTargetSource()
102               instanceof PoolableObjectFactory);
103       // Check that returned object is correct with good name.
104       PoolableObjectFactory pool = (PoolableObjectFactory)((TargetSourceMessageListener)listener).getTargetSource();
105       try{
106          Object obj = pool.makeObject();
107          assertTrue("Returned object is a EchoJMSConsumerBean", obj instanceof EchoJMSConsumerBean);
108          assertEquals("Returned object has correct name", "test", ((EchoJMSConsumerBean)obj).getName());
109       }
110       catch (Exception e){
111          fail("Retrieved object in pool is not correct: " + e.getMessage());
112       }
113    }
114 }