KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.util.monolog.api.Logger;
24
25 import java.io.FileInputStream JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.jdo.JDOHelper;
32 import javax.jdo.JDOUserException;
33 import javax.jdo.PersistenceManager;
34 import javax.jdo.PersistenceManagerFactory;
35 import javax.jdo.Query;
36 import javax.jdo.Transaction;
37
38 import junit.framework.Assert;
39
40 public class PersistenceManagerTck extends SpeedoTestHelper {
41
42     public static final int TRANSIENT = 0;
43     public static final int PERSISTENT_NEW = 1;
44     public static final int PERSISTENT_CLEAN = 2;
45     public static final int PERSISTENT_DIRTY = 3;
46     public static final int HOLLOW = 4;
47     public static final int TRANSIENT_CLEAN = 5;
48     public static final int TRANSIENT_DIRTY = 6;
49     public static final int PERSISTENT_NEW_DELETED = 7;
50     public static final int PERSISTENT_DELETED = 8;
51     public static final int PERSISTENT_NONTRANSACTIONAL = 9;
52     public static final int NUM_STATES = 10;
53     public static final int ILLEGAL_STATE = 10;
54
55     public static final String JavaDoc[] states =
56         {
57             "transient",
58             "persistent-new",
59             "persistent-clean",
60             "persistent-dirty",
61             "hollow",
62             "transient-clean",
63             "transient-dirty",
64             "persistent-new-deleted",
65             "persistent-deleted",
66             "persistent-nontransactional",
67             "illegal" };
68
69     private static final int IS_PERSISTENT = 0;
70     private static final int IS_TRANSACTIONAL = 1;
71     private static final int IS_DIRTY = 2;
72     private static final int IS_NEW = 3;
73     private static final int IS_DELETED = 4;
74     private static final int NUM_STATUSES = 5;
75
76     private static final boolean state_statuses[][] = {
77         // IS_PERSISTENT IS_TRANSACTIONAL IS_DIRTY IS_NEW IS_DELETED
78
// transient
79
{ false, false, false, false, false },
80
81         // persistent-new
82
{
83             true, true, true, true, false },
84
85         // persistent-clean
86
{
87             true, true, false, false, false },
88
89         // persistent-dirty
90
{
91             true, true, true, false, false },
92
93         // hollow
94
{
95             true, false, false, false, false },
96
97         // transient-clean
98
{
99             false, true, false, false, false },
100
101         // transient-dirty
102
{
103             false, true, true, false, false },
104
105         // persistent-new-deleted
106
{
107             true, true, true, true, true },
108
109         // persistent-deleted
110
{
111             true, true, true, false, true },
112
113         // persistent-nontransactional
114
{
115             true, false, false, false, false }
116     };
117
118     /**
119      *This utility method returns a <code>String</code> that indicates the current state of an instance.
120      *@param o The object.
121      *@return The current state of the instance, by using the <code>JDOHelper</code> state
122      *interrogation methods.
123      */

124     public static String JavaDoc getStateOfInstance(Object JavaDoc o)
125     {
126         boolean existingEntries = false;
127         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("{");
128         if( JDOHelper.isPersistent(o) ){
129             buff.append("persistent");
130             existingEntries = true;
131         }
132         if( JDOHelper.isTransactional(o) ){
133             if( existingEntries ) buff.append(", ");
134             buff.append("transactional");
135             existingEntries = true;
136         }
137         if( JDOHelper.isDirty(o) ){
138             if( existingEntries ) buff.append(", ");
139             buff.append("dirty");
140             existingEntries = true;
141         }
142         if( JDOHelper.isNew(o) ){
143             if( existingEntries ) buff.append(", ");
144             buff.append("new");
145             existingEntries = true;
146         }
147         if( JDOHelper.isDeleted(o) ){
148             if( existingEntries ) buff.append(", ");
149             buff.append("deleted");
150         }
151         buff.append("}");
152         return buff.toString();
153     }
154     
155     public static int currentState(Object JavaDoc o) {
156         boolean[] status = new boolean[5];
157         status[IS_PERSISTENT] = JDOHelper.isPersistent(o);
158         status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o);
159         status[IS_DIRTY] = JDOHelper.isDirty(o);
160         status[IS_NEW] = JDOHelper.isNew(o);
161         status[IS_DELETED] = JDOHelper.isDeleted(o);
162         int i, j;
163         outerloop : for (i = 0; i < NUM_STATES; ++i) {
164             for (j = 0; j < NUM_STATUSES; ++j) {
165                 if (status[j] != state_statuses[i][j])
166                     continue outerloop;
167             }
168             return i;
169         }
170         return NUM_STATES;
171     }
172
173 // These values MUST correspond to index in sorted array below.
174
private static final int OPTION_APPLICATIONIDENTITY = 0;
175     private static final int OPTION_ARRAY = 1;
176     private static final int OPTION_ARRAYLIST = 2;
177     private static final int OPTION_CHANGEAPPLICATIONIDENTITY = 3;
178     private static final int OPTION_DATASTOREIDENTITY = 4;
179     private static final int OPTION_HASHMAP = 5;
180     private static final int OPTION_HASHTABLE = 6;
181     private static final int OPTION_LINKEDLIST = 7;
182     private static final int OPTION_LIST = 8;
183     private static final int OPTION_MAP = 9;
184     private static final int OPTION_NONDURABLEIDENTITY = 10;
185     private static final int OPTION_NONTRANSACTIONALREAD = 11;
186     private static final int OPTION_NONTRANSACTIONALWRITE = 12;
187     private static final int OPTION_NULLCOLLECTION = 13;
188     private static final int OPTION_OPTIMISTIC = 14;
189     private static final int OPTION_RETAINVALUES = 15;
190     private static final int OPTION_TRANSIENTTRANSACTIONAL = 16;
191     private static final int OPTION_TREEMAP = 17;
192     private static final int OPTION_TREESET = 18;
193     private static final int OPTION_VECTOR = 19;
194
195     private static boolean[] options = new boolean[OPTION_VECTOR + 1];
196     
197     
198     static {
199         for (int i=0; i< OPTION_VECTOR+1; i++)
200             options[i] = true;
201         
202         options[OPTION_CHANGEAPPLICATIONIDENTITY] = false;
203         options[OPTION_NONDURABLEIDENTITY] = false;
204     }
205     
206     
207     /**
208      * Reports whether Optimistic is supported.
209      */

210     public static boolean isOptimisticSupported() {
211         return options[OPTION_OPTIMISTIC];
212     }
213
214
215     static PersistenceManager pm10 = null;
216     static PersistenceManager pm20 = null;
217
218     
219     
220     BasicA ba1 = null;
221     BasicA ba2 = null;
222     BasicA ba3 = null;
223     BasicA ba4 = null;
224     BasicA ba5 = null;
225     private PersistenceManager pm;
226     private Transaction tx;
227
228     BasicA b1 = null;
229     private int totalThreadCount = 10;
230     private int completedThreadCount = 0;
231     private int succeeds = 0;
232     private int catchNumber = 0;
233
234     public PersistenceManagerTck(String JavaDoc s) {
235         super(s);
236     }
237
238     protected String JavaDoc getLoggerName() {
239         return LOG_NAME + ".rt.tck.PersistenceManagerTck";
240     }
241
242     
243     protected PersistenceManager getPM() {
244         if (pm == null) {
245             pm = getPMF().getPersistenceManager();
246         }
247         return pm;
248     }
249
250     protected void closePM() {
251         if (pm != null) {
252             pm.close();
253             pm = null;
254         }
255     }
256
257 /*
258     private void createObjects() {
259         try {
260
261             getPM().currentTransaction().begin();
262             ba1 = new BasicA();
263             ba2 = new BasicA();
264             ba3 = new BasicA();
265             ba4 = new BasicA();
266             ba5 = new BasicA();
267
268             getPM().makePersistent(ba1);
269             getPM().makePersistent(ba2);
270             getPM().makePersistent(ba3);
271             getPM().makePersistent(ba4);
272             getPM().makePersistent(ba5);
273
274             getPM().currentTransaction().commit();
275
276         } catch (Exception ex) {
277             logger.log(BasicLevel.ERROR, "Unexception caught in createObjects", ex);
278             closePM();
279             fail(ex.getMessage());
280         }
281     }
282 */

283     
284     public void testConcurrentPersistenceManagersSameClasses() {
285
286         logger.log(BasicLevel.INFO, "testConcurrentPersistenceManagersSameClasses");
287         Properties JavaDoc pmfProperties = loadPMF2Properties();
288         PersistenceManagerFactory pmf2 = JDOHelper.getPersistenceManagerFactory(pmfProperties);
289         PersistenceManager pm2 = pmf2.getPersistenceManager();
290         logger.log(BasicLevel.DEBUG, "begin getTransactions");
291         Transaction tx2 = pm2.currentTransaction();
292         tx = getPM().currentTransaction();
293         logger.log(BasicLevel.DEBUG, "begin initData");
294         tx.begin();
295         tx2.begin();
296
297         BasicA ba11 = new BasicA();
298         ba11.writeF1("ba11");
299         BasicA ba12 = new BasicA();
300         ba11.writeF1("ba12");
301
302         getPM().makePersistent(ba11);
303         getPM().makePersistent(ba12);
304
305         BasicA ba21 = new BasicA();
306         ba11.writeF1("ba21");
307         BasicA ba22 = new BasicA();
308         ba11.writeF1("ba22");
309
310         pm2.makePersistent(ba21);
311         pm2.makePersistent(ba22);
312
313         tx.commit();
314         tx2.commit();
315
316         logger.log(BasicLevel.DEBUG, "begin test");
317         tx.begin();
318         tx2.begin();
319
320         BasicA ba11a = findBasicA(pm, "ba11");
321         Assert.assertEquals(ba11a, ba11);
322
323         BasicA ba21a = findBasicA(pm2, "ba21");
324         Assert.assertEquals(ba21a, ba21);
325
326         tx.commit();
327         tx2.commit();
328
329         closePM();
330         pm2.close();
331
332         logger.log(BasicLevel.DEBUG, "passed in testConcurrentPersistenceManagersSameClasses");
333
334     }
335
336     Properties JavaDoc loadPMF2Properties() {
337 /*
338         String PMF2 = System.getProperty("PMF2Properties", "c:\\jdotck\\jdori2.properties");
339         logger.log(BasicLevel.DEBUG, "Got PMF2Properties file name:" + PMF2);
340         Properties ret = loadProperties(PMF2);
341         logger.log(BasicLevel.DEBUG, "Got PMF2Properties: " + ret);
342 */

343         return getPMFPropertiesFromFile();
344     }
345
346     BasicA findBasicA(PersistenceManager pm, String JavaDoc param) {
347         Query q = pm.newQuery(BasicA.class);
348         q.declareParameters("String param");
349         q.setFilter("(param == f1)");
350         Collection JavaDoc results = (Collection JavaDoc) q.execute(param);
351         Iterator JavaDoc it = results.iterator();
352         BasicA ret = (BasicA) it.next();
353         return ret;
354     }
355
356     /**
357      * This method load Properties from a given file.
358      */

359     protected Properties JavaDoc loadProperties(String JavaDoc fileName) {
360         if (fileName == null) {
361             fileName = System.getProperty("user.dir") + "/jdori2.properties";
362         }
363         Properties JavaDoc props = new Properties JavaDoc();
364         InputStream JavaDoc propStream = null;
365         try {
366             propStream = new FileInputStream JavaDoc(fileName);
367         } catch (java.io.IOException JavaDoc ex) {
368             logger.log(BasicLevel.ERROR, "Could not open properties file \"" + fileName + "\"");
369             logger.log(
370                 BasicLevel.ERROR,
371                 "Please specify PMF properties file name"
372                     + " -PMFProperties argument (defaults to {user.dir}/jdori2.properties)");
373             System.exit(1);
374         }
375         try {
376             props.load(propStream);
377         } catch (java.io.IOException JavaDoc ex) {
378             logger.log(BasicLevel.ERROR, "Error loading properties file \"" + fileName + "\"", ex);
379             System.exit(1);
380         }
381         return props;
382     }
383
384     public void testDeletePersistentFailsIfInstanceManagedByAnotherPersistenceManager() {
385         logger.log(BasicLevel.INFO, "testDeletePersistentFailsIfInstanceManagedByAnotherPersistenceManager");
386         pmf = getPMF();
387         PersistenceManager pm1 = pmf.getPersistenceManager();
388         PersistenceManager pm2 = pmf.getPersistenceManager();
389         
390         
391         Assert.assertTrue("same persistence manager", !pm1.equals(pm2));
392
393         try {
394
395             pm1.currentTransaction().begin();
396             ba1 = new BasicA();
397             ba2 = new BasicA();
398             ba3 = new BasicA();
399             ba4 = new BasicA();
400             ba5 = new BasicA();
401
402             pm1.makePersistent(ba1);
403             pm1.makePersistent(ba2);
404             pm1.makePersistent(ba3);
405             pm1.makePersistent(ba4);
406             pm1.makePersistent(ba5);
407             pm = JDOHelper.getPersistenceManager(ba1);
408             logger.log(BasicLevel.DEBUG, "before commit pm="+pm);
409             pm1.currentTransaction().commit();
410             pm = JDOHelper.getPersistenceManager(ba1);
411             logger.log(BasicLevel.DEBUG, "after commit pm="+pm);
412             
413
414         } catch (Exception JavaDoc ex) {
415             logger.log(
416                 BasicLevel.ERROR,
417                 "Unexception caught in testDeletePersistentFailsIfInstanceManagedByAnotherPersistenceManager",
418                 ex);
419             pm1.close();
420             pm2.close();
421             fail(ex.getMessage());
422         }
423
424         /* test deletePersistent (Object pc) */
425         try {
426
427             pm2.currentTransaction().begin();
428             pm = JDOHelper.getPersistenceManager(ba1);
429             logger.log(BasicLevel.DEBUG, "pm1="+pm1 + " pm="+pm);
430             Assert.assertTrue("not same persistence manager 1", pm1.equals(pm));
431             pm2.deletePersistent(ba1);
432             logger.log(BasicLevel.DEBUG, "pm2="+pm2 + " pm="+pm);
433                         
434             throw new Exception JavaDoc("FAILED in deletePersistentPM2() - should not go that further");
435         } catch (Exception JavaDoc ex) {
436             if (ex instanceof JDOUserException) {
437                 ; // expected result
438
} else {
439                 logger.log(BasicLevel.ERROR, "Unexception caught in deletePersistentPM2", ex);
440                 
441             }
442             pm2.currentTransaction().rollback();
443         }
444         
445         /* test deletePersistentAll (Collection pcs) */
446         try {
447             pm2.currentTransaction().begin();
448
449             Collection JavaDoc col1 = new java.util.HashSet JavaDoc();
450             col1.add(ba2);
451             col1.add(ba3);
452
453             pm2.deletePersistentAll(col1);
454
455             throw new Exception JavaDoc("FAILED in deletePersistentAll1() - should not go that further");
456         } catch (Exception JavaDoc ex) {
457             if (ex instanceof JDOUserException) {
458                 ; // expected result
459
} else {
460                 logger.log(BasicLevel.ERROR, "Unexception caught in deletePersistentAll1", ex);
461             }
462             pm2.currentTransaction().rollback();
463         }
464         
465         /* test deletePersistentAll (Object[] o) */
466         try {
467
468             pm2.currentTransaction().begin();
469
470             Collection JavaDoc col1 = new java.util.HashSet JavaDoc();
471             col1.add(ba4);
472             col1.add(ba5);
473
474             Object JavaDoc[] obj1 = col1.toArray();
475
476             pm2.deletePersistentAll(obj1);
477
478             throw new Exception JavaDoc("FAILED in deletePersistentAll2() - should not go that further");
479         } catch (Exception JavaDoc ex) {
480             if (ex instanceof JDOUserException) {
481                 ; // expected result
482
} else {
483                 logger.log(BasicLevel.ERROR, "Unexception caught in deletePersistentAll2", ex);
484             }
485             pm2.currentTransaction().rollback();
486         }
487         
488
489         logger.log(BasicLevel.DEBUG, "passed in testDeletePersistentFailsIfInstanceManagedByAnotherPersistenceManager");
490
491         pm1.close();
492         pm2.close();
493
494     }
495
496
497     public void testOneInstanceOfObjectPerPersistenceManager() {
498         logger.log(BasicLevel.INFO, "testOneInstanceOfObjectPerPersistenceManager");
499         tx = getPM().currentTransaction();
500         //tx.setRetainValues(false);
501
tx.setRestoreValues(false);
502
503         tx.begin();
504         BasicA ba11 = new BasicA();
505         ba11.writeF1("ba11");
506         getPM().makePersistent(ba11);
507         tx.commit();
508
509         tx.begin();
510         Object JavaDoc p1Id = getPM().getObjectId(ba11);
511         BasicA ba11a = (BasicA) getPM().getObjectById(p1Id, true);
512         BasicA ba11c = findBasicA(getPM(), "ba11");
513         tx.commit();
514         closePM();
515
516         Assert.assertEquals("Assertion A5.4-2 failed; getObjectById results differ", ba11a, ba11);
517         Assert.assertEquals("Assertion A5.4-2 failed; query results differ", ba11c, ba11);
518         tx = getPM().currentTransaction();
519         tx.begin();
520         getPM().deletePersistent(ba11);
521         tx.commit();
522         closePM();
523
524         logger.log(BasicLevel.DEBUG, "passed in testOneInstanceOfObjectPerPersistenceManager");
525     }
526
527     public void testRefreshAllWithNoParameters() {
528         logger.log(BasicLevel.INFO, "testRefreshAllWithNoParameters");
529         tx = getPM().currentTransaction();
530         tx.begin();
531         BasicA ba11 = new BasicA();
532         ba11.writeF1("ba11");
533         ba11.writeF2(200);
534         getPM().makePersistent(ba11);
535         tx.commit();
536
537         tx.begin();
538         ba11.writeF1("xx");
539         //ba11.writeF2(800);
540

541         /*
542          * Collection col1 = new java.util.HashSet(); col1.add(ba11); try { getPM().refresh(ba11); } catch (Exception
543          * ex) { logger.log(BasicLevel.DEBUG, "passed in refresh(ba11)" + ex.getMessage()); }
544          *
545          * try { getPM().refresh(col1); } catch (Exception ex) { logger.log(BasicLevel.DEBUG, "passed in refresh(col1)" +
546          * ex.getMessage()); }
547          */

548         getPM().refreshAll();
549
550         Assert.assertEquals("Assertion A12.5.1-6 failed; values not refreshed", ba11.readF1(), "ba11");
551         Assert.assertEquals("Assertion A12.5.1-6 failed; values not refreshed", ba11.readF2(), 200);
552         tx.commit();
553         closePM();
554         logger.log(BasicLevel.DEBUG, "passed in testRefreshAllWithNoParameters");
555     }
556
557     public void testThreadSafe() {
558         logger.log(BasicLevel.INFO, "testThreadSafe");
559         try {
560
561             /**
562              * The implementation must synchronize instances of PersistenceCapable during state transitions that
563              * replace the StateManager. This is to avoid race conditions where the application attempts to make the
564              * same instance persistent in multiple PersistenceManagers.
565              */

566
567             b1 = new BasicA();
568             for (int i = 0; i < totalThreadCount; i++) {
569                 Thread JavaDoc t = new Thread JavaDoc(new PMThread(pmf, b1, this));
570                 t.setName("ThreadSafeID-" + i);
571                 t.start();
572             }
573
574             while (totalThreadCount != completedThreadCount) {
575                 try {
576                     Thread.sleep(100);
577                 } catch (InterruptedException JavaDoc ex) {
578                     logger.log(BasicLevel.INFO, "interrupted");
579                 }
580             }
581
582             //logger.log(BasicLevel.DEBUG, "succeeds=" + succeeds + " catchNumber=" + catchNumber);
583

584             /**
585              * Only the first thread may
586              */

587             Assert.assertTrue("Failed: succeeds = "+succeeds+ " catchNumber = "+catchNumber, ((succeeds == 1) && (catchNumber == totalThreadCount - 1)));
588
589         } catch (Exception JavaDoc ex) {
590             logger.log(BasicLevel.ERROR, "Unexception caught in testThreadSafe", ex);
591             fail(ex.getMessage());
592         }
593
594     }
595
596     protected synchronized void complete() {
597         completedThreadCount++;
598     }
599
600     protected synchronized void winning(BasicA pc) {
601         logger.log(BasicLevel.DEBUG, "*** ThreadID : " + Thread.currentThread().getName() + " succeeds");
602         succeeds++;
603     }
604
605     class PMThread implements Runnable JavaDoc {
606         private final PersistenceManager pm;
607         private final Object JavaDoc pc;
608         private final PersistenceManagerTck owner;
609
610         PMThread(PersistenceManagerFactory pmf, BasicA pc, PersistenceManagerTck owner) {
611
612             this.pm = pmf.getPersistenceManager(); // should return different pm
613
this.pc = pc;
614             this.owner = owner;
615         }
616
617         public void run() {
618             Transaction tx = null;
619             try {
620                 logger.log(BasicLevel.DEBUG, " Running thread: " + Thread.currentThread().getName());
621                 // the application attempts to make the same instance persistent in multiple PersistenceManagers.
622
tx = pm.currentTransaction();
623                 tx.begin();
624                 logger.log(BasicLevel.DEBUG, "before makePersistent pm="+pm);
625                 pm.makePersistent(pc);
626                 logger.log(BasicLevel.DEBUG, "after makePersistent pm="+JDOHelper.getPersistenceManager(pc));
627                 tx.commit();
628                 winning((BasicA) pc);
629                 complete();
630
631                 while (totalThreadCount != completedThreadCount) {
632                     try {
633                         Thread.sleep(100);
634                     } catch (InterruptedException JavaDoc ex) {
635                         logger.log(BasicLevel.INFO, "interrupted");
636                     }
637                 }
638             } catch (Exception JavaDoc ex) {
639                 catchNumber++;
640                 if (ex instanceof JDOUserException) {
641                     logger.log(
642                         BasicLevel.DEBUG,
643                         "JDOUserException caught in PMThread.run()" + Thread.currentThread().getName(),
644                         ex);
645                     ; // expected result
646
} else {
647                     logger.log(
648                         BasicLevel.ERROR,
649                         "Unexception caught in PMThread.run()" + Thread.currentThread().getName(),
650                         ex);
651                 }
652                 complete();
653             } finally {
654                 try {
655                     if (tx.isActive()) {
656                         tx.rollback();
657                     }
658                     pm.close();
659                 } catch (Exception JavaDoc fex) {
660                     logger.log(BasicLevel.ERROR, "Unexception caught in PMThread()", fex);
661                 }
662             }
663         } // run
664

665     } // class PMThread
666

667     public Object JavaDoc createBasicAInstance() {
668         try {
669             BasicA p1 = new BasicA();
670             pm = getPM();
671
672             tx = pm.currentTransaction();
673             tx.begin();
674             pm.makePersistent(p1);
675             Object JavaDoc oid = pm.getObjectId(p1);
676             tx.commit();
677             return oid;
678         } catch (Exception JavaDoc ex) {
679             logger.log(BasicLevel.ERROR, "Unexception caught in createPCPointInstance", ex);
680             return null;
681         }
682     }
683     
684     public void testGetObjectById() {
685         logger.log(BasicLevel.INFO, "testGetObjectById");
686         try {
687
688             Object JavaDoc oid = createBasicAInstance();
689
690             // nontransactional access
691
logger.log(BasicLevel.DEBUG, "nontransactional");
692             pm = null; // make sure to get a different pm so obj is not in cache
693
tx = getPM().currentTransaction();
694             tx.setNontransactionalRead(true);
695             Object JavaDoc obj = pm.getObjectById(oid, true);
696             int state = currentState(obj);
697             if (state != PERSISTENT_NONTRANSACTIONAL & state != HOLLOW) {
698                 //incrementErrorCount();
699
logger.log(
700                     BasicLevel.ERROR,
701                     "Expected persistent-nontransactional or hollow; got " + getStateOfInstance(obj));
702             }
703
704             // pessimistic transactional access
705
logger.log(BasicLevel.DEBUG, "pessimistic");
706             pm = null; // make sure to get a different pm so obj is not in cache
707
tx = getPM().currentTransaction();
708             tx.setOptimistic(false);
709             tx.begin();
710             obj = pm.getObjectById(oid, true);
711             state = currentState(obj);
712             tx.commit();
713             if (state != PERSISTENT_CLEAN) {
714                 //incrementErrorCount();
715
logger.log(BasicLevel.ERROR, "Expected persistent-clean; got " + getStateOfInstance(obj));
716             }
717
718             // optimistic transactional access
719
if (isOptimisticSupported()) {
720                 logger.log(BasicLevel.DEBUG, "optimistic");
721                 pm = null; // make sure to get a different pm so obj is not in cache
722
tx = getPM().currentTransaction();
723                 tx.setOptimistic(true);
724                 tx.begin();
725                 obj = pm.getObjectById(oid, true);
726                 state = currentState(obj);
727                 tx.commit();
728                 if (state != PERSISTENT_NONTRANSACTIONAL & state != HOLLOW) {
729                     //incrementErrorCount();
730
logger.log(
731                         BasicLevel.ERROR,
732                         "Expected persistent-nontransactional; got " + state + getStateOfInstance(obj));
733                 }
734             } else {
735                 logger.log(BasicLevel.INFO, "optimistic test skipped");
736             }
737         } catch (Exception JavaDoc ex) {
738             logger.log(BasicLevel.ERROR, "Unexception caught in testGetObjectById", ex);
739             fail(ex.getMessage());
740         }
741     }
742     
743     public void testRefreshSideEffects() {
744         logger.log(BasicLevel.INFO, "testRefreshSideEffects");
745
746         pm10 = getPMF().getPersistenceManager();
747         pm20 = getPMF().getPersistenceManager();
748         
749         try {
750
751             RefreshThreadT1 thread1 = new RefreshThreadT1(logger);
752             Thread JavaDoc T1 = new Thread JavaDoc(thread1);
753             RefreshThreadT2 thread2 = new RefreshThreadT2(logger);
754             Thread JavaDoc T2 = new Thread JavaDoc(thread2);
755
756             T1.start();
757             T2.start();
758
759             T1.join();
760             T2.join();
761
762             logger.log(BasicLevel.DEBUG, "thread1.state="+thread1.state);
763             logger.log(BasicLevel.DEBUG, "thread2.state="+thread2.state);
764             if (thread1.state != 1 || thread2.state != 1)
765                 logger.log(BasicLevel.ERROR, "RefreshSideEffects: thread1.state="+thread1.state+" thread2.state="+thread2.state);
766                 //incrementErrorCount();
767

768         } catch (Exception JavaDoc ex) {
769             logger.log(BasicLevel.ERROR, "Unexception caught in testRefreshSideEffects", ex);
770             fail(ex.getMessage());
771         }
772         
773     }
774     
775 }
776
777 class RefreshThreadT1 implements Runnable JavaDoc {
778     
779     volatile int state=0;
780     static final int DELAY=100;
781     static boolean commitDone = false;
782     static boolean doneFlag = false;
783     Transaction tx1 = null;
784     Logger logger = null;
785     static BasicA n1 = new BasicA();
786
787     RefreshThreadT1(Logger logger) {
788         tx1 = PersistenceManagerTck.pm10.currentTransaction();
789         tx1.setOptimistic(true);
790         this.logger = logger;
791         
792     }
793
794     public static void initVariables() {
795         commitDone = false;
796         doneFlag = false;
797     }
798
799     synchronized public void run() {
800         try {
801             state=1;
802             tx1.begin();
803             n1.writeF2(500);
804             PersistenceManagerTck.pm10.makePersistent(n1);
805             PersistenceManagerTck.pm10.refresh(n1);
806
807             while (! RefreshThreadT2.doneFlag) {
808                 Thread.sleep(DELAY);
809             }
810
811             tx1.commit();
812             commitDone = true;
813
814
815         } catch (Exception JavaDoc ex) {
816             //out.println (" Exception in T1 run ");
817
//ex.printStackTrace(out);
818
state = 13;
819             logger.log(BasicLevel.ERROR, "Unexception caught in RefreshThreadT1", ex);
820         }
821     }
822
823 }
824
825 class RefreshThreadT2 implements Runnable JavaDoc {
826
827
828     volatile int state=0;
829     static final int DELAY=100;
830     static boolean commitDone = false;
831     static boolean doneFlag = false;
832     Transaction tx2 = null;
833     Logger logger = null;
834     static BasicA p1 = new BasicA();
835
836     RefreshThreadT2(Logger logger) {
837         tx2 = PersistenceManagerTck.pm20.currentTransaction();
838         tx2.setOptimistic(true);
839         this.logger = logger;
840     }
841
842     public static void initVariables() {
843         commitDone = false;
844         doneFlag = false;
845     }
846
847     /* test refresh() */
848     synchronized public void run() {
849         try {
850             state=1;
851             tx2.begin();
852             p1.writeF2(201);
853             PersistenceManagerTck.pm20.makePersistent(p1);
854             PersistenceManagerTck.pm20.refresh(p1);
855             doneFlag = true;
856
857             while (! RefreshThreadT1.commitDone) {
858                 Thread.sleep(DELAY);
859
860             }
861             tx2.commit();
862             commitDone = true;
863
864
865         } catch (Exception JavaDoc ex) {
866             state=10;
867             logger.log(BasicLevel.ERROR, "Unexception caught in RefreshThreadT2", ex);
868         }
869     }
870
871 }
872
873
874
Popular Tags