1 4 5 package com.triactive.jdo.test; 6 7 import java.io.BufferedInputStream ; 8 import java.io.BufferedOutputStream ; 9 import java.io.ByteArrayInputStream ; 10 import java.io.ByteArrayOutputStream ; 11 import java.io.IOException ; 12 import java.io.ObjectInputStream ; 13 import java.io.ObjectOutputStream ; 14 import java.sql.Connection ; 15 import java.util.Properties ; 16 import javax.jdo.JDOUserException; 17 import javax.jdo.JDOHelper; 18 import javax.jdo.JDOUnsupportedOptionException; 19 import javax.jdo.PersistenceManager; 20 import javax.jdo.PersistenceManagerFactory; 21 import junit.framework.Assert; 22 import com.triactive.jdo.DatabaseProperties; 23 import com.triactive.jdo.PersistenceManagerFactoryImpl; 24 25 26 33 34 public class PersistenceManagerFactoryImplTest 35 extends PersistenceTestCase 36 { 37 38 42 public class PMFProperties 43 extends Properties 44 { 45 private boolean optimistic = false; 46 private boolean retainValues = false; 47 private boolean restoreValues = false; 48 private boolean ignoreCache = false; 49 private boolean nontransactionalRead = false; 50 private boolean nontransactionalWrite = false; 51 private boolean multithreaded = false; 52 53 private String driverName = null; 54 private String url = null; 55 private String userName = null; 56 private String password = null; 57 private String factoryName = null; 58 private String factory2Name = null; 59 60 private boolean validateTables; 61 private boolean validateConstraints; 62 private boolean autoCreateTables; 63 private int isolationLevel; 64 65 69 public PMFProperties() 70 { 71 super(); 72 this.setProperty("javax.jdo.PersistenceManagerFactoryClass", 73 "com.triactive.jdo.PersistenceManagerFactoryImpl"); 74 75 79 PersistenceManagerFactoryImpl pmf = new PersistenceManagerFactoryImpl(); 80 validateTables = pmf.getValidateTables(); 81 validateConstraints = pmf.getValidateConstraints(); 82 autoCreateTables = pmf.getAutoCreateTables(); 83 isolationLevel = pmf.getTransactionIsolation(); 84 } 85 86 public void setOptimistic(boolean b) 87 { 88 this.optimistic = b; 89 this.setProperty("javax.jdo.option.Optimistic", new Boolean (b).toString()); 90 } 91 92 public void setRetainValues(boolean b) 93 { 94 this.retainValues = b; 95 this.setProperty("javax.jdo.option.RetainValues", new Boolean (b).toString()); 96 } 97 98 public void setRestoreValues(boolean b) 99 { 100 this.restoreValues = b; 101 this.setProperty("javax.jdo.option.RestoreValues", new Boolean (b).toString()); 102 } 103 104 public void setIgnoreCache(boolean b) 105 { 106 this.ignoreCache = b; 107 this.setProperty("javax.jdo.option.IgnoreCache", new Boolean (b).toString()); 108 } 109 110 public void setNontransactionalRead(boolean b) 111 { 112 this.nontransactionalRead = b; 113 this.setProperty("javax.jdo.option.NontransactionalRead", new Boolean (b).toString()); 114 } 115 116 public void setNontransactionalWrite(boolean b) 117 { 118 this.nontransactionalWrite = b; 119 this.setProperty("javax.jdo.option.NontransactionalWrite", new Boolean (b).toString()); 120 } 121 122 public void setMultithreaded(boolean b) 123 { 124 this.multithreaded = b; 125 this.setProperty("javax.jdo.option.Multithreaded", new Boolean (b).toString()); 126 } 127 128 public void setDriverName(String s) 129 { 130 this.driverName = s; 131 this.setProperty("javax.jdo.option.ConnectionDriverName", s); 132 } 133 134 public void setUserName(String s) 135 { 136 this.userName = s; 137 this.setProperty("javax.jdo.option.ConnectionUserName", s); 138 } 139 140 public void setPassword(String s) 141 { 142 this.password = s; 143 this.setProperty("javax.jdo.option.ConnectionPassword", s); 144 } 145 146 public void setURL(String s) 147 { 148 this.url = s; 149 this.setProperty("javax.jdo.option.ConnectionURL", s); 150 } 151 152 public void setFactoryName(String s) 153 { 154 this.factoryName = s; 155 this.setProperty("javax.jdo.option.ConnectionFactoryName", s); 156 } 157 158 public void setFactory2Name(String s) 159 { 160 this.factory2Name = s; 161 this.setProperty("javax.jdo.option.ConnectionFactory2Name", s); 162 } 163 164 public void setValidateTables(boolean b) 165 { 166 this.validateTables = b; 167 this.setProperty(PersistenceManagerFactoryImpl.VALIDATE_TABLES_PROPERTY, new Boolean (b).toString()); 168 } 169 170 public void setValidateConstraints(boolean b) 171 { 172 this.validateConstraints = b; 173 this.setProperty(PersistenceManagerFactoryImpl.VALIDATE_CONSTRAINTS_PROPERTY, new Boolean (b).toString()); 174 } 175 176 public void setAutoCreateTables(boolean b) 177 { 178 this.autoCreateTables = b; 179 this.setProperty(PersistenceManagerFactoryImpl.AUTO_CREATE_TABLES_PROPERTY, new Boolean (b).toString()); 180 } 181 182 public void setTransactionIsolation(int i) 183 { 184 this.isolationLevel = i; 185 String name; 186 187 switch (i) 188 { 189 case Connection.TRANSACTION_READ_UNCOMMITTED: 190 name = "read uncommitted"; 191 break; 192 case Connection.TRANSACTION_READ_COMMITTED: 193 name = "read committed"; 194 break; 195 case Connection.TRANSACTION_REPEATABLE_READ: 196 name = "repeatable read"; 197 break; 198 case Connection.TRANSACTION_SERIALIZABLE: 199 name = "serializable"; 200 break; 201 default: 202 name = "bad level " + Integer.toString(i); 203 break; 204 } 205 206 this.setProperty(PersistenceManagerFactoryImpl.TRANSACTION_ISOLATION_PROPERTY, name); 207 } 208 209 public void assertMatchesPMF(PersistenceManagerFactory pmf, Assert a) 210 { 211 a.assertEquals(optimistic, pmf.getOptimistic()); 212 a.assertEquals(retainValues, pmf.getRetainValues()); 213 a.assertEquals(restoreValues, pmf.getRestoreValues()); 214 a.assertEquals(ignoreCache, pmf.getIgnoreCache()); 215 a.assertEquals(nontransactionalRead, pmf.getNontransactionalRead()); 216 a.assertEquals(nontransactionalWrite, pmf.getNontransactionalWrite()); 217 a.assertEquals(multithreaded, pmf.getMultithreaded()); 218 a.assertEquals(driverName, pmf.getConnectionDriverName()); 219 a.assertEquals(url, pmf.getConnectionURL()); 220 a.assertEquals(userName, pmf.getConnectionUserName()); 221 a.assertEquals(factoryName, pmf.getConnectionFactoryName()); 222 a.assertEquals(factory2Name, pmf.getConnectionFactory2Name()); 223 224 PersistenceManagerFactoryImpl myPMF = (PersistenceManagerFactoryImpl)pmf; 225 a.assertEquals(validateTables, myPMF.getValidateTables()); 226 a.assertEquals(validateConstraints, myPMF.getValidateConstraints()); 227 a.assertEquals(autoCreateTables, myPMF.getAutoCreateTables()); 228 a.assertEquals(isolationLevel, myPMF.getTransactionIsolation()); 229 } 230 } 231 232 233 234 240 241 public PersistenceManagerFactoryImplTest(String name) 242 { 243 super(name); 244 } 245 246 247 251 public void testJDOHelperInstantiation() 252 { 253 263 264 267 boolean optimistic = true; 268 boolean retainValues = true; 269 boolean restoreValues = true; 270 boolean ignoreCache = true; 271 boolean nontransactionalRead = true; 272 boolean nontransactionalWrite = true; 273 boolean multithreaded = true; 274 String driverName = DatabaseProperties.dbDriver; 275 String url = DatabaseProperties.dbURL; 276 String userName = DatabaseProperties.dbUser; 277 String password = DatabaseProperties.dbPassword; 278 boolean validateTables = true; 281 boolean validateConstraints = true; 282 boolean autoCreate = true; 283 int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED; 284 285 PMFProperties props = new PMFProperties(); 286 props.setOptimistic(optimistic); 287 props.setRetainValues(retainValues); 288 props.setRestoreValues(restoreValues); 289 props.setIgnoreCache(ignoreCache); 290 props.setNontransactionalRead(nontransactionalRead); 291 props.setNontransactionalWrite(nontransactionalWrite); 292 props.setMultithreaded(multithreaded); 293 props.setDriverName(driverName); 294 props.setUserName(userName); 295 props.setPassword(password); 296 props.setURL(url); 297 props.setValidateTables(validateTables); 300 props.setValidateConstraints(validateConstraints); 301 props.setAutoCreateTables(autoCreate); 302 props.setTransactionIsolation(transactionIsolation); 303 304 PersistenceManagerFactory pmf = 305 JDOHelper.getPersistenceManagerFactory(props); 306 assertTrue("PMF should be com.triactive.jdo.PersistenceManagerFactoryImpl.", 307 (pmf instanceof PersistenceManagerFactoryImpl)); 308 props.assertMatchesPMF(pmf, this); 309 310 313 pmf.setOptimistic(!optimistic); 314 pmf.setRetainValues(!retainValues); 315 pmf.setRestoreValues(!restoreValues); 316 pmf.setIgnoreCache(!ignoreCache); 317 pmf.setNontransactionalRead(!nontransactionalRead); 318 pmf.setNontransactionalWrite(!nontransactionalWrite); 319 pmf.setMultithreaded(!multithreaded); 320 321 322 props = new PMFProperties(); 323 props.setDriverName(driverName); 324 props.setUserName(userName); 325 props.setPassword(password); 326 props.setURL(url); 327 328 331 props.setDriverName("my.test.driver.should.not.exist"); 332 pmf = JDOHelper.getPersistenceManagerFactory(props); 333 334 try 335 { 336 pmf.getPersistenceManager(); 337 fail("Invalid driver name should have thrown exception"); 338 } 339 catch (Exception e) { } 340 props.setDriverName(driverName); 342 343 346 props.setTransactionIsolation(8738); 347 348 try 349 { 350 pmf = JDOHelper.getPersistenceManagerFactory(props); 351 fail("Setting invalid transaction isolation should have thrown exception"); 352 } 353 catch (Exception e) { } 354 props.setTransactionIsolation(transactionIsolation); 356 357 360 pmf = JDOHelper.getPersistenceManagerFactory(props); 361 PersistenceManager pm = pmf.getPersistenceManager(); 362 363 try 364 { 365 pmf.setRetainValues(!retainValues); 366 fail("Setting properties after frozen should throw exception"); 367 } 368 catch (JDOUserException e) { } 369 finally 370 { 371 pm.close(); 372 } 373 374 375 378 props.setProperty("com.triactive.jdo.MyTestUnknownProperty", "unknown"); 379 pmf = JDOHelper.getPersistenceManagerFactory(props); 380 props.assertMatchesPMF(pmf, this); 381 } 382 383 384 public void testSerialization() throws Exception 385 { 386 387 PersistenceManagerFactory pmf1 = makeNonpooledPMF(); 388 389 PersistenceManagerFactory pmf2 = toPMF(toByteArray(pmf1)); 390 391 assertTrue("PMF did not deserialize into a different object", pmf1 != pmf2); 392 assertEquals("PMF did not survive serialization intact", pmf1, pmf2); 393 } 394 395 396 private static byte[] toByteArray(PersistenceManagerFactory pmf) throws IOException 397 { 398 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 399 ObjectOutputStream oout = new ObjectOutputStream (new BufferedOutputStream (bout)); 400 oout.writeObject(pmf); 401 oout.close(); 402 403 return bout.toByteArray(); 404 } 405 406 407 private static PersistenceManagerFactory toPMF(byte[] ba) throws IOException , ClassNotFoundException 408 { 409 ByteArrayInputStream bin = new ByteArrayInputStream (ba); 410 ObjectInputStream oin = new ObjectInputStream (new BufferedInputStream (bin)); 411 412 return (PersistenceManagerFactory)oin.readObject(); 413 } 414 415 416 public void testClose() throws Exception 417 { 418 419 PersistenceManagerFactory pmf1 = makeNonpooledPMF(); 420 PersistenceManager[] pms = new PersistenceManager[5]; 421 422 for (int i = 0; i < pms.length; ++i) 423 pms[i] = pmf1.getPersistenceManager(); 424 425 pms[2].currentTransaction().begin(); 426 427 try 428 { 429 pmf1.close(); 430 fail("PMF.close() should have failed, a TX is active"); 431 } 432 catch (JDOUserException e) 433 { 434 Throwable nested[] = e.getNestedExceptions(); 435 436 assertNotNull("No nested exceptions: " + e, nested); 437 assertEquals("Wrong number of nested exceptions: " + e, 1, nested.length); 438 } 439 440 pms[2].currentTransaction().rollback(); 441 442 for (int i = 0; i < pms.length; ++i) 443 assertTrue("Failed PMF.close() errnoneously closed a PM: " + pms[i], !pms[i].isClosed()); 444 445 pmf1.close(); 446 447 for (int i = 0; i < pms.length; ++i) 448 assertTrue("PMF.close() did not close PM: " + pms[i], pms[i].isClosed()); 449 450 try 451 { 452 pmf1.getPersistenceManager(); 453 fail("PMF.getPersistenceManager() should have failed after PMF.close()"); 454 } 455 catch (JDOUserException e) 456 { 457 } 459 } 460 } 461 | Popular Tags |