1 6 7 8 package org.objectweb.speedo.pobjects.tck; 9 10 import java.io.Serializable ; 11 import java.util.Date ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 15 import javax.jdo.Extent; 16 import javax.jdo.InstanceCallbacks; 17 import javax.jdo.JDOHelper; 18 import javax.jdo.PersistenceManager; 19 import javax.jdo.Transaction; 20 21 22 public class InstanceCallbackClass implements InstanceCallbacks{ 23 public static final int TRANSIENT = 0; 24 public static final int PERSISTENT_NEW = 1; 25 public static final int PERSISTENT_CLEAN = 2; 26 public static final int PERSISTENT_DIRTY = 3; 27 public static final int HOLLOW = 4; 28 public static final int TRANSIENT_CLEAN = 5; 29 public static final int TRANSIENT_DIRTY = 6; 30 public static final int PERSISTENT_NEW_DELETED = 7; 31 public static final int PERSISTENT_DELETED = 8; 32 public static final int PERSISTENT_NONTRANSACTIONAL = 9; 33 public static final int NUM_STATES = 10; 34 public static final int ILLEGAL_STATE = 10; 35 36 public static final String [] states = { 37 "transient", 38 "persistent-new", 39 "persistent-clean", 40 "persistent-dirty", 41 "hollow", 42 "transient-clean", 43 "transient-dirty", 44 "persistent-new-deleted", 45 "persistent-deleted", 46 "persistent-nontransactional", 47 "illegal" 48 }; 49 50 private static final int IS_PERSISTENT = 0; 51 private static final int IS_TRANSACTIONAL = 1; 52 private static final int IS_DIRTY = 2; 53 private static final int IS_NEW = 3; 54 private static final int IS_DELETED = 4; 55 private static final int NUM_STATUSES = 5; 56 57 private static final boolean state_statuses[][] = { 58 { false, false, false, false, false}, 61 62 { true, true, true, true, false}, 64 65 { true, true, false, false, false}, 67 68 { true, true, true, false, false}, 70 71 { true, false, false, false, false}, 73 74 { false, true, false, false, false}, 76 77 { false, true, true, false, false}, 79 80 { true, true, true, true, true}, 82 83 { true, true, true, false, true}, 85 86 { true, false, false, false, false} 88 }; 89 90 public static int currentState(Object o) 91 { 92 boolean[] status = new boolean[5]; 93 status[IS_PERSISTENT] = JDOHelper.isPersistent(o); 94 status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o); 95 status[IS_DIRTY] = JDOHelper.isDirty(o); 96 status[IS_NEW] = JDOHelper.isNew(o); 97 status[IS_DELETED] = JDOHelper.isDeleted(o); 98 int i, j; 99 outerloop: 100 for( i = 0; i < NUM_STATES; ++i ){ 101 for( j = 0; j < NUM_STATUSES; ++j ){ 102 if( status[j] != state_statuses[i][j] ) 103 continue outerloop; 104 } 105 return i; 106 } 107 return NUM_STATES; 108 } 109 110 111 112 113 114 115 static public boolean performPreClearTests; 116 static public boolean performPreDeleteTests; 117 static public boolean performPreStoreTests; 118 119 static boolean preDeleteCalled; 121 static int objectState; 122 123 static public int arraySize = 5; 125 static public String [] capturedName = new String [arraySize]; 126 static public Date [] capturedTimeStamp = new Date [arraySize]; 127 static public double[] capturedDoubleValue = new double[arraySize]; 128 static public short[] capturedChildToDelete = new short[arraySize]; 129 static public char[] capturedCharValue = new char[arraySize]; 130 static public String [] capturedNextObjName = new String [arraySize]; 131 static public int[] numberOfChildren = new int[arraySize]; 132 static public int[] sumOfChildrenIntValue = new int[arraySize]; 133 static public boolean[] processedIndex = new boolean[arraySize]; 134 135 static public boolean[] transactionActive = new boolean[arraySize]; 136 137 static private int nextKeyValue = 1; 138 private int keyValue; public String name; 140 public Date timeStamp; 141 public InstanceCallbackClass nextObj; 142 public HashSet children; 143 public int intValue; 144 public double doubleValue; 145 public short childToDelete; 146 public char charValue; 147 148 static public void initializeStaticsForTest() 149 { 150 performPreClearTests = false; 151 performPreDeleteTests = false; 152 performPreStoreTests = false; 153 preDeleteCalled = false; 154 for( int i = 0; i < arraySize; ++i){ 155 processedIndex[i] = false; 156 transactionActive[i] = false; 157 } 158 } 159 160 static public void removeAllInstances(PersistenceManager pm) 161 { 162 Extent e = pm.getExtent(org.objectweb.speedo.pobjects.tck.InstanceCallbackClass.class, false); 164 Iterator i = e.iterator(); 165 while( i.hasNext() ){ 166 pm.deletePersistent(i.next()); 167 } 168 } 169 170 InstanceCallbackClass() { } 172 173 public InstanceCallbackClass(String label,Date createTime,int intValue,double doubleValue,short childToDelete,char charValue,InstanceCallbackClass obj) { 174 keyValue = nextKeyValue++; 175 name = label; 176 timeStamp = createTime; 177 nextObj = obj; 178 children = new HashSet (); 179 this.intValue = intValue; 180 this.doubleValue = doubleValue; 181 this.childToDelete = childToDelete; 182 this.charValue = charValue; 183 } 184 185 public void addChild(InstanceCallbackClass child) { 186 children.add(child); 187 } 188 189 public void jdoPreStore() { 190 if(!performPreStoreTests) { return; } 191 captureValues2(); 192 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 193 usePM(pm); 194 } 195 196 public void jdoPreDelete() { 197 if(!performPreDeleteTests) { return; } 198 preDeleteCalled = true; 200 objectState = InstanceCallbackClass.currentState(this); 201 202 captureValues2(); 203 204 if(intValue >= 0 & intValue < arraySize) { 207 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 208 usePM(pm); 209 if(nextObj != null) { 210 pm.deletePersistent(nextObj); 212 for(Iterator i = children.iterator(); i.hasNext();) { 214 InstanceCallbackClass obj = (InstanceCallbackClass)i.next(); 215 if( obj.intValue == childToDelete) { 216 pm.deletePersistent(obj); 217 break; 218 } 219 } 220 } 221 } 222 } 223 224 public void jdoPostLoad() { 225 } 226 227 public void jdoPreClear() { 228 if(!performPreClearTests) { return; } 229 if(intValue >= 0 & intValue < arraySize) { 233 processedIndex[intValue] = true; 234 235 capturedName[intValue] = name; 237 capturedTimeStamp[intValue] = timeStamp; 238 capturedDoubleValue[intValue] = doubleValue; 239 numberOfChildren[intValue] = children.size(); 240 sumOfChildrenIntValue[intValue] = 0; 241 for(Iterator i = children.iterator(); i.hasNext();) { 242 InstanceCallbackClass o = (InstanceCallbackClass)i.next(); 243 sumOfChildrenIntValue[intValue] += o.intValue; 244 } 245 capturedChildToDelete[intValue] = childToDelete; 246 capturedCharValue[intValue] = charValue; 247 if(nextObj != null) { 248 capturedNextObjName[intValue] = nextObj.name; 249 } else { 250 capturedNextObjName[intValue] = null; 251 } 252 } 253 } 254 255 private void captureValues2() { 256 if(intValue >= 0 & intValue < arraySize) { 258 processedIndex[intValue] = true; 259 260 capturedName[intValue] = name; 262 capturedTimeStamp[intValue] = timeStamp; 263 capturedDoubleValue[intValue] = doubleValue; 264 numberOfChildren[intValue] = children.size(); 265 sumOfChildrenIntValue[intValue] = 0; 266 for(Iterator i = children.iterator(); i.hasNext();) { 267 InstanceCallbackClass o = (InstanceCallbackClass)i.next(); 268 sumOfChildrenIntValue[intValue] += o.intValue; 269 } 270 capturedChildToDelete[intValue] = childToDelete; 271 capturedCharValue[intValue] = charValue; 272 if(nextObj != null) { 273 capturedNextObjName[intValue] = nextObj.name; 274 } else { 275 capturedNextObjName[intValue] = null; 276 } 277 } 278 } 279 280 private void usePM(PersistenceManager pm) { 281 if(intValue >= 0 & intValue < arraySize) { 282 Transaction t = pm.currentTransaction(); 283 if(t.isActive()) { 284 transactionActive[intValue] = true; 285 } 286 } 287 } 288 289 public static class KeyClass implements Serializable { 290 public int keyValue; 291 292 public KeyClass() { 293 } 294 295 public KeyClass(String s) { 296 try{ keyValue = Integer.parseInt(s);} 297 catch(NumberFormatException e){ 298 keyValue = 0;} 299 } 300 301 public boolean equals(Object obj) { 302 if( obj == null || !this.getClass().equals(obj.getClass()) ) return false; 303 else return keyValue == ((KeyClass)obj).keyValue; 304 } 305 306 public int hashCode() { 307 return keyValue; 308 } 309 310 public String toString() { 311 return Integer.toString(keyValue); 312 } 313 } 314 } 315 | Popular Tags |