KickJava   Java API By Example, From Geeks To Geeks.

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


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.FosAccess;
13 import org.objectweb.perseus.fos.api.FosException;
14 import org.objectweb.perseus.fos.api.FosManager;
15 import org.objectweb.perseus.fos.api.FosStructure;
16 import org.objectweb.perseus.fos.api.FosTransaction;
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.Properties JavaDoc;
22
23 /**
24  * Tests standard transactions that correctly executes (correct begin/commit
25  * sequences).
26  */

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

47     /**
48      * Initialisation sequence executed before each test case.
49      */

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

72     protected void tearDown() {
73     }
74
75     // Tests transaction operations (no data manipulation).
76

77     /**
78      * Tests start of an empty FOS repository.
79      */

80     public void testRecoverInitialized() {
81         try {
82             fm = new FosTxContextFactory();
83             fm.setDbDir(dbDir);
84
85             fm.setMonologFactory(LogBoot.getLoggerFactory());
86             fm.start();
87             fm = new FosTxContextFactory();
88             fm.setDbDir(dbDir);
89
90             fm.setMonologFactory(LogBoot.getLoggerFactory());
91             fm.start();
92             fm.stop();
93         } catch (FosException fe) {
94             fe.printStackTrace();
95             fail("FOS error.");
96         } catch (Exception JavaDoc e) {
97             e.printStackTrace();
98             fail("Log configuration error.");
99         }
100     }
101
102     /**
103      * Tests start of a FOS repository with a committed creation.
104      */

105     public void testRecoverCommitCreate() {
106         try {
107             fm = new FosTxContextFactory();
108             fm.setDbDir(dbDir);
109
110             fm.setMonologFactory(LogBoot.getLoggerFactory());
111             fm.start();
112             FosTransaction ft = fm.createTxContext();
113             ft.begin();
114             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
115             ft.commit();
116             fm = new FosTxContextFactory();
117             fm.setDbDir(dbDir);
118
119             fm.setMonologFactory(LogBoot.getLoggerFactory());
120             fm.start();
121             ft.begin();
122             boolean ex = ft.exist(POBJDIR, pobjId[0]);
123             ft.commit();
124             fm.stop();
125             if (!ex)
126                 fail();
127         } catch (FosException fe) {
128             fe.printStackTrace();
129             fail("FOS error.");
130         } catch (Exception JavaDoc e) {
131             e.printStackTrace();
132             fail("Log configuration error.");
133         }
134     }
135
136     /**
137      * Tests start of a FOS repository with a prepared creation.
138      */

139     public void testRecoverPrepareCreate() {
140         try {
141             fm = new FosTxContextFactory();
142             fm.setDbDir(dbDir);
143
144             fm.setMonologFactory(LogBoot.getLoggerFactory());
145             fm.start();
146             FosTransaction ft = fm.createTxContext();
147             ft.begin();
148             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
149             ft.prepare();
150             fm = new FosTxContextFactory();
151             fm.setDbDir(dbDir);
152
153             fm.setMonologFactory(LogBoot.getLoggerFactory());
154             fm.start();
155             ft.begin();
156             boolean ex = ft.exist(POBJDIR, pobjId[0]);
157             ft.commit();
158             fm.stop();
159             if (!ex)
160                 fail();
161         } catch (FosException fe) {
162             fe.printStackTrace();
163             fail("FOS error.");
164         } catch (Exception JavaDoc e) {
165             e.printStackTrace();
166             fail("Log configuration error.");
167         }
168     }
169
170     /**
171      * Tests start of a FOS repository with a rolled back creation.
172      */

173     public void testRecoverRollbackCreate() {
174         try {
175             fm = new FosTxContextFactory();
176             fm.setDbDir(dbDir);
177
178             fm.setMonologFactory(LogBoot.getLoggerFactory());
179             fm.start();
180             FosTransaction ft = fm.createTxContext();
181             ft.begin();
182             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
183             ft.rollback();
184             fm = new FosTxContextFactory();
185             fm.setDbDir(dbDir);
186
187             fm.setMonologFactory(LogBoot.getLoggerFactory());
188             fm.start();
189             ft.begin();
190             boolean ex = ft.exist(POBJDIR, pobjId[0]);
191             ft.commit();
192             fm.stop();
193             if (ex)
194                 fail();
195         } catch (FosException fe) {
196             fe.printStackTrace();
197             fail("FOS error.");
198         } catch (Exception JavaDoc e) {
199             e.printStackTrace();
200             fail("Log configuration error.");
201         }
202     }
203
204     /**
205      * Tests start of a FOS repository with a uncommitted creation.
206      */

207     public void testRecoverCreate() {
208         try {
209             fm = new FosTxContextFactory();
210             fm.setDbDir(dbDir);
211
212             fm.setMonologFactory(LogBoot.getLoggerFactory());
213             fm.start();
214             FosTransaction ft = fm.createTxContext();
215             ft.begin();
216             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
217             fm = new FosTxContextFactory();
218             fm.setDbDir(dbDir);
219
220             fm.setMonologFactory(LogBoot.getLoggerFactory());
221             fm.start();
222             ft.begin();
223             boolean ex = ft.exist(POBJDIR, pobjId[0]);
224             ft.commit();
225             fm.stop();
226             if (ex)
227                 fail();
228         } catch (FosException fe) {
229             fe.printStackTrace();
230             fail("FOS error.");
231         } catch (Exception JavaDoc e) {
232             e.printStackTrace();
233             fail("Log configuration error.");
234         }
235     }
236
237     /**
238      * Tests start of a FOS repository with a committed deletion.
239      */

240     public void testRecoverCommitDelete() {
241         try {
242             fm = new FosTxContextFactory();
243             fm.setDbDir(dbDir);
244
245             fm.setMonologFactory(LogBoot.getLoggerFactory());
246             fm.start();
247             FosTransaction ft = fm.createTxContext();
248             ft.begin();
249             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
250             ft.commit();
251             ft.begin();
252             ft.delete(POBJDIR, pobjId[0]);
253             ft.commit();
254             fm = new FosTxContextFactory();
255             fm.setDbDir(dbDir);
256
257             fm.setMonologFactory(LogBoot.getLoggerFactory());
258             fm.start();
259             ft.begin();
260             boolean ex = ft.exist(POBJDIR, pobjId[0]);
261             ft.commit();
262             fm.stop();
263             if (ex)
264                 fail();
265         } catch (FosException fe) {
266             fe.printStackTrace();
267             fail("FOS error.");
268         } catch (Exception JavaDoc e) {
269             e.printStackTrace();
270             fail("Log configuration error.");
271         }
272     }
273
274     /**
275      * Tests start of a FOS repository with a prepared deletion.
276      */

277     public void testRecoverPrepareDelete() {
278         try {
279             fm = new FosTxContextFactory();
280             fm.setDbDir(dbDir);
281
282             fm.setMonologFactory(LogBoot.getLoggerFactory());
283             fm.start();
284             FosTransaction ft = fm.createTxContext();
285             ft.begin();
286             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
287             ft.commit();
288             ft.begin();
289             ft.delete(POBJDIR, pobjId[0]);
290             ft.prepare();
291             fm = new FosTxContextFactory();
292             fm.setDbDir(dbDir);
293
294             fm.setMonologFactory(LogBoot.getLoggerFactory());
295             fm.start();
296             ft.begin();
297             boolean ex = ft.exist(POBJDIR, pobjId[0]);
298             ft.commit();
299             fm.stop();
300             if (ex)
301                 fail();
302         } catch (FosException fe) {
303             fe.printStackTrace();
304             fail("FOS error.");
305         } catch (Exception JavaDoc e) {
306             e.printStackTrace();
307             fail("Log configuration error.");
308         }
309     }
310
311     /**
312      * Tests start of a FOS repository with a rolled back deletion.
313      */

314     public void testRecoverRollbackDelete() {
315         try {
316             fm = new FosTxContextFactory();
317             fm.setDbDir(dbDir);
318
319             fm.setMonologFactory(LogBoot.getLoggerFactory());
320             fm.start();
321             FosTransaction ft = fm.createTxContext();
322             ft.begin();
323             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
324             ft.commit();
325             ft.begin();
326             ft.delete(POBJDIR, pobjId[0]);
327             ft.rollback();
328             fm = new FosTxContextFactory();
329             fm.setDbDir(dbDir);
330
331             fm.setMonologFactory(LogBoot.getLoggerFactory());
332             fm.start();
333             ft.begin();
334             boolean ex = ft.exist(POBJDIR, pobjId[0]);
335             ft.commit();
336             fm.stop();
337             if (!ex)
338                 fail();
339         } catch (FosException fe) {
340             fe.printStackTrace();
341             fail("FOS error.");
342         } catch (Exception JavaDoc e) {
343             e.printStackTrace();
344             fail("Log configuration error.");
345         }
346     }
347
348     /**
349      * Tests start of a FOS repository with a uncommitted deletion.
350      */

351     public void testRecoverDelete() {
352         try {
353             fm = new FosTxContextFactory();
354             fm.setDbDir(dbDir);
355
356             fm.setMonologFactory(LogBoot.getLoggerFactory());
357             fm.start();
358             FosTransaction ft = fm.createTxContext();
359             ft.begin();
360             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
361             ft.commit();
362             ft.begin();
363             ft.delete(POBJDIR, pobjId[0]);
364             fm = new FosTxContextFactory();
365             fm.setDbDir(dbDir);
366
367             fm.setMonologFactory(LogBoot.getLoggerFactory());
368             fm.start();
369             ft.begin();
370             boolean ex = ft.exist(POBJDIR, pobjId[0]);
371             ft.commit();
372             fm.stop();
373             if (!ex)
374                 fail();
375         } catch (FosException fe) {
376             fe.printStackTrace();
377             fail("FOS error.");
378         } catch (Exception JavaDoc e) {
379             e.printStackTrace();
380             fail("Log configuration error.");
381         }
382     }
383
384     /**
385      * Tests start of a FOS repository with a committed modification.
386      */

387     public void testRecoverCommitModify() {
388         try {
389             fm = new FosTxContextFactory();
390             fm.setDbDir(dbDir);
391
392             fm.setMonologFactory(LogBoot.getLoggerFactory());
393             fm.start();
394             FosTransaction ft = fm.createTxContext();
395             ft.begin();
396             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
397             ft.commit();
398             ft.begin();
399             pobjVal[0].setValue(NEWVALUE);
400             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
401             ft.commit();
402             fm = new FosTxContextFactory();
403             fm.setDbDir(dbDir);
404
405             fm.setMonologFactory(LogBoot.getLoggerFactory());
406             fm.start();
407             pobjVal[0].setValue(NULLVALUE);
408             ft.begin();
409             ft.read(POBJDIR, pobjId[0], pobjVal[0], null);
410             ft.commit();
411             fm.stop();
412             if (pobjVal[0].getValue() != NEWVALUE)
413                 fail();
414         } catch (FosException fe) {
415             fe.printStackTrace();
416             fail("FOS error.");
417         } catch (Exception JavaDoc e) {
418             e.printStackTrace();
419             fail("Log configuration error.");
420         }
421     }
422
423     /**
424      * Tests start of a FOS repository with a prepared modification.
425      */

426     public void testRecoverPrepareModify() {
427         try {
428             fm = new FosTxContextFactory();
429             fm.setDbDir(dbDir);
430
431             fm.setMonologFactory(LogBoot.getLoggerFactory());
432             fm.start();
433             FosTransaction ft = fm.createTxContext();
434             ft.begin();
435             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
436             ft.commit();
437             ft.begin();
438             pobjVal[0].setValue(NEWVALUE);
439             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
440             ft.prepare();
441             fm = new FosTxContextFactory();
442             fm.setDbDir(dbDir);
443
444             fm.setMonologFactory(LogBoot.getLoggerFactory());
445             fm.start();
446             pobjVal[0].setValue(NULLVALUE);
447             ft.begin();
448             ft.read(POBJDIR, pobjId[0], pobjVal[0], null);
449             ft.commit();
450             fm.stop();
451             if (pobjVal[0].getValue() != NEWVALUE)
452                 fail();
453         } catch (FosException fe) {
454             fe.printStackTrace();
455             fail("FOS error.");
456         } catch (Exception JavaDoc e) {
457             e.printStackTrace();
458             fail("Log configuration error.");
459         }
460     }
461
462     /**
463      * Tests start of a FOS repository with a rolled back modification.
464      */

465     public void testRecoverRollbackModify() {
466         try {
467             fm = new FosTxContextFactory();
468             fm.setDbDir(dbDir);
469
470             fm.setMonologFactory(LogBoot.getLoggerFactory());
471             fm.start();
472             FosTransaction ft = fm.createTxContext();
473             ft.begin();
474             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
475             ft.commit();
476             ft.begin();
477             pobjVal[0].setValue(NEWVALUE);
478             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
479             ft.rollback();
480             fm = new FosTxContextFactory();
481             fm.setDbDir(dbDir);
482
483             fm.setMonologFactory(LogBoot.getLoggerFactory());
484             fm.start();
485             pobjVal[0].setValue(NULLVALUE);
486             ft.begin();
487             ft.read(POBJDIR, pobjId[0], pobjVal[0], null);
488             ft.commit();
489             fm.stop();
490             if (pobjVal[0].getValue() != INITIALVALUE)
491                 fail();
492         } catch (FosException fe) {
493             fe.printStackTrace();
494             fail("FOS error.");
495         } catch (Exception JavaDoc e) {
496             e.printStackTrace();
497             fail("Log configuration error.");
498         }
499     }
500
501     /**
502      * Tests start of a FOS repository with a uncommitted modification.
503      */

504     public void testRecoverModify() {
505         try {
506             fm = new FosTxContextFactory();
507             fm.setDbDir(dbDir);
508
509             fm.setMonologFactory(LogBoot.getLoggerFactory());
510             fm.start();
511             FosTransaction ft = fm.createTxContext();
512             ft.begin();
513             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
514             ft.commit();
515             ft.begin();
516             pobjVal[0].setValue(NEWVALUE);
517             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
518             fm = new FosTxContextFactory();
519             fm.setDbDir(dbDir);
520
521             fm.setMonologFactory(LogBoot.getLoggerFactory());
522             fm.start();
523             pobjVal[0].setValue(NULLVALUE);
524             ft.begin();
525             ft.read(POBJDIR, pobjId[0], pobjVal[0], null);
526             ft.commit();
527             fm.stop();
528             if (pobjVal[0].getValue() != INITIALVALUE)
529                 fail();
530         } catch (FosException fe) {
531             fe.printStackTrace();
532             fail("FOS error.");
533         } catch (Exception JavaDoc e) {
534             e.printStackTrace();
535             fail("Log configuration error.");
536         }
537     }
538
539     // For managing the TestCase as a standalone Test Suite.
540

541     /**
542      * Constructs the suite containing all test cases defined within
543      * TestRecovery.
544      */

545     public static Test suite() {
546         return new TestSuite(TestRecovery.class);
547     }
548
549     /**
550      * For running the TestRecovery suite standalone.
551      */

552     public static void main(String JavaDoc args[]) {
553         TestRunner.run(suite());
554     }
555
556     /**
557      * Constructs a TestRecovery.
558      */

559     public TestRecovery(String JavaDoc tn) throws Exception JavaDoc {
560         super(tn);
561         Properties JavaDoc prop = new Properties JavaDoc();
562         prop.load(ClassLoader.getSystemResourceAsStream("owfos.properties"));
563         dbDir = prop.getProperty("dbdir", null);
564         if (dbDir == null) {
565             throw new Exception JavaDoc(
566                 "Cannot find the 'dbdir' property");
567         }
568     }
569
570     /**
571      *
572      */

573     class IntPObj implements FosStructure {
574         /**
575          * The value of the integer persistent object.
576          */

577         private int value = INITIALVALUE;
578
579         /**
580          * Reads the content of a persistent object from the file represented by
581          * the given ObjectInputStream.
582          * @param ois The ObjectInputStream representing the file from which to
583          * read.
584          * @param conn The connection used to access the File Object Store.
585          */

586         public void readFile(ObjectInputStream JavaDoc ois, FosAccess conn, Object JavaDoc ctxt) throws Exception JavaDoc {
587             value = ois.readInt();
588         }
589
590         /**
591          * Writes the content of a persistent object to the file represented by
592          * the given ObjectOutputStream.
593          * @param oos The ObjectOutputStream representing the file to which to
594          * write.
595          * @param conn The connection used to access the File Object Store.
596          */

597         public void writeFile(ObjectOutputStream JavaDoc oos, FosAccess conn, Object JavaDoc ctxt) throws Exception JavaDoc {
598             oos.writeInt(value);
599         }
600
601         /**
602          * Returns the value of this integer persistent object.
603          */

604         int getValue() {
605             return value;
606         }
607
608         /**
609          * Assigns the value of this integer persistent object.
610          */

611         void setValue(int val) {
612             value = val;
613         }
614     }
615 }
Popular Tags