This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseTMIntegrationTest.java
185 lines (161 loc) · 4.86 KB
/
BaseTMIntegrationTest.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.neo4j.integration.spring;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.XADataSource;
import javax.transaction.Transaction;
import javax.transaction.UserTransaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.jta.JtaTransactionManager;
/**
* Base class that outlines the expected behavior of every transaction manager.
* Subclasses are expected to override the <code>getConfigName()</code> to return
* a String with the name of their configuration file and provide any
* implementation specific configuration via <code>@Before</code> and
* <code>@After</code> methods.
*
* @author Chris Gioran
*/
public abstract class BaseTMIntegrationTest {
protected ApplicationContext ctx;
protected GraphDatabaseService gds;
protected XADataSource ds;
protected JtaTransactionManager tm;
public BaseTMIntegrationTest() {
super();
}
/**
* Hook for testing the loaded configuration. Can be no-op.
*/
@Test
public void testLoadConfig() throws Exception
{}
@Before
public void setUp() throws Exception {
ctx = new ClassPathXmlApplicationContext(getConfigName());
tm = ctx.getBean("jtaTransactionManager", JtaTransactionManager.class);
gds = ctx.getBean(GraphDatabaseService.class);
ds = ctx.getBean("dataSource", XADataSource.class);
ds.getXAConnection().getConnection()
.prepareStatement("delete from Node").execute();
}
@After
public void tearDown() throws Exception {
gds.shutdown();
}
/**
* Tests just the graph database with some indexing on top.
* Writes a node into the database, indexes it, commits and
* then reads it back, once without a transaction running and
* once with.
*/
@Test
public void testIndexDependencies() throws Exception {
UserTransaction tx = tm.getUserTransaction();
tx.begin();
Node node = null;
Index<Node> index = gds.index().forNodes("node");
IndexHits<Node> indexHits = index.get("field", "value");
node = gds.createNode();
assertNotNull(node);
assertEquals(false, indexHits.hasNext());
tx.commit();
Node readBackOutsideOfTx = gds.getNodeById(node.getId());
assertEquals(node, readBackOutsideOfTx);
tx = tm.getUserTransaction();
tx.begin();
Node readBackInsideOfTx = gds.getNodeById(node.getId());
assertEquals(node, readBackInsideOfTx);
tx.commit();
}
/**
* Reads in the id of every node in the database, outside
* of any transaction.
*/
@Test
public void testReadOutsideTx() {
for (Node n : gds.getAllNodes()) {
n.getId();
}
}
@Test
@Ignore
public void testJustMySQL() throws Exception {
ds.getXAConnection().getConnection()
.prepareStatement("select * from Node").execute();
}
@Test
@Ignore
public void testBothSimpleInsert() throws Exception {
UserTransaction tx = tm.getUserTransaction();
try {
tx.begin();
Node n = gds.createNode();
PreparedStatement stmt = ds.getXAConnection().getConnection()
.prepareStatement("insert into Node values(?)");
stmt.setLong(1, n.getId());
stmt.execute();
gds.getNodeById(n.getId());
stmt = ds
.getXAConnection()
.getConnection()
.prepareStatement(
"select count(*) from Node where NodeId=?");
stmt.setLong(1, n.getId());
stmt.execute();
ResultSet rs = stmt.getResultSet();
rs.next();
assertEquals(1, rs.getInt(1));
tx.commit();
} catch (Exception e) {
tx.rollback();
throw e;
}
}
@Test
@Ignore
public void testBothSimpleInsertRollback() throws Exception {
tm.getTransactionManager().begin();
Transaction tx = tm.getTransactionManager().getTransaction();
Node n = gds.createNode();
Connection conn = ds.getXAConnection().getConnection();
conn.setAutoCommit(false);
PreparedStatement stmt = conn
.prepareStatement("insert into Node values(?)");
stmt.setLong(1, n.getId());
stmt.execute();
conn.rollback();
tx.rollback();
boolean exceptionThrown = false;
try {
gds.getNodeById(n.getId());
} catch (NotFoundException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
stmt = conn
.prepareStatement("select count(*) from Node where NodeId=?");
stmt.setLong(1, n.getId());
stmt.execute();
ResultSet rs = stmt.getResultSet();
rs.next();
assertEquals(0, rs.getInt(1));
}
/**
* @return Returns the filename of the configuration file for the test.
*/
protected abstract String getConfigName();
}