001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.tck;
018:
019: import org.objectweb.speedo.SpeedoTestHelper;
020: import org.objectweb.speedo.pobjects.basic.BasicA;
021: import org.objectweb.util.monolog.api.BasicLevel;
022:
023: import javax.jdo.JDOHelper;
024: import javax.jdo.PersistenceManager;
025: import javax.jdo.Transaction;
026:
027: public class JdoHelperTck extends SpeedoTestHelper {
028:
029: public static final int TRANSIENT = 0;
030: public static final int PERSISTENT_NEW = 1;
031: public static final int PERSISTENT_CLEAN = 2;
032: public static final int PERSISTENT_DIRTY = 3;
033: public static final int HOLLOW = 4;
034: public static final int TRANSIENT_CLEAN = 5;
035: public static final int TRANSIENT_DIRTY = 6;
036: public static final int PERSISTENT_NEW_DELETED = 7;
037: public static final int PERSISTENT_DELETED = 8;
038: public static final int PERSISTENT_NONTRANSACTIONAL = 9;
039: public static final int NUM_STATES = 10;
040: public static final int ILLEGAL_STATE = 10;
041:
042: private static final int IS_PERSISTENT = 0;
043: private static final int IS_TRANSACTIONAL = 1;
044: private static final int IS_DIRTY = 2;
045: private static final int IS_NEW = 3;
046: private static final int IS_DELETED = 4;
047: private static final int NUM_STATUSES = 5;
048:
049: private static final boolean state_statuses[][] = {
050: // IS_PERSISTENT IS_TRANSACTIONAL IS_DIRTY IS_NEW IS_DELETED
051: // transient
052: { false, false, false, false, false },
053:
054: // persistent-new
055: { true, true, true, true, false },
056:
057: // persistent-clean
058: { true, true, false, false, false },
059:
060: // persistent-dirty
061: { true, true, true, false, false },
062:
063: // hollow
064: { true, false, false, false, false },
065:
066: // transient-clean
067: { false, true, false, false, false },
068:
069: // transient-dirty
070: { false, true, true, false, false },
071:
072: // persistent-new-deleted
073: { true, true, true, true, true },
074:
075: // persistent-deleted
076: { true, true, true, false, true },
077:
078: // persistent-nontransactional
079: { true, false, false, false, false } };
080:
081: /**
082: * This utility method returns a <code>String</code> that indicates the current state of an instance.
083: *
084: * @param o
085: * The object.
086: * @return The current state of the instance, by using the <code>JDOHelper</code> state interrogation methods.
087: */
088: public static String getStateOfInstance(Object o) {
089: boolean existingEntries = false;
090: StringBuffer buff = new StringBuffer("{");
091: if (JDOHelper.isPersistent(o)) {
092: buff.append("persistent");
093: existingEntries = true;
094: }
095: if (JDOHelper.isTransactional(o)) {
096: if (existingEntries)
097: buff.append(", ");
098: buff.append("transactional");
099: existingEntries = true;
100: }
101: if (JDOHelper.isDirty(o)) {
102: if (existingEntries)
103: buff.append(", ");
104: buff.append("dirty");
105: existingEntries = true;
106: }
107: if (JDOHelper.isNew(o)) {
108: if (existingEntries)
109: buff.append(", ");
110: buff.append("new");
111: existingEntries = true;
112: }
113: if (JDOHelper.isDeleted(o)) {
114: if (existingEntries)
115: buff.append(", ");
116: buff.append("deleted");
117: }
118: buff.append("}");
119: return buff.toString();
120: }
121:
122: public static int currentState(Object o) {
123: boolean[] status = new boolean[5];
124: status[IS_PERSISTENT] = JDOHelper.isPersistent(o);
125: status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o);
126: status[IS_DIRTY] = JDOHelper.isDirty(o);
127: status[IS_NEW] = JDOHelper.isNew(o);
128: status[IS_DELETED] = JDOHelper.isDeleted(o);
129: int i, j;
130: outerloop: for (i = 0; i < NUM_STATES; ++i) {
131: for (j = 0; j < NUM_STATUSES; ++j) {
132: if (status[j] != state_statuses[i][j])
133: continue outerloop;
134: }
135: return i;
136: }
137: return NUM_STATES;
138: }
139:
140: private PersistenceManager pm;
141: private PersistenceManager pm1;
142: private Transaction transaction;
143:
144: public JdoHelperTck(String s) {
145: super (s);
146: //pm = pmf.getPersistenceManager();
147: }
148:
149: protected String getLoggerName() {
150: return LOG_NAME + ".rt.tck.JdoHelperTck";
151: }
152:
153: /*
154: public void testGetObjectId() {
155: logger.log(BasicLevel.INFO, "testGetObjectId");
156: try {
157: JDOTransactionItf tx = pm.currentTransaction();
158: tx.begin();
159: BasicA nba1 = new BasicA();
160: pm.makePersistent(nba1);
161:
162: Object p2 = JDOHelper.getObjectId(nba1);
163: tx.commit();
164:
165: Assert.assertNotNull("bad getObjectId value", p2);
166:
167: } catch (Exception ex) {
168: logger.log(BasicLevel.ERROR, "Unexception caught in testGetObjectId", ex);
169: fail(ex.getMessage());
170: }
171:
172: }
173:
174: public void testGetObjectIdForNull() {
175: logger.log(BasicLevel.INFO, "testGetObjectIdForNull");
176: try {
177: JDOTransactionItf tx = pm.currentTransaction();
178: tx.begin();
179: Object p1 = null;
180: Object oid = JDOHelper.getObjectId(p1);
181: tx.commit();
182:
183: Assert.assertTrue("bad oid value", oid == null);
184:
185: } catch (Exception ex) {
186: logger.log(BasicLevel.ERROR, "Unexception caught in testGetObjectIdForNull", ex);
187: fail(ex.getMessage());
188: }
189: }
190:
191: public void testGetPersistenceManager() {
192: logger.log(BasicLevel.INFO, "testGetPersistenceManager");
193: try {
194:
195: JDOTransactionItf tx = pm.currentTransaction();
196: tx.begin();
197: BasicA nba1 = new BasicA();
198: pm.makePersistent(nba1);
199: pm1 = JDOHelper.getPersistenceManager(nba1);
200: tx.commit();
201: assertTrue("bad getPersistenceManager value", (pm1 != null));
202:
203: } catch (Exception ex) {
204: logger.log(BasicLevel.ERROR, "Unexception caught in testGetPersistenceManager", ex);
205: fail(ex.getMessage());
206: }
207: }
208:
209: public void testGetTransactionalObjectId() {
210: logger.log(BasicLevel.INFO, "testGetTransactionalObjectId");
211: try {
212: JDOTransactionItf tx = pm.currentTransaction();
213: tx.begin();
214: BasicA nba1 = new BasicA();
215: pm.makePersistent(nba1);
216:
217: Object p2 = JDOHelper.getTransactionalObjectId(nba1);
218: tx.commit();
219:
220: assertTrue("bad getTransactionalObjectId value", (p2 != null));
221:
222: } catch (Exception ex) {
223: logger.log(BasicLevel.ERROR, "Unexception caught in testGetTransactionalObjectId", ex);
224: fail(ex.getMessage());
225: }
226:
227: }
228:
229: public void testIsDeletedFalse() {
230: logger.log(BasicLevel.INFO, "testIsDeletedFalse");
231: try {
232:
233: JDOTransactionItf tx = pm.currentTransaction();
234: tx.begin();
235: BasicA p1 = new BasicA();
236: pm.makePersistent(p1);
237: //pm.deletePersistent(p1);
238: boolean flag = JDOHelper.isDeleted(p1);
239: tx.commit();
240:
241: assertTrue("bad isDeleted value", !flag);
242:
243: } catch (Exception ex) {
244: logger.log(BasicLevel.ERROR, "Unexception caught in testIsTransactionalFalse", ex);
245: fail(ex.getMessage());
246: }
247:
248: }
249:
250: public void testIsTransactionalFalse() {
251: logger.log(BasicLevel.INFO, "testIsTransactionalFalse");
252:
253: try {
254: JDOTransactionItf tx = pm.currentTransaction();
255: tx.begin();
256: BasicA p1 = new BasicA();
257: pm.makePersistent(p1);
258: boolean flag = JDOHelper.isTransactional(p1);
259: tx.commit();
260:
261: JDOTransactionItf tx1 = pm.currentTransaction();
262: tx1.begin();
263: flag = JDOHelper.isTransactional(p1);
264: tx1.commit();
265:
266: assertTrue("bad isTransactional value", !flag);
267:
268: } catch (Exception ex) {
269: logger.log(BasicLevel.ERROR, "Unexception caught in testIsTransactionalFalse", ex);
270: fail(ex.getMessage());
271: }
272: }
273: */
274: public void testMakeDirty() {
275: logger.log(BasicLevel.INFO, "testMakeDirty");
276: try {
277: pm = pmf.getPersistenceManager();
278: Transaction tx = pm.currentTransaction();
279: tx.begin();
280: BasicA p1 = new BasicA();
281: pm.makePersistent(p1);
282: tx.commit();
283:
284: Transaction tx1 = pm.currentTransaction();
285: tx1.begin();
286: JDOHelper.makeDirty(p1, "f1");
287:
288: int state = currentState(p1);
289: logger.log(BasicLevel.INFO, "currentState(p1)=" + state
290: + "getStateOfInstance(p1)="
291: + getStateOfInstance(p1));
292:
293: boolean flag = JDOHelper.isDirty(p1);
294: tx1.commit();
295:
296: assertTrue("bad isDirty value", flag);
297:
298: } catch (Exception ex) {
299: logger.log(BasicLevel.ERROR,
300: "Unexception caught in testMakeDirty", ex);
301: fail(ex.getMessage());
302: } finally {
303: pm.close();
304: }
305: }
306:
307: public void testIsDeletedForTransient() {
308: logger.log(BasicLevel.INFO, "testIsDeletedForTransient");
309: try {
310: pm = pmf.getPersistenceManager();
311: Transaction tx = pm.currentTransaction();
312: tx.begin();
313: BasicA p1 = new BasicA();
314:
315: int state = currentState(p1);
316:
317: logger.log(BasicLevel.INFO, "IS_PERSISTENT="
318: + JDOHelper.isPersistent(p1));
319: logger.log(BasicLevel.INFO, "IS_TRANSACTIONAL="
320: + JDOHelper.isTransactional(p1));
321: logger.log(BasicLevel.INFO, "IS_DIRTY="
322: + JDOHelper.isDirty(p1));
323: logger
324: .log(BasicLevel.INFO, "IS_NEW="
325: + JDOHelper.isNew(p1));
326: logger.log(BasicLevel.INFO, "IS_DELETED="
327: + JDOHelper.isDeleted(p1));
328:
329: logger.log(BasicLevel.INFO, "currentState(p1)=" + state
330: + " getStateOfInstance(p1)="
331: + getStateOfInstance(p1));
332:
333: boolean flag = JDOHelper.isDeleted(p1);
334: tx.commit();
335:
336: assertTrue("bad isDeletedforTransient value", !flag);
337:
338: } catch (Exception ex) {
339: logger.log(BasicLevel.ERROR,
340: "Unexception caught in testIsDeletedForTransient",
341: ex);
342: fail(ex.getMessage());
343: } finally {
344: pm.close();
345: }
346: }
347:
348: }
|