-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
JdbcThinBatchSelfTest.java
334 lines (254 loc) · 9.94 KB
/
JdbcThinBatchSelfTest.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.jdbc.thin;
import java.sql.BatchUpdateException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.Callable;
import org.apache.ignite.testframework.GridTestUtils;
/**
* Statement test.
*/
public class JdbcThinBatchSelfTest extends JdbcThinAbstractDmlStatementSelfTest {
/** SQL query. */
private static final String SQL_PREPARED = "insert into Person(_key, id, firstName, lastName, age) values " +
"(?, ?, ?, ?, ?)";
/** Statement. */
private Statement stmt;
/** Prepared statement. */
private PreparedStatement pstmt;
/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
super.beforeTest();
stmt = conn.createStatement();
pstmt = conn.prepareStatement(SQL_PREPARED);
assertNotNull(stmt);
assertFalse(stmt.isClosed());
assertNotNull(pstmt);
assertFalse(pstmt.isClosed());
}
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
if (stmt != null && !stmt.isClosed())
stmt.close();
if (pstmt != null && !pstmt.isClosed())
pstmt.close();
assertTrue(pstmt.isClosed());
assertTrue(stmt.isClosed());
super.afterTest();
}
/**
* @throws SQLException If failed.
*/
public void testBatch() throws SQLException {
final int BATCH_SIZE = 10;
for (int idx = 0, i = 0; i < BATCH_SIZE; ++i, idx += i) {
stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values "
+ generateValues(idx, i + 1));
}
int [] updCnts = stmt.executeBatch();
assertEquals("Invalid update counts size", BATCH_SIZE, updCnts.length);
for (int i = 0; i < BATCH_SIZE; ++i)
assertEquals("Invalid update count",i + 1, updCnts[i]);
}
/**
* @throws SQLException If failed.
*/
public void testBatchOnClosedStatement() throws SQLException {
final Statement stmt2 = conn.createStatement();
final PreparedStatement pstmt2 = conn.prepareStatement("");
stmt2.close();
pstmt2.close();
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt2.addBatch("");
return null;
}
}, SQLException.class, "Statement is closed.");
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt2.clearBatch();
return null;
}
}, SQLException.class, "Statement is closed.");
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt2.executeBatch();
return null;
}
}, SQLException.class, "Statement is closed.");
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
pstmt2.addBatch();
return null;
}
}, SQLException.class, "Statement is closed.");
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
pstmt2.clearBatch();
return null;
}
}, SQLException.class, "Statement is closed.");
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
pstmt2.executeBatch();
return null;
}
}, SQLException.class, "Statement is closed.");
}
/**
* @throws SQLException If failed.
*/
public void testBatchException() throws SQLException {
final int BATCH_SIZE = 7;
for (int idx = 0, i = 0; i < BATCH_SIZE; ++i, idx += i) {
stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values "
+ generateValues(idx, i + 1));
}
stmt.addBatch("select * from Person");
stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values "
+ generateValues(100, 1));
try {
stmt.executeBatch();
fail("BatchUpdateException must be thrown");
} catch(BatchUpdateException e) {
int [] updCnts = e.getUpdateCounts();
assertEquals("Invalid update counts size", BATCH_SIZE, updCnts.length);
for (int i = 0; i < BATCH_SIZE; ++i)
assertEquals("Invalid update count",i + 1, updCnts[i]);
if (!e.getMessage().contains("Given statement type does not match that declared by JDBC driver")) {
log.error("Invalid exception: ", e);
fail();
}
}
}
/**
* @throws SQLException If failed.
*/
public void testBatchClear() throws SQLException {
final int BATCH_SIZE = 7;
for (int idx = 0, i = 0; i < BATCH_SIZE; ++i, idx += i) {
stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values "
+ generateValues(idx, i + 1));
}
stmt.clearBatch();
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt.executeBatch();
return null;
}
}, SQLException.class, "Batch is empty.");
}
/**
* @throws SQLException If failed.
*/
public void testBatchPrepared() throws SQLException {
final int BATCH_SIZE = 10;
for (int i = 0; i < BATCH_SIZE; ++i) {
int paramCnt = 1;
pstmt.setString(paramCnt++, "p" + i);
pstmt.setInt(paramCnt++, i);
pstmt.setString(paramCnt++, "Name" + i);
pstmt.setString(paramCnt++, "Lastname" + i);
pstmt.setInt(paramCnt++, 20 + i);
pstmt.addBatch();
}
int [] updCnts = pstmt.executeBatch();
assertEquals("Invalid update counts size", BATCH_SIZE, updCnts.length);
for (int i = 0; i < BATCH_SIZE; ++i)
assertEquals("Invalid update count",1, updCnts[i]);
}
/**
* @throws SQLException If failed.
*/
public void testBatchExceptionPrepared() throws SQLException {
final int BATCH_SIZE = 7;
for (int i = 0; i < BATCH_SIZE; ++i) {
int paramCnt = 1;
pstmt.setString(paramCnt++, "p" + i);
pstmt.setInt(paramCnt++, i);
pstmt.setString(paramCnt++, "Name" + i);
pstmt.setString(paramCnt++, "Lastname" + i);
pstmt.setInt(paramCnt++, 20 + i);
pstmt.addBatch();
}
int paramCnt = 1;
pstmt.setString(paramCnt++, "p" + 100);
pstmt.setString(paramCnt++, "x");
pstmt.setString(paramCnt++, "Name" + 100);
pstmt.setString(paramCnt++, "Lastname" + 100);
pstmt.setInt(paramCnt++, 20 + 100);
pstmt.addBatch();
try {
pstmt.executeBatch();
fail("BatchUpdateException must be thrown");
}
catch(BatchUpdateException e) {
int [] updCnts = e.getUpdateCounts();
assertEquals("Invalid update counts size", BATCH_SIZE, updCnts.length);
for (int i = 0; i < BATCH_SIZE; ++i)
assertEquals("Invalid update count",1, updCnts[i]);
if (!e.getMessage().contains("Value conversion failed")) {
log.error("Invalid exception: ", e);
fail();
}
}
}
/**
* @throws SQLException If failed.
*/
public void testBatchClearPrepared() throws SQLException {
final int BATCH_SIZE = 10;
for (int i = 0; i < BATCH_SIZE; ++i) {
int paramCnt = 1;
pstmt.setString(paramCnt++, "p" + i);
pstmt.setInt(paramCnt++, i);
pstmt.setString(paramCnt++, "Name" + i);
pstmt.setString(paramCnt++, "Lastname" + i);
pstmt.setInt(paramCnt++, 20 + i);
pstmt.addBatch();
}
pstmt.clearBatch();
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
pstmt.executeBatch();
return null;
}
}, SQLException.class, "Batch is empty.");
}
/**
* @param beginIndex Begin row index.
* @param cnt Count of rows.
* @return String contains values for 'cnt' rows.
*/
private String generateValues(int beginIndex, int cnt) {
StringBuilder sb = new StringBuilder();
int lastIdx = beginIndex + cnt - 1;
for (int i = beginIndex; i < lastIdx; ++i)
sb.append(valuesRow(i)).append(',');
sb.append(valuesRow(lastIdx));
return sb.toString();
}
/**
* @param idx Index of the row.
* @return String with row values.
*/
private String valuesRow(int idx) {
return String.format("('p%d', %d, 'Name%d', 'Lastname%d', %d)", idx, idx, idx, idx, 20 + idx);
}
}