1 22 package org.jboss.test.jca.adapter; 23 24 import javax.sql.XADataSource ; 25 import javax.sql.XAConnection ; 26 import javax.sql.ConnectionEventListener ; 27 import javax.transaction.xa.XAResource ; 28 import javax.transaction.xa.XAException ; 29 import javax.transaction.xa.Xid ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.Arrays ; 33 import java.io.PrintWriter ; 34 import java.sql.SQLException ; 35 import java.sql.Connection ; 36 import java.sql.DatabaseMetaData ; 37 import java.sql.SQLWarning ; 38 import java.sql.Savepoint ; 39 import java.sql.Statement ; 40 import java.sql.CallableStatement ; 41 import java.sql.PreparedStatement ; 42 import java.lang.reflect.Proxy ; 43 import java.lang.reflect.InvocationHandler ; 44 import java.lang.reflect.Method ; 45 46 50 public class MockedXADataSource 51 implements XADataSource 52 { 53 private static final Map instances = new HashMap (); 54 55 public static MockedXADataSource getInstance(String url) 56 { 57 return (MockedXADataSource)instances.get(url); 58 } 59 60 public static void stop(String url) 61 { 62 getInstance(url).stopped = true; 63 } 64 65 public static void start(String url) 66 { 67 getInstance(url).stopped = false; 68 } 69 70 public static String [] getUrls() 71 { 72 return (String [])instances.keySet().toArray(new String [instances.size()]); 73 } 74 75 private String url; 76 private boolean stopped; 77 private int loginTimeout; 78 private PrintWriter logWriter; 79 80 public String getURL() 81 { 82 return url; 83 } 84 85 public void setURL(String url) 86 { 87 this.url = url; 88 instances.put(url, this); 89 } 90 91 public int getLoginTimeout() throws SQLException 92 { 93 return loginTimeout; 94 } 95 96 public void setLoginTimeout(int seconds) throws SQLException 97 { 98 this.loginTimeout = seconds; 99 } 100 101 public PrintWriter getLogWriter() throws SQLException 102 { 103 return logWriter; 104 } 105 106 public void setLogWriter(PrintWriter out) throws SQLException 107 { 108 this.logWriter = out; 109 } 110 111 public XAConnection getXAConnection() throws SQLException 112 { 113 return new MockedXAConnection(); 114 } 115 116 public XAConnection getXAConnection(String user, String password) throws SQLException 117 { 118 return new MockedXAConnection(); 119 } 120 121 123 public class MockedXAConnection 124 implements XAConnection 125 { 126 private boolean closed; 127 private Connection con = new MockedConnection(); 128 private XAResource xaResource = new MockedXAResource(); 129 130 public XAResource getXAResource() throws SQLException 131 { 132 return xaResource; 133 } 134 135 public void close() throws SQLException 136 { 137 closed = true; 138 } 139 140 public Connection getConnection() throws SQLException 141 { 142 return con; 143 } 144 145 public void addConnectionEventListener(ConnectionEventListener listener) 146 { 147 } 148 149 public void removeConnectionEventListener(ConnectionEventListener listener) 150 { 151 } 152 153 class MockedConnection 154 implements Connection 155 { 156 private int holdability; 157 private int txIsolation; 158 private boolean autoCommit; 159 private boolean readOnly; 160 private String catalog; 161 162 public String getUrl() 163 { 164 return url; 165 } 166 167 public int getHoldability() throws SQLException 168 { 169 check(); 170 return holdability; 171 } 172 173 public int getTransactionIsolation() throws SQLException 174 { 175 check(); 176 return txIsolation; 177 } 178 179 public void clearWarnings() throws SQLException 180 { 181 check(); 182 } 183 184 public void close() throws SQLException 185 { 186 check(); 187 closed = true; 188 } 189 190 public void commit() throws SQLException 191 { 192 check(); 193 } 194 195 public void rollback() throws SQLException 196 { 197 check(); 198 } 199 200 public boolean getAutoCommit() throws SQLException 201 { 202 check(); 203 return autoCommit; 204 } 205 206 public boolean isClosed() throws SQLException 207 { 208 check(); 209 return closed; 210 } 211 212 public boolean isReadOnly() throws SQLException 213 { 214 check(); 215 return readOnly; 216 } 217 218 public void setHoldability(int holdability) throws SQLException 219 { 220 check(); 221 this.holdability = holdability; 222 } 223 224 public void setTransactionIsolation(int level) throws SQLException 225 { 226 check(); 227 this.txIsolation = level; 228 } 229 230 public void setAutoCommit(boolean autoCommit) throws SQLException 231 { 232 check(); 233 this.autoCommit = autoCommit; 234 } 235 236 public void setReadOnly(boolean readOnly) throws SQLException 237 { 238 check(); 239 this.readOnly = readOnly; 240 } 241 242 public String getCatalog() throws SQLException 243 { 244 check(); 245 return catalog; 246 } 247 248 public void setCatalog(String catalog) throws SQLException 249 { 250 check(); 251 this.catalog = catalog; 252 } 253 254 public DatabaseMetaData getMetaData() throws SQLException 255 { 256 check(); 257 return (DatabaseMetaData )Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 258 new Class []{DatabaseMetaData .class}, 259 new InvocationHandler () 260 { 261 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 262 { 263 if("getURL".equals(method.getName())) 264 { 265 return url; 266 } 267 268 return new UnsupportedOperationException ( 269 "Not implemented: method=" + 270 method.getName() + 271 ", args=" + 272 (args == null ? (Object )"null" : Arrays.asList(args)) 273 ); 274 } 275 } 276 ); 277 } 278 279 public SQLWarning getWarnings() throws SQLException 280 { 281 check(); 282 return null; 283 } 284 285 public Savepoint setSavepoint() throws SQLException 286 { 287 check(); 288 throw new UnsupportedOperationException ("setSavepoint() is not implemented."); 289 } 290 291 public void releaseSavepoint(Savepoint savepoint) throws SQLException 292 { 293 check(); 294 throw new UnsupportedOperationException ("releaseSavepoint() is not implemented."); 295 } 296 297 public void rollback(Savepoint savepoint) throws SQLException 298 { 299 check(); 300 } 301 302 public Statement createStatement() throws SQLException 303 { 304 check(); 305 return (Statement )Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 306 new Class []{Statement .class}, 307 new InvocationHandler () 308 { 309 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 310 { 311 String methodName = method.getName(); 312 if("execute".equals(methodName)) 313 { 314 return Boolean.FALSE; 316 } 317 318 return new UnsupportedOperationException ( 319 "Not implemented: method=" + 320 methodName + 321 ", args=" + 322 (args == null ? (Object )"null" : Arrays.asList(args)) 323 ); 324 } 325 } 326 ); 327 } 328 329 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException 330 { 331 check(); 332 throw new UnsupportedOperationException ("Not implemented."); 333 } 334 335 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) 336 throws SQLException 337 { 338 check(); 339 throw new UnsupportedOperationException ("Not implemented."); 340 } 341 342 public Map getTypeMap() throws SQLException 343 { 344 check(); 345 throw new UnsupportedOperationException ("Not implemented."); 346 } 347 348 public void setTypeMap(Map map) throws SQLException 349 { 350 check(); 351 throw new UnsupportedOperationException ("Not implemented."); 352 } 353 354 public String nativeSQL(String sql) throws SQLException 355 { 356 check(); 357 throw new UnsupportedOperationException ("Not implemented."); 358 } 359 360 public CallableStatement prepareCall(String sql) throws SQLException 361 { 362 check(); 363 throw new UnsupportedOperationException ("Not implemented."); 364 } 365 366 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) 367 throws SQLException 368 { 369 check(); 370 throw new UnsupportedOperationException ("Not implemented."); 371 } 372 373 public CallableStatement prepareCall(String sql, 374 int resultSetType, 375 int resultSetConcurrency, 376 int resultSetHoldability) throws SQLException 377 { 378 check(); 379 throw new UnsupportedOperationException ("Not implemented."); 380 } 381 382 public PreparedStatement prepareStatement(String sql) throws SQLException 383 { 384 check(); 385 throw new UnsupportedOperationException ("Not implemented."); 386 } 387 388 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException 389 { 390 check(); 391 throw new UnsupportedOperationException ("Not implemented."); 392 } 393 394 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) 395 throws SQLException 396 { 397 check(); 398 throw new UnsupportedOperationException ("Not implemented."); 399 } 400 401 public PreparedStatement prepareStatement(String sql, 402 int resultSetType, 403 int resultSetConcurrency, 404 int resultSetHoldability) throws SQLException 405 { 406 check(); 407 throw new UnsupportedOperationException ("Not implemented."); 408 } 409 410 public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException 411 { 412 check(); 413 throw new UnsupportedOperationException ("Not implemented."); 414 } 415 416 public Savepoint setSavepoint(String name) throws SQLException 417 { 418 check(); 419 throw new UnsupportedOperationException ("Not implemented."); 420 } 421 422 public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException 423 { 424 check(); 425 throw new UnsupportedOperationException ("Not implemented."); 426 } 427 428 430 private void check() throws SQLException 431 { 432 if(stopped) 433 { 434 throw new SQLException ("The database is not available: " + url); 435 } 436 } 437 } 438 } 439 440 private static class MockedXAResource 441 implements XAResource 442 { 443 private int txTimeOut; 444 445 public int getTransactionTimeout() throws XAException 446 { 447 return txTimeOut; 448 } 449 450 public boolean setTransactionTimeout(int i) throws XAException 451 { 452 this.txTimeOut = i; 453 return true; 454 } 455 456 public boolean isSameRM(XAResource xaResource) throws XAException 457 { 458 return xaResource instanceof MockedXAResource; 459 } 460 461 public Xid [] recover(int i) throws XAException 462 { 463 throw new UnsupportedOperationException ("recover is not implemented."); 464 } 465 466 public int prepare(Xid xid) throws XAException 467 { 468 return XAResource.XA_OK; 469 } 470 471 public void forget(Xid xid) throws XAException 472 { 473 } 474 475 public void rollback(Xid xid) throws XAException 476 { 477 } 478 479 public void end(Xid xid, int i) throws XAException 480 { 481 } 482 483 public void start(Xid xid, int i) throws XAException 484 { 485 } 486 487 public void commit(Xid xid, boolean b) throws XAException 488 { 489 } 490 } 491 } 492 | Popular Tags |