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.test.integration.transport.jdbc;
8   
9   
10  import org.mule.api.MuleEventContext;
11  import org.mule.api.context.notification.TransactionNotificationListener;
12  import org.mule.api.endpoint.EndpointBuilder;
13  import org.mule.api.endpoint.InboundEndpoint;
14  import org.mule.api.endpoint.OutboundEndpoint;
15  import org.mule.api.routing.OutboundRouterCollection;
16  import org.mule.api.service.Service;
17  import org.mule.api.source.CompositeMessageSource;
18  import org.mule.api.transaction.Transaction;
19  import org.mule.api.transaction.TransactionConfig;
20  import org.mule.api.transaction.TransactionFactory;
21  import org.mule.component.DefaultJavaComponent;
22  import org.mule.context.notification.TransactionNotification;
23  import org.mule.endpoint.EndpointURIEndpointBuilder;
24  import org.mule.exception.DefaultServiceExceptionStrategy;
25  import org.mule.model.seda.SedaService;
26  import org.mule.object.PrototypeObjectFactory;
27  import org.mule.routing.outbound.DefaultOutboundRouterCollection;
28  import org.mule.routing.outbound.OutboundPassThroughRouter;
29  import org.mule.service.AbstractService;
30  import org.mule.tck.functional.EventCallback;
31  import org.mule.transaction.MuleTransactionConfig;
32  
33  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
34  import org.junit.Test;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertNotNull;
38  import static org.junit.Assert.assertNull;
39  import static org.junit.Assert.assertTrue;
40  
41  public abstract class AbstractJdbcTransactionalFunctionalTestCase extends AbstractJdbcFunctionalTestCase  implements TransactionNotificationListener<TransactionNotification>
42  {
43  
44      private Transaction currentTx;
45      protected boolean rollbacked = false;
46  
47      @Override
48      protected void doSetUp() throws Exception
49      {
50          super.doSetUp();
51          muleContext.registerListener(this);
52          currentTx = null;
53      }
54  
55      @Test
56      public void testReceiveAndSendWithException() throws Exception
57      {
58          final AtomicBoolean called = new AtomicBoolean(false);
59  
60          EventCallback callback = new EventCallback()
61          {
62              public void eventReceived(MuleEventContext context, Object component) throws Exception
63              {
64                  try
65                  {
66                      called.set(true);
67                      currentTx = context.getCurrentTransaction();
68                      assertNotNull(currentTx);
69                      assertTrue(currentTx.isBegun());
70                      currentTx.setRollbackOnly();
71                  }
72                  finally
73                  {
74                      synchronized (called)
75                      {
76                          called.notifyAll();
77                      }
78                  }
79              }
80          };
81  
82          // Start the server
83          initialiseService(TransactionConfig.ACTION_ALWAYS_BEGIN, callback);
84          muleContext.start();
85  
86          execSqlUpdate("INSERT INTO TEST(TYPE, DATA, ACK, RESULT) VALUES (1, '" + DEFAULT_MESSAGE
87                        + "', NULL, NULL)");
88  
89          synchronized (called)
90          {
91              called.wait(20000);
92          }
93          assertTrue(called.get());
94  
95          Thread.sleep(1000);
96  
97          assertTrue(rollbacked);
98  
99          Object[] obj = execSqlQuery("SELECT COUNT(*) FROM TEST WHERE TYPE = 2");
100         assertNotNull(obj);
101         assertEquals(1, obj.length);
102         assertEquals(new Integer(0), obj[0]);
103         obj = execSqlQuery("SELECT ACK FROM TEST WHERE TYPE = 1");
104         assertNotNull(obj);
105         assertEquals(1, obj.length);
106         assertNull(obj[0]);
107     }
108 
109     public Service initialiseService(byte txBeginAction, EventCallback callback) throws Exception
110     {
111         Service service = new SedaService(muleContext);
112         ((AbstractService) service).setExceptionListener(new DefaultServiceExceptionStrategy(muleContext));
113         service.setName("testComponent");
114         service.setComponent(new DefaultJavaComponent(new PrototypeObjectFactory(JdbcFunctionalTestComponent.class)));
115 
116         TransactionFactory tf = getTransactionFactory();
117         TransactionConfig txConfig = new MuleTransactionConfig();
118         txConfig.setFactory(tf);
119         txConfig.setAction(txBeginAction);
120         
121         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(getInDest(), muleContext);
122         endpointBuilder.setName("testIn");
123         endpointBuilder.setConnector(connector);
124         endpointBuilder.setTransactionConfig(txConfig);
125         InboundEndpoint endpoint = muleContext.getEndpointFactory().getInboundEndpoint(
126             endpointBuilder);
127 
128         EndpointBuilder endpointBuilder2 = new EndpointURIEndpointBuilder(getOutDest(), muleContext);
129         endpointBuilder2.setName("testOut");
130         endpointBuilder2.setConnector(connector);
131         OutboundEndpoint outProvider = muleContext.getEndpointFactory().getOutboundEndpoint(
132             endpointBuilder2);
133         
134         service.setOutboundMessageProcessor(new DefaultOutboundRouterCollection());
135         OutboundPassThroughRouter router = new OutboundPassThroughRouter();
136         router.addRoute(outProvider);
137         ((OutboundRouterCollection) service.getOutboundMessageProcessor()).addRoute(router);
138         ((CompositeMessageSource) service.getMessageSource()).addSource(endpoint);
139 
140         // these tests no longer work - they need replacing with config driven tests
141         // furthemore, nothing is read from service properties any more
142         // (except for axis and cxf related hacks)
143         // so i am removing the code below since it's a pointless call to a deprecated method
144 //        HashMap props = new HashMap();
145 //        props.put("eventCallback", callback);
146 //        service.setProperties(props);
147         service.setModel(model);
148         muleContext.getRegistry().registerService(service);
149         return service;
150     }
151 
152     public void onNotification(TransactionNotification notification)
153     {
154         if (notification.getAction() == TransactionNotification.TRANSACTION_ROLLEDBACK)
155         {
156             this.rollbacked = true;
157         }
158     }
159 
160     abstract protected TransactionFactory getTransactionFactory();
161 
162 }