1 10 11 package com.triactive.jdo.test.npt; 12 13 import com.triactive.jdo.test.*; 14 import javax.jdo.JDOHelper; 15 import javax.jdo.PersistenceManager; 16 import javax.jdo.Transaction; 17 import org.apache.log4j.Category; 18 19 20 28 29 public class NptTest extends StorageTestCase 30 { 31 private static final Category LOG = Category.getInstance(NptTest.class); 32 33 private boolean schemaInitialized = false; 34 private PersistenceManager pm; 35 private Transaction tx; 36 private NptWidget pobjs[] = new NptWidget[TEST_OBJECT_COUNT]; 37 38 39 45 46 public NptTest(String name) 47 { 48 super(name); 49 } 50 51 52 protected void setUp() throws Exception 53 { 54 super.setUp(); 55 56 if (!schemaInitialized) 57 { 58 addClassesToSchema(new Class [] { NptWidget.class, }); 59 schemaInitialized = true; 60 } 61 62 pm = pmf.getPersistenceManager(); 63 tx = pm.currentTransaction(); 64 65 tx.setNontransactionalRead(true); 66 } 67 68 69 protected void tearDown() throws Exception 70 { 71 if (tx.isActive()) 72 tx.rollback(); 73 74 pm.close(); 75 76 super.tearDown(); 77 } 78 79 80 public void testBasicStorage() throws Exception 81 { 82 runStorageTestFor(NptWidget.class); 83 } 84 85 86 public void testCommitWithRetainFalse() 87 { 88 runCommitTest(false); 89 } 90 91 92 public void testCommitWithRetainTrue() 93 { 94 runCommitTest(true); 95 } 96 97 98 public void runCommitTest(boolean retainValues) 99 { 100 LOG.info("NPT test: commit with RetainValues=" + retainValues); 101 102 tx.setRetainValues(retainValues); 103 104 108 tx.begin(); 109 insert(); 110 tx.commit(); 111 112 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 113 { 114 NptWidget expect = (NptWidget)objs[i]; 115 NptWidget actual = pobjs[i]; 116 117 assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(actual)); 118 assertEquals("Transactional field txInteger not retained", expect.getTxInteger(), actual.getTxInteger()); 119 assertEquals("Transactional field txString not retained", expect.getTxString(), actual.getTxString()); 120 } 121 122 126 tx.begin(); 127 128 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 129 { 130 NptWidget expect = (NptWidget)objs[i]; 131 NptWidget actual = pobjs[i]; 132 133 expect.fillTxRandom(); 134 actual.setTxInteger(expect.getTxInteger()); 135 actual.setTxString(expect.getTxString()); 136 137 assertTrue("Object is not dirty: " + ids[i], JDOHelper.isDirty(actual)); 138 } 139 140 tx.commit(); 141 142 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 143 { 144 NptWidget expect = (NptWidget)objs[i]; 145 NptWidget actual = pobjs[i]; 146 147 assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(actual)); 148 assertEquals("Transactional field txInteger not retained", expect.getTxInteger(), actual.getTxInteger()); 149 assertEquals("Transactional field txString not retained", expect.getTxString(), actual.getTxString()); 150 } 151 152 153 tx.begin(); 154 pm.deletePersistentAll(pobjs); 155 tx.commit(); 156 } 157 158 159 public void testRollbackWithRestoreFalse() 160 { 161 LOG.info("NPT test: rollback with RestoreValues=false"); 162 163 tx.setRestoreValues(false); 164 165 169 tx.begin(); 170 insert(); 171 tx.rollback(); 172 173 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 174 { 175 NptWidget expect = (NptWidget)objs[i]; 176 NptWidget actual = pobjs[i]; 177 178 assertTrue("Object is persistent: " + actual, !JDOHelper.isPersistent(actual)); 179 assertEquals("Transactional field txInteger affected by rollback", expect.getTxInteger(), actual.getTxInteger()); 180 assertEquals("Transactional field txString affected by rollback", expect.getTxString(), actual.getTxString()); 181 } 182 } 183 184 185 public void testRollbackWithRestoreTrue() 186 { 187 LOG.info("NPT test: rollback with RestoreValues=true"); 188 189 tx.setRestoreValues(true); 190 191 195 tx.begin(); 196 197 insert(); 198 199 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 200 pobjs[i].fillRandom(); 201 202 tx.rollback(); 203 204 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 205 { 206 NptWidget expect = (NptWidget)objs[i]; 207 NptWidget actual = pobjs[i]; 208 209 assertTrue("Object is persistent: " + actual, !JDOHelper.isPersistent(actual)); 210 assertEquals("Transactional field txInteger not restored", expect.getTxInteger(), actual.getTxInteger()); 211 assertEquals("Transactional field txString not restored", expect.getTxString(), actual.getTxString()); 212 } 213 214 218 tx.begin(); 219 insert(); 220 tx.commit(); 221 222 tx.begin(); 223 224 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 225 pobjs[i].fillRandom(); 226 227 tx.rollback(); 228 229 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 230 { 231 NptWidget expect = (NptWidget)objs[i]; 232 NptWidget actual = pobjs[i]; 233 234 assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(actual)); 235 assertEquals("Transactional field txInteger not restored", expect.getTxInteger(), actual.getTxInteger()); 236 assertEquals("Transactional field txString not restored", expect.getTxString(), actual.getTxString()); 237 } 238 239 240 tx.begin(); 241 pm.deletePersistentAll(pobjs); 242 tx.commit(); 243 } 244 245 246 private void insert() 247 { 248 LOG.info("Inserting " + TEST_OBJECT_COUNT + " " + NptWidget.class.getName() + " objects"); 249 250 for (int i = 0; i < TEST_OBJECT_COUNT; ++i) 251 { 252 objs[i] = new NptWidget(); 253 objs[i].fillRandom(); 254 255 pobjs[i] = (NptWidget)objs[i].clone(); 256 assertFieldsEqual(objs[i], pobjs[i]); 257 258 pm.makePersistent(pobjs[i]); 259 ids[i] = JDOHelper.getObjectId(pobjs[i]); 260 } 261 } 262 } 263 | Popular Tags |