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