KickJava   Java API By Example, From Geeks To Geeks.

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


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.TestSuite;
9 import junit.textui.TestRunner;
10 import org.objectweb.perseus.LogBoot;
11 import org.objectweb.perseus.fos.api.FosException;
12 import org.objectweb.perseus.fos.api.FosTransaction;
13 import org.objectweb.perseus.fos.lib.FosTxContextFactory;
14
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * Tests standard transactions that correctly executes (correct begin/commit
19  * sequences).
20  */

21 public class TestTxOpsUniqMan extends TestTxOps {
22     // IMPLEMENTATION OF METHODS OF THE TestCase CLASS
23

24     /**
25      * Initialisation sequence executed before each test case.
26      */

27     protected void setUp() {
28     }
29
30     /**
31      * Termination sequence executed after each test case.
32      */

33     protected void tearDown() {
34     }
35
36     /**
37      * Tests rollback of a simple creation.
38      */

39     public void testRollbackCreation() {
40         try {
41             FosTransaction ft = fm.createTxContext();
42             ft.begin();
43             if (ft.exist(POBJDIR, pobjId[0]))
44                 ft.delete(POBJDIR, pobjId[0]);
45             ft.commit();
46             ft.begin();
47             ft.write(POBJDIR, pobjId[0], pobjVal[0], null);
48             ft.rollback();
49             ft.begin();
50             boolean ex = ft.exist(POBJDIR, pobjId[0]);
51             ft.commit();
52             fm.releaseTxContext(ft);
53             if (ex)
54                 fail("Object should not exist: creation rolled back!");
55         } catch (FosException fe) {
56             if (fe.getNestedException() != null)
57                 fe.getNestedException().printStackTrace();
58             fe.printStackTrace();
59             fail();
60         }
61     }
62
63     /**
64      * Tests rollback of multiple creations.
65      */

66     public void testRollbackCreations() {
67         try {
68             FosTransaction ft = fm.createTxContext();
69             ft.begin();
70             for (int i = 0; i < pobjId.length; i++)
71                 if (ft.exist(POBJDIR, pobjId[i]))
72                     ft.delete(POBJDIR, pobjId[i]);
73             ft.commit();
74             ft.begin();
75             for (int i = 0; i < pobjId.length; i++)
76                 ft.write(POBJDIR, pobjId[i], pobjVal[i], null);
77             ft.rollback();
78             ft.begin();
79             boolean ex = false;
80             for (int i = 0; i < pobjId.length; i++)
81                 ex |= ft.exist(POBJDIR, pobjId[i]);
82             ft.commit();
83             fm.releaseTxContext(ft);
84             if (ex)
85                 fail("Objects should not exist: creations rolled back!");
86         } catch (FosException fe) {
87             if (fe.getNestedException() != null)
88                 fe.getNestedException().printStackTrace();
89             fe.printStackTrace();
90             fail();
91         }
92     }
93
94     /**
95      * Tests a simple exist test where the object does not exist.
96      */

97     public void testExistNoObj() {
98         try {
99             FosTransaction ft = fm.createTxContext();
100             ft.begin();
101             try {
102                 ft.delete(POBJDIR, pobjId[0]);
103             } catch (FosException fe) {
104             }
105             ft.commit();
106             ft.begin();
107             boolean ex = ft.exist(POBJDIR, pobjId[0]);
108             ft.commit();
109             fm.releaseTxContext(ft);
110             if (ex)
111                 fail("Objects should not exist: no object!");
112         } catch (FosException fe) {
113             if (fe.getNestedException() != null)
114                 fe.getNestedException().printStackTrace();
115             fe.printStackTrace();
116             fail();
117         }
118     }
119
120     /**
121      * Tests the scan of the extension of a directory with no object.
122      */

123     public void testScanNoObj() {
124         try {
125             FosTransaction ft = fm.createTxContext();
126             ft.begin();
127             for (int i = 0; i < pobjId.length; i++)
128                 if (ft.exist(POBJDIR, pobjId[i]))
129                     ft.delete(POBJDIR, pobjId[i]);
130             ft.commit();
131             ft.begin();
132             Iterator JavaDoc it = ft.scan(POBJDIR);
133             ft.commit();
134             fm.releaseTxContext(ft);
135             if (it.hasNext())
136                 fail("Extension is not composed as required.");
137         } catch (FosException fe) {
138             if (fe.getNestedException() != null)
139                 fe.getNestedException().printStackTrace();
140             fe.printStackTrace();
141             fail();
142         }
143     }
144
145     // For managing the TestCase as a standalone Test Suite.
146

147     /**
148      * Constructs the suite containing all test cases defined within
149      * TestTxOpsUniqMan.
150      */

151     public static Test suite() {
152         return new TestSuite(TestTxOpsUniqMan.class);
153     }
154
155     /**
156      * For running the TestTxOpsUniqMan suite standalone.
157      */

158     public static void main(String JavaDoc args[]) {
159         TestRunner.run(suite());
160     }
161
162     /**
163      * Constructs a TestTxOpsUniqMan.
164      */

165     public TestTxOpsUniqMan(String JavaDoc tn) throws Exception JavaDoc {
166         super(tn);
167         try {
168             fm = new FosTxContextFactory();
169             fm.setDbDir(dbDir);
170             fm.setMonologFactory(LogBoot.getLoggerFactory());
171             fm.initialize();
172             fm.start();
173         } catch (FosException fe) {
174             System.out.println("Error during setUp - 1");
175             if (fe.getNestedException() != null)
176                 fe.getNestedException().printStackTrace();
177             fe.printStackTrace();
178             System.exit(0);
179         } catch (Exception JavaDoc e) {
180             System.out.println("Error during setUp - 2");
181             e.printStackTrace();
182             System.exit(0);
183         }
184     }
185 }
Popular Tags