This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
WrappedConnectionProviderTest.java
114 lines (99 loc) · 3.63 KB
/
WrappedConnectionProviderTest.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
package org.zanata.database;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import lombok.Cleanup;
import org.assertj.core.api.Fail;
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.JDBCException;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.junit.Before;
import org.junit.Test;
import org.zanata.ZanataDbunitJpaTest;
public class WrappedConnectionProviderTest extends ZanataDbunitJpaTest {
private Session session;
@Override
protected void prepareDBUnitOperations() {
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/ProjectsData.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/TextFlowTestData.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/LocalesData.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(
"org/zanata/test/model/AccountData.dbunit.xml",
DatabaseOperation.CLEAN_INSERT));
}
@Before
public void setup() {
session = getSession();
}
@Test
public void testWrapperWithNestedExecute() throws Exception {
@Cleanup
ScrollableResults scroll1 = streamQuery("from HTextFlow");
try {
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
Statement statement = connection.createStatement();
statement.executeUpdate("delete from HTextFlow where 0=1");
}
});
concurrentResultSetNotDetected();
} catch (JDBCException e) {
checkExceptionType(e);
}
}
@Test
public void testWrapperWithNestedStreaming() throws Exception {
@Cleanup
ScrollableResults scroll1 = streamQuery("from HTextFlow");
try {
@Cleanup
ScrollableResults scroll2 = streamQuery("from HTextFlowTarget");
concurrentResultSetNotDetected();
} catch (JDBCException e) {
checkExceptionType(e);
}
}
@Test
public void testWrapperWithNestedResults() throws Exception {
@Cleanup
ScrollableResults scroll1 = streamQuery("from HTextFlow");
try {
@Cleanup
ScrollableResults scroll2 = scrollQuery("from HTextFlowTarget");
concurrentResultSetNotDetected();
} catch (JDBCException e) {
checkExceptionType(e);
}
}
private void concurrentResultSetNotDetected() {
Fail.fail("Failed to detect concurrent ResultSet - is " +
"Wrapped*ConnectionProvider enabled in persistence.xml?");
}
private void checkExceptionType(JDBCException e) {
if (!(e.getSQLException() instanceof StreamingResultSetSQLException)) {
throw e;
}
}
private ScrollableResults streamQuery(String queryString) {
Query q = session.createQuery(queryString);
q.setFetchSize(Integer.MIN_VALUE);
ScrollableResults scroll = q.scroll(ScrollMode.FORWARD_ONLY);
return scroll;
}
private ScrollableResults scrollQuery(String queryString) {
Query q = session.createQuery(queryString);
ScrollableResults scroll = q.scroll();
return scroll;
}
}