Debian Codegraph @

src/test/java/com/zaxxer/hikari/pool/StatementTest.java

raw
1 /*
2 * Copyright (C) 2013, 2014 Brett Wooldridge
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.zaxxer.hikari.pool;
18
19 import static com.zaxxer.hikari.pool.TestElf.newHikariConfig;
20 import static com.zaxxer.hikari.pool.TestElf.getPool;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.sql.Connection;
25 import java.sql.SQLException;
26 import java.sql.Statement;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 import com.zaxxer.hikari.HikariConfig;
33 import com.zaxxer.hikari.HikariDataSource;
34
35 public class StatementTest
36 {
37 private HikariDataSource ds;
38
39 @Before
40 public void setup()
41 {
42 HikariConfig config = newHikariConfig();
43 config.setMinimumIdle(1);
44 config.setMaximumPoolSize(2);
45 config.setConnectionTestQuery("VALUES 1");
46 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
47
48 ds = new HikariDataSource(config);
49 }
50
51 @After
52 public void teardown()
53 {
54 ds.close();
55 }
56
57 @Test
58 public void testStatementClose() throws SQLException
59 {
60 ds.getConnection().close();
61
62 HikariPool pool = getPool(ds);
63 assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
64 assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 1);
65
66 try (Connection connection = ds.getConnection()) {
67 assertNotNull(connection);
68
69 assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
70 assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 0);
71
72 Statement statement = connection.createStatement();
73 assertNotNull(statement);
74
75 connection.close();
76
77 assertTrue(statement.isClosed());
78 }
79 }
80
81 @Test
82 public void testAutoStatementClose() throws SQLException
83 {
84 try (Connection connection = ds.getConnection()) {
85 assertNotNull(connection);
86
87 Statement statement1 = connection.createStatement();
88 assertNotNull(statement1);
89 Statement statement2 = connection.createStatement();
90 assertNotNull(statement2);
91
92 connection.close();
93
94 assertTrue(statement1.isClosed());
95 assertTrue(statement2.isClosed());
96 }
97 }
98
99 @Test
100 public void testStatementResultSetProxyClose() throws SQLException {
101 try (Connection connection = ds.getConnection()) {
102 assertNotNull(connection);
103
104 Statement statement1 = connection.createStatement();
105 assertNotNull(statement1);
106 Statement statement2 = connection.createStatement();
107 assertNotNull(statement2);
108
109 statement1.getResultSet().getStatement().close();
110 statement2.getGeneratedKeys().getStatement().close();
111
112 assertTrue(statement1.isClosed());
113 assertTrue(statement2.isClosed());
114 }
115 }
116
117 @Test
118 public void testDoubleStatementClose() throws SQLException
119 {
120 try (Connection connection = ds.getConnection();
121 Statement statement1 = connection.createStatement()) {
122 statement1.close();
123 statement1.close();
124 }
125 }
126
127 @Test
128 public void testOutOfOrderStatementClose() throws SQLException
129 {
130 try (Connection connection = ds.getConnection();
131 Statement statement1 = connection.createStatement();
132 Statement statement2 = connection.createStatement()) {
133 statement1.close();
134 statement2.close();
135 }
136 }
137 }