1 26 27 package org.objectweb.perseus.connector.ra; 28 29 import java.util.Enumeration ; 30 import java.util.Vector ; 31 import javax.resource.ResourceException ; 32 import javax.resource.cci.Connection ; 33 import javax.resource.cci.ConnectionFactory ; 34 import javax.resource.cci.LocalTransaction ; 35 import javax.resource.spi.ConnectionEvent ; 36 import javax.resource.spi.ConnectionEventListener ; 37 import javax.resource.spi.ConnectionManager ; 38 import javax.resource.spi.ConnectionRequestInfo ; 39 import javax.resource.spi.ManagedConnection ; 40 import javax.resource.spi.ManagedConnectionFactory ; 41 42 45 49 public abstract class TestRAMan extends TestRA 50 implements ConnectionManager , ConnectionEventListener { 51 52 private boolean isConnectionClosed = false; 53 private boolean isConnectionError = false; 54 private boolean isLTCommited = false; 55 private boolean isLTRollbacked = false; 56 private boolean isLTStarted = false; 57 58 private boolean isAllocated = false; 59 60 63 public TestRAMan(String tn) { 64 super(tn); 65 } 66 67 68 74 protected void initMCF() throws Exception { 75 Object bcf = mcf.createConnectionFactory( 76 (javax.resource.spi.ConnectionManager ) this); 77 86 } 87 88 92 95 protected void setUp() { 96 isConnectionClosed = false; 97 isConnectionError = false; 98 isLTCommited = false; 99 isLTRollbacked = false; 100 isLTStarted = false; 101 isAllocated = false; 102 super.setUp(); 103 } 104 105 109 public Object allocateConnection(ManagedConnectionFactory _mcf, 110 ConnectionRequestInfo cri) throws ResourceException { 111 isAllocated = true; 112 if (mcf != _mcf) 113 fail(); 114 ManagedConnection mc = _mcf.createManagedConnection(null, cri); 115 mc.addConnectionEventListener(this); 116 Object connection = mc.getConnection(null, cri); 117 mc.associateConnection(connection); 118 return connection; 119 } 120 121 125 public void connectionClosed(ConnectionEvent event) { 126 isConnectionClosed = true; 127 } 128 129 public void connectionErrorOccurred(ConnectionEvent event) { 130 isConnectionError = true; 131 } 132 133 public void localTransactionCommitted(ConnectionEvent event) { 134 isLTCommited = true; 135 } 136 137 public void localTransactionRolledback(ConnectionEvent event) { 138 isLTRollbacked = true; 139 } 140 141 public void localTransactionStarted(ConnectionEvent event) { 142 isLTStarted = true; 143 } 144 145 146 150 151 155 public void testNotifyAllocation() { 156 try { 157 isAllocated = false; 159 160 Connection c = cf.getConnection(); 162 163 if (!isAllocated) 165 fail(); 166 167 c.close(); 169 } catch (Exception e) { 170 fail(); 171 } 172 } 173 174 181 public void testNotifyCloseCEL() { 182 try { 183 isConnectionClosed = false; 185 186 Connection c = cf.getConnection(); 188 189 c.close(); 190 191 if (!isConnectionClosed) 193 fail(); 194 } catch (Exception e) { 195 fail(); 196 } 197 } 198 199 203 public void testNotifyLTCommitCEL() { 204 try { 205 isLTStarted = false; 207 isLTCommited = false; 208 209 Connection c = cf.getConnection(); 211 LocalTransaction lt = c.getLocalTransaction(); 212 lt.begin(); 213 if (!isLTStarted) 214 fail(); 215 Enumeration e = writings.elements(); 216 Writing w = null; 217 while (e.hasMoreElements()) { 218 w = (Writing) e.nextElement(); 219 w.init(c); 220 w.write(c); 221 } 222 lt.commit(); 223 if (!isLTCommited) 224 fail(); 225 226 c.close(); 228 } catch (Exception e) { 229 fail(); 230 } 231 } 232 233 237 public void testNotifyLTRollbackCEL() { 238 try { 239 isLTStarted = false; 241 isLTRollbacked = false; 242 243 Connection c = cf.getConnection(); 245 LocalTransaction lt = c.getLocalTransaction(); 246 lt.begin(); 247 if (!isLTStarted) 248 fail(); 249 Enumeration e = writings.elements(); 250 Writing w = null; 251 while (e.hasMoreElements()) { 252 w = (Writing) e.nextElement(); 253 w.init(c); 254 w.write(c); 255 } 256 lt.rollback(); 257 if (!isLTRollbacked) 258 fail(); 259 260 c.close(); 262 } catch (Exception e) { 263 fail(); 264 } 265 } 266 } | Popular Tags |