forked from pgjdbc/pgjdbc
/
CloseOnCompletionTest.java
101 lines (84 loc) · 2.58 KB
/
CloseOnCompletionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright (c) 2007, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc4.jdbc41;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.postgresql.test.TestUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CloseOnCompletionTest {
private Connection conn;
@Before
public void setUp() throws Exception {
conn = TestUtil.openDB();
TestUtil.createTable(conn, "table1", "id integer");
}
@After
public void tearDown() throws SQLException {
TestUtil.dropTable(conn, "table1");
TestUtil.closeDB(conn);
}
/**
* Test that the statement is not automatically closed if we do not ask for it.
*/
@Test
public void testWithoutCloseOnCompletion() throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(TestUtil.selectSQL("table1", "*"));
rs.close();
assertFalse(stmt.isClosed());
}
/**
* Test the behavior of closeOnCompletion with a single result set.
*/
@Test
public void testSingleResultSet() throws SQLException {
Statement stmt = conn.createStatement();
stmt.closeOnCompletion();
ResultSet rs = stmt.executeQuery(TestUtil.selectSQL("table1", "*"));
rs.close();
assertTrue(stmt.isClosed());
}
/**
* Test the behavior of closeOnCompletion with a multiple result sets.
*/
@Test
public void testMultipleResultSet() throws SQLException {
Statement stmt = conn.createStatement();
stmt.closeOnCompletion();
stmt.execute(TestUtil.selectSQL("table1", "*") + ";" + TestUtil.selectSQL("table1", "*") + ";");
ResultSet rs = stmt.getResultSet();
rs.close();
assertFalse(stmt.isClosed());
stmt.getMoreResults();
rs = stmt.getResultSet();
rs.close();
assertTrue(stmt.isClosed());
}
/**
* Test that when execution does not produce any result sets, closeOnCompletion has no effect
* (spec).
*/
@Test
public void testNoResultSet() throws SQLException {
Statement stmt = conn.createStatement();
stmt.closeOnCompletion();
stmt.executeUpdate(TestUtil.insertSQL("table1", "1"));
assertFalse(stmt.isClosed());
}
@Test
public void testExecuteTwice() throws SQLException {
PreparedStatement s = conn.prepareStatement("SELECT 1");
s.closeOnCompletion();
s.executeQuery();
s.executeQuery();
}
}