View Javadoc

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