forked from jbosstm/quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgroalTest.java
136 lines (111 loc) · 5.02 KB
/
AgroalTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* JBoss, Home of Professional Open Source.
* Copyright 2018, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package io.narayana;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.transaction.TransactionManager;
import org.jboss.byteman.contrib.bmunit.BMScript;
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import io.narayana.util.AgroalH2Utils;
/**
* Tests showing that commit/rollback/recovery functionality works fine
* when Narayana is integrated with Agroal jdbc pooling library.
*/
@RunWith(BMUnitRunner.class)
public class AgroalTest {
Connection connForVerification1, connForVerification2;
@Before
public void setUp() {
connForVerification1 = AgroalH2Utils.getConnection(AgroalH2Utils.DB_1);
connForVerification2 = AgroalH2Utils.getConnection(AgroalH2Utils.DB_2);
AgroalH2Utils.dropTableSilently(connForVerification1);
AgroalH2Utils.dropTableSilently(connForVerification2);
AgroalH2Utils.createTable(connForVerification1);
AgroalH2Utils.createTable(connForVerification2);
}
@After
public void tearDown() throws Exception {
// cleaning possible active global transaction
TransactionManager txn = com.arjuna.ats.jta.TransactionManager.transactionManager();
if(txn != null) {
if(txn.getStatus() == javax.transaction.Status.STATUS_ACTIVE)
txn.rollback();
if(txn.getStatus() != javax.transaction.Status.STATUS_NO_TRANSACTION)
txn.suspend();
}
try {
connForVerification1.close();
connForVerification2.close();
} catch (Exception ignored) {}
}
@Test
public void commit() throws Exception {
AgroalDatasource ag = new AgroalDatasource();
ag.process(() -> {});
ResultSet rs1 = AgroalH2Utils.select(connForVerification1);
ResultSet rs2 = AgroalH2Utils.select(connForVerification2);
Assert.assertTrue("First database does not contain data as expected to be commited", rs1.next());
Assert.assertTrue("Second database does not contain data as expected to be commited", rs2.next());
}
@Test
public void rollback() throws Exception {
AgroalDatasource ag = new AgroalDatasource();
try {
ag.process(() -> {throw new RuntimeException("expected");});
} catch (Exception e) {
checkcException(e);
}
ResultSet rs1 = AgroalH2Utils.select(connForVerification1);
ResultSet rs2 = AgroalH2Utils.select(connForVerification2);
Assert.assertFalse("First database contains data which is not expected as rolled-back", rs1.next());
Assert.assertFalse("Second database contains data which is not expected as rolled-back", rs2.next());
ag.closeConnections();
}
@BMScript("xaexception.rmfail")
@Test
public void recovery() throws Exception {
AgroalDatasource ag = new AgroalDatasource();
ag.process(() -> {});
ResultSet rs1 = AgroalH2Utils.select(connForVerification1);
ResultSet rs2 = AgroalH2Utils.select(connForVerification2);
Assert.assertFalse("Both databases [" + connForVerification1 + ", " + connForVerification2 +
"] are committed even one was expected to fail with XAException",
rs1.next() && rs2.next());
// manually run recovery manager to check if integration with Agroal works
// this verifies that XAResourceRecovery was setup and if recovery finishes the failed transaction
ag.getRecoveryManager().scan();
ag.getRecoveryManager().terminate();
rs1 = AgroalH2Utils.select(connForVerification1);
Assert.assertTrue("First database does not contain data as expected to be commited", rs1.next());
ag.closeConnections();
}
private void checkcException(Exception e) {
if (!e.getMessage().toLowerCase().contains("expected"))
Assert.fail("Exception message does not contain 'expected' but it's '"
+ e.getClass().getName() + ":" + e.getMessage() + "'");
}
}