001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * Use is subject to license terms.
004: *
005: */
006:
007: package org.objectweb.speedo.pobjects.tck;
008:
009: import java.io.Serializable;
010: import java.util.Date;
011: import java.util.HashSet;
012: import java.util.Iterator;
013:
014: import javax.jdo.Extent;
015: import javax.jdo.InstanceCallbacks;
016: import javax.jdo.JDOHelper;
017: import javax.jdo.PersistenceManager;
018: import javax.jdo.Transaction;
019:
020: public class InstanceCallbackClass implements InstanceCallbacks {
021: public static final int TRANSIENT = 0;
022: public static final int PERSISTENT_NEW = 1;
023: public static final int PERSISTENT_CLEAN = 2;
024: public static final int PERSISTENT_DIRTY = 3;
025: public static final int HOLLOW = 4;
026: public static final int TRANSIENT_CLEAN = 5;
027: public static final int TRANSIENT_DIRTY = 6;
028: public static final int PERSISTENT_NEW_DELETED = 7;
029: public static final int PERSISTENT_DELETED = 8;
030: public static final int PERSISTENT_NONTRANSACTIONAL = 9;
031: public static final int NUM_STATES = 10;
032: public static final int ILLEGAL_STATE = 10;
033:
034: public static final String[] states = { "transient",
035: "persistent-new", "persistent-clean", "persistent-dirty",
036: "hollow", "transient-clean", "transient-dirty",
037: "persistent-new-deleted", "persistent-deleted",
038: "persistent-nontransactional", "illegal" };
039:
040: private static final int IS_PERSISTENT = 0;
041: private static final int IS_TRANSACTIONAL = 1;
042: private static final int IS_DIRTY = 2;
043: private static final int IS_NEW = 3;
044: private static final int IS_DELETED = 4;
045: private static final int NUM_STATUSES = 5;
046:
047: private static final boolean state_statuses[][] = {
048: // IS_PERSISTENT IS_TRANSACTIONAL IS_DIRTY IS_NEW IS_DELETED
049: // transient
050: { false, false, false, false, false },
051:
052: // persistent-new
053: { true, true, true, true, false },
054:
055: // persistent-clean
056: { true, true, false, false, false },
057:
058: // persistent-dirty
059: { true, true, true, false, false },
060:
061: // hollow
062: { true, false, false, false, false },
063:
064: // transient-clean
065: { false, true, false, false, false },
066:
067: // transient-dirty
068: { false, true, true, false, false },
069:
070: // persistent-new-deleted
071: { true, true, true, true, true },
072:
073: // persistent-deleted
074: { true, true, true, false, true },
075:
076: // persistent-nontransactional
077: { true, false, false, false, false } };
078:
079: public static int currentState(Object o) {
080: boolean[] status = new boolean[5];
081: status[IS_PERSISTENT] = JDOHelper.isPersistent(o);
082: status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o);
083: status[IS_DIRTY] = JDOHelper.isDirty(o);
084: status[IS_NEW] = JDOHelper.isNew(o);
085: status[IS_DELETED] = JDOHelper.isDeleted(o);
086: int i, j;
087: outerloop: for (i = 0; i < NUM_STATES; ++i) {
088: for (j = 0; j < NUM_STATUSES; ++j) {
089: if (status[j] != state_statuses[i][j])
090: continue outerloop;
091: }
092: return i;
093: }
094: return NUM_STATES;
095: }
096:
097: static public boolean performPreClearTests;
098: static public boolean performPreDeleteTests;
099: static public boolean performPreStoreTests;
100:
101: // The following two variables used in CallingJdoPreDelete tests
102: static boolean preDeleteCalled;
103: static int objectState;
104:
105: // The rest of these variables used in FieldsInPredelete tests, except to set a variable to make object dirty.
106: static public int arraySize = 5;
107: static public String[] capturedName = new String[arraySize];
108: static public Date[] capturedTimeStamp = new Date[arraySize];
109: static public double[] capturedDoubleValue = new double[arraySize];
110: static public short[] capturedChildToDelete = new short[arraySize];
111: static public char[] capturedCharValue = new char[arraySize];
112: static public String[] capturedNextObjName = new String[arraySize];
113: static public int[] numberOfChildren = new int[arraySize];
114: static public int[] sumOfChildrenIntValue = new int[arraySize];
115: static public boolean[] processedIndex = new boolean[arraySize];
116:
117: static public boolean[] transactionActive = new boolean[arraySize];
118:
119: static private int nextKeyValue = 1;
120: private int keyValue; // persistent--used as key field in application identity
121: public String name;
122: public Date timeStamp;
123: public InstanceCallbackClass nextObj;
124: public HashSet children;
125: public int intValue;
126: public double doubleValue;
127: public short childToDelete;
128: public char charValue;
129:
130: static public void initializeStaticsForTest() {
131: performPreClearTests = false;
132: performPreDeleteTests = false;
133: performPreStoreTests = false;
134: preDeleteCalled = false;
135: for (int i = 0; i < arraySize; ++i) {
136: processedIndex[i] = false;
137: transactionActive[i] = false;
138: }
139: }
140:
141: static public void removeAllInstances(PersistenceManager pm) {
142: //Extent e = pm.getExtent(org.objectweb.speedo.runtime.tck.InstanceCallbackClass.class, true);
143: Extent e = pm
144: .getExtent(
145: org.objectweb.speedo.pobjects.tck.InstanceCallbackClass.class,
146: false);
147: Iterator i = e.iterator();
148: while (i.hasNext()) {
149: pm.deletePersistent(i.next());
150: }
151: }
152:
153: InstanceCallbackClass() { // not used by application
154: }
155:
156: public InstanceCallbackClass(String label, Date createTime,
157: int intValue, double doubleValue, short childToDelete,
158: char charValue, InstanceCallbackClass obj) {
159: keyValue = nextKeyValue++;
160: name = label;
161: timeStamp = createTime;
162: nextObj = obj;
163: children = new HashSet();
164: this .intValue = intValue;
165: this .doubleValue = doubleValue;
166: this .childToDelete = childToDelete;
167: this .charValue = charValue;
168: }
169:
170: public void addChild(InstanceCallbackClass child) {
171: children.add(child);
172: }
173:
174: public void jdoPreStore() {
175: if (!performPreStoreTests) {
176: return;
177: }
178: captureValues2();
179: PersistenceManager pm = JDOHelper.getPersistenceManager(this );
180: usePM(pm);
181: }
182:
183: public void jdoPreDelete() {
184: if (!performPreDeleteTests) {
185: return;
186: }
187: // The following two variables set for CallingJdoPreDelete tests
188: preDeleteCalled = true;
189: objectState = InstanceCallbackClass.currentState(this );
190:
191: captureValues2();
192:
193: // The rest of this routine used for Accessing FieldsInPredelete tests
194: // check that intValue is a valid index
195: if (intValue >= 0 & intValue < arraySize) {
196: PersistenceManager pm = JDOHelper
197: .getPersistenceManager(this );
198: usePM(pm);
199: if (nextObj != null) {
200: pm.deletePersistent(nextObj); // delete referenced object
201:
202: // delete designated child
203: for (Iterator i = children.iterator(); i.hasNext();) {
204: InstanceCallbackClass obj = (InstanceCallbackClass) i
205: .next();
206: if (obj.intValue == childToDelete) {
207: pm.deletePersistent(obj);
208: break;
209: }
210: }
211: }
212: }
213: }
214:
215: public void jdoPostLoad() {
216: }
217:
218: public void jdoPreClear() {
219: if (!performPreClearTests) {
220: return;
221: }
222: // the following code is copied from captureValues, because it must
223: // not be enhanced for execution during jdoPreClear.
224: // check that intValue is a valid index
225: if (intValue >= 0 & intValue < arraySize) {
226: processedIndex[intValue] = true;
227:
228: // capture values of the attributes
229: capturedName[intValue] = name;
230: capturedTimeStamp[intValue] = timeStamp;
231: capturedDoubleValue[intValue] = doubleValue;
232: numberOfChildren[intValue] = children.size();
233: sumOfChildrenIntValue[intValue] = 0;
234: for (Iterator i = children.iterator(); i.hasNext();) {
235: InstanceCallbackClass o = (InstanceCallbackClass) i
236: .next();
237: sumOfChildrenIntValue[intValue] += o.intValue;
238: }
239: capturedChildToDelete[intValue] = childToDelete;
240: capturedCharValue[intValue] = charValue;
241: if (nextObj != null) {
242: capturedNextObjName[intValue] = nextObj.name;
243: } else {
244: capturedNextObjName[intValue] = null;
245: }
246: }
247: }
248:
249: private void captureValues2() {
250: // check that intValue is a valid index
251: if (intValue >= 0 & intValue < arraySize) {
252: processedIndex[intValue] = true;
253:
254: // capture values of the attributes
255: capturedName[intValue] = name;
256: capturedTimeStamp[intValue] = timeStamp;
257: capturedDoubleValue[intValue] = doubleValue;
258: numberOfChildren[intValue] = children.size();
259: sumOfChildrenIntValue[intValue] = 0;
260: for (Iterator i = children.iterator(); i.hasNext();) {
261: InstanceCallbackClass o = (InstanceCallbackClass) i
262: .next();
263: sumOfChildrenIntValue[intValue] += o.intValue;
264: }
265: capturedChildToDelete[intValue] = childToDelete;
266: capturedCharValue[intValue] = charValue;
267: if (nextObj != null) {
268: capturedNextObjName[intValue] = nextObj.name;
269: } else {
270: capturedNextObjName[intValue] = null;
271: }
272: }
273: }
274:
275: private void usePM(PersistenceManager pm) {
276: if (intValue >= 0 & intValue < arraySize) {
277: Transaction t = pm.currentTransaction();
278: if (t.isActive()) {
279: transactionActive[intValue] = true;
280: }
281: }
282: }
283:
284: public static class KeyClass implements Serializable {
285: public int keyValue;
286:
287: public KeyClass() {
288: }
289:
290: public KeyClass(String s) {
291: try {
292: keyValue = Integer.parseInt(s);
293: } catch (NumberFormatException e) {
294: keyValue = 0;
295: }
296: }
297:
298: public boolean equals(Object obj) {
299: if (obj == null || !this .getClass().equals(obj.getClass()))
300: return false;
301: else
302: return keyValue == ((KeyClass) obj).keyValue;
303: }
304:
305: public int hashCode() {
306: return keyValue;
307: }
308:
309: public String toString() {
310: return Integer.toString(keyValue);
311: }
312: }
313: }
|