KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > tck > JdoHelperTck


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.runtime.tck;
19
20 import org.objectweb.speedo.SpeedoTestHelper;
21 import org.objectweb.speedo.pobjects.basic.BasicA;
22 import org.objectweb.util.monolog.api.BasicLevel;
23
24 import javax.jdo.JDOHelper;
25 import javax.jdo.PersistenceManager;
26 import javax.jdo.Transaction;
27
28 public class JdoHelperTck extends SpeedoTestHelper {
29
30     public static final int TRANSIENT = 0;
31     public static final int PERSISTENT_NEW = 1;
32     public static final int PERSISTENT_CLEAN = 2;
33     public static final int PERSISTENT_DIRTY = 3;
34     public static final int HOLLOW = 4;
35     public static final int TRANSIENT_CLEAN = 5;
36     public static final int TRANSIENT_DIRTY = 6;
37     public static final int PERSISTENT_NEW_DELETED = 7;
38     public static final int PERSISTENT_DELETED = 8;
39     public static final int PERSISTENT_NONTRANSACTIONAL = 9;
40     public static final int NUM_STATES = 10;
41     public static final int ILLEGAL_STATE = 10;
42
43     private static final int IS_PERSISTENT = 0;
44     private static final int IS_TRANSACTIONAL = 1;
45     private static final int IS_DIRTY = 2;
46     private static final int IS_NEW = 3;
47     private static final int IS_DELETED = 4;
48     private static final int NUM_STATUSES = 5;
49
50     private static final boolean state_statuses[][] = {
51         // IS_PERSISTENT IS_TRANSACTIONAL IS_DIRTY IS_NEW IS_DELETED
52
// transient
53
{ false, false, false, false, false },
54
55         // persistent-new
56
{
57             true, true, true, true, false },
58
59         // persistent-clean
60
{
61             true, true, false, false, false },
62
63         // persistent-dirty
64
{
65             true, true, true, false, false },
66
67         // hollow
68
{
69             true, false, false, false, false },
70
71         // transient-clean
72
{
73             false, true, false, false, false },
74
75         // transient-dirty
76
{
77             false, true, true, false, false },
78
79         // persistent-new-deleted
80
{
81             true, true, true, true, true },
82
83         // persistent-deleted
84
{
85             true, true, true, false, true },
86
87         // persistent-nontransactional
88
{
89             true, false, false, false, false }
90     };
91
92     /**
93      * This utility method returns a <code>String</code> that indicates the current state of an instance.
94      *
95      * @param o
96      * The object.
97      * @return The current state of the instance, by using the <code>JDOHelper</code> state interrogation methods.
98      */

99     public static String JavaDoc getStateOfInstance(Object JavaDoc o) {
100         boolean existingEntries = false;
101         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("{");
102         if (JDOHelper.isPersistent(o)) {
103             buff.append("persistent");
104             existingEntries = true;
105         }
106         if (JDOHelper.isTransactional(o)) {
107             if (existingEntries)
108                 buff.append(", ");
109             buff.append("transactional");
110             existingEntries = true;
111         }
112         if (JDOHelper.isDirty(o)) {
113             if (existingEntries)
114                 buff.append(", ");
115             buff.append("dirty");
116             existingEntries = true;
117         }
118         if (JDOHelper.isNew(o)) {
119             if (existingEntries)
120                 buff.append(", ");
121             buff.append("new");
122             existingEntries = true;
123         }
124         if (JDOHelper.isDeleted(o)) {
125             if (existingEntries)
126                 buff.append(", ");
127             buff.append("deleted");
128         }
129         buff.append("}");
130         return buff.toString();
131     }
132
133     public static int currentState(Object JavaDoc o) {
134         boolean[] status = new boolean[5];
135         status[IS_PERSISTENT] = JDOHelper.isPersistent(o);
136         status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o);
137         status[IS_DIRTY] = JDOHelper.isDirty(o);
138         status[IS_NEW] = JDOHelper.isNew(o);
139         status[IS_DELETED] = JDOHelper.isDeleted(o);
140         int i, j;
141         outerloop : for (i = 0; i < NUM_STATES; ++i) {
142             for (j = 0; j < NUM_STATUSES; ++j) {
143                 if (status[j] != state_statuses[i][j])
144                     continue outerloop;
145             }
146             return i;
147         }
148         return NUM_STATES;
149     }
150
151     private PersistenceManager pm;
152     private PersistenceManager pm1;
153     private Transaction transaction;
154
155     public JdoHelperTck(String JavaDoc s) {
156         super(s);
157         //pm = pmf.getPersistenceManager();
158
}
159
160     protected String JavaDoc getLoggerName() {
161         return LOG_NAME + ".rt.tck.JdoHelperTck";
162     }
163 /*
164     public void testGetObjectId() {
165         logger.log(BasicLevel.INFO, "testGetObjectId");
166         try {
167             Transaction tx = pm.currentTransaction();
168             tx.begin();
169             BasicA nba1 = new BasicA();
170             pm.makePersistent(nba1);
171
172             Object p2 = JDOHelper.getObjectId(nba1);
173             tx.commit();
174
175             Assert.assertNotNull("bad getObjectId value", p2);
176
177         } catch (Exception ex) {
178             logger.log(BasicLevel.ERROR, "Unexception caught in testGetObjectId", ex);
179             fail(ex.getMessage());
180         }
181
182     }
183
184     public void testGetObjectIdForNull() {
185         logger.log(BasicLevel.INFO, "testGetObjectIdForNull");
186         try {
187             Transaction tx = pm.currentTransaction();
188             tx.begin();
189             Object p1 = null;
190             Object oid = JDOHelper.getObjectId(p1);
191             tx.commit();
192
193             Assert.assertTrue("bad oid value", oid == null);
194
195         } catch (Exception ex) {
196             logger.log(BasicLevel.ERROR, "Unexception caught in testGetObjectIdForNull", ex);
197             fail(ex.getMessage());
198         }
199     }
200
201     public void testGetPersistenceManager() {
202         logger.log(BasicLevel.INFO, "testGetPersistenceManager");
203         try {
204
205             Transaction tx = pm.currentTransaction();
206             tx.begin();
207             BasicA nba1 = new BasicA();
208             pm.makePersistent(nba1);
209             pm1 = JDOHelper.getPersistenceManager(nba1);
210             tx.commit();
211             assertTrue("bad getPersistenceManager value", (pm1 != null));
212
213         } catch (Exception ex) {
214             logger.log(BasicLevel.ERROR, "Unexception caught in testGetPersistenceManager", ex);
215             fail(ex.getMessage());
216         }
217     }
218
219     public void testGetTransactionalObjectId() {
220         logger.log(BasicLevel.INFO, "testGetTransactionalObjectId");
221         try {
222             Transaction tx = pm.currentTransaction();
223             tx.begin();
224             BasicA nba1 = new BasicA();
225             pm.makePersistent(nba1);
226
227             Object p2 = JDOHelper.getTransactionalObjectId(nba1);
228             tx.commit();
229
230             assertTrue("bad getTransactionalObjectId value", (p2 != null));
231
232         } catch (Exception ex) {
233             logger.log(BasicLevel.ERROR, "Unexception caught in testGetTransactionalObjectId", ex);
234             fail(ex.getMessage());
235         }
236
237     }
238
239     public void testIsDeletedFalse() {
240         logger.log(BasicLevel.INFO, "testIsDeletedFalse");
241         try {
242
243             Transaction tx = pm.currentTransaction();
244             tx.begin();
245             BasicA p1 = new BasicA();
246             pm.makePersistent(p1);
247             //pm.deletePersistent(p1);
248             boolean flag = JDOHelper.isDeleted(p1);
249             tx.commit();
250
251             assertTrue("bad isDeleted value", !flag);
252
253         } catch (Exception ex) {
254             logger.log(BasicLevel.ERROR, "Unexception caught in testIsTransactionalFalse", ex);
255             fail(ex.getMessage());
256         }
257
258     }
259
260     public void testIsTransactionalFalse() {
261         logger.log(BasicLevel.INFO, "testIsTransactionalFalse");
262
263         try {
264             Transaction tx = pm.currentTransaction();
265             tx.begin();
266             BasicA p1 = new BasicA();
267             pm.makePersistent(p1);
268             boolean flag = JDOHelper.isTransactional(p1);
269             tx.commit();
270
271             Transaction tx1 = pm.currentTransaction();
272             tx1.begin();
273             flag = JDOHelper.isTransactional(p1);
274             tx1.commit();
275
276             assertTrue("bad isTransactional value", !flag);
277
278         } catch (Exception ex) {
279             logger.log(BasicLevel.ERROR, "Unexception caught in testIsTransactionalFalse", ex);
280             fail(ex.getMessage());
281         }
282     }
283 */

284     public void testMakeDirty() {
285         logger.log(BasicLevel.INFO, "testMakeDirty");
286         try {
287             pm = pmf.getPersistenceManager();
288             Transaction tx = pm.currentTransaction();
289             tx.begin();
290             BasicA p1 = new BasicA();
291             pm.makePersistent(p1);
292             tx.commit();
293
294             Transaction tx1 = pm.currentTransaction();
295             tx1.begin();
296             JDOHelper.makeDirty(p1, "f1");
297
298             int state = currentState(p1);
299             logger.log(
300                 BasicLevel.INFO,
301                 "currentState(p1)=" + state + "getStateOfInstance(p1)=" + getStateOfInstance(p1));
302
303             boolean flag = JDOHelper.isDirty(p1);
304             tx1.commit();
305
306             assertTrue("bad isDirty value", flag);
307
308         } catch (Exception JavaDoc ex) {
309             logger.log(BasicLevel.ERROR, "Unexception caught in testMakeDirty", ex);
310             fail(ex.getMessage());
311         } finally {
312             pm.close();
313         }
314     }
315
316     public void testIsDeletedForTransient() {
317         logger.log(BasicLevel.INFO, "testIsDeletedForTransient");
318         try {
319             pm = pmf.getPersistenceManager();
320             Transaction tx = pm.currentTransaction();
321             tx.begin();
322             BasicA p1 = new BasicA();
323             
324             int state = currentState(p1);
325
326             logger.log(BasicLevel.INFO, "IS_PERSISTENT=" + JDOHelper.isPersistent(p1));
327             logger.log(BasicLevel.INFO, "IS_TRANSACTIONAL=" + JDOHelper.isTransactional(p1));
328             logger.log(BasicLevel.INFO, "IS_DIRTY=" + JDOHelper.isDirty(p1));
329             logger.log(BasicLevel.INFO, "IS_NEW=" + JDOHelper.isNew(p1));
330             logger.log(BasicLevel.INFO, "IS_DELETED=" + JDOHelper.isDeleted(p1));
331
332             logger.log(
333                 BasicLevel.INFO,
334                 "currentState(p1)=" + state + " getStateOfInstance(p1)=" + getStateOfInstance(p1));
335
336             boolean flag = JDOHelper.isDeleted(p1);
337             tx.commit();
338
339             assertTrue("bad isDeletedforTransient value", !flag);
340
341         } catch (Exception JavaDoc ex) {
342             logger.log(BasicLevel.ERROR, "Unexception caught in testIsDeletedForTransient", ex);
343             fail(ex.getMessage());
344         } finally {
345             pm.close();
346         }
347     }
348
349 }
350
Popular Tags