1 18 package org.objectweb.speedo.runtime.tck; 19 20 import java.util.Calendar ; 21 import java.util.Date ; 22 23 import javax.jdo.JDODataStoreException; 24 import javax.jdo.JDOUserException; 25 import javax.jdo.PersistenceManager; 26 import javax.jdo.PersistenceManagerFactory; 27 import javax.jdo.Transaction; 28 29 import org.objectweb.speedo.SpeedoTestHelper; 30 import org.objectweb.speedo.pobjects.tck.InstanceCallbackClass; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 33 public class JdoCallbacksTck extends SpeedoTestHelper { 34 35 36 37 public JdoCallbacksTck(String s) { 38 super(s); 39 } 40 41 42 void checkInstances(String label, int intValue, String capturedNextObjName, int numberOfChildren, int sumOfChildrenIntValue) { 43 if(InstanceCallbackClass.processedIndex[intValue] != true) { 44 logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue); 45 return; 47 } 48 if(capturedNextObjName != null & InstanceCallbackClass.capturedNextObjName[intValue] == null) { 49 logger.log(BasicLevel.ERROR, label + "nextObj attribute for object with intValue = " + intValue + " should not have been null."); 50 } else if(capturedNextObjName == null & InstanceCallbackClass.capturedNextObjName[intValue] != null) { 52 logger.log(BasicLevel.ERROR, label + "nextObj attribute for object with intValue = " + intValue + " should have been null."); 53 } else if(capturedNextObjName != null && !InstanceCallbackClass.capturedNextObjName[intValue].equals(capturedNextObjName)) { 55 logger.log(BasicLevel.ERROR, label + "nextObj.name attribute for object with intValue = " + intValue + " should have been \"" + 56 capturedNextObjName + "\". It was \"" + InstanceCallbackClass.capturedNextObjName[intValue] + "\" instead."); 57 } 59 60 if(InstanceCallbackClass.numberOfChildren[intValue] != numberOfChildren) { 61 logger.log(BasicLevel.ERROR, label + "Number of instances in attribute children for object with intValue = " + intValue + " should have been " + 62 numberOfChildren + ". It was " + InstanceCallbackClass.numberOfChildren[intValue] + " instead."); 63 } 65 66 if(InstanceCallbackClass.sumOfChildrenIntValue[intValue] != sumOfChildrenIntValue) { 67 logger.log(BasicLevel.ERROR, label + "Sum of intValue of instances in attribute children for object with intValue = " + intValue + " should have been " + 68 sumOfChildrenIntValue + ". It was " + InstanceCallbackClass.sumOfChildrenIntValue[intValue] + " instead."); 69 } 71 } 72 73 void checkPMAccess(String label, int intValue, boolean transactionActive) { 74 if(InstanceCallbackClass.processedIndex[intValue] != true) { 75 logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue); 76 return; 78 } 79 if(transactionActive & InstanceCallbackClass.transactionActive[intValue] != true) { 81 logger.log(BasicLevel.ERROR, label + "PersistenceManager.currentTransaction.isAcive() returned false"); 82 } 84 } 85 86 void checkFieldValues(String label, int intValue, String name, Date timeStamp, double doubleValue, short childToDelete, char charValue) { 88 if(InstanceCallbackClass.processedIndex[intValue] != true) { 89 logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue); 90 return; 92 } 93 94 if(!InstanceCallbackClass.capturedName[intValue].equals(name)) { 95 logger.log(BasicLevel.ERROR, label + "name attribute for object with intValue = " + intValue + " should be \"" + name + "\". It was \"" + 96 InstanceCallbackClass.capturedName[intValue] + "\" instead."); 97 } 99 100 if(!InstanceCallbackClass.capturedTimeStamp[intValue].equals(timeStamp)) { 101 logger.log(BasicLevel.ERROR, label + "timeStamp attribute for object with intValue = " + intValue + " should be " + timeStamp + ". It was " + 102 InstanceCallbackClass.capturedTimeStamp[intValue] + " instead."); 103 } 105 106 if(InstanceCallbackClass.capturedDoubleValue[intValue] != doubleValue) { 107 logger.log(BasicLevel.ERROR, label + "doubleValue attribute for object with intValue = " + intValue + " should be " + doubleValue + ". It was " + 108 InstanceCallbackClass.capturedDoubleValue[intValue] + " instead."); 109 } 111 112 if(InstanceCallbackClass.capturedCharValue[intValue] != charValue) { 113 logger.log(BasicLevel.ERROR, label + "charValue attribute for object with intValue = " + intValue + " should be " + charValue + ". It was " + 114 InstanceCallbackClass.capturedCharValue[intValue] + " instead."); 115 } 117 118 if(InstanceCallbackClass.capturedChildToDelete[intValue] != childToDelete) { 119 logger.log(BasicLevel.ERROR, label + "childToDelete attribute for object with intValue = " + intValue + " should be " + childToDelete + ". It was " + 120 InstanceCallbackClass.capturedChildToDelete[intValue] + " instead."); 121 } 123 } 124 125 127 double touchFields (InstanceCallbackClass o) { 128 double rc = o.doubleValue; 130 rc += o.intValue; 131 rc += o.charValue; 132 rc += o.childToDelete; 133 rc += o.name.length(); 134 rc += o.timeStamp.getTime(); 135 return rc; 136 } 137 138 protected String getLoggerName() { 139 return LOG_NAME + ".rt.tck.JdoCallbacksTck"; 140 } 141 142 public void testCallingJdoPreclear() { 143 logger.log(BasicLevel.INFO, "testCallingJdoPreclear"); 144 145 PersistenceManagerFactory fact = getPMF(); 146 PersistenceManager pm = fact.getPersistenceManager(); 147 Transaction t = pm.currentTransaction(); 148 t.setRetainValues(false); 150 InstanceCallbackClass.initializeStaticsForTest(); 151 t.begin(); 152 InstanceCallbackClass.removeAllInstances(pm); t.commit(); 154 155 t.begin(); 156 Calendar cal = Calendar.getInstance(); 157 cal.set(1999, 1, 15, 12, 0); 158 Date createTime = cal.getTime(); 159 cal.set(2002, 1, 15, 12, 0); 160 Date laterDate = cal.getTime(); 161 InstanceCallbackClass secondaryObj = new InstanceCallbackClass("secondaryObj", createTime, 2, 2.2, (short)-20, '2', null); 162 InstanceCallbackClass primaryObj = new InstanceCallbackClass("primaryObj", laterDate, 1, 1.1, (short)-10, '1', secondaryObj); 163 pm.makePersistent(primaryObj); 164 pm.makePersistent(secondaryObj); 165 Object secondaryObjId = pm.getObjectId(secondaryObj); 166 Object primaryObjId = pm.getObjectId(primaryObj); 167 t.commit(); 168 169 InstanceCallbackClass.performPreClearTests = true; 170 t.begin(); 171 try { 172 primaryObj = (InstanceCallbackClass)pm.getObjectById(primaryObjId, true); 173 touchFields(primaryObj); 175 } catch (JDOUserException e) { 176 logger.log(BasicLevel.ERROR, "Failed to find primaryObj created in previous transaction. Got JDOUserException " + e); 177 fail("CallingJdoPreclear: Failed to find primaryObj created in previous transaction."); 178 179 } catch (JDODataStoreException e) { 180 logger.log(BasicLevel.ERROR, "Failed to find primaryObj created in previous transaction. Got JDOUserException " + e); 181 fail("CallingJdoPreclear: Failed to find primaryObj created in previous transaction."); 182 } 183 184 secondaryObj = primaryObj.nextObj; 185 if(secondaryObj == null) { 186 logger.log(BasicLevel.ERROR, "Failed to find secondaryObj created in previous transaction using reference from primaryObj."); 187 fail("CallingJdoPreclear: Failed to find secondaryObj created in previous transaction."); 188 } 189 touchFields(secondaryObj); 190 191 primaryObj.addChild(secondaryObj); 193 cal.set(2005, 6, 28, 0, 0); 194 Date stillLaterDate = cal.getTime(); 195 InstanceCallbackClass ternaryObj = new InstanceCallbackClass("ternaryObj", stillLaterDate, 3, 3.3, (short)-30, '3', null); 196 pm.makePersistent(ternaryObj); 197 ternaryObj.addChild(secondaryObj); 198 ternaryObj.addChild(primaryObj); 199 t.commit(); 200 201 checkFieldValues("jdoPreClear attribute access: ", 2, "secondaryObj", createTime, 2.2, (short)-20, '2'); 203 204 checkFieldValues("jdoPreClear attribute access: ", 1, "primaryObj", laterDate, 1.1, (short)-10, '1'); 206 207 208 checkFieldValues("jdoPreClear attribute access: ", 3, "ternaryObj", stillLaterDate, 3.3, (short)-30, '3'); 210 pm.close(); 211 217 } 218 219 } 220 221 | Popular Tags |