View Javadoc

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