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