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