Skip to content

Commit

Permalink
More try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed May 18, 2024
1 parent 67b0164 commit 750a881
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 58 deletions.
86 changes: 45 additions & 41 deletions src/test/java/org/sqlite/ConnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,46 +159,49 @@ public void loadExtension() throws SQLiteException {

@Test
public void limit() throws SQLiteException {
final Conn c = open();
final int max = c.getLimit(SQLITE_LIMIT_VARIABLE_NUMBER);
assertEquals(max, c.setLimit(SQLITE_LIMIT_VARIABLE_NUMBER, max+1));
assertEquals(max, c.getLimit(SQLITE_LIMIT_VARIABLE_NUMBER)); // SQLITE_MAX_VARIABLE_NUMBER
assertEquals(max, c.setLimit(SQLITE_LIMIT_VARIABLE_NUMBER, max-1));
assertEquals(max-1, c.getLimit(SQLITE_LIMIT_VARIABLE_NUMBER));
try (Conn c = open()) {
final int max = c.getLimit(SQLITE_LIMIT_VARIABLE_NUMBER);
assertEquals(max, c.setLimit(SQLITE_LIMIT_VARIABLE_NUMBER, max + 1));
assertEquals(max, c.getLimit(SQLITE_LIMIT_VARIABLE_NUMBER)); // SQLITE_MAX_VARIABLE_NUMBER
assertEquals(max, c.setLimit(SQLITE_LIMIT_VARIABLE_NUMBER, max - 1));
assertEquals(max - 1, c.getLimit(SQLITE_LIMIT_VARIABLE_NUMBER));
}
}

@Test
public void trace() throws SQLiteException {
final Conn c = open();
final String[] traces = new String[1];
c.trace(new TraceCallback() {
private int i;
try (Conn c = open()) {
final String[] traces = new String[1];
c.trace(new TraceCallback() {
private int i;

@Override
public void trace(String sql) {
traces[i++] = sql;
}
});
final String sql = "SELECT 1";
c.fastExec(sql);
assertArrayEquals("traces", new String[]{sql}, traces);
@Override
public void trace(String sql) {
traces[i++] = sql;
}
});
final String sql = "SELECT 1";
c.fastExec(sql);
assertArrayEquals("traces", new String[]{sql}, traces);
}
}

@Test
public void profile() throws SQLiteException {
final Conn c = open();
final String[] profiles = new String[1];
c.profile(new ProfileCallback() {
private int i;
try (Conn c = open()) {
final String[] profiles = new String[1];
c.profile(new ProfileCallback() {
private int i;

@Override
public void profile(String sql, long ns) {
profiles[i++] = sql;
}
});
final String sql = "SELECT 1";
c.fastExec(sql);
assertArrayEquals("profiles", new String[]{sql}, profiles);
@Override
public void profile(String sql, long ns) {
profiles[i++] = sql;
}
});
final String sql = "SELECT 1";
c.fastExec(sql);
assertArrayEquals("profiles", new String[]{sql}, profiles);
}
}

@Test
Expand Down Expand Up @@ -318,17 +321,18 @@ public void virtualTable() throws SQLiteException {

@Test
public void updateHook() throws SQLiteException {
final Conn c = open();
AtomicInteger count = new AtomicInteger();
c.updateHook(new UpdateHook() {
@Override
public void update(int actionCode, String dbName, String tblName, long rowId) {
count.incrementAndGet();
}
});
c.fastExec("CREATE TABLE test AS SELECT 0 as x;");
assertEquals(1, c.execDml("UPDATE test SET x = 1;", false));
assertEquals(1, count.get());
try (Conn c = open()) {
AtomicInteger count = new AtomicInteger();
c.updateHook(new UpdateHook() {
@Override
public void update(int actionCode, String dbName, String tblName, long rowId) {
count.incrementAndGet();
}
});
c.fastExec("CREATE TABLE test AS SELECT 0 as x;");
assertEquals(1, c.execDml("UPDATE test SET x = 1;", false));
assertEquals(1, count.get());
}
}

private static class ConnState {
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/org/sqlite/StmtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ public void checkBind() throws SQLiteException {

@Test
public void expandedSql() throws SQLiteException {
final Conn c = ConnTest.open();
final Stmt s = c.prepare("SELECT ?", false);
assertNotNull(s);
s.bind("TEST");
assertEquals("SELECT 'TEST'", s.getExpandedSql());
try (Conn c = ConnTest.open()) {
final Stmt s = c.prepare("SELECT ?", false);
assertNotNull(s);
s.bind("TEST");
assertEquals("SELECT 'TEST'", s.getExpandedSql());
}
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/sqlite/driver/BasicQueriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testBadFunction() throws Exception {
@Test
public void testImmediateExecuteQuery2() throws Exception {
try (Statement stmt = conn.createStatement()) {
stmt.executeQuery("select 1");
try(ResultSet rs = stmt.executeQuery("select 1")) {}
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/test/java/org/sqlite/driver/BlobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ public void setBinaryStream() throws SQLException {
try (Statement stmt = c.createStatement()) {
stmt.execute("CREATE TABLE test (data BLOB)");
assertEquals(1, stmt.executeUpdate("INSERT INTO test (data) VALUES (zeroblob(1024))"));
ResultSet rs = stmt.executeQuery("SELECT last_insert_rowid()");
assertTrue(rs.next());
rowid = rs.getLong(1);
assertTrue(rowid > 0);
assertFalse(rs.next());
try (ResultSet rs = stmt.executeQuery("SELECT last_insert_rowid()")) {
assertTrue(rs.next());
rowid = rs.getLong(1);
assertTrue(rowid > 0);
assertFalse(rs.next());
}
}

try (PreparedStatement pstmt = c.prepareStatement("UPDATE test SET data = :blob WHERE rowid = :rowid")) {
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/org/sqlite/driver/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,8 @@ public void URIFilenames() throws SQLException {
stmt1.executeUpdate("create table tbl (col int)");
stmt1.executeUpdate("insert into tbl values(100)");
}

Connection conn2 = DriverManager.getConnection("jdbc:sqlite:file:memdb1?mode=memory&cache=shared");
try (Statement stmt2 = conn2.createStatement();
try (Connection conn2 = DriverManager.getConnection("jdbc:sqlite:file:memdb1?mode=memory&cache=shared");
Statement stmt2 = conn2.createStatement();
ResultSet rs = stmt2.executeQuery("select * from tbl")) {
assertTrue(rs.next());
assertEquals(100, rs.getInt(1));
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/org/sqlite/driver/PrepStmtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ public void colNameAccess() throws SQLException {
prep.setNull(1, 0);
prep.setFloat(2, Float.MIN_VALUE);
prep.setShort(3, Short.MIN_VALUE);
prep.executeQuery();
try (ResultSet rs = prep.executeQuery()) {
assertTrue(rs.next());
assertNull(rs.getString("col1"));
Expand Down Expand Up @@ -531,8 +530,10 @@ public void metaData() throws SQLException {
prep.setInt(1, 2);
prep.setInt(2, 3);
prep.setInt(3, -1);
meta = prep.executeQuery().getMetaData();
assertEquals(3, meta.getColumnCount());
try (ResultSet resultSet = prep.executeQuery()) {
meta = resultSet.getMetaData();
assertEquals(3, meta.getColumnCount());
}
}
}
#endif
Expand Down

0 comments on commit 750a881

Please sign in to comment.