View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.jms.integration;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  
12  import org.junit.Test;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertNull;
17  
18  /**
19   * Requires the following connector config: <jms:connector name="jmsConnector1"
20   * jndiInitialFactory="org.apache.activemq.jndi.ActiveMQInitialContextFactory"
21   * jndiProviderUrl="vm://localhost?broker.persistent=false&amp;broker.useJmx=false"
22   * connectionFactoryJndiName="ConnectionFactory" /> <jms:xxx-connector
23   * name="jmsConnector2"
24   * jndiInitialFactory="org.apache.activemq.jndi.ActiveMQInitialContextFactory"
25   * jndiProviderUrl="vm://localhost?broker.persistent=false&amp;broker.useJmx=false"
26   * jndiProviderProperties-ref="providerProperties" jndiDestinations="true"
27   * forceJndiDestinations="true" connectionFactoryJndiName="ConnectionFactory" />
28   * <spring:beans> <util:properties id="providerProperties"> <!-- see
29   * http://activemq.apache.org/jndi-support.html for configuring queues/topics through
30   * JNDI properties for other Jms vendors these JNDI properties will need to be
31   * available from the JNDI context --> <spring:prop key="queue.in2">in-queue2</spring:prop>
32   * <spring:prop key="topic.some/long/path/in3">in-topic3</spring:prop>
33   * </util:properties> </spring:beans>
34   */
35  public class JmsConnectorJndiTestCase extends AbstractJmsFunctionalTestCase
36  {
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "integration/jms-jndi-config.xml";
42      }
43  
44      @Test
45      public void testConnectionFactoryFromJndi() throws Exception
46      {
47          // No need to specifically test anything here, if the ConnectionFactory
48          // is not successfully looked up from JNDI, Mule won't even start up.
49      }
50  
51      @Test
52      public void testQueueFromJndi() throws Exception
53      {
54          MuleClient client = new MuleClient(muleContext);
55  
56          client.dispatch("ep_jndi-queue", DEFAULT_INPUT_MESSAGE, null);
57  
58          MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
59          assertNotNull(result);
60          assertEquals(DEFAULT_INPUT_MESSAGE, result.getPayloadAsString());
61      }
62  
63      @Test
64      public void testTopicFromJndi() throws Exception
65      {
66          MuleClient client = new MuleClient(muleContext);
67  
68          client.dispatch("ep_jndi-topic", DEFAULT_INPUT_MESSAGE, null);
69  
70          MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
71          assertNotNull(result);
72          assertEquals(DEFAULT_INPUT_MESSAGE, result.getPayloadAsString());
73      }
74  
75      /**
76       * Use a non-JNDI Destination when jndiDestinations="false", test should pass.
77       */
78      @Test
79      public void testNonJndiDestination() throws Exception
80      {
81          MuleClient client = new MuleClient(muleContext);
82  
83          client.dispatch("ep_non-jndi-queue", DEFAULT_INPUT_MESSAGE, null);
84  
85          MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
86          assertNotNull(result);
87          assertEquals(DEFAULT_INPUT_MESSAGE, result.getPayloadAsString());
88      }
89  
90      /**
91       * Use a non-JNDI Destination when jndiDestinations="true" but forceJndiDestinations="false", test should pass.
92       */
93      @Test
94      public void testNonJndiDestinationOptional() throws Exception
95      {
96          MuleClient client = new MuleClient(muleContext);
97  
98          client.dispatch("ep_non-jndi-queue-optional-jndi", DEFAULT_INPUT_MESSAGE, null);
99  
100         MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
101         assertNotNull(result);
102         assertEquals(DEFAULT_INPUT_MESSAGE, result.getPayloadAsString());
103     }
104 
105     /**
106      * Use a non-JNDI Destination when forceJndiDestinations="true", test should fail.
107      */
108     @Test
109     public void testNonJndiDestinationForce() throws Exception
110     {
111         MuleClient client = new MuleClient(muleContext);
112 
113         client.dispatch("ep_non-jndi-queue-force-jndi", DEFAULT_INPUT_MESSAGE, null);
114 
115         MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
116         assertNull("Attempt to look up a non-existant JNDI Destination should have failed", result);
117     }
118 
119     @Test
120     public void testQueueFromJndiWithJndiNameResolver() throws Exception
121     {
122         MuleClient client = new MuleClient(muleContext);
123 
124         client.dispatch("ep_jndi-queue-with-jndi-name-resolver", DEFAULT_INPUT_MESSAGE, null);
125 
126         MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
127         assertNotNull(result);
128         assertEquals(DEFAULT_INPUT_MESSAGE, result.getPayloadAsString());
129     }
130 
131     @Test
132     public void testTopicFromJndiWithJndiNameResolver() throws Exception
133     {
134         MuleClient client = new MuleClient(muleContext);
135 
136         client.dispatch("ep_jndi-topic-with-jndi-name-resolver", DEFAULT_INPUT_MESSAGE, null);
137 
138         MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
139         assertNotNull(result);
140         assertEquals(DEFAULT_INPUT_MESSAGE, result.getPayloadAsString());
141     }
142 
143     /**
144     * Use a non-JNDI Destination when jndiDestinations="false", test should pass.
145     */
146     @Test
147     public void testNonJndiDestinationWithJndiNameResolver() throws Exception
148     {
149         MuleClient client = new MuleClient(muleContext);
150 
151         client.dispatch("ep_non-jndi-queue-with-jndi-name-resolver", DEFAULT_INPUT_MESSAGE, null);
152 
153         MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
154         assertNotNull(result);
155         assertEquals(DEFAULT_INPUT_MESSAGE, result.getPayloadAsString());
156     }
157 
158     /**
159     * Use a non-JNDI Destination when jndiDestinations="true" but forceJndiDestinations="false", test should pass.
160     */
161     @Test
162     public void testNonJndiDestinationOptionalWithJndiNameResolver() throws Exception
163     {
164         MuleClient client = new MuleClient(muleContext);
165 
166         client.dispatch("ep_non-jndi-queue-optional-jndi-with-jndi-name-resolver", DEFAULT_INPUT_MESSAGE, null);
167 
168         MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
169         assertNotNull(result);
170         assertEquals(DEFAULT_INPUT_MESSAGE, result.getPayloadAsString());
171     }
172 
173     /**
174     * Use a non-JNDI Destination when forceJndiDestinations="true", test should fail.
175     */
176     @Test
177     public void testNonJndiDestinationForceWithJndiNameResolver() throws Exception
178     {
179         MuleClient client = new MuleClient(muleContext);
180 
181         client.dispatch("ep_non-jndi-queue-force-jndi-with-jndi-name-resolver", DEFAULT_INPUT_MESSAGE, null);
182 
183         MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
184         assertNull("Attempt to look up a non-existant JNDI Destination should have failed", result);
185     }
186 }