1 27 28 package org.objectweb.perseus.connector.ra; 29 30 import junit.framework.TestCase; 31 32 import java.util.Enumeration ; 33 import java.util.Iterator ; 34 import java.util.Vector ; 35 import javax.naming.Context ; 36 import javax.resource.NotSupportedException ; 37 import javax.resource.ResourceException ; 38 import javax.resource.cci.Connection ; 39 import javax.resource.cci.ConnectionFactory ; 40 import javax.resource.cci.LocalTransaction ; 41 import javax.resource.spi.ManagedConnectionFactory ; 42 43 46 public abstract class TestRA extends TestCase { 47 50 protected ManagedConnectionFactory mcf = null; 51 52 55 protected ConnectionFactory cf = null; 56 57 62 protected Vector writings = null; 63 64 67 protected TestRA(String tn) { 68 super(tn); 69 writings = getWritings(); 70 } 71 72 73 77 82 protected abstract ManagedConnectionFactory getNewMCFInstance() 83 throws Exception ; 84 85 88 protected abstract void initMCF() throws Exception ; 89 90 protected abstract void endMCF() throws Exception ; 91 92 96 protected abstract Vector getWritings(); 97 98 99 103 106 protected void setUp() { 107 try { 108 mcf = getNewMCFInstance(); 109 initMCF(); 110 cf = (ConnectionFactory ) mcf.createConnectionFactory(); 111 } catch (Exception e) { 112 System.out.println("Error during setUp"); 113 e.printStackTrace(); 114 } 116 } 117 118 121 protected void tearDown() { 122 try { 123 endMCF(); 124 } catch (Exception e) { 125 System.out.println("Error during tearDown"); 126 e.printStackTrace(); 127 } 129 mcf = null; 130 cf = null; 131 } 132 133 134 138 142 public void testGetnClose() { 143 try { 144 Connection c = cf.getConnection(); 145 c.close(); 146 } catch (Exception e) { 147 e.printStackTrace(); 148 fail(); 149 } 150 } 151 152 156 public void testLTCommit() { 157 try { 158 Connection c = cf.getConnection(); 159 Enumeration e = writings.elements(); 160 Writing w; 161 while (e.hasMoreElements()) { 162 w = (Writing) e.nextElement(); 163 w.setConnection(c); 164 c.setAutoCommit(true); 165 w.init(); 166 if (!w.read(false)) { 167 System.err.println("Fail-1!!!"); 168 fail(); 169 } 170 c.setAutoCommit(false); 171 LocalTransaction lt = c.getLocalTransaction(); 172 lt.begin(); 173 if (w.read(true)) { 174 System.err.println("Fail-2!!!"); 175 fail(); 176 } 177 w.write(); 178 if (w.read(false)) { 179 System.err.println("Fail-3!!!"); 180 fail(); 181 } 182 lt.commit(); 183 } 184 c.close(); 185 186 c = cf.getConnection(); 187 e = writings.elements(); 188 while (e.hasMoreElements()) { 189 w = (Writing) e.nextElement(); 190 w.setConnection(c); 191 if (w.read(false)) { 192 System.err.println("Fail-4!!!"); 193 fail(); 194 } 195 } 196 c.close(); 197 } catch (Exception e) { 198 e.printStackTrace(); 199 fail(); 200 } 201 } 202 203 207 public void testLTRollback() { 208 try { 209 Connection c = cf.getConnection(); 210 Enumeration e = writings.elements(); 211 Writing w = null; 212 while (e.hasMoreElements()) { 213 w = (Writing) e.nextElement(); 214 w.setConnection(c); 215 c.setAutoCommit(true); 216 w.init(); 217 if (!w.read(false)) { 218 System.err.println("Fail-1!!!"); 219 fail(); 220 } 221 c.setAutoCommit(false); 222 LocalTransaction lt = c.getLocalTransaction(); 223 lt.begin(); 224 if (w.read(true)) { 225 System.err.println("Fail-2!!!"); 226 fail(); 227 } 228 w.write(); 229 if (w.read(false)) { 230 System.err.println("Fail-3!!!"); 231 fail(); 232 } 233 lt.rollback(); 234 } 235 c.close(); 236 237 c = cf.getConnection(); 238 e = writings.elements(); 239 while (e.hasMoreElements()) { 240 w = (Writing) e.nextElement(); 241 w.setConnection(c); 242 if (!w.read(false)) { 243 System.err.println("Fail-4!!!"); 244 fail(); 245 } 246 } 247 c.close(); 248 } catch (Exception e) { 249 e.printStackTrace(); 250 fail(); 251 } 252 } 253 254 258 public void testCFMetaData() { 259 try { 260 try { 261 cf.getMetaData().getAdapterName(); 262 } catch (NotSupportedException nse) { 263 return; 264 } 265 cf.getMetaData().getAdapterName(); 266 cf.getMetaData().getAdapterShortDescription(); 267 cf.getMetaData().getAdapterVendorName(); 268 cf.getMetaData().getAdapterVersion(); 269 cf.getMetaData().getSpecVersion(); 270 cf.getMetaData().supportsExecuteWithInputAndOutputRecord(); 271 cf.getMetaData().supportsExecuteWithInputRecordOnly(); 272 cf.getMetaData().supportsLocalTransactionDemarcation(); 273 } catch (Exception e) { 274 fail(); 275 } 276 } 277 278 281 public void testCFRecordFactory() { 282 try { 283 cf.getRecordFactory().createIndexedRecord("test1"); 284 } catch (NotSupportedException nse) { 285 } catch (Exception e) { 287 fail(); 288 } 289 290 try { 291 cf.getRecordFactory().createMappedRecord("test2"); 292 } catch (NotSupportedException nse) { 293 } catch (Exception e) { 295 fail(); 296 } 297 } 298 299 302 public void testAutoCommitFalse() { 303 System.out.println("\n** testAutoCommitFalse doesn't run !"); 304 346 } 347 348 351 public void testAutoCommitTrue() { 352 try { 353 Connection c = cf.getConnection(); 354 c.setAutoCommit(true); 355 if (!c.getAutoCommit()) 357 fail(); 358 Enumeration e = writings.elements(); 360 Writing w = null; 361 while (e.hasMoreElements()) { 362 w = (Writing) e.nextElement(); 363 w.init(c); 364 if (!w.read(c, false)) 365 fail(); 366 w.write(c); 367 if (!w.read(c, true)) 368 fail(); 369 } 370 c.close(); 371 372 c = cf.getConnection(); 374 e = writings.elements(); 375 w = null; 376 while (e.hasMoreElements()) { 377 w = (Writing) e.nextElement(); 378 if (!w.read(c, true)) 379 fail(); 380 } 381 c.close(); 382 } catch (NotSupportedException nse) { 383 } catch (Exception e) { 385 fail(); 386 } 387 } 388 } | Popular Tags |