From 5b1d9568d5b5a568a2dc708af41933898505213c Mon Sep 17 00:00:00 2001 From: Markus S Date: Fri, 24 Nov 2023 14:13:05 +0100 Subject: [PATCH] net-sf-ucanaccess-fork: Clarify method usage, only assign Statement variable once --- .../java/net/ucanaccess/jdbc/ComplexTest.java | 6 +-- .../java/net/ucanaccess/jdbc/CrudTest.java | 16 ++++---- .../ucanaccess/jdbc/PhysicalRollbackTest.java | 2 +- .../net/ucanaccess/jdbc/TransactionTest.java | 18 ++++----- .../ucanaccess/test/UcanaccessBaseTest.java | 38 ++++++++++--------- 5 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/test/java/net/ucanaccess/jdbc/ComplexTest.java b/src/test/java/net/ucanaccess/jdbc/ComplexTest.java index 916745b9..c1be7a73 100644 --- a/src/test/java/net/ucanaccess/jdbc/ComplexTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ComplexTest.java @@ -110,7 +110,7 @@ private void complex1() throws SQLException, IOException { ps.setObject(1, svs); ps.execute(); checkQuery("SELECT * FROM t_complex ORDER BY id"); - assertEquals(7, getCount("SELECT COUNT(*) FROM t_complex")); + assertEquals(7, getVerifyCount("SELECT COUNT(*) FROM t_complex")); } } @@ -125,7 +125,7 @@ void testComplexRollback(AccessVersion _accessVersion) throws SQLException, IOEx ucanaccess.setAutoCommit(false); - int countBefore = getCount("SELECT COUNT(*) FROM t_complex"); + int countBefore = getVerifyCount("SELECT COUNT(*) FROM t_complex"); try (PreparedStatement ps = ucanaccess.prepareStatement( "INSERT INTO t_complex(id, [memo-data], [append-memo-data], [multi-value-data], [attach-data]) VALUES (?,?,?,?,?)")) { @@ -155,7 +155,7 @@ void testComplexRollback(AccessVersion _accessVersion) throws SQLException, IOEx checkQuery("SELECT * FROM t_complex ORDER BY id"); dumpQueryResult("SELECT * FROM t_complex"); checkQuery("SELECT * FROM t_complex WHERE id='row12' ORDER BY id"); - assertEquals(countBefore, getCount("SELECT COUNT(*) FROM t_complex")); + assertEquals(countBefore, getVerifyCount("SELECT COUNT(*) FROM t_complex")); } } diff --git a/src/test/java/net/ucanaccess/jdbc/CrudTest.java b/src/test/java/net/ucanaccess/jdbc/CrudTest.java index ed245c01..40645d02 100644 --- a/src/test/java/net/ucanaccess/jdbc/CrudTest.java +++ b/src/test/java/net/ucanaccess/jdbc/CrudTest.java @@ -29,11 +29,11 @@ void testCrud(AccessVersion _accessVersion) throws SQLException { st.execute("DELETE FROM t1"); st.execute("INSERT INTO t1 (id, descr) VALUES(" + id1 + ", 'nel mezzo del cammin di nostra vita')"); - assertEquals(1, getCount("SELECT COUNT(*) FROM t1"), "Insert failed"); + assertEquals(1, getVerifyCount("SELECT COUNT(*) FROM t1"), "Insert failed"); st.executeUpdate("UPDATE t1 SET id=" + id2 + " WHERE id=" + id1); - assertEquals(1, getCount("SELECT COUNT(*) FROM t1 where id=" + id2), "Update failed"); + assertEquals(1, getVerifyCount("SELECT COUNT(*) FROM t1 where id=" + id2), "Update failed"); st.executeUpdate("DELETE FROM t1 WHERE id=" + id2); - assertEquals(0, getCount("SELECT COUNT(*) FROM t1 where id=" + id2), "Delete failed"); + assertEquals(0, getVerifyCount("SELECT COUNT(*) FROM t1 where id=" + id2), "Delete failed"); } } @@ -51,20 +51,20 @@ void testCrudPS(AccessVersion _accessVersion) throws SQLException { ps.setInt(1, id1); ps.setString(2, "Prep1"); ps.execute(); - assertEquals(1, getCount("SELECT COUNT(*) FROM t1"), "Insert failed"); + assertEquals(1, getVerifyCount("SELECT COUNT(*) FROM t1"), "Insert failed"); } try (PreparedStatement ps2 = ucanaccess.prepareStatement("UPDATE t1 SET id=? WHERE id=?")) { ps2.setInt(1, id2); ps2.setInt(2, id1); ps2.executeUpdate(); - assertEquals(1, getCount("SELECT COUNT(*) FROM t1 where id=" + id2), "Update failed"); + assertEquals(1, getVerifyCount("SELECT COUNT(*) FROM t1 where id=" + id2), "Update failed"); } try (PreparedStatement ps3 = ucanaccess.prepareStatement("DELETE * FROM t1 WHERE id=?")) { ps3.setInt(1, id2); ps3.executeUpdate(); - assertEquals(0, getCount("SELECT COUNT(*) FROM t1 WHERE id=" + id2), "Delete failed"); + assertEquals(0, getVerifyCount("SELECT COUNT(*) FROM t1 WHERE id=" + id2), "Delete failed"); } } @@ -86,13 +86,13 @@ void testCrudPSBatch(AccessVersion _accessVersion) throws SQLException { ps.clearBatch(); } checkQuery("SELECT * FROM t1", recs(rec(id1, "Prep1"), rec(id2, "Prep2"))); - assertEquals(2, getCount("SELECT COUNT(*) FROM t1 where id in (" + id1 + ", " + id2 + ")"), "Insert failed"); + assertEquals(2, getVerifyCount("SELECT COUNT(*) FROM t1 where id in (" + id1 + ", " + id2 + ")"), "Insert failed"); try (PreparedStatement ps = ucanaccess.prepareStatement("DELETE FROM t1")) { ps.addBatch(); ps.executeBatch(); } - assertEquals(0, getCount("SELECT COUNT(*) FROM t1"), "Delete failed"); + assertEquals(0, getVerifyCount("SELECT COUNT(*) FROM t1"), "Delete failed"); } @ParameterizedTest(name = "[{index}] {0}") diff --git a/src/test/java/net/ucanaccess/jdbc/PhysicalRollbackTest.java b/src/test/java/net/ucanaccess/jdbc/PhysicalRollbackTest.java index 0d6d9082..5d839e5e 100644 --- a/src/test/java/net/ucanaccess/jdbc/PhysicalRollbackTest.java +++ b/src/test/java/net/ucanaccess/jdbc/PhysicalRollbackTest.java @@ -52,7 +52,7 @@ void testCommit(AccessVersion _accessVersion) throws SQLException { ucanaccess = createUcanaccessConnection(); dumpQueryResult("SELECT * FROM T4"); - assertEquals(0, getCount("SELECT COUNT(*) FROM T4")); + assertEquals(0, getVerifyCount("SELECT COUNT(*) FROM T4")); } } diff --git a/src/test/java/net/ucanaccess/jdbc/TransactionTest.java b/src/test/java/net/ucanaccess/jdbc/TransactionTest.java index 5f4bb5fe..a0c34422 100644 --- a/src/test/java/net/ucanaccess/jdbc/TransactionTest.java +++ b/src/test/java/net/ucanaccess/jdbc/TransactionTest.java @@ -22,11 +22,11 @@ void testCommit(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); ucanaccess.setAutoCommit(false); try (UcanaccessStatement st = ucanaccess.createStatement()) { - int i = getCount("SELECT COUNT(*) FROM T4"); + int count = getVerifyCount("SELECT COUNT(*) FROM T4"); st.execute("INSERT INTO T4 (id, descr) VALUES(6666554, 'nel mezzo del cammin di nostra vita')"); - assertEquals(i, getCount("SELECT COUNT(*) FROM T4", false)); + assertEquals(count, getVerifyCount("SELECT COUNT(*) FROM T4", false)); ucanaccess.commit(); - assertEquals(i + 1, getCount("SELECT COUNT(*) FROM T4")); + assertEquals(count + 1, getVerifyCount("SELECT COUNT(*) FROM T4")); } } @@ -34,16 +34,16 @@ void testCommit(AccessVersion _accessVersion) throws SQLException { @EnumSource(value = AccessVersion.class) void testSavepoint(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); - int count = getCount("SELECT COUNT(*) FROM T4"); + int count = getVerifyCount("SELECT COUNT(*) FROM T4"); ucanaccess.setAutoCommit(false); try (UcanaccessStatement st = ucanaccess.createStatement()) { st.execute("INSERT INTO T4 (id, descr) VALUES(1, 'nel mezzo del cammin di nostra vita')"); Savepoint sp = ucanaccess.setSavepoint(); - assertEquals(count, getCount("SELECT COUNT(*) FROM T4", false)); + assertEquals(count, getVerifyCount("SELECT COUNT(*) FROM T4", false)); st.execute("INSERT INTO T4 (id, descr) VALUES(2, 'nel mezzo del cammin di nostra vita')"); ucanaccess.rollback(sp); ucanaccess.commit(); - assertEquals(count + 1, getCount("SELECT COUNT(*) FROM T4")); + assertEquals(count + 1, getVerifyCount("SELECT COUNT(*) FROM T4")); ucanaccess.setAutoCommit(false); } } @@ -52,16 +52,16 @@ void testSavepoint(AccessVersion _accessVersion) throws SQLException { @EnumSource(value = AccessVersion.class) void testSavepoint2(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); - int count = getCount("SELECT COUNT(*) FROM T4"); + int count = getVerifyCount("SELECT COUNT(*) FROM T4"); ucanaccess.setAutoCommit(false); try (UcanaccessStatement st = ucanaccess.createStatement()) { st.execute("INSERT INTO T4 (id, descr) VALUES(1, 'nel mezzo del cammin di nostra vita')"); Savepoint sp = ucanaccess.setSavepoint("Gord svp"); - assertEquals(count, getCount("SELECT COUNT(*) FROM T4", false)); + assertEquals(count, getVerifyCount("SELECT COUNT(*) FROM T4", false)); st.execute("INSERT INTO T4 (id, descr) VALUES(2, 'nel mezzo del cammin di nostra vita')"); ucanaccess.rollback(sp); ucanaccess.commit(); - assertEquals(count + 1, getCount("SELECT COUNT(*) FROM T4")); + assertEquals(count + 1, getVerifyCount("SELECT COUNT(*) FROM T4")); ucanaccess.setAutoCommit(false); } } diff --git a/src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java b/src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java index 7e87467d..eb7ac247 100644 --- a/src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java +++ b/src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java @@ -335,29 +335,31 @@ protected File copyResourceToTempFile(String _resourcePath) { } } - public int getCount(CharSequence _sql) throws SQLException { - return getCount(_sql, true); + public int getVerifyCount(CharSequence _sql) throws SQLException { + return getVerifyCount(_sql, true); } - public int getCount(CharSequence _sql, boolean _equals) throws SQLException { + public int getVerifyCount(CharSequence _sql, boolean _equals) throws SQLException { String sql = _sql == null ? null : _sql.toString(); initVerifyConnection(); - UcanaccessStatement st = verifyConnection.createStatement(); - ResultSet expectedResultset = st.executeQuery(sql); - expectedResultset.next(); - int expectedCount = expectedResultset.getInt(1); - - st = ucanaccess.createStatement(); - ResultSet actualResultset = st.executeQuery(sql); - actualResultset.next(); - int actualCount = actualResultset.getInt(1); - - if (_equals) { - assertEquals(expectedCount, actualCount); - } else { - assertNotEquals(expectedCount, actualCount); + + try (UcanaccessStatement stVerify = verifyConnection.createStatement(); + UcanaccessStatement stActual = ucanaccess.createStatement()) { + ResultSet expectedRs = stVerify.executeQuery(sql); + expectedRs.next(); + int expectedCount = expectedRs.getInt(1); + + ResultSet actualRs = stActual.executeQuery(sql); + actualRs.next(); + int actualCount = actualRs.getInt(1); + + if (_equals) { + assertEquals(expectedCount, actualCount); + } else { + assertNotEquals(expectedCount, actualCount); + } + return expectedCount; } - return expectedCount; } public String getName() {