16
16
17
17
package com .mongodb .hibernate .jdbc ;
18
18
19
- import static com .mongodb .hibernate .internal .MongoAssertions .assertNotNull ;
20
19
import static org .junit .jupiter .api .Assertions .assertEquals ;
21
20
22
21
import java .sql .Connection ;
27
26
import org .bson .BsonDocument ;
28
27
import org .hibernate .Session ;
29
28
import org .hibernate .SessionFactory ;
29
+ import org .hibernate .cfg .AvailableSettings ;
30
30
import org .hibernate .cfg .Configuration ;
31
- import org .jspecify .annotations .Nullable ;
31
+ import org .jspecify .annotations .NullUnmarked ;
32
32
import org .junit .jupiter .api .AfterAll ;
33
33
import org .junit .jupiter .api .AfterEach ;
34
34
import org .junit .jupiter .api .BeforeAll ;
37
37
import org .junit .jupiter .params .ParameterizedTest ;
38
38
import org .junit .jupiter .params .provider .ValueSource ;
39
39
40
+ @ NullUnmarked
40
41
class MongoPreparedStatementIntegrationTests {
42
+ private static SessionFactory sessionFactory ;
41
43
42
- private static @ Nullable SessionFactory sessionFactory ;
43
-
44
- private @ Nullable Session session ;
44
+ private Session session ;
45
45
46
46
@ BeforeAll
47
47
static void beforeAll () {
@@ -57,7 +57,18 @@ static void afterAll() {
57
57
58
58
@ BeforeEach
59
59
void setUp () {
60
- session = assertNotNull (sessionFactory ).openSession ();
60
+ session = sessionFactory .openSession ();
61
+ session .doWork (conn -> {
62
+ conn .createStatement ()
63
+ .executeUpdate (
64
+ """
65
+ {
66
+ delete: "books",
67
+ deletes: [
68
+ { q: {}, limit: 0 }
69
+ ]
70
+ }""" );
71
+ });
61
72
}
62
73
63
74
@ AfterEach
@@ -70,21 +81,6 @@ void tearDown() {
70
81
@ Nested
71
82
class ExecuteUpdateTests {
72
83
73
- @ BeforeEach
74
- void setUp () {
75
- assertNotNull (session ).doWork (conn -> {
76
- conn .createStatement ()
77
- .executeUpdate (
78
- """
79
- {
80
- delete: "books",
81
- deletes: [
82
- { q: {}, limit: 0 }
83
- ]
84
- }""" );
85
- });
86
- }
87
-
88
84
private static final String INSERT_MQL =
89
85
"""
90
86
{
@@ -181,7 +177,7 @@ void testUpdate(boolean autoCommit) {
181
177
}
182
178
183
179
private void prepareData () {
184
- assertNotNull ( session ) .doWork (connection -> {
180
+ session .doWork (connection -> {
185
181
connection .setAutoCommit (true );
186
182
var statement = connection .createStatement ();
187
183
statement .executeUpdate (INSERT_MQL );
@@ -193,7 +189,7 @@ private void assertExecuteUpdate(
193
189
boolean autoCommit ,
194
190
int expectedUpdatedRowCount ,
195
191
Set <? extends BsonDocument > expectedDocuments ) {
196
- assertNotNull ( session ) .doWork (connection -> {
192
+ session .doWork (connection -> {
197
193
connection .setAutoCommit (autoCommit );
198
194
try (var pstmt = pstmtProvider .apply (connection )) {
199
195
try {
@@ -212,4 +208,133 @@ private void assertExecuteUpdate(
212
208
});
213
209
}
214
210
}
211
+
212
+ @ Nested
213
+ class BatchTests {
214
+ private static final int BATCH_SIZE = 2 ;
215
+
216
+ private static SessionFactory batchableSessionFactory ;
217
+
218
+ private Session batchableSession ;
219
+
220
+ private static final String MQL =
221
+ """
222
+ {
223
+ insert: "books",
224
+ documents: [
225
+ {
226
+ _id: { $undefined: true },
227
+ title: { $undefined: true }
228
+ }
229
+ ]
230
+ }""" ;
231
+
232
+ @ BeforeAll
233
+ static void beforeAll () {
234
+ batchableSessionFactory = new Configuration ()
235
+ .setProperty (AvailableSettings .STATEMENT_BATCH_SIZE , BATCH_SIZE )
236
+ .buildSessionFactory ();
237
+ }
238
+
239
+ @ AfterAll
240
+ static void afterAll () {
241
+ if (batchableSessionFactory != null ) {
242
+ batchableSessionFactory .close ();
243
+ }
244
+ }
245
+
246
+ @ BeforeEach
247
+ void setUp () {
248
+ batchableSession = batchableSessionFactory .openSession ();
249
+ }
250
+
251
+ @ AfterEach
252
+ void tearDown () {
253
+ if (batchableSession != null ) {
254
+ batchableSession .close ();
255
+ }
256
+ }
257
+
258
+ @ ParameterizedTest
259
+ @ ValueSource (booleans = {true , false })
260
+ void testExecuteBatch (boolean autoCommit ) {
261
+ batchableSession .doWork (connection -> {
262
+ connection .setAutoCommit (autoCommit );
263
+ try (var pstmt = connection .prepareStatement (MQL )) {
264
+ try {
265
+ pstmt .setInt (1 , 1 );
266
+ pstmt .setString (2 , "War and Peace" );
267
+ pstmt .addBatch ();
268
+
269
+ pstmt .setInt (1 , 2 );
270
+ pstmt .setString (2 , "Anna Karenina" );
271
+ pstmt .addBatch ();
272
+
273
+ pstmt .executeBatch ();
274
+
275
+ pstmt .setInt (1 , 3 );
276
+ pstmt .setString (2 , "Crime and Punishment" );
277
+ pstmt .addBatch ();
278
+
279
+ pstmt .setInt (1 , 4 );
280
+ pstmt .setString (2 , "Notes from Underground" );
281
+ pstmt .addBatch ();
282
+
283
+ pstmt .executeBatch ();
284
+
285
+ pstmt .setInt (1 , 5 );
286
+ pstmt .setString (2 , "Fathers and Sons" );
287
+
288
+ pstmt .addBatch ();
289
+
290
+ pstmt .executeBatch ();
291
+ } finally {
292
+ if (!autoCommit ) {
293
+ connection .commit ();
294
+ }
295
+ pstmt .clearBatch ();
296
+ }
297
+
298
+ var expectedDocuments = Set .of (
299
+ BsonDocument .parse (
300
+ """
301
+ {
302
+ _id: 1,
303
+ title: "War and Peace"
304
+ }""" ),
305
+ BsonDocument .parse (
306
+ """
307
+ {
308
+ _id: 2,
309
+ title: "Anna Karenina"
310
+ }""" ),
311
+ BsonDocument .parse (
312
+ """
313
+ {
314
+ _id: 3,
315
+ title: "Crime and Punishment"
316
+ }""" ),
317
+ BsonDocument .parse (
318
+ """
319
+ {
320
+ _id: 4,
321
+ title: "Notes from Underground"
322
+ }""" ),
323
+ BsonDocument .parse (
324
+ """
325
+ {
326
+ _id: 5,
327
+ title: "Fathers and Sons"
328
+ }""" ));
329
+
330
+ var realDocuments = ((MongoPreparedStatement ) pstmt )
331
+ .getMongoDatabase ()
332
+ .getCollection ("books" , BsonDocument .class )
333
+ .find ()
334
+ .into (new HashSet <>());
335
+ assertEquals (expectedDocuments , realDocuments );
336
+ }
337
+ });
338
+ }
339
+ }
215
340
}
0 commit comments