| 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 org.junit.Assert.assertFalse; |
| 21 |
import static org.junit.Assert.assertNotNull; |
| 22 |
import static org.junit.Assert.assertTrue; |
| 23 |
import static org.junit.Assert.fail; |
| 24 |
|
| 25 |
import java.sql.Connection; |
| 26 |
import java.sql.PreparedStatement; |
| 27 |
import java.sql.ResultSet; |
| 28 |
import java.sql.SQLException; |
| 29 |
import java.util.concurrent.TimeUnit; |
| 30 |
|
| 31 |
import org.junit.Test; |
| 32 |
|
| 33 |
import com.zaxxer.hikari.HikariConfig; |
| 34 |
import com.zaxxer.hikari.HikariDataSource; |
| 35 |
import com.zaxxer.hikari.mocks.StubConnection; |
| 36 |
import com.zaxxer.hikari.mocks.StubStatement; |
| 37 |
|
| 38 |
public class TestProxies |
| 39 |
{ |
| 40 |
@Test |
| 41 |
public void testProxyCreation() throws SQLException |
| 42 |
{ |
| 43 |
HikariConfig config = newHikariConfig(); |
| 44 |
config.setMinimumIdle(0); |
| 45 |
config.setMaximumPoolSize(1); |
| 46 |
config.setConnectionTestQuery("VALUES 1"); |
| 47 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 48 |
|
| 49 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 50 |
Connection conn = ds.getConnection(); |
| 51 |
|
| 52 |
assertNotNull(conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE)); |
| 53 |
assertNotNull(conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT)); |
| 54 |
assertNotNull(conn.prepareCall("some sql")); |
| 55 |
assertNotNull(conn.prepareCall("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE)); |
| 56 |
assertNotNull(conn.prepareCall("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT)); |
| 57 |
assertNotNull(conn.prepareStatement("some sql", PreparedStatement.NO_GENERATED_KEYS)); |
| 58 |
assertNotNull(conn.prepareStatement("some sql", new int[3])); |
| 59 |
assertNotNull(conn.prepareStatement("some sql", new String[3])); |
| 60 |
assertNotNull(conn.prepareStatement("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE)); |
| 61 |
assertNotNull(conn.prepareStatement("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT)); |
| 62 |
assertNotNull(conn.toString()); |
| 63 |
|
| 64 |
assertTrue(conn.isWrapperFor(Connection.class)); |
| 65 |
assertTrue(conn.isValid(10)); |
| 66 |
assertFalse(conn.isClosed()); |
| 67 |
assertTrue(conn.unwrap(StubConnection.class) instanceof StubConnection); |
| 68 |
try { |
| 69 |
conn.unwrap(TestProxies.class); |
| 70 |
fail(); |
| 71 |
} |
| 72 |
catch (SQLException e) { |
| 73 |
// pass |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void testStatementProxy() throws SQLException |
| 80 |
{ |
| 81 |
HikariConfig config = newHikariConfig(); |
| 82 |
config.setMinimumIdle(0); |
| 83 |
config.setMaximumPoolSize(1); |
| 84 |
config.setConnectionTestQuery("VALUES 1"); |
| 85 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 86 |
|
| 87 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 88 |
Connection conn = ds.getConnection(); |
| 89 |
|
| 90 |
PreparedStatement stmt = conn.prepareStatement("some sql"); |
| 91 |
stmt.executeQuery(); |
| 92 |
stmt.executeQuery("some sql"); |
| 93 |
assertFalse(stmt.isClosed()); |
| 94 |
assertNotNull(stmt.getGeneratedKeys()); |
| 95 |
assertNotNull(stmt.getResultSet()); |
| 96 |
assertNotNull(stmt.getConnection()); |
| 97 |
assertTrue(stmt.unwrap(StubStatement.class) instanceof StubStatement); |
| 98 |
try { |
| 99 |
stmt.unwrap(TestProxies.class); |
| 100 |
fail(); |
| 101 |
} |
| 102 |
catch (SQLException e) { |
| 103 |
// pass |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void testStatementExceptions() throws SQLException |
| 110 |
{ |
| 111 |
HikariConfig config = newHikariConfig(); |
| 112 |
config.setMinimumIdle(0); |
| 113 |
config.setMaximumPoolSize(1); |
| 114 |
config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(1)); |
| 115 |
config.setConnectionTestQuery("VALUES 1"); |
| 116 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 117 |
|
| 118 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 119 |
Connection conn = ds.getConnection(); |
| 120 |
StubConnection stubConnection = conn.unwrap(StubConnection.class); |
| 121 |
stubConnection.throwException = true; |
| 122 |
|
| 123 |
try { |
| 124 |
conn.createStatement(); |
| 125 |
fail(); |
| 126 |
} |
| 127 |
catch (SQLException e) { |
| 128 |
// pass |
| 129 |
} |
| 130 |
|
| 131 |
try { |
| 132 |
conn.createStatement(0, 0); |
| 133 |
fail(); |
| 134 |
} |
| 135 |
catch (SQLException e) { |
| 136 |
// pass |
| 137 |
} |
| 138 |
|
| 139 |
try { |
| 140 |
conn.createStatement(0, 0, 0); |
| 141 |
fail(); |
| 142 |
} |
| 143 |
catch (SQLException e) { |
| 144 |
// pass |
| 145 |
} |
| 146 |
|
| 147 |
try { |
| 148 |
conn.prepareCall(""); |
| 149 |
fail(); |
| 150 |
} |
| 151 |
catch (SQLException e) { |
| 152 |
// pass |
| 153 |
} |
| 154 |
|
| 155 |
try { |
| 156 |
conn.prepareCall("", 0, 0); |
| 157 |
fail(); |
| 158 |
} |
| 159 |
catch (SQLException e) { |
| 160 |
// pass |
| 161 |
} |
| 162 |
|
| 163 |
try { |
| 164 |
conn.prepareCall("", 0, 0, 0); |
| 165 |
fail(); |
| 166 |
} |
| 167 |
catch (SQLException e) { |
| 168 |
// pass |
| 169 |
} |
| 170 |
|
| 171 |
try { |
| 172 |
conn.prepareStatement(""); |
| 173 |
fail(); |
| 174 |
} |
| 175 |
catch (SQLException e) { |
| 176 |
// pass |
| 177 |
} |
| 178 |
|
| 179 |
try { |
| 180 |
conn.prepareStatement("", 0); |
| 181 |
fail(); |
| 182 |
} |
| 183 |
catch (SQLException e) { |
| 184 |
// pass |
| 185 |
} |
| 186 |
|
| 187 |
try { |
| 188 |
conn.prepareStatement("", new int[0]); |
| 189 |
fail(); |
| 190 |
} |
| 191 |
catch (SQLException e) { |
| 192 |
// pass |
| 193 |
} |
| 194 |
|
| 195 |
try { |
| 196 |
conn.prepareStatement("", new String[0]); |
| 197 |
fail(); |
| 198 |
} |
| 199 |
catch (SQLException e) { |
| 200 |
// pass |
| 201 |
} |
| 202 |
|
| 203 |
try { |
| 204 |
conn.prepareStatement("", 0, 0); |
| 205 |
fail(); |
| 206 |
} |
| 207 |
catch (SQLException e) { |
| 208 |
// pass |
| 209 |
} |
| 210 |
|
| 211 |
try { |
| 212 |
conn.prepareStatement("", 0, 0, 0); |
| 213 |
fail(); |
| 214 |
} |
| 215 |
catch (SQLException e) { |
| 216 |
// pass |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
@Test |
| 222 |
public void testOtherExceptions() throws SQLException |
| 223 |
{ |
| 224 |
HikariConfig config = newHikariConfig(); |
| 225 |
config.setMinimumIdle(0); |
| 226 |
config.setMaximumPoolSize(1); |
| 227 |
config.setConnectionTestQuery("VALUES 1"); |
| 228 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 229 |
|
| 230 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 231 |
try (Connection conn = ds.getConnection()) { |
| 232 |
StubConnection stubConnection = conn.unwrap(StubConnection.class); |
| 233 |
stubConnection.throwException = true; |
| 234 |
|
| 235 |
try { |
| 236 |
conn.setTransactionIsolation(Connection.TRANSACTION_NONE); |
| 237 |
fail(); |
| 238 |
} |
| 239 |
catch (SQLException e) { |
| 240 |
// pass |
| 241 |
} |
| 242 |
|
| 243 |
try { |
| 244 |
conn.isReadOnly(); |
| 245 |
fail(); |
| 246 |
} |
| 247 |
catch (SQLException e) { |
| 248 |
// pass |
| 249 |
} |
| 250 |
|
| 251 |
try { |
| 252 |
conn.setReadOnly(false); |
| 253 |
fail(); |
| 254 |
} |
| 255 |
catch (SQLException e) { |
| 256 |
// pass |
| 257 |
} |
| 258 |
|
| 259 |
try { |
| 260 |
conn.setCatalog(""); |
| 261 |
fail(); |
| 262 |
} |
| 263 |
catch (SQLException e) { |
| 264 |
// pass |
| 265 |
} |
| 266 |
|
| 267 |
try { |
| 268 |
conn.setAutoCommit(false); |
| 269 |
fail(); |
| 270 |
} |
| 271 |
catch (SQLException e) { |
| 272 |
// pass |
| 273 |
} |
| 274 |
|
| 275 |
try { |
| 276 |
conn.clearWarnings(); |
| 277 |
fail(); |
| 278 |
} |
| 279 |
catch (SQLException e) { |
| 280 |
// pass |
| 281 |
} |
| 282 |
|
| 283 |
try { |
| 284 |
conn.isValid(0); |
| 285 |
fail(); |
| 286 |
} |
| 287 |
catch (SQLException e) { |
| 288 |
// pass |
| 289 |
} |
| 290 |
|
| 291 |
try { |
| 292 |
conn.isWrapperFor(getClass()); |
| 293 |
fail(); |
| 294 |
} |
| 295 |
catch (SQLException e) { |
| 296 |
// pass |
| 297 |
} |
| 298 |
|
| 299 |
try { |
| 300 |
conn.unwrap(getClass()); |
| 301 |
fail(); |
| 302 |
} |
| 303 |
catch (SQLException e) { |
| 304 |
// pass |
| 305 |
} |
| 306 |
|
| 307 |
try { |
| 308 |
conn.close(); |
| 309 |
fail(); |
| 310 |
} |
| 311 |
catch (SQLException e) { |
| 312 |
// pass |
| 313 |
} |
| 314 |
|
| 315 |
try { |
| 316 |
assertFalse(conn.isValid(0)); |
| 317 |
} |
| 318 |
catch (SQLException e) { |
| 319 |
fail(); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
} |