1 package org.objectweb.cjdbc.scenario.tools.mock; 2 3 import java.sql.Connection ; 4 import java.sql.Driver ; 5 import java.sql.DriverPropertyInfo ; 6 import java.sql.SQLException ; 7 import java.util.Properties ; 8 9 import mockmaker.ExceptionalReturnValue; 10 import mockmaker.ReturnValues; 11 12 import com.mockobjects.ExpectationCounter; 13 import com.mockobjects.ExpectationList; 14 15 18 public class MockDriver implements Driver 19 { 20 private ExpectationCounter myConnectCalls = new ExpectationCounter( 21 "java.sql.Driver ConnectCalls"); 22 private ReturnValues myActualConnectReturnValues = new ReturnValues( 23 false); 24 private ExpectationList myConnectParameter0Values = new ExpectationList( 25 "java.sql.Driver java.lang.String"); 26 private ExpectationList myConnectParameter1Values = new ExpectationList( 27 "java.sql.Driver java.util.Properties"); 28 private ExpectationCounter myAcceptsURLCalls = new ExpectationCounter( 29 "java.sql.Driver AcceptsURLCalls"); 30 private ReturnValues myActualAcceptsURLReturnValues = new ReturnValues( 31 false); 32 private ExpectationList myAcceptsURLParameter0Values = new ExpectationList( 33 "java.sql.Driver java.lang.String"); 34 private ExpectationCounter myGetPropertyInfoCalls = new ExpectationCounter( 35 "java.sql.Driver GetPropertyInfoCalls"); 36 private ReturnValues myActualGetPropertyInfoReturnValues = new ReturnValues( 37 false); 38 private ExpectationList myGetPropertyInfoParameter0Values = new ExpectationList( 39 "java.sql.Driver java.lang.String"); 40 private ExpectationList myGetPropertyInfoParameter1Values = new ExpectationList( 41 "java.sql.Driver java.util.Properties"); 42 private ExpectationCounter myGetMajorVersionCalls = new ExpectationCounter( 43 "java.sql.Driver GetMajorVersionCalls"); 44 private ReturnValues myActualGetMajorVersionReturnValues = new ReturnValues( 45 false); 46 private ExpectationCounter myGetMinorVersionCalls = new ExpectationCounter( 47 "java.sql.Driver GetMinorVersionCalls"); 48 private ReturnValues myActualGetMinorVersionReturnValues = new ReturnValues( 49 false); 50 private ExpectationCounter myJdbcCompliantCalls = new ExpectationCounter( 51 "java.sql.Driver JdbcCompliantCalls"); 52 private ReturnValues myActualJdbcCompliantReturnValues = new ReturnValues( 53 false); 54 55 60 public void setExpectedConnectCalls(int calls) 61 { 62 myConnectCalls.setExpected(calls); 63 } 64 65 71 public void addExpectedConnectValues(String arg0, Properties arg1) 72 { 73 myConnectParameter0Values.addExpected(arg0); 74 myConnectParameter1Values.addExpected(arg1); 75 } 76 77 85 public Connection connect(String arg0, Properties arg1) throws SQLException 86 { 87 myConnectCalls.inc(); 88 myConnectParameter0Values.addActual(arg0); 89 myConnectParameter1Values.addActual(arg1); 90 Object nextReturnValue = myActualConnectReturnValues.getNext(); 91 if (nextReturnValue instanceof ExceptionalReturnValue 92 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof SQLException ) 93 throw (SQLException ) ((ExceptionalReturnValue) nextReturnValue) 94 .getException(); 95 if (nextReturnValue instanceof ExceptionalReturnValue 96 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof RuntimeException ) 97 throw (RuntimeException ) ((ExceptionalReturnValue) nextReturnValue) 98 .getException(); 99 return (Connection ) nextReturnValue; 100 } 101 102 107 public void setupExceptionConnect(Throwable arg) 108 { 109 myActualConnectReturnValues.add(new ExceptionalReturnValue(arg)); 110 } 111 112 117 public void setupConnect(Connection arg) 118 { 119 myActualConnectReturnValues.add(arg); 120 } 121 122 127 public void setExpectedAcceptsURLCalls(int calls) 128 { 129 myAcceptsURLCalls.setExpected(calls); 130 } 131 132 137 public void addExpectedAcceptsURLValues(String arg0) 138 { 139 myAcceptsURLParameter0Values.addExpected(arg0); 140 } 141 142 145 public boolean acceptsURL(String arg0) throws SQLException 146 { 147 myAcceptsURLCalls.inc(); 148 myAcceptsURLParameter0Values.addActual(arg0); 149 Object nextReturnValue = myActualAcceptsURLReturnValues.getNext(); 150 if (nextReturnValue instanceof ExceptionalReturnValue 151 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof SQLException ) 152 throw (SQLException ) ((ExceptionalReturnValue) nextReturnValue) 153 .getException(); 154 if (nextReturnValue instanceof ExceptionalReturnValue 155 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof RuntimeException ) 156 throw (RuntimeException ) ((ExceptionalReturnValue) nextReturnValue) 157 .getException(); 158 return ((Boolean ) nextReturnValue).booleanValue(); 159 } 160 161 166 public void setupExceptionAcceptsURL(Throwable arg) 167 { 168 myActualAcceptsURLReturnValues.add(new ExceptionalReturnValue(arg)); 169 } 170 171 176 public void setupAcceptsURL(boolean arg) 177 { 178 myActualAcceptsURLReturnValues.add(new Boolean (arg)); 179 } 180 181 186 public void setExpectedGetPropertyInfoCalls(int calls) 187 { 188 myGetPropertyInfoCalls.setExpected(calls); 189 } 190 191 197 public void addExpectedGetPropertyInfoValues(String arg0, Properties arg1) 198 { 199 myGetPropertyInfoParameter0Values.addExpected(arg0); 200 myGetPropertyInfoParameter1Values.addExpected(arg1); 201 } 202 203 207 public DriverPropertyInfo [] getPropertyInfo(String arg0, Properties arg1) 208 throws SQLException 209 { 210 myGetPropertyInfoCalls.inc(); 211 myGetPropertyInfoParameter0Values.addActual(arg0); 212 myGetPropertyInfoParameter1Values.addActual(arg1); 213 Object nextReturnValue = myActualGetPropertyInfoReturnValues.getNext(); 214 if (nextReturnValue instanceof ExceptionalReturnValue 215 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof SQLException ) 216 throw (SQLException ) ((ExceptionalReturnValue) nextReturnValue) 217 .getException(); 218 if (nextReturnValue instanceof ExceptionalReturnValue 219 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof RuntimeException ) 220 throw (RuntimeException ) ((ExceptionalReturnValue) nextReturnValue) 221 .getException(); 222 return (DriverPropertyInfo []) nextReturnValue; 223 } 224 225 230 public void setupExceptionGetPropertyInfo(Throwable arg) 231 { 232 myActualGetPropertyInfoReturnValues.add(new ExceptionalReturnValue(arg)); 233 } 234 235 240 public void setupGetPropertyInfo(DriverPropertyInfo [] arg) 241 { 242 myActualGetPropertyInfoReturnValues.add(arg); 243 } 244 245 250 public void setExpectedGetMajorVersionCalls(int calls) 251 { 252 myGetMajorVersionCalls.setExpected(calls); 253 } 254 255 258 public int getMajorVersion() 259 { 260 myGetMajorVersionCalls.inc(); 261 Object nextReturnValue = myActualGetMajorVersionReturnValues.getNext(); 262 if (nextReturnValue instanceof ExceptionalReturnValue 263 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof RuntimeException ) 264 throw (RuntimeException ) ((ExceptionalReturnValue) nextReturnValue) 265 .getException(); 266 return ((Integer ) nextReturnValue).intValue(); 267 } 268 269 274 public void setupExceptionGetMajorVersion(Throwable arg) 275 { 276 myActualGetMajorVersionReturnValues.add(new ExceptionalReturnValue(arg)); 277 } 278 279 284 public void setupGetMajorVersion(int arg) 285 { 286 myActualGetMajorVersionReturnValues.add(new Integer (arg)); 287 } 288 289 294 public void setExpectedGetMinorVersionCalls(int calls) 295 { 296 myGetMinorVersionCalls.setExpected(calls); 297 } 298 299 302 public int getMinorVersion() 303 { 304 myGetMinorVersionCalls.inc(); 305 Object nextReturnValue = myActualGetMinorVersionReturnValues.getNext(); 306 if (nextReturnValue instanceof ExceptionalReturnValue 307 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof RuntimeException ) 308 throw (RuntimeException ) ((ExceptionalReturnValue) nextReturnValue) 309 .getException(); 310 return ((Integer ) nextReturnValue).intValue(); 311 } 312 313 318 public void setupExceptionGetMinorVersion(Throwable arg) 319 { 320 myActualGetMinorVersionReturnValues.add(new ExceptionalReturnValue(arg)); 321 } 322 323 328 public void setupGetMinorVersion(int arg) 329 { 330 myActualGetMinorVersionReturnValues.add(new Integer (arg)); 331 } 332 333 338 public void setExpectedJdbcCompliantCalls(int calls) 339 { 340 myJdbcCompliantCalls.setExpected(calls); 341 } 342 343 348 public boolean jdbcCompliant() 349 { 350 myJdbcCompliantCalls.inc(); 351 Object nextReturnValue = myActualJdbcCompliantReturnValues.getNext(); 352 if (nextReturnValue instanceof ExceptionalReturnValue 353 && ((ExceptionalReturnValue) nextReturnValue).getException() instanceof RuntimeException ) 354 throw (RuntimeException ) ((ExceptionalReturnValue) nextReturnValue) 355 .getException(); 356 return ((Boolean ) nextReturnValue).booleanValue(); 357 } 358 359 364 public void setupExceptionJdbcCompliant(Throwable arg) 365 { 366 myActualJdbcCompliantReturnValues.add(new ExceptionalReturnValue(arg)); 367 } 368 369 374 public void setupJdbcCompliant(boolean arg) 375 { 376 myActualJdbcCompliantReturnValues.add(new Boolean (arg)); 377 } 378 379 382 public void verify() 383 { 384 myConnectCalls.verify(); 385 myConnectParameter0Values.verify(); 386 myConnectParameter1Values.verify(); 387 myAcceptsURLCalls.verify(); 388 myAcceptsURLParameter0Values.verify(); 389 myGetPropertyInfoCalls.verify(); 390 myGetPropertyInfoParameter0Values.verify(); 391 myGetPropertyInfoParameter1Values.verify(); 392 myGetMajorVersionCalls.verify(); 393 myGetMinorVersionCalls.verify(); 394 myJdbcCompliantCalls.verify(); 395 } 396 } 397 | Popular Tags |