| 1 |
/* |
| 2 |
* Copyright (C) 2013 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.mocks; |
| 18 |
|
| 19 |
import java.sql.Array; |
| 20 |
import java.sql.Blob; |
| 21 |
import java.sql.CallableStatement; |
| 22 |
import java.sql.Clob; |
| 23 |
import java.sql.Connection; |
| 24 |
import java.sql.DatabaseMetaData; |
| 25 |
import java.sql.NClob; |
| 26 |
import java.sql.PreparedStatement; |
| 27 |
import java.sql.SQLClientInfoException; |
| 28 |
import java.sql.SQLException; |
| 29 |
import java.sql.SQLWarning; |
| 30 |
import java.sql.SQLXML; |
| 31 |
import java.sql.Savepoint; |
| 32 |
import java.sql.Statement; |
| 33 |
import java.sql.Struct; |
| 34 |
import java.util.Map; |
| 35 |
import java.util.Properties; |
| 36 |
import java.util.concurrent.Executor; |
| 37 |
import java.util.concurrent.atomic.AtomicInteger; |
| 38 |
|
| 39 |
import com.zaxxer.hikari.util.UtilityElf; |
| 40 |
|
| 41 |
/** |
| 42 |
* |
| 43 |
* @author Brett Wooldridge |
| 44 |
*/ |
| 45 |
public class StubConnection extends StubBaseConnection implements Connection |
| 46 |
{ |
| 47 |
public static final AtomicInteger count = new AtomicInteger(); |
| 48 |
public static volatile boolean slowCreate; |
| 49 |
public static volatile boolean oldDriver; |
| 50 |
|
| 51 |
private static long foo; |
| 52 |
private boolean autoCommit; |
| 53 |
private int isolation = Connection.TRANSACTION_READ_COMMITTED; |
| 54 |
private String catalog; |
| 55 |
|
| 56 |
static { |
| 57 |
foo = System.currentTimeMillis(); |
| 58 |
} |
| 59 |
|
| 60 |
public StubConnection() { |
| 61 |
count.incrementAndGet(); |
| 62 |
if (slowCreate) { |
| 63 |
UtilityElf.quietlySleep(1000); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** {@inheritDoc} */ |
| 68 |
@SuppressWarnings("unchecked") |
| 69 |
@Override |
| 70 |
public <T> T unwrap(Class<T> iface) throws SQLException |
| 71 |
{ |
| 72 |
if (throwException) { |
| 73 |
throw new SQLException(); |
| 74 |
} |
| 75 |
|
| 76 |
if (iface.isInstance(this)) { |
| 77 |
return (T) this; |
| 78 |
} |
| 79 |
|
| 80 |
throw new SQLException("Wrapped connection is not an instance of " + iface); |
| 81 |
} |
| 82 |
|
| 83 |
/** {@inheritDoc} */ |
| 84 |
@Override |
| 85 |
public boolean isWrapperFor(Class<?> iface) throws SQLException |
| 86 |
{ |
| 87 |
if (throwException) { |
| 88 |
throw new SQLException(); |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
/** {@inheritDoc} */ |
| 94 |
@Override |
| 95 |
public CallableStatement prepareCall(String sql) throws SQLException |
| 96 |
{ |
| 97 |
if (throwException) { |
| 98 |
throw new SQLException(); |
| 99 |
} |
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
/** {@inheritDoc} */ |
| 104 |
@Override |
| 105 |
public String nativeSQL(String sql) throws SQLException |
| 106 |
{ |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
/** {@inheritDoc} */ |
| 111 |
@Override |
| 112 |
public void setAutoCommit(boolean autoCommit) throws SQLException |
| 113 |
{ |
| 114 |
if (throwException) { |
| 115 |
throw new SQLException(); |
| 116 |
} |
| 117 |
this.autoCommit = autoCommit; |
| 118 |
} |
| 119 |
|
| 120 |
/** {@inheritDoc} */ |
| 121 |
@Override |
| 122 |
public boolean getAutoCommit() throws SQLException |
| 123 |
{ |
| 124 |
return autoCommit; |
| 125 |
} |
| 126 |
|
| 127 |
/** {@inheritDoc} */ |
| 128 |
@Override |
| 129 |
public void commit() throws SQLException |
| 130 |
{ |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
/** {@inheritDoc} */ |
| 135 |
@Override |
| 136 |
public void rollback() throws SQLException |
| 137 |
{ |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
/** {@inheritDoc} */ |
| 142 |
@Override |
| 143 |
public void close() throws SQLException |
| 144 |
{ |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** {@inheritDoc} */ |
| 149 |
@Override |
| 150 |
public boolean isClosed() throws SQLException |
| 151 |
{ |
| 152 |
if (throwException) { |
| 153 |
throw new SQLException(); |
| 154 |
} |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
/** {@inheritDoc} */ |
| 159 |
@Override |
| 160 |
public DatabaseMetaData getMetaData() throws SQLException |
| 161 |
{ |
| 162 |
return null; |
| 163 |
} |
| 164 |
|
| 165 |
/** {@inheritDoc} */ |
| 166 |
@Override |
| 167 |
public void setReadOnly(boolean readOnly) throws SQLException |
| 168 |
{ |
| 169 |
if (throwException) { |
| 170 |
throw new SQLException(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** {@inheritDoc} */ |
| 175 |
@Override |
| 176 |
public boolean isReadOnly() throws SQLException |
| 177 |
{ |
| 178 |
if (throwException) { |
| 179 |
throw new SQLException(); |
| 180 |
} |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
/** {@inheritDoc} */ |
| 185 |
@Override |
| 186 |
public void setCatalog(String catalog) throws SQLException |
| 187 |
{ |
| 188 |
if (throwException) { |
| 189 |
throw new SQLException(); |
| 190 |
} |
| 191 |
this.catalog = catalog; |
| 192 |
} |
| 193 |
|
| 194 |
/** {@inheritDoc} */ |
| 195 |
@Override |
| 196 |
public String getCatalog() throws SQLException |
| 197 |
{ |
| 198 |
return catalog; |
| 199 |
} |
| 200 |
|
| 201 |
/** {@inheritDoc} */ |
| 202 |
@Override |
| 203 |
public void setTransactionIsolation(int level) throws SQLException |
| 204 |
{ |
| 205 |
if (throwException) { |
| 206 |
throw new SQLException(); |
| 207 |
} |
| 208 |
this.isolation = level; |
| 209 |
} |
| 210 |
|
| 211 |
/** {@inheritDoc} */ |
| 212 |
@Override |
| 213 |
public int getTransactionIsolation() throws SQLException |
| 214 |
{ |
| 215 |
return isolation; |
| 216 |
} |
| 217 |
|
| 218 |
/** {@inheritDoc} */ |
| 219 |
@Override |
| 220 |
public SQLWarning getWarnings() throws SQLException |
| 221 |
{ |
| 222 |
return null; |
| 223 |
} |
| 224 |
|
| 225 |
/** {@inheritDoc} */ |
| 226 |
@Override |
| 227 |
public void clearWarnings() throws SQLException |
| 228 |
{ |
| 229 |
if (throwException) { |
| 230 |
throw new SQLException(); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/** {@inheritDoc} */ |
| 235 |
@Override |
| 236 |
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException |
| 237 |
{ |
| 238 |
if (throwException) { |
| 239 |
throw new SQLException(); |
| 240 |
} |
| 241 |
return null; |
| 242 |
} |
| 243 |
|
| 244 |
/** {@inheritDoc} */ |
| 245 |
@Override |
| 246 |
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException |
| 247 |
{ |
| 248 |
if (throwException) { |
| 249 |
throw new SQLException(); |
| 250 |
} |
| 251 |
return new StubPreparedStatement(this); |
| 252 |
} |
| 253 |
|
| 254 |
/** {@inheritDoc} */ |
| 255 |
@Override |
| 256 |
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException |
| 257 |
{ |
| 258 |
if (throwException) { |
| 259 |
throw new SQLException(); |
| 260 |
} |
| 261 |
return null; |
| 262 |
} |
| 263 |
|
| 264 |
/** {@inheritDoc} */ |
| 265 |
@Override |
| 266 |
public Map<String, Class<?>> getTypeMap() throws SQLException |
| 267 |
{ |
| 268 |
return null; |
| 269 |
} |
| 270 |
|
| 271 |
/** {@inheritDoc} */ |
| 272 |
@Override |
| 273 |
public void setTypeMap(Map<String, Class<?>> map) throws SQLException |
| 274 |
{ |
| 275 |
} |
| 276 |
|
| 277 |
/** {@inheritDoc} */ |
| 278 |
@Override |
| 279 |
public void setHoldability(int holdability) throws SQLException |
| 280 |
{ |
| 281 |
} |
| 282 |
|
| 283 |
/** {@inheritDoc} */ |
| 284 |
@Override |
| 285 |
public int getHoldability() throws SQLException |
| 286 |
{ |
| 287 |
return (int) foo; |
| 288 |
} |
| 289 |
|
| 290 |
/** {@inheritDoc} */ |
| 291 |
@Override |
| 292 |
public Savepoint setSavepoint() throws SQLException |
| 293 |
{ |
| 294 |
return null; |
| 295 |
} |
| 296 |
|
| 297 |
/** {@inheritDoc} */ |
| 298 |
@Override |
| 299 |
public Savepoint setSavepoint(String name) throws SQLException |
| 300 |
{ |
| 301 |
return null; |
| 302 |
} |
| 303 |
|
| 304 |
/** {@inheritDoc} */ |
| 305 |
@Override |
| 306 |
public void rollback(Savepoint savepoint) throws SQLException |
| 307 |
{ |
| 308 |
} |
| 309 |
|
| 310 |
/** {@inheritDoc} */ |
| 311 |
@Override |
| 312 |
public void releaseSavepoint(Savepoint savepoint) throws SQLException |
| 313 |
{ |
| 314 |
} |
| 315 |
|
| 316 |
/** {@inheritDoc} */ |
| 317 |
@Override |
| 318 |
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException |
| 319 |
{ |
| 320 |
if (throwException) { |
| 321 |
throw new SQLException(); |
| 322 |
} |
| 323 |
return null; |
| 324 |
} |
| 325 |
|
| 326 |
/** {@inheritDoc} */ |
| 327 |
@Override |
| 328 |
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException |
| 329 |
{ |
| 330 |
if (throwException) { |
| 331 |
throw new SQLException(); |
| 332 |
} |
| 333 |
return new StubPreparedStatement(this); |
| 334 |
} |
| 335 |
|
| 336 |
/** {@inheritDoc} */ |
| 337 |
@Override |
| 338 |
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException |
| 339 |
{ |
| 340 |
if (throwException) { |
| 341 |
throw new SQLException(); |
| 342 |
} |
| 343 |
return null; |
| 344 |
} |
| 345 |
|
| 346 |
/** {@inheritDoc} */ |
| 347 |
@Override |
| 348 |
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException |
| 349 |
{ |
| 350 |
if (throwException) { |
| 351 |
throw new SQLException(); |
| 352 |
} |
| 353 |
return new StubPreparedStatement(this); |
| 354 |
} |
| 355 |
|
| 356 |
/** {@inheritDoc} */ |
| 357 |
@Override |
| 358 |
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException |
| 359 |
{ |
| 360 |
if (throwException) { |
| 361 |
throw new SQLException(); |
| 362 |
} |
| 363 |
return new StubPreparedStatement(this); |
| 364 |
} |
| 365 |
|
| 366 |
/** {@inheritDoc} */ |
| 367 |
@Override |
| 368 |
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException |
| 369 |
{ |
| 370 |
if (throwException) { |
| 371 |
throw new SQLException(); |
| 372 |
} |
| 373 |
return new StubPreparedStatement(this); |
| 374 |
} |
| 375 |
|
| 376 |
/** {@inheritDoc} */ |
| 377 |
@Override |
| 378 |
public Clob createClob() throws SQLException |
| 379 |
{ |
| 380 |
return null; |
| 381 |
} |
| 382 |
|
| 383 |
/** {@inheritDoc} */ |
| 384 |
@Override |
| 385 |
public Blob createBlob() throws SQLException |
| 386 |
{ |
| 387 |
return null; |
| 388 |
} |
| 389 |
|
| 390 |
/** {@inheritDoc} */ |
| 391 |
@Override |
| 392 |
public NClob createNClob() throws SQLException |
| 393 |
{ |
| 394 |
return null; |
| 395 |
} |
| 396 |
|
| 397 |
/** {@inheritDoc} */ |
| 398 |
@Override |
| 399 |
public SQLXML createSQLXML() throws SQLException |
| 400 |
{ |
| 401 |
return null; |
| 402 |
} |
| 403 |
|
| 404 |
/** {@inheritDoc} */ |
| 405 |
@Override |
| 406 |
public boolean isValid(int timeout) throws SQLException |
| 407 |
{ |
| 408 |
if (throwException) { |
| 409 |
throw new SQLException(); |
| 410 |
} |
| 411 |
return true; |
| 412 |
} |
| 413 |
|
| 414 |
/** {@inheritDoc} */ |
| 415 |
@Override |
| 416 |
public void setClientInfo(String name, String value) throws SQLClientInfoException |
| 417 |
{ |
| 418 |
} |
| 419 |
|
| 420 |
/** {@inheritDoc} */ |
| 421 |
@Override |
| 422 |
public void setClientInfo(Properties properties) throws SQLClientInfoException |
| 423 |
{ |
| 424 |
} |
| 425 |
|
| 426 |
/** {@inheritDoc} */ |
| 427 |
@Override |
| 428 |
public String getClientInfo(String name) throws SQLException |
| 429 |
{ |
| 430 |
return null; |
| 431 |
} |
| 432 |
|
| 433 |
/** {@inheritDoc} */ |
| 434 |
@Override |
| 435 |
public Properties getClientInfo() throws SQLException |
| 436 |
{ |
| 437 |
return null; |
| 438 |
} |
| 439 |
|
| 440 |
/** {@inheritDoc} */ |
| 441 |
@Override |
| 442 |
public Array createArrayOf(String typeName, Object[] elements) throws SQLException |
| 443 |
{ |
| 444 |
return null; |
| 445 |
} |
| 446 |
|
| 447 |
/** {@inheritDoc} */ |
| 448 |
@Override |
| 449 |
public Struct createStruct(String typeName, Object[] attributes) throws SQLException |
| 450 |
{ |
| 451 |
return null; |
| 452 |
} |
| 453 |
|
| 454 |
/** {@inheritDoc} */ |
| 455 |
public void setSchema(String schema) throws SQLException |
| 456 |
{ |
| 457 |
} |
| 458 |
|
| 459 |
/** {@inheritDoc} */ |
| 460 |
public String getSchema() throws SQLException |
| 461 |
{ |
| 462 |
return null; |
| 463 |
} |
| 464 |
|
| 465 |
/** {@inheritDoc} */ |
| 466 |
public void abort(Executor executor) throws SQLException |
| 467 |
{ |
| 468 |
throw new SQLException("Intentional exception during abort"); |
| 469 |
} |
| 470 |
|
| 471 |
/** {@inheritDoc} */ |
| 472 |
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException |
| 473 |
{ |
| 474 |
} |
| 475 |
|
| 476 |
/** {@inheritDoc} */ |
| 477 |
public int getNetworkTimeout() throws SQLException |
| 478 |
{ |
| 479 |
if (oldDriver) { |
| 480 |
throw new AbstractMethodError(); |
| 481 |
} |
| 482 |
|
| 483 |
return 0; |
| 484 |
} |
| 485 |
|
| 486 |
} |