From ce60eac5838a93428974aa19b182f43dc1321ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20P=C3=B6hler=20=28JPo=29?= Date: Fri, 20 Nov 2020 15:56:08 +0100 Subject: [PATCH] ADD: failing testcases for executeBatch() on SqlUpdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jonas Pöhler (JPo) --- .../org/tests/update/TestSqlUpdateBatch.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 ebean-core/src/test/java/org/tests/update/TestSqlUpdateBatch.java diff --git a/ebean-core/src/test/java/org/tests/update/TestSqlUpdateBatch.java b/ebean-core/src/test/java/org/tests/update/TestSqlUpdateBatch.java new file mode 100644 index 000000000..86958965e --- /dev/null +++ b/ebean-core/src/test/java/org/tests/update/TestSqlUpdateBatch.java @@ -0,0 +1,65 @@ +package org.tests.update; + +import io.ebean.BaseTestCase; +import io.ebean.DB; +import io.ebean.SqlUpdate; +import io.ebean.Transaction; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Testclass that ensures the correct behaviour of SqlUpdate in combination with batching. + * + * @author Jonas Pöhler, FOCONIS AG + */ +public class TestSqlUpdateBatch extends BaseTestCase { + + /** + * This test ensures the correct behaviour of update batching in the {@code BatchedPstmtHolder} after a flush. Two batch updates + * are executed with each a batchsize of 21, which is the default limit for automatic flushing on {@code addBatch()}. After that + * the statements are each executed with {@code executeBatch()}. The desired behaviour is, that each execution succeeds although + * technically the batch was already flushed. + */ + @Test + public void testTwoParallelBatches() { + try (Transaction txn = DB.beginTransaction()) { + // Dummy updates, that effectively do nothing, but ebean doesn't need to know this. + final SqlUpdate update = DB.sqlUpdate("update uuone set name = ? where 0=1"); + final SqlUpdate delete = DB.sqlUpdate("delete from uuone where ?=-1"); + + for (int i = 0; i <= 20; i++) { + update + .setParameter(1, String.valueOf(i)) + .addBatch(); + delete + .setParameter(1, String.valueOf(i)) + .addBatch(); + } + + delete.executeBatch(); + update.executeBatch(); + } + } + + /** + * This test checks that for a batch update a correct array with update counts is returned. If a update with 40 entries is + * executed, it is expected, that {@code executeBatch()} returns a array with 40 elements each containing the update count for + * the given parameters. + */ + @Test + public void testBatchReturnArrayLength() { + try (Transaction txn = DB.beginTransaction()) { + // Dummy update, that effectively does nothing, but ebean doesn't need to know this. + final SqlUpdate update = DB.sqlUpdate("update uuone set name = ? where 0=1"); + + for (int i = 0; i <= 40; i++) { + update + .setParameter(1, String.valueOf(i)) + .addBatch(); + } + assertEquals(40, update.executeBatch().length); + } + } + +}