Skip to content

Commit

Permalink
net-sf-ucanaccess-fork: Clarify method usage, only assign Statement v…
Browse files Browse the repository at this point in the history
…ariable once
  • Loading branch information
spannm committed Nov 24, 2023
1 parent 9ceea10 commit 5b1d956
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/test/java/net/ucanaccess/jdbc/ComplexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

Expand All @@ -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 (?,?,?,?,?)")) {
Expand Down Expand Up @@ -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"));
}

}
16 changes: 8 additions & 8 deletions src/test/java/net/ucanaccess/jdbc/CrudTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

}
18 changes: 9 additions & 9 deletions src/test/java/net/ucanaccess/jdbc/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ 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"));
}
}

@ParameterizedTest(name = "[{index}] {0}")
@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);
}
}
Expand All @@ -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);
}
}
Expand Down
38 changes: 20 additions & 18 deletions src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5b1d956

Please sign in to comment.