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