1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.transaction;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.module.client.MuleClient;
15 import org.mule.tck.FunctionalTestCase;
16 import org.mule.tck.util.MuleDerbyTestUtils;
17 import org.mule.test.integration.transaction.extras.Book;
18 import org.mule.transport.jdbc.JdbcUtils;
19
20 import java.sql.Connection;
21 import java.sql.DriverManager;
22 import java.util.List;
23
24 import org.apache.commons.dbutils.QueryRunner;
25 import org.apache.commons.dbutils.handlers.ArrayListHandler;
26
27 public class XATransactionsWithSpringDAO extends FunctionalTestCase
28 {
29 private static final int RECEIVE_TIMEOUT = 10000;
30 private static String connectionString;
31
32 protected String getConfigResources()
33 {
34 return "org/mule/test/integration/transaction/xatransactions-with-spring-dao-config.xml";
35 }
36
37 protected void suitePreSetUp() throws Exception
38 {
39 String dbName = MuleDerbyTestUtils.loadDatabaseName("derby.properties", "database.name");
40
41 MuleDerbyTestUtils.defaultDerbyCleanAndInit("derby.properties", "database.name");
42 connectionString = "jdbc:derby:" + dbName;
43
44 super.suitePreSetUp();
45 }
46
47 protected void doSetUp() throws Exception
48 {
49 emptyTable();
50 }
51
52 protected void emptyTable() throws Exception
53 {
54 try
55 {
56 execSqlUpdate("DELETE FROM BOOK");
57 }
58 catch (Exception e)
59 {
60 execSqlUpdate("CREATE TABLE BOOK(ID INTEGER NOT NULL PRIMARY KEY,TITLE VARCHAR(255),AUTHOR VARCHAR(255))");
61 }
62 }
63
64 protected Connection getConnection() throws Exception
65 {
66 Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
67 return DriverManager.getConnection(connectionString);
68 }
69
70 public List execSqlQuery(String sql) throws Exception
71 {
72 Connection con = null;
73 try
74 {
75 con = getConnection();
76 return (List) new QueryRunner().query(con, sql, new ArrayListHandler());
77 }
78 finally
79 {
80 JdbcUtils.close(con);
81 }
82 }
83
84 protected int execSqlUpdate(String sql) throws Exception
85 {
86 Connection con = null;
87 try
88 {
89 con = getConnection();
90 return new QueryRunner().update(con, sql);
91 }
92 finally
93 {
94 JdbcUtils.close(con);
95 }
96 }
97
98 public void testXATransactionUsingSpringDaoNoRollback() throws Exception
99 {
100 MuleClient client = new MuleClient(muleContext);
101 Book book = new Book(1, "testBook", "testAuthor");
102 client.sendNoReceive("jms://my.queue", book, null);
103 MuleMessage result = client.request("vm://output", RECEIVE_TIMEOUT);
104 assertNotNull(result);
105 assertNotNull(result.getPayload());
106 assertTrue(((Boolean) result.getPayload()).booleanValue());
107 int res = execSqlUpdate("UPDATE BOOK SET TITLE = 'My Test' WHERE TITLE='testBook'");
108 if (res < 0)
109 {
110 fail();
111 }
112 }
113
114 public void testXATransactionUsingSpringDaoWithRollback() throws Exception
115 {
116 MuleClient client = new MuleClient(muleContext);
117
118 Book book = new Book(1, "testBook", "testAuthor");
119 client.sendNoReceive("jms://my.queue", book, null);
120 MuleMessage result = client.request("vm://output", RECEIVE_TIMEOUT);
121 assertNotNull(result);
122 assertNotNull(result.getPayload());
123 assertTrue(((Boolean) result.getPayload()).booleanValue());
124 int res = execSqlUpdate("UPDATE BOOK SET TITLE = 'My Test' WHERE TITLE='testBook'");
125 if (res < 0)
126 {
127 fail();
128 }
129
130 client.sendNoReceive("jms://my.queue", book, null);
131 result = client.request("vm://output", 5000);
132
133
134 assertNull(result);
135 }
136
137 }