1   /*
2    * $Id: JmsConnectionFactoryTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.jms;
12  
13  import org.mule.MuleManager;
14  import org.mule.tck.AbstractMuleTestCase;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import javax.jms.Connection;
20  import javax.jms.ConnectionFactory;
21  import javax.jms.JMSException;
22  import javax.jms.QueueConnection;
23  import javax.jms.QueueConnectionFactory;
24  
25  public class JmsConnectionFactoryTestCase extends AbstractMuleTestCase
26  {
27  
28      /**
29       * Test providerProperties set on JmsConnector are not passed to the underlying
30       * ConnectionFactory.
31       */
32      public void testProviderPropertiesNotPassed() throws Exception
33      {
34          // needed so that when the connecotr is initialsied the manage eventing
35          // model will be available. This is used by Jms for reconnection
36          // notifications
37          ((MuleManager)MuleManager.getInstance()).initialise();
38  
39          JmsConnector connector = new JmsConnector();
40          Map providerProperties = new HashMap(1);
41          final String testProviderProperty = "providerProperty";
42          final String testValue = "TEST_VALUE";
43          providerProperties.put(testProviderProperty, testValue);
44          connector.setJndiProviderProperties(providerProperties);
45  
46          ConnectionFactory cf = new TestConnectionFactory();
47          connector.setConnectionFactory(cf);
48  
49          connector.initialise();
50  
51          assertEquals("Provider properties should not be passed to the ConnectionFactory.", "NOT_SET",
52              ((TestConnectionFactory)cf).getProviderProperty());
53      }
54  
55      /**
56       * Test connectionFactoryProperties set on JmsConnector are actually passed to
57       * the underlying ConnectionFactory.
58       */
59      public void testConnectionFactoryPropertiesPassed() throws Exception
60      {
61          // needed so that when the connecotr is initialsied the manage eventing
62          // model will be available. This is used by Jms for reconnection
63          // notifications
64          ((MuleManager)MuleManager.getInstance()).initialise();
65  
66          JmsConnector connector = new JmsConnector();
67          Map connectionFactoryProperties = new HashMap(1);
68          final String testConnectionFactoryProperty = "connectionFactoryProperty";
69          final String testValue = "TEST_VALUE";
70          connectionFactoryProperties.put(testConnectionFactoryProperty, testValue);
71          connector.setConnectionFactoryProperties(connectionFactoryProperties);
72  
73          ConnectionFactory cf = new TestConnectionFactory();
74          connector.setConnectionFactory(cf);
75  
76          connector.initialise();
77          connector.startConnector();
78  
79          assertEquals("ConnectionFactory properties should be passed to the ConnectionFactory.", "TEST_VALUE",
80              ((TestConnectionFactory)cf).getConnectionFactoryProperty());
81      }
82  
83      public static final class TestConnectionFactory implements QueueConnectionFactory
84      {
85          private String providerProperty = "NOT_SET";
86          private String connectionFactoryProperty = "NOT_SET";
87  
88          public Connection createConnection() throws JMSException
89          {
90              return null;
91          }
92  
93          public Connection createConnection(String string, String string1) throws JMSException
94          {
95              return null;
96          }
97  
98          public String getProviderProperty()
99          {
100             return providerProperty;
101         }
102 
103         /**
104          * Should NOT be called.
105          */
106         public void setProviderProperty(final String providerProperty)
107         {
108             throw new IllegalStateException("Should never be called.");
109         }
110 
111         public String getConnectionFactoryProperty()
112         {
113             return connectionFactoryProperty;
114         }
115 
116         /**
117          * MUST be called
118          */
119         public void setConnectionFactoryProperty(final String connectionFactoryProperty)
120         {
121             this.connectionFactoryProperty = connectionFactoryProperty;
122         }
123 
124         public QueueConnection createQueueConnection() throws JMSException
125         {
126             return null;
127         }
128 
129         public QueueConnection createQueueConnection(String string, String string1) throws JMSException
130         {
131             return null;
132         }
133     }
134 
135 }