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