diff --git a/checker/tests/resourceleak/JdbcResourceLeak.java b/checker/tests/resourceleak/JdbcResourceLeak.java new file mode 100644 index 000000000000..f9ccaf8235d6 --- /dev/null +++ b/checker/tests/resourceleak/JdbcResourceLeak.java @@ -0,0 +1,67 @@ +// Tests for JDBC types resource leak detection +// Test case for issue 6354: +// https://github.com/typetools/checker-framework/issues/6354 + +import java.sql.*; + +class JdbcResourceLeak { + + // ========== ResultSet Tests ========== + + void resultSetNotClosed(Statement stmt) throws SQLException { + ResultSet rs = stmt.executeQuery("SELECT 1"); + // :: error: [required.method.not.called] + } + + void resultSetClosed(Statement stmt) throws SQLException { + ResultSet rs = stmt.executeQuery("SELECT 1"); + rs.close(); + } + + // ========== Statement Tests ========== + + void statementNotClosed(Connection conn) throws SQLException { + Statement stmt = conn.createStatement(); + // :: error: [required.method.not.called] + } + + void statementClosed(Connection conn) throws SQLException { + Statement stmt = conn.createStatement(); + stmt.close(); + } + + // ========== PreparedStatement Tests ========== + + void preparedStatementNotClosed(Connection conn) throws SQLException { + PreparedStatement ps = conn.prepareStatement("SELECT ?"); + // :: error: [required.method.not.called] + } + + void preparedStatementClosed(Connection conn) throws SQLException { + PreparedStatement ps = conn.prepareStatement("SELECT ?"); + ps.close(); + } + + // ========== Nested Resources ========== + + void nestedBothClosed(Connection conn) throws SQLException { + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT 1"); + rs.close(); + stmt.close(); + } + + void nestedStatementNotClosed(Connection conn) throws SQLException { + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT 1"); + rs.close(); + // :: error: [required.method.not.called] + } + + void nestedResultSetNotClosed(Connection conn) throws SQLException { + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT 1"); + stmt.close(); + // :: error: [required.method.not.called] + } +} diff --git a/checker/tests/resourceleak/RowSetResourceLeak.java b/checker/tests/resourceleak/RowSetResourceLeak.java new file mode 100644 index 000000000000..d2fdb93b6144 --- /dev/null +++ b/checker/tests/resourceleak/RowSetResourceLeak.java @@ -0,0 +1,5 @@ +// Tests for RowSet types resource leak detection +// Test case for issue 6354: +// https://github.com/typetools/checker-framework/issues/6354 + +class RowSetResourceLeak {}