1   /*
2    * $Id: JmsNamespaceHandlerTestCase.java 11592 2008-04-18 10:09:22Z 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  package org.mule.transport.jms.config;
11  
12  import org.mule.api.endpoint.EndpointException;
13  import org.mule.api.endpoint.ImmutableEndpoint;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.routing.filter.Filter;
16  import org.mule.routing.filters.logic.NotFilter;
17  import org.mule.tck.FunctionalTestCase;
18  import org.mule.tck.testmodels.mule.TestTransactionFactory;
19  import org.mule.transport.jms.DefaultRedeliveryHandler;
20  import org.mule.transport.jms.DefaultRedeliveryHandlerFactory;
21  import org.mule.transport.jms.JmsConnector;
22  import org.mule.transport.jms.filters.JmsPropertyFilter;
23  import org.mule.transport.jms.filters.JmsSelectorFilter;
24  import org.mule.transport.jms.test.TestConnectionFactory;
25  import org.mule.transport.jms.test.TestRedeliveryHandler;
26  
27  import javax.jms.Session;
28  
29  
30  /**
31   * Tests the "jms" namespace.
32   */
33  public class JmsNamespaceHandlerTestCase extends FunctionalTestCase
34  {
35      public JmsNamespaceHandlerTestCase()
36      {
37          setStartContext(false);
38      }
39      
40      protected String getConfigResources()
41      {
42          return "jms-namespace-config.xml";
43      }
44  
45      public void testDefaultConfig() throws Exception
46      {
47          JmsConnector c = (JmsConnector)muleContext.getRegistry().lookupConnector("jmsConnectorDefaults");
48          assertNotNull(c);
49  
50          assertNotNull(c.getConnectionFactory());
51          assertTrue(c.getConnectionFactory() instanceof TestConnectionFactory);
52          assertEquals(Session.AUTO_ACKNOWLEDGE, c.getAcknowledgementMode());
53          assertNull(c.getUsername());
54          assertNull(c.getPassword());
55  
56          assertNotNull(c.getRedeliveryHandlerFactory());
57          assertTrue(c.getRedeliveryHandlerFactory() instanceof DefaultRedeliveryHandlerFactory);
58          assertTrue(c.getRedeliveryHandlerFactory().create() instanceof DefaultRedeliveryHandler);
59          
60          assertNull(c.getClientId());
61          assertFalse(c.isDurable());
62          assertFalse(c.isNoLocal());
63          assertFalse(c.isPersistentDelivery());
64          assertEquals(0, c.getMaxRedelivery());
65          assertFalse(c.isCacheJmsSessions());
66          assertTrue(c.isRecoverJmsConnections());
67          assertTrue(c.isEagerConsumer());
68      }
69      
70      public void testConnectorConfig() throws Exception
71      {
72          JmsConnector c = (JmsConnector) muleContext.getRegistry().lookupConnector("jmsConnector1");
73          assertNotNull(c);
74  
75          assertNotNull(c.getConnectionFactory());
76          
77          assertTrue(c.getConnectionFactory() instanceof TestConnectionFactory);
78          assertEquals(Session.DUPS_OK_ACKNOWLEDGE, c.getAcknowledgementMode());
79          assertEquals("myuser", c.getUsername());
80          assertEquals("mypass", c.getPassword());
81  
82          assertNotNull(c.getRedeliveryHandlerFactory());
83          assertTrue(c.getRedeliveryHandlerFactory().create() instanceof TestRedeliveryHandler);
84          
85          assertEquals("myClient", c.getClientId());
86          assertTrue(c.isDurable());
87          assertTrue(c.isNoLocal());
88          assertTrue(c.isPersistentDelivery());
89          assertEquals(5, c.getMaxRedelivery());
90          assertTrue(c.isCacheJmsSessions());
91          assertFalse(c.isRecoverJmsConnections());
92          assertFalse(c.isEagerConsumer());
93  
94          assertEquals("1.1", c.getSpecification()); // 1.0.2b is the default, should be changed in the config
95          //test properties, default is 4
96          assertEquals(c.getNumberOfConcurrentTransactedReceivers(),7);
97  
98  
99      }
100 
101     public void testCustomConnectorConfig() throws Exception
102     {
103         JmsConnector c = (JmsConnector) muleContext.getRegistry().lookupConnector("jmsConnector2");
104         assertNotNull(c);
105 
106         assertEquals("1.1", c.getSpecification()); // 1.0.2b is the default, should be changed in the config
107     }
108     
109     public void testTestConnectorConfig() throws Exception
110     {
111         JmsConnector c = (JmsConnector) muleContext.getRegistry().lookupConnector("jmsConnector3");
112         assertNotNull(c);
113 
114         assertNotNull(c.getConnectionFactory());
115 
116         assertTrue(c.getConnectionFactory() instanceof TestConnectionFactory);
117         assertEquals(Session.DUPS_OK_ACKNOWLEDGE, c.getAcknowledgementMode());
118 
119         assertNotNull(c.getRedeliveryHandlerFactory());
120         assertTrue(c.getRedeliveryHandlerFactory().create() instanceof TestRedeliveryHandler);
121 
122         assertEquals("myClient", c.getClientId());
123         assertTrue(c.isDurable());
124         assertTrue(c.isNoLocal());
125         assertTrue(c.isPersistentDelivery());
126         assertEquals(5, c.getMaxRedelivery());
127         assertTrue(c.isCacheJmsSessions());
128         assertFalse(c.isRecoverJmsConnections());
129         assertFalse(c.isEagerConsumer());
130 
131         assertEquals("1.1", c.getSpecification()); // 1.0.2b is the default, should be changed in the config
132     }
133 
134     public void testEndpointConfig() throws EndpointException, InitialisationException
135     {
136         ImmutableEndpoint endpoint1 = muleContext.getRegistry().lookupEndpointBuilder("endpoint1").buildInboundEndpoint();
137         assertNotNull(endpoint1);
138         Filter filter1 = endpoint1.getFilter();
139         assertNotNull(filter1);
140         assertTrue(filter1 instanceof JmsSelectorFilter);
141         ImmutableEndpoint endpoint2 = muleContext.getRegistry().lookupEndpointBuilder("endpoint2").buildOutboundEndpoint();
142         assertNotNull(endpoint2);
143         Filter filter2 = endpoint2.getFilter();
144         assertNotNull(filter2);
145         assertTrue(filter2 instanceof NotFilter);
146         Filter filter3 = ((NotFilter) filter2).getFilter();
147         assertNotNull(filter3);
148         assertTrue(filter3 instanceof JmsPropertyFilter);
149     }
150 
151     public void testCustomTransactions() throws EndpointException, InitialisationException
152     {
153         ImmutableEndpoint endpoint3 = muleContext.getRegistry().lookupEndpointBuilder("endpoint3").buildInboundEndpoint();
154         assertNotNull(endpoint3);
155         TestTransactionFactory factory = (TestTransactionFactory) endpoint3.getTransactionConfig().getFactory();
156         assertNotNull(factory);
157         assertEquals("foo", factory.getValue());
158     }
159 
160 }