1 18 package org.objectweb.speedo.runtime.tck; 19 20 import org.objectweb.speedo.SpeedoTestHelper; 21 import org.objectweb.speedo.pobjects.basic.BasicA; 22 import org.objectweb.util.monolog.api.BasicLevel; 23 24 import javax.jdo.JDOHelper; 25 import javax.jdo.PersistenceManager; 26 import javax.jdo.Transaction; 27 28 public class JdoHelperTck extends SpeedoTestHelper { 29 30 public static final int TRANSIENT = 0; 31 public static final int PERSISTENT_NEW = 1; 32 public static final int PERSISTENT_CLEAN = 2; 33 public static final int PERSISTENT_DIRTY = 3; 34 public static final int HOLLOW = 4; 35 public static final int TRANSIENT_CLEAN = 5; 36 public static final int TRANSIENT_DIRTY = 6; 37 public static final int PERSISTENT_NEW_DELETED = 7; 38 public static final int PERSISTENT_DELETED = 8; 39 public static final int PERSISTENT_NONTRANSACTIONAL = 9; 40 public static final int NUM_STATES = 10; 41 public static final int ILLEGAL_STATE = 10; 42 43 private static final int IS_PERSISTENT = 0; 44 private static final int IS_TRANSACTIONAL = 1; 45 private static final int IS_DIRTY = 2; 46 private static final int IS_NEW = 3; 47 private static final int IS_DELETED = 4; 48 private static final int NUM_STATUSES = 5; 49 50 private static final boolean state_statuses[][] = { 51 { false, false, false, false, false }, 54 55 { 57 true, true, true, true, false }, 58 59 { 61 true, true, false, false, false }, 62 63 { 65 true, true, true, false, false }, 66 67 { 69 true, false, false, false, false }, 70 71 { 73 false, true, false, false, false }, 74 75 { 77 false, true, true, false, false }, 78 79 { 81 true, true, true, true, true }, 82 83 { 85 true, true, true, false, true }, 86 87 { 89 true, false, false, false, false } 90 }; 91 92 99 public static String getStateOfInstance(Object o) { 100 boolean existingEntries = false; 101 StringBuffer buff = new StringBuffer ("{"); 102 if (JDOHelper.isPersistent(o)) { 103 buff.append("persistent"); 104 existingEntries = true; 105 } 106 if (JDOHelper.isTransactional(o)) { 107 if (existingEntries) 108 buff.append(", "); 109 buff.append("transactional"); 110 existingEntries = true; 111 } 112 if (JDOHelper.isDirty(o)) { 113 if (existingEntries) 114 buff.append(", "); 115 buff.append("dirty"); 116 existingEntries = true; 117 } 118 if (JDOHelper.isNew(o)) { 119 if (existingEntries) 120 buff.append(", "); 121 buff.append("new"); 122 existingEntries = true; 123 } 124 if (JDOHelper.isDeleted(o)) { 125 if (existingEntries) 126 buff.append(", "); 127 buff.append("deleted"); 128 } 129 buff.append("}"); 130 return buff.toString(); 131 } 132 133 public static int currentState(Object o) { 134 boolean[] status = new boolean[5]; 135 status[IS_PERSISTENT] = JDOHelper.isPersistent(o); 136 status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o); 137 status[IS_DIRTY] = JDOHelper.isDirty(o); 138 status[IS_NEW] = JDOHelper.isNew(o); 139 status[IS_DELETED] = JDOHelper.isDeleted(o); 140 int i, j; 141 outerloop : for (i = 0; i < NUM_STATES; ++i) { 142 for (j = 0; j < NUM_STATUSES; ++j) { 143 if (status[j] != state_statuses[i][j]) 144 continue outerloop; 145 } 146 return i; 147 } 148 return NUM_STATES; 149 } 150 151 private PersistenceManager pm; 152 private PersistenceManager pm1; 153 private Transaction transaction; 154 155 public JdoHelperTck(String s) { 156 super(s); 157 } 159 160 protected String getLoggerName() { 161 return LOG_NAME + ".rt.tck.JdoHelperTck"; 162 } 163 284 public void testMakeDirty() { 285 logger.log(BasicLevel.INFO, "testMakeDirty"); 286 try { 287 pm = pmf.getPersistenceManager(); 288 Transaction tx = pm.currentTransaction(); 289 tx.begin(); 290 BasicA p1 = new BasicA(); 291 pm.makePersistent(p1); 292 tx.commit(); 293 294 Transaction tx1 = pm.currentTransaction(); 295 tx1.begin(); 296 JDOHelper.makeDirty(p1, "f1"); 297 298 int state = currentState(p1); 299 logger.log( 300 BasicLevel.INFO, 301 "currentState(p1)=" + state + "getStateOfInstance(p1)=" + getStateOfInstance(p1)); 302 303 boolean flag = JDOHelper.isDirty(p1); 304 tx1.commit(); 305 306 assertTrue("bad isDirty value", flag); 307 308 } catch (Exception ex) { 309 logger.log(BasicLevel.ERROR, "Unexception caught in testMakeDirty", ex); 310 fail(ex.getMessage()); 311 } finally { 312 pm.close(); 313 } 314 } 315 316 public void testIsDeletedForTransient() { 317 logger.log(BasicLevel.INFO, "testIsDeletedForTransient"); 318 try { 319 pm = pmf.getPersistenceManager(); 320 Transaction tx = pm.currentTransaction(); 321 tx.begin(); 322 BasicA p1 = new BasicA(); 323 324 int state = currentState(p1); 325 326 logger.log(BasicLevel.INFO, "IS_PERSISTENT=" + JDOHelper.isPersistent(p1)); 327 logger.log(BasicLevel.INFO, "IS_TRANSACTIONAL=" + JDOHelper.isTransactional(p1)); 328 logger.log(BasicLevel.INFO, "IS_DIRTY=" + JDOHelper.isDirty(p1)); 329 logger.log(BasicLevel.INFO, "IS_NEW=" + JDOHelper.isNew(p1)); 330 logger.log(BasicLevel.INFO, "IS_DELETED=" + JDOHelper.isDeleted(p1)); 331 332 logger.log( 333 BasicLevel.INFO, 334 "currentState(p1)=" + state + " getStateOfInstance(p1)=" + getStateOfInstance(p1)); 335 336 boolean flag = JDOHelper.isDeleted(p1); 337 tx.commit(); 338 339 assertTrue("bad isDeletedforTransient value", !flag); 340 341 } catch (Exception ex) { 342 logger.log(BasicLevel.ERROR, "Unexception caught in testIsDeletedForTransient", ex); 343 fail(ex.getMessage()); 344 } finally { 345 pm.close(); 346 } 347 } 348 349 } 350 | Popular Tags |