1 22 23 24 package com.mchange.v2.c3p0.util; 25 26 import java.sql.*; 27 import javax.sql.*; 28 import java.lang.reflect.*; 29 import java.util.Random ; 30 import com.mchange.v2.sql.SqlUtils; 31 import com.mchange.v2.c3p0.C3P0ProxyConnection; 32 33 public final class TestUtils 34 { 35 private final static Method OBJECT_EQUALS; 36 private final static Method IDENTITY_HASHCODE; 37 private final static Method IPCFP; 38 39 40 static 41 { 42 try 43 { 44 OBJECT_EQUALS = Object .class.getMethod("equals", new Class [] { Object .class }); 45 IDENTITY_HASHCODE = System .class.getMethod("identityHashCode", new Class [] {Object .class}); 46 47 IPCFP = TestUtils.class.getMethod("isPhysicalConnectionForProxy", new Class [] {Connection.class, C3P0ProxyConnection.class}); 49 } 50 catch ( Exception e ) 51 { 52 e.printStackTrace(); 53 throw new RuntimeException ("Huh? Can't reflectively get ahold of expected methods?"); 54 } 55 } 56 57 62 public static boolean samePhysicalConnection( C3P0ProxyConnection con1, C3P0ProxyConnection con2 ) throws SQLException 63 { 64 try 65 { 66 Object out = con1.rawConnectionOperation( IPCFP, null, new Object [] { C3P0ProxyConnection.RAW_CONNECTION, con2 } ); 67 return ((Boolean ) out).booleanValue(); 68 } 69 catch (Exception e) 70 { 71 e.printStackTrace(); 72 throw SqlUtils.toSQLException( e ); 73 } 74 } 75 76 public static boolean isPhysicalConnectionForProxy( Connection physicalConnection, C3P0ProxyConnection proxy ) throws SQLException 77 { 78 try 79 { 80 Object out = proxy.rawConnectionOperation( OBJECT_EQUALS, physicalConnection, new Object [] { C3P0ProxyConnection.RAW_CONNECTION } ); 81 return ((Boolean ) out).booleanValue(); 82 } 83 catch (Exception e) 84 { 85 e.printStackTrace(); 86 throw SqlUtils.toSQLException( e ); 87 } 88 } 89 90 public static int physicalConnectionIdentityHashCode( C3P0ProxyConnection conn ) throws SQLException 91 { 92 try 93 { 94 Object out = conn.rawConnectionOperation( IDENTITY_HASHCODE, null, new Object [] { C3P0ProxyConnection.RAW_CONNECTION } ); 95 return ((Integer ) out).intValue(); 96 } 97 catch (Exception e) 98 { 99 e.printStackTrace(); 100 throw SqlUtils.toSQLException( e ); 101 } 102 } 103 104 105 public static DataSource unreliableCommitDataSource(DataSource ds) throws Exception 106 { 107 return (DataSource) Proxy.newProxyInstance( TestUtils.class.getClassLoader(), 108 new Class [] { DataSource.class }, 109 new StupidDataSourceInvocationHandler( ds ) ); 110 } 111 112 private TestUtils() 113 {} 114 115 static class StupidDataSourceInvocationHandler implements InvocationHandler 116 { 117 DataSource ds; 118 Random r = new Random (); 119 120 StupidDataSourceInvocationHandler(DataSource ds) 121 { this.ds = ds; } 122 123 public Object invoke(Object proxy, Method method, Object [] args) 124 throws Throwable 125 { 126 if ( "getConnection".equals(method.getName()) ) 127 { 128 Connection conn = (Connection) method.invoke( ds, args ); 129 return Proxy.newProxyInstance( TestUtils.class.getClassLoader(), 130 new Class [] { Connection.class }, 131 new StupidConnectionInvocationHandler( conn ) ); 132 } 133 else 134 return method.invoke( ds, args ); 135 } 136 } 137 138 static class StupidConnectionInvocationHandler implements InvocationHandler 139 { 140 Connection conn; 141 Random r = new Random (); 142 143 boolean invalid = false; 144 145 StupidConnectionInvocationHandler(Connection conn) 146 { this.conn = conn; } 147 148 public Object invoke(Object proxy, Method method, Object [] args) 149 throws Throwable 150 { 151 if ("close".equals(method.getName())) 152 { 153 if (invalid) 154 { 155 new Exception ("Duplicate close() called on Connection!!!").printStackTrace(); 156 } 157 else 158 { 159 invalid = true; 161 } 162 return null; 163 } 164 else if ( invalid ) 165 throw new SQLException("Connection closed -- cannot " + method.getName()); 166 else if ( "commit".equals(method.getName()) && r.nextInt(100) == 0 ) 167 { 168 conn.rollback(); 169 throw new SQLException("Random commit exception!!!"); 170 } 171 else if (r.nextInt(200) == 0) 172 { 173 conn.rollback(); 174 conn.close(); 175 throw new SQLException("Random Fatal Exception Occurred!!!"); 176 } 177 else 178 return method.invoke( conn, args ); 179 } 180 } 181 182 } 183 | Popular Tags |