KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > fos > TestTxOps


1 /**
2  * Copyright (C) 2001-2002 France Telecom R&D - INRIA
3  */

4
5 package org.objectweb.perseus.fos;
6
7 import junit.framework.Test;
8 import junit.framework.TestCase;
9 import junit.framework.TestSuite;
10 import junit.textui.TestRunner;
11 import org.objectweb.perseus.LogBoot;
12 import org.objectweb.perseus.fos.api.FosException;
13 import org.objectweb.perseus.fos.api.FosManager;
14 import org.objectweb.perseus.fos.api.FosStructure;
15 import org.objectweb.perseus.fos.api.FosTransaction;
16 import org.objectweb.perseus.fos.api.FosAccess;
17 import org.objectweb.perseus.fos.lib.FosTxContextFactory;
18
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 /**
25  * Tests standard transactions that correctly executes (correct begin/commit
26  * sequences).
27  */

28 public class TestTxOps extends TestCase {
29     private static final int INITIALVALUE = 10;
30     private static final int NEWVALUE = 20;
31     private static final int NULLVALUE = 0;
32     FosAccess tx;
33     FosManager fm;
34     static final String JavaDoc POBJDIR = "c1";
35     String JavaDoc[] pobjId = {
36         "obj1", "obj2", "obj3", "obj4", "obj5"
37     };
38     String JavaDoc dbDir;
39     IntPObj[] pobjVal = {
40         new IntPObj(),
41         new IntPObj(),
42         new IntPObj(),
43         new IntPObj(),
44         new IntPObj()
45     };
46
47     // IMPLEMENTATION OF METHODS OF THE TestCase CLASS
48

49     /**
50      * Initialisation sequence executed before each test case.
51      */

52     protected void setUp() {
53         try {
54             fm = new FosTxContextFactory();
55             fm.setDbDir(dbDir);
56             fm.setMonologFactory(LogBoot.getLoggerFactory());
57             fm.initialize();
58             fm.start();
59         } catch (FosException fe) {
60             System.out.println("Error during setUp - 1");
61             if (fe.getNestedException() != null)
62                 fe.getNestedException().printStackTrace();
63             fe.printStackTrace();
64             System.exit(0);
65         } catch (Exception JavaDoc e) {
66             System.out.println("Error during setUp - 2");
67             e.printStackTrace();
68             System.exit(0);
69         }
70     }
71
72     /**
73      * Termination sequence executed after each test case.
74      */

75     protected void tearDown() {
76         try {
77             fm.stop();
78         } catch (FosException fe) {
79             System.out.println("Error during tearDown");
80             if (fe.getNestedException() != null)
81                 fe.getNestedException().printStackTrace();
82             fe.printStackTrace();
83         }
84     }
85
86     // Tests transaction operations (no data manipulation).
87

88     /**
89      * Tests a simple creation and releasing of a transaction that
90      * is correct.
91      */

92     public void testCreateRelease() {
93         try {
94             FosTransaction ft = fm.createTxContext();
95             fm.releaseTxContext(ft);
96         } catch (FosException fe) {
97             fail();
98         }
99     }
100
101     /**
102      * Tests a simple creation and releasing of a transaction along
103      * with a begin/commit sequence.
104      */

105     public void testCRBeginCommit() {
106         try {
107             FosTransaction ft = fm.createTxContext();
108             ft.begin();
109             ft.commit();
110             fm.releaseTxContext(ft);
111         } catch (FosException fe) {
112             fail();
113         }
114     }
115
116     /**
117      * Tests a simple creation and releasing of a transaction along
118      * with a begin/prepare/commit sequence.
119      */

120     public void testCRBeginPrepareCommit() {
121         try {
122             FosTransaction ft = fm.createTxContext();
123             ft.begin();
124             ft.prepare();
125             ft.commit();
126             fm.releaseTxContext(ft);
127         } catch (FosException fe) {
128             fail();
129         }
130     }
131
132     /**
133      * Tests a simple creation and releasing of a transaction along
134      * with a begin/prepare/rollback sequence.
135      */

136     public void testCRBeginPrepareRollback() {
137         try {
138             FosTransaction ft = fm.createTxContext();
139             ft.begin();
140             ft.prepare();
141             ft.rollback();
142             fm.releaseTxContext(ft);
143         } catch (FosException fe) {
144             fail();
145         }
146     }
147
148     /**
149      * Tests a simple creation and releasing of a transaction along
150      * with a begin/rollback sequence.
151      */

152     public void testCRBeginRollback() {
153         try {
154             FosTransaction ft = fm.createTxContext();
155             ft.begin();
156             ft.rollback();
157             fm.releaseTxContext(ft);
158         } catch (FosException fe) {
159             fail();
160         }
161     }
162
163     /**
164      * Tests a simple creation and releasing of a transaction along
165      * with a simple begin: rollback forced.
166      */

167     public void testCRBeginAlone() {
168         try {
169             FosTransaction ft = fm.createTxContext();
170             ft.begin();
171             fm.releaseTxContext(ft);
172         } catch (FosException fe) {
173             fail();
174         }
175     }
176
177     /**
178      * Tests a simple creation and releasing of a transaction along
179      * with a simple begin/prepare sequence: commit forced.
180      */

181     public void testCRBeginPrepare() {
182         try {
183             FosTransaction ft = fm.createTxContext();
184             ft.begin();
185             ft.prepare();
186             fm.releaseTxContext(ft);
187         } catch (FosException fe) {
188             fail();
189         }
190     }
191
192     // Tests FosAccess operations (data manipulations).
193

194     /**
195      * Tests a simple exist test where the object does not exist.
196      */

197     public void testExistNoObj() {
198         try {
199             FosTransaction ft = fm.createTxContext();
200             ft.begin();
201             boolean ex = ft.exist(POBJDIR, pobjId[0]);
202             ft.commit();
203             fm.releaseTxContext(ft);
204             if (ex)
205                 fail();
206         } catch (FosException fe) {
207             if (fe.getNestedException() != null)
208                 fe.getNestedException().printStackTrace();
209             fe.printStackTrace();
210             fail();
211         }
212     }
213
214     /**
215      * Tests commit of a simple creation and exist on an existing object.
216      */

217     public void testCommitCreation() {
218         try {
219             FosTransaction ft = fm.createTxContext();
220             ft.begin();
221             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
222             ft.commit();
223             ft.begin();
224             boolean ex = ft.exist(POBJDIR, pobjId[0]);
225             ft.commit();
226             fm.releaseTxContext(ft);
227             if (!ex)
228                 fail("Object should exist: creation committed!");
229         } catch (FosException fe) {
230             if (fe.getNestedException() != null)
231                 fe.getNestedException().printStackTrace();
232             fe.printStackTrace();
233             fail();
234         }
235     }
236
237     /**
238      * Tests rollback of a simple creation.
239      */

240     public void testRollbackCreation() {
241         try {
242             FosTransaction ft = fm.createTxContext();
243             ft.begin();
244             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
245             ft.rollback();
246             ft.begin();
247             boolean ex = ft.exist(POBJDIR, pobjId[0]);
248             ft.commit();
249             fm.releaseTxContext(ft);
250             if (ex)
251                 fail("Object should not exist: creation rolled back!");
252         } catch (FosException fe) {
253             if (fe.getNestedException() != null)
254                 fe.getNestedException().printStackTrace();
255             fe.printStackTrace();
256             fail();
257         }
258     }
259
260     /**
261      * Tests rollback of a simple read.
262      */

263     public void testRead() {
264         try {
265             FosTransaction ft = fm.createTxContext();
266             ft.begin();
267             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
268             ft.commit();
269             pobjVal[0].setValue(NULLVALUE);
270             ft.begin();
271             ft.read(POBJDIR, pobjId[0], pobjVal[0], null);
272             ft.commit();
273             fm.releaseTxContext(ft);
274             if (pobjVal[0].getValue() != INITIALVALUE)
275                 fail("Object read: wrong value!");
276         } catch (FosException fe) {
277             if (fe.getNestedException() != null)
278                 fe.getNestedException().printStackTrace();
279             fe.printStackTrace();
280             fail();
281         }
282     }
283
284     /**
285      * Tests commit of a simple deletion.
286      */

287     public void testCommitDeletion() {
288         try {
289             FosTransaction ft = fm.createTxContext();
290             ft.begin();
291             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
292             ft.commit();
293             ft.begin();
294             ft.delete(POBJDIR, pobjId[0]);
295             ft.commit();
296             ft.begin();
297             boolean ex = ft.exist(POBJDIR, pobjId[0]);
298             ft.commit();
299             fm.releaseTxContext(ft);
300             if (ex)
301                 fail("Object should not exist: deletion committed!");
302         } catch (FosException fe) {
303             if (fe.getNestedException() != null)
304                 fe.getNestedException().printStackTrace();
305             fe.printStackTrace();
306             fail();
307         }
308     }
309
310     /**
311      * Tests rollback of a simple deletion.
312      */

313     public void testRollbackDeletion() {
314         try {
315             FosTransaction ft = fm.createTxContext();
316             ft.begin();
317             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
318             ft.commit();
319             ft.begin();
320             ft.delete(POBJDIR, pobjId[0]);
321             ft.rollback();
322             ft.begin();
323             boolean ex = ft.exist(POBJDIR, pobjId[0]);
324             ft.commit();
325             fm.releaseTxContext(ft);
326             if (!ex)
327                 fail("Object should exist: deletion rolled back!");
328         } catch (FosException fe) {
329             if (fe.getNestedException() != null)
330                 fe.getNestedException().printStackTrace();
331             fe.printStackTrace();
332             fail();
333         }
334     }
335
336     /**
337      * Tests commit of a simple modification.
338      */

339     public void testCommitModification() {
340         try {
341             FosTransaction ft = fm.createTxContext();
342             ft.begin();
343             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
344             ft.commit();
345             ft.begin();
346             pobjVal[0].setValue(NEWVALUE);
347             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
348             ft.commit();
349             ft.begin();
350             ft.read(POBJDIR, pobjId[0], pobjVal[0], null);
351             ft.commit();
352             fm.releaseTxContext(ft);
353             if (pobjVal[0].getValue() != NEWVALUE)
354                 fail("Modification committed: wrong value!");
355         } catch (FosException fe) {
356             if (fe.getNestedException() != null)
357                 fe.getNestedException().printStackTrace();
358             fe.printStackTrace();
359             fail();
360         }
361     }
362
363     /**
364      * Tests rollback of a simple modification.
365      */

366     public void testRollbackModification() {
367         try {
368             FosTransaction ft = fm.createTxContext();
369             ft.begin();
370             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
371             ft.commit();
372             ft.begin();
373             pobjVal[0].setValue(NEWVALUE);
374             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
375             ft.rollback();
376             ft.begin();
377             ft.read(POBJDIR, pobjId[0], pobjVal[0], null);
378             ft.commit();
379             fm.releaseTxContext(ft);
380             if (pobjVal[0].getValue() != INITIALVALUE)
381                 fail("Modification rolled back: wrong value!");
382         } catch (FosException fe) {
383             if (fe.getNestedException() != null)
384                 fe.getNestedException().printStackTrace();
385             fe.printStackTrace();
386             fail();
387         }
388     }
389
390     /**
391      * Tests commit multiple creations.
392      */

393     public void testCommitCreations() {
394         try {
395             FosTransaction ft = fm.createTxContext();
396             ft.begin();
397             for (int i = 0; i < pobjId.length; i++)
398                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
399             ft.commit();
400             ft.begin();
401             boolean ex = true;
402             for (int i = 0; i < pobjId.length; i++)
403                 ex &= ft.exist(POBJDIR, pobjId[i]);
404             ft.commit();
405             fm.releaseTxContext(ft);
406             if (!ex)
407                 fail("Objects should exist: creations committed!");
408         } catch (FosException fe) {
409             if (fe.getNestedException() != null)
410                 fe.getNestedException().printStackTrace();
411             fe.printStackTrace();
412             fail();
413         }
414     }
415
416     /**
417      * Tests rollback of multiple creations.
418      */

419     public void testRollbackCreations() {
420         try {
421             FosTransaction ft = fm.createTxContext();
422             ft.begin();
423             for (int i = 0; i < pobjId.length; i++)
424                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
425             ft.rollback();
426             ft.begin();
427             boolean ex = false;
428             for (int i = 0; i < pobjId.length; i++)
429                 ex |= ft.exist(POBJDIR, pobjId[i]);
430             ft.commit();
431             fm.releaseTxContext(ft);
432             if (ex)
433                 fail("Objects should not exist: creations rolled back!");
434         } catch (FosException fe) {
435             if (fe.getNestedException() != null)
436                 fe.getNestedException().printStackTrace();
437             fe.printStackTrace();
438             fail();
439         }
440     }
441
442     /**
443      * Tests multiple reads.
444      */

445     public void testReads() {
446         try {
447             FosTransaction ft = fm.createTxContext();
448             ft.begin();
449             for (int i = 0; i < pobjId.length; i++)
450                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
451             ft.commit();
452             for (int i = 0; i < pobjId.length; i++)
453                 pobjVal[i].setValue(NULLVALUE);
454             ft.begin();
455             for (int i = 0; i < pobjId.length; i++)
456                 ft.read(POBJDIR, pobjId[i], pobjVal[i], null);
457             ft.commit();
458             fm.releaseTxContext(ft);
459             for (int i = 0; i < pobjId.length; i++)
460                 if (pobjVal[i].getValue() != INITIALVALUE) {
461                     fail("Objects read: wrong value!");
462                     break;
463                 }
464         } catch (FosException fe) {
465             if (fe.getNestedException() != null)
466                 fe.getNestedException().printStackTrace();
467             fe.printStackTrace();
468             fail();
469         }
470     }
471
472     /**
473      * Tests commit of multiple deletions.
474      */

475     public void testCommitDeletions() {
476         try {
477             FosTransaction ft = fm.createTxContext();
478             ft.begin();
479             for (int i = 0; i < pobjId.length; i++)
480                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
481             ft.commit();
482             ft.begin();
483             for (int i = 0; i < pobjId.length; i++)
484                 ft.delete(POBJDIR, pobjId[i]);
485             ft.commit();
486             ft.begin();
487             boolean nex = true;
488             for (int i = 0; i < pobjId.length; i++)
489                 nex &= !ft.exist(POBJDIR, pobjId[i]);
490             ft.commit();
491             fm.releaseTxContext(ft);
492             if (!nex)
493                 fail("Objects should not exist: deletions committed!");
494         } catch (FosException fe) {
495             if (fe.getNestedException() != null)
496                 fe.getNestedException().printStackTrace();
497             fe.printStackTrace();
498             fail();
499         }
500     }
501
502     /**
503      * Tests commit of multiple deletions.
504      */

505     public void testRollbackDeletions() {
506         try {
507             FosTransaction ft = fm.createTxContext();
508             ft.begin();
509             for (int i = 0; i < pobjId.length; i++)
510                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
511             ft.commit();
512             ft.begin();
513             for (int i = 0; i < pobjId.length; i++)
514                 ft.delete(POBJDIR, pobjId[i]);
515             ft.rollback();
516             ft.begin();
517             boolean ex = true;
518             for (int i = 0; i < pobjId.length; i++)
519                 ex &= ft.exist(POBJDIR, pobjId[i]);
520             ft.commit();
521             fm.releaseTxContext(ft);
522             if (!ex)
523                 fail("Objects should exist: deletions rolled back!");
524         } catch (FosException fe) {
525             if (fe.getNestedException() != null)
526                 fe.getNestedException().printStackTrace();
527             fe.printStackTrace();
528             fail();
529         }
530     }
531
532     /**
533      * Tests commit of multiple modifications.
534      */

535     public void testCommitModifications() {
536         try {
537             FosTransaction ft = fm.createTxContext();
538             ft.begin();
539             for (int i = 0; i < pobjId.length; i++)
540                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
541             ft.commit();
542             ft.begin();
543             for (int i = 0; i < pobjId.length; i++) {
544                 pobjVal[i].setValue(NEWVALUE);
545                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
546             }
547             ft.commit();
548             for (int i = 0; i < pobjId.length; i++)
549                 pobjVal[i].setValue(NULLVALUE);
550             ft.begin();
551             for (int i = 0; i < pobjId.length; i++)
552                 ft.read(POBJDIR, pobjId[i], pobjVal[i], null);
553             ft.commit();
554             fm.releaseTxContext(ft);
555             for (int i = 0; i < pobjId.length; i++)
556                 if (pobjVal[i].getValue() != NEWVALUE) {
557                     fail("Modifications committed: wrong value!");
558                     break;
559                 }
560         } catch (FosException fe) {
561             if (fe.getNestedException() != null)
562                 fe.getNestedException().printStackTrace();
563             fe.printStackTrace();
564             fail();
565         }
566     }
567
568     /**
569      * Tests rollback of multiple modifications.
570      */

571     public void testRollbackModifications() {
572         try {
573             FosTransaction ft = fm.createTxContext();
574             ft.begin();
575             for (int i = 0; i < pobjId.length; i++)
576                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
577             ft.commit();
578             ft.begin();
579             for (int i = 0; i < pobjId.length; i++) {
580                 pobjVal[i].setValue(NEWVALUE);
581                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
582             }
583             ft.rollback();
584             for (int i = 0; i < pobjId.length; i++)
585                 pobjVal[i].setValue(NULLVALUE);
586             ft.begin();
587             for (int i = 0; i < pobjId.length; i++)
588                 ft.read(POBJDIR, pobjId[i], pobjVal[i], null);
589             ft.commit();
590             fm.releaseTxContext(ft);
591             for (int i = 0; i < pobjId.length; i++)
592                 if (pobjVal[i].getValue() != INITIALVALUE)
593                     fail("Modification rolled back: wrong value!");
594         } catch (FosException fe) {
595             if (fe.getNestedException() != null)
596                 fe.getNestedException().printStackTrace();
597             fe.printStackTrace();
598             fail();
599         }
600     }
601
602     /**
603      * Tests the scan of the extension of a directory with no object.
604      */

605     public void testScanNoObj() {
606         try {
607             FosTransaction ft = fm.createTxContext();
608             ft.begin();
609             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
610             ft.commit();
611             ft.begin();
612             ft.delete(POBJDIR, pobjId[0]);
613             ft.commit();
614             ft.begin();
615             Iterator JavaDoc it = ft.scan(POBJDIR);
616             ft.commit();
617             fm.releaseTxContext(ft);
618             if (it.hasNext())
619                 fail("Extension is not composed as required.");
620         } catch (FosException fe) {
621             if (fe.getNestedException() != null)
622                 fe.getNestedException().printStackTrace();
623             fe.printStackTrace();
624             fail();
625         }
626     }
627
628     /**
629      * Tests the scan of the extension of a directory with many objects.
630      */

631     public void testScanManyObj() {
632         try {
633             FosTransaction ft = fm.createTxContext();
634             ft.begin();
635             for (int i = 0; i < pobjId.length; i++)
636                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
637             ft.commit();
638             ft.begin();
639             Iterator JavaDoc it = ft.scan(POBJDIR);
640             ft.commit();
641             fm.releaseTxContext(ft);
642             boolean ex = true;
643             int no = 0;
644             while (it.hasNext()) {
645                 String JavaDoc pobj = (String JavaDoc) it.next();
646                 boolean found = false;
647                 for (int i = 0; i < pobjId.length; i++)
648                     if (pobjId[i].equals(pobj)) {
649                         found = true;
650                         no++;
651                     }
652                 ex &= found;
653             }
654             if ((!ex) || (no != pobjId.length))
655                 fail("Extension is not composed as required.");
656         } catch (FosException fe) {
657             if (fe.getNestedException() != null)
658                 fe.getNestedException().printStackTrace();
659             fe.printStackTrace();
660             fail();
661         }
662     }
663
664     /**
665      * Tests if the TX context correctly pass through when the FosStructure is
666      * activated.
667      */

668     public void testConnPassThrough() {
669         try {
670             FosTransaction ft = fm.createTxContext();
671             ft.begin();
672             tx = null;
673             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
674             if (tx != ft)
675                 fail();
676             ft.commit();
677             ft.begin();
678             tx = null;
679             ft.read(POBJDIR, pobjId[0], pobjVal[0], null);
680             if (tx != ft)
681                 fail();
682             ft.commit();
683             fm.releaseTxContext(ft);
684         } catch (FosException fe) {
685             if (fe.getNestedException() != null)
686                 fe.getNestedException().printStackTrace();
687             fe.printStackTrace();
688             fail();
689         }
690     }
691
692     // For managing the TestCase as a standalone Test Suite.
693

694     /**
695      * Constructs the suite containing all test cases defined within
696      * TestTxOps.
697      */

698     public static Test suite() {
699         return new TestSuite(TestTxOps.class);
700     }
701
702     /**
703      * For running the TestTxOps suite standalone.
704      */

705     public static void main(String JavaDoc args[]) {
706         TestRunner.run(suite());
707     }
708
709     /**
710      * Constructs a TestTxOps.
711      */

712     public TestTxOps(String JavaDoc tn) throws Exception JavaDoc {
713         super(tn);
714         Properties JavaDoc prop = new Properties JavaDoc();
715         prop.load(ClassLoader.getSystemResourceAsStream("owfos.properties"));
716         dbDir = prop.getProperty("dbdir", null);
717         if (dbDir == null) {
718             throw new Exception JavaDoc(
719                 "Cannot find the 'dbdir' property");
720         }
721     }
722
723     /**
724      * The class that implements the I/O for a serialized "int" Object File.
725      */

726     class IntPObj implements FosStructure {
727         /**
728          * The value of the integer persistent object.
729          */

730         private int value = INITIALVALUE;
731
732         /**
733          * Reads the content of a persistent object from the file represented by
734          * the given ObjectInputStream.
735          * @param ois The ObjectInputStream representing the file from which to
736          * read.
737          * @param conn The connection used to access the File Object Store.
738          */

739         public void readFile(ObjectInputStream JavaDoc ois, FosAccess conn, Object JavaDoc ctxt) throws Exception JavaDoc {
740             value = ois.readInt();
741             tx = conn;
742         }
743
744         /**
745          * Writes the content of a persistent object to the file represented by
746          * the given ObjectOutputStream.
747          * @param oos The ObjectOutputStream representing the file to which to
748          * write.
749          * @param conn The connection used to access the File Object Store.
750          */

751         public void writeFile(ObjectOutputStream JavaDoc oos, FosAccess conn, Object JavaDoc ctxt) throws Exception JavaDoc {
752             oos.writeInt(value);
753             tx = conn;
754         }
755
756         /**
757          * Returns the value of this integer persistent object.
758          */

759         int getValue() {
760             return value;
761         }
762
763         /**
764          * Assigns the value of this integer persistent object.
765          */

766         void setValue(int val) {
767             value = val;
768         }
769     }
770 }
Popular Tags