View Javadoc

1   /*
2    * $Id: MuleClientTransactionTestCase.java 22654 2011-08-12 07:32:57Z mike.schilling $
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.test.integration.client;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.endpoint.EndpointBuilder;
16  import org.mule.api.endpoint.ImmutableEndpoint;
17  import org.mule.api.transaction.Transaction;
18  import org.mule.api.transaction.TransactionCallback;
19  import org.mule.api.transaction.TransactionConfig;
20  import org.mule.endpoint.EndpointURIEndpointBuilder;
21  import org.mule.endpoint.URIBuilder;
22  import org.mule.module.client.MuleClient;
23  import org.mule.tck.junit4.FunctionalTestCase;
24  import org.mule.transaction.MuleTransactionConfig;
25  import org.mule.transaction.TransactionCoordination;
26  import org.mule.transaction.TransactionTemplate;
27  import org.mule.transport.jms.JmsTransactionFactory;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import org.junit.Test;
33  
34  import static org.junit.Assert.assertNotNull;
35  import static org.junit.Assert.assertNull;
36  import static org.junit.Assert.fail;
37  
38  public class MuleClientTransactionTestCase extends FunctionalTestCase
39  {
40  
41      @Override
42      protected String getConfigResources()
43      {
44          return "org/mule/test/integration/client/test-client-jms-mule-config.xml";
45      }
46  
47      @Test
48      public void testTransactionsWithSetRollbackOnly() throws Exception
49      {
50          final MuleClient client = new MuleClient(muleContext);
51          final Map<String, Object> props = new HashMap<String, Object>();
52          props.put("JMSReplyTo", "replyTo.queue");
53          props.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, "false");
54  
55          // Empty reply queue
56          while (client.request("jms://replyTo.queue", 2000) != null)
57          {
58              // slurp
59          }
60  
61          MuleTransactionConfig tc = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
62          tc.setFactory(new JmsTransactionFactory());
63  
64          // This enpoint needs to be registered prior to use cause we need to set
65          // the transaction config so that the endpoint will "know" it is transacted
66          // and not close the session itself but leave it up to the transaction.
67          EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(
68              new URIBuilder("jms://test.queue", muleContext));
69          endpointBuilder.setTransactionConfig(tc);
70          endpointBuilder.setName("TransactedTest.Queue");
71          ImmutableEndpoint inboundEndpoint = muleContext.getEndpointFactory()
72                  .getOutboundEndpoint(endpointBuilder);
73          client.getMuleContext().getRegistry().registerEndpoint(inboundEndpoint);
74  
75          TransactionTemplate<Void> tt = new TransactionTemplate<Void>(tc, muleContext);
76          tt.execute(new TransactionCallback<Void>()
77          {
78              public Void doInTransaction() throws Exception
79              {
80                  for (int i = 0; i < 100; i++)
81                  {
82                      client.send("jms://test.queue", "Test Client Dispatch message " + i, props);
83                  }
84                  Transaction tx = TransactionCoordination.getInstance().getTransaction();
85                  assertNotNull(tx);
86                  tx.setRollbackOnly();
87                  return null;
88              }
89          });
90  
91          MuleMessage result = client.request("jms://replyTo.queue", 2000);
92          assertNull(result);
93      }
94  
95      @Test
96      public void testTransactionsWithExceptionThrown() throws Exception
97      {
98          final MuleClient client = new MuleClient(muleContext);
99          final Map<String, Object> props = new HashMap<String, Object>();
100         props.put("JMSReplyTo", "replyTo.queue");
101         props.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, "false");
102 
103         // Empty reply queue
104         while (client.request("jms://replyTo.queue", 2000) != null)
105         {
106             // hmm..mesages
107         }
108 
109         MuleTransactionConfig tc = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
110         tc.setFactory(new JmsTransactionFactory());
111 
112         // This enpoint needs to be registered prior to use cause we need to set
113         // the transaction config so that the endpoint will "know" it is transacted
114         // and not close the session itself but leave it up to the transaction.
115         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(
116             new URIBuilder("jms://test.queue", muleContext));
117         endpointBuilder.setTransactionConfig(tc);
118         endpointBuilder.setName("TransactedTest.Queue");
119         ImmutableEndpoint inboundEndpoint = muleContext.getEndpointFactory()
120                 .getOutboundEndpoint(endpointBuilder);
121         client.getMuleContext().getRegistry().registerEndpoint(inboundEndpoint);
122 
123         TransactionTemplate<Void> tt = new TransactionTemplate<Void>(tc, muleContext);
124         try
125         {
126             tt.execute(new TransactionCallback<Void>()
127             {
128                 public Void doInTransaction() throws Exception
129                 {
130                     for (int i = 0; i < 100; i++)
131                     {
132                         client.send("jms://test.queue", "Test Client Dispatch message " + i, props);
133                     }
134                     throw new Exception();
135                 }
136             });
137             fail();
138         }
139         catch (Exception e)
140         {
141             // this is ok
142         }
143 
144         MuleMessage result = client.request("jms://replyTo.queue", 2000);
145         assertNull(result);
146     }
147 
148     @Test
149     public void testTransactionsWithCommit() throws Exception
150     {
151         final MuleClient client = new MuleClient(muleContext);
152         final Map<String, Object> props = new HashMap<String, Object>();
153         props.put("JMSReplyTo", "replyTo.queue");
154         props.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, "false");
155         props.put("transacted", "true");
156 
157         // Empty reply queue
158         while (client.request("jms://replyTo.queue", 2000) != null)
159         {
160             // yum!
161         }
162 
163         MuleTransactionConfig tc = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
164         tc.setFactory(new JmsTransactionFactory());
165 
166         // This enpoint needs to be registered prior to use cause we need to set
167         // the transaction config so that the endpoint will "know" it is transacted
168         // and not close the session itself but leave it up to the transaction.
169         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(
170             new URIBuilder("jms://test.queue", muleContext));
171         endpointBuilder.setTransactionConfig(tc);
172         endpointBuilder.setName("TransactedTest.Queue");
173         ImmutableEndpoint inboundEndpoint = muleContext.getEndpointFactory()
174                 .getOutboundEndpoint(endpointBuilder);
175         client.getMuleContext().getRegistry().registerEndpoint(inboundEndpoint);
176 
177         TransactionTemplate<Void> tt = new TransactionTemplate<Void>(tc, muleContext);
178         tt.execute(new TransactionCallback<Void>()
179         {
180             public Void doInTransaction() throws Exception
181             {
182                 for (int i = 0; i < 100; i++)
183                 {
184                     client.send("jms://test.queue", "Test Client Dispatch message " + i, props);
185                 }
186                 return null;
187             }
188         });
189 
190         for (int i = 0; i < 100; i++)
191         {
192             MuleMessage result = client.request("jms://replyTo.queue", 2000);
193             assertNotNull(result);
194         }
195         MuleMessage result = client.request("jms://replyTo.queue", 2000);
196         assertNull(result);
197     }
198 
199     protected void emptyReplyQueue() throws Exception
200     {
201         final MuleClient client = new MuleClient(muleContext);
202         MuleTransactionConfig tc = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
203         tc.setFactory(new JmsTransactionFactory());
204         TransactionTemplate<Void> tt = new TransactionTemplate<Void>(tc, muleContext);
205         tt.execute(new TransactionCallback<Void>()
206         {
207             public Void doInTransaction() throws Exception
208             {
209                 while (client.request("jms://replyTo.queue", 2000) != null)
210                 {
211                     // munch..
212                 }
213 
214                 return null;
215             }
216         });
217     }
218 
219 }