1 58 59 61 79 package com.mockobjects.sql; 80 81 import com.mockobjects.*; 82 83 import java.sql.*; 84 import java.util.Map ; 85 86 abstract class CommonMockConnection extends MockObject implements Connection { 87 88 90 private boolean autoCommit = false; 91 92 private final ExpectationValue myResultSetConcurrency; 93 private final ExpectationValue myResultSetType; 94 95 private final ExpectationList myAutoCommit; 96 private final ExpectationCounter myCloseCalls; 97 private final ExpectationCounter myCommitCalls; 98 private final ExpectationCounter myCreateStatementCalls; 99 100 private boolean myIsClosed; 101 102 private SQLException myCloseException; 103 104 private SQLException myIsClosedException; 105 106 private DatabaseMetaData myMetaData; 107 108 112 private final ReturnObjectList myPreparedStatements; 113 114 118 private final ExpectationCollection myPreparedStatementStrings; 119 120 private final ExpectationCounter myRollbackCalls; 121 122 private Statement myStatement; 123 124 private SQLException myStatementException = null; 125 126 public CommonMockConnection() { 127 this(CommonMockConnection.class.getName()); 128 } 129 130 public CommonMockConnection(String name) { 131 myAutoCommit = new ExpectationList(name + ".setAutoCommit"); 132 myCloseCalls = new ExpectationCounter(name + ".close"); 133 myCommitCalls = new ExpectationCounter(name + ".commit"); 134 myCreateStatementCalls = new ExpectationCounter(name + ".createStatement"); 135 myPreparedStatements = new ReturnObjectList(name + ".PreparedStatements"); 136 myPreparedStatementStrings = new ExpectationList(name + ".preparedStatementString"); 137 myRollbackCalls = new ExpectationCounter(name + ".rollback"); 138 myResultSetType = new ExpectationValue(name + ".resultSetType"); 139 myResultSetConcurrency = new ExpectationValue(name + ".resultSetConcurrency"); 140 } 141 142 146 public void addExpectedPreparedStatementString(String sql) { 147 myPreparedStatementStrings.addExpected(sql); 148 } 149 150 152 155 public void addExpectedAutoCommit(boolean autoCommit) { 156 myAutoCommit.addExpected(new Boolean (autoCommit)); 157 } 158 159 160 165 public void setExpectedCloseCalls(int callCount) { 166 myCloseCalls.setExpected(callCount); 167 } 168 169 174 public void setExpectedCommitCalls(int callCount) { 175 myCommitCalls.setExpected(callCount); 176 } 177 178 183 public void setExpectedCreateStatementCalls(int calls) { 184 myCreateStatementCalls.setExpected(calls); 185 } 186 187 192 public void setExpectedRollbackCalls(int callCount) { 193 myRollbackCalls.setExpected(callCount); 194 } 195 196 198 206 public void setExpectedResultSetConcurrency(int resultSetConcurrency) { 207 myResultSetConcurrency.setExpected(resultSetConcurrency); 208 } 209 210 218 public void setExpectedResultSetType(int resultSetType) { 219 myResultSetType.setExpected(resultSetType); 220 } 221 222 223 228 public void setupAddPreparedStatement(PreparedStatement prepared) { 229 myPreparedStatements.addObjectToReturn(prepared); 230 } 231 232 236 public void setupCloseException(SQLException aCloseException) { 237 myCloseException = aCloseException; 238 } 239 240 243 public void setupIsClose(boolean aIsClosed) { 244 myIsClosed = aIsClosed; 245 } 246 247 251 public void setupIsClosed(boolean aIsClosed) { 252 myIsClosed = aIsClosed; 253 } 254 255 259 public void setupIsClosedException(SQLException aIsClosedException) { 260 myIsClosedException = aIsClosedException; 261 } 262 263 266 public void setupMetaData(DatabaseMetaData metaData) { 267 myMetaData = metaData; 268 } 269 270 273 public void setupStatement(Statement statement) { 274 myStatement = statement; 275 } 276 277 281 public void setupThrowExceptionOnPrepareOrCreate(SQLException exception) { 282 myStatementException = exception; 283 } 284 285 public void setupAutoCommit(boolean autoCommitToReturn) { 286 autoCommit = autoCommitToReturn; 287 } 288 289 291 295 void throwStatementExceptionIfAny() throws SQLException { 296 if (null != myStatementException) { 297 throw myStatementException; 298 } 299 } 300 301 303 307 public void close() throws SQLException { 308 if (myCloseException != null) { 309 throw myCloseException; 310 } 311 myCloseCalls.inc(); 312 } 313 314 317 public void commit() throws SQLException { 318 myCommitCalls.inc(); 319 } 320 321 327 public Statement createStatement() throws SQLException { 328 myCreateStatementCalls.inc(); 329 throwStatementExceptionIfAny(); 330 return myStatement; 331 } 332 333 336 public DatabaseMetaData getMetaData() 337 throws SQLException { 338 return myMetaData; 339 } 340 341 345 public boolean isClosed() throws SQLException { 346 if (myIsClosedException != null) { 347 throw myIsClosedException; 348 } 349 return myIsClosed; 350 } 351 352 358 public PreparedStatement prepareStatement(String sql) throws SQLException { 359 myPreparedStatementStrings.addActual(sql); 360 throwStatementExceptionIfAny(); 361 return (PreparedStatement) myPreparedStatements.nextReturnObject(); 362 } 363 364 367 public void rollback() throws SQLException { 368 myRollbackCalls.inc(); 369 } 370 371 376 public void setAutoCommit(boolean autoCommit) throws SQLException { 377 myAutoCommit.addActual(new Boolean (autoCommit)); 378 } 379 380 382 385 public void clearWarnings() throws SQLException { 386 notImplemented(); 387 } 388 389 392 public Statement createStatement( 393 int resultSetType, 394 int resultSetConcurrency) 395 throws SQLException { 396 myCreateStatementCalls.inc(); 397 throwStatementExceptionIfAny(); 398 399 myResultSetType.setActual(resultSetType); 400 myResultSetConcurrency.setActual(resultSetConcurrency); 401 402 return myStatement; 403 } 404 405 public boolean getAutoCommit() throws SQLException { 406 return autoCommit; 407 } 408 409 412 public String getCatalog() throws SQLException { 413 notImplemented(); 414 return null; 415 } 416 417 420 public int getTransactionIsolation() throws SQLException { 421 notImplemented(); 422 return 0; 423 } 424 425 428 public Map getTypeMap() throws SQLException { 429 notImplemented(); 430 return null; 431 } 432 433 436 public SQLWarning getWarnings() throws SQLException { 437 notImplemented(); 438 return null; 439 } 440 441 444 public boolean isReadOnly() throws SQLException { 445 notImplemented(); 446 return false; 447 } 448 449 452 public String nativeSQL(String sql) throws SQLException { 453 notImplemented(); 454 return null; 455 } 456 457 460 public CallableStatement prepareCall(String sql) 461 throws SQLException { 462 notImplemented(); 463 return null; 464 } 465 466 469 public CallableStatement prepareCall( 470 String sql, 471 int resultSetType, 472 int resultSetConcurrency) 473 throws SQLException { 474 notImplemented(); 475 return null; 476 } 477 478 481 public PreparedStatement prepareStatement( 482 String sql, 483 int resultSetType, 484 int resultSetConcurrency) 485 throws SQLException { 486 notImplemented(); 487 return null; 488 } 489 490 493 public void setCatalog(String catalog) throws SQLException { 494 notImplemented(); 495 } 496 497 500 public void setReadOnly(boolean readOnly) throws SQLException { 501 notImplemented(); 502 } 503 504 507 public void setTransactionIsolation(int level) 508 throws SQLException { 509 notImplemented(); 510 } 511 512 515 public void setTypeMap(Map map) throws SQLException { 516 notImplemented(); 517 } 518 519 public void setHoldability(int holdability) throws SQLException { 520 notImplemented(); 521 } 522 523 public int getHoldability() throws SQLException { 524 notImplemented(); 525 return 0; 526 } 527 528 public Statement createStatement(int resultSetType, int resultSetConcurrency, 529 int resultSetHoldability) throws SQLException { 530 notImplemented(); 531 return null; 532 } 533 534 public PreparedStatement prepareStatement(String sql, int resultSetType, 535 int resultSetConcurrency, int resultSetHoldability) 536 throws SQLException { 537 notImplemented(); 538 return null; 539 } 540 541 public CallableStatement prepareCall(String sql, int resultSetType, 542 int resultSetConcurrency, 543 int resultSetHoldability) throws SQLException { 544 notImplemented(); 545 return null; 546 } 547 548 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) 549 throws SQLException { 550 notImplemented(); 551 return null; 552 } 553 554 public PreparedStatement prepareStatement(String sql, int columnIndexes[]) 555 throws SQLException { 556 notImplemented(); 557 return null; 558 } 559 560 public PreparedStatement prepareStatement(String sql, String columnNames[]) 561 throws SQLException { 562 notImplemented(); 563 return null; 564 } 565 566 } 567
| Popular Tags
|