KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > test > core > suite > MultiOptimisticTestCase


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.test.core.suite;
25
26 import junit.framework.Test;
27 import org.objectweb.jalisto.se.exception.JalistoException;
28 import org.objectweb.jalisto.se.exception.IdentityException;
29 import org.objectweb.jalisto.se.exception.TransactionException;
30 import org.objectweb.jalisto.se.api.*;
31 import org.objectweb.jalisto.se.test.data.Book;
32 import org.objectweb.jalisto.se.test.workbench.JalistoTestCase;
33 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite;
34 import org.objectweb.jalisto.se.JalistoFactory;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39 public class MultiOptimisticTestCase extends JalistoTestCase {
40     public MultiOptimisticTestCase() {
41     }
42
43     public MultiOptimisticTestCase(String JavaDoc name) {
44         super(name);
45     }
46
47     public static Test suite() {
48         JalistoTestSuite suite = new JalistoTestSuite();
49         MultiOptimisticTestCase tc = (MultiOptimisticTestCase) newTestCase(suite, new MultiOptimisticTestCase());
50
51         tc.define(Book.getMetaDescription());
52
53         tc.createSessions(5);
54
55         tc.createTest();
56         tc.updateTest();
57         tc.deleteTest();
58         tc.extentTest(20);
59
60         tc.refreshTest();
61
62         tc.testMakeOidRollback01();
63         tc.testMakeOidRollback02();
64
65         tc.restructureMultiTest();
66
67         return suite;
68     }
69
70     public void createSessions(int nbr) {
71         if (!canRun) {
72             System.out.println("can't execute this test with implementation other than 'multi'");
73             return;
74         }
75
76         ArrayList JavaDoc sessions = new ArrayList JavaDoc();
77         JalistoProperties properties = JalistoFactory.getInternalFactory().getProperties(getJalistoPropertiesFilename());
78         overrideJalistoProperties(properties);
79         for (int i = 0; i < nbr; i++) {
80             Session s = JalistoFactory.getSession(getJalistoPropertiesFilename());
81             s.openSession();
82             s.currentTransaction().begin();
83             s.currentTransaction().commit();
84             sessions.add(s);
85         }
86         for (int i = 0; i < nbr; i++) {
87             Session s = (Session) sessions.get(i);
88             s.closeSession();
89         }
90     }
91
92     public void restructureMultiTest() {
93         if (!canRun) {
94             System.out.println("can't execute this test with implementation other than 'multi'");
95             return;
96         }
97
98         JalistoProperties properties = JalistoFactory.getInternalFactory().getProperties(getJalistoPropertiesFilename());
99         overrideJalistoProperties(properties);
100         Session s1 = JalistoFactory.getSession(getJalistoPropertiesFilename());
101         Session s2 = JalistoFactory.getSession(getJalistoPropertiesFilename());
102         s1.openSession();
103         s1.currentTransaction().begin();
104         s2.openSession();
105         s2.currentTransaction().begin();
106         s1.currentTransaction().commit();
107         s1.closeSession();
108         try {
109             s1.reorganize();
110             assertTrue("must raise an exception", true);
111         } catch (JalistoException e) {
112             assertTrue("must begin with", e.getMessage().startsWith(
113                     "There are active sessions during attempt to reorganize datastore"));
114         }
115         s2.currentTransaction().commit();
116         try {
117             s1.reorganize();
118             assertTrue("must raise an exception", true);
119         } catch (JalistoException e) {
120             assertTrue("must begin with", e.getMessage().startsWith(
121                     "There are active sessions during attempt to reorganize datastore"));
122         }
123         s2.closeSession();
124         s1.reorganize();
125     }
126
127     public void createTest() {
128         if (!canRun) {
129             System.out.println("can't execute this test with implementation other than 'multi'");
130             return;
131         }
132
133         JalistoProperties properties = JalistoFactory.getInternalFactory().getProperties(getJalistoPropertiesFilename());
134         overrideJalistoProperties(properties);
135         Session session1 = JalistoFactory.getSession(getJalistoPropertiesFilename());
136         session1.openSession();
137         Transaction tx1 = session1.currentTransaction();
138         Session session2 = JalistoFactory.getSession(getJalistoPropertiesFilename());
139         session2.openSession();
140         Transaction tx2 = session2.currentTransaction();
141
142         {
143             tx1.begin();
144             tx2.begin();
145             Object JavaDoc oid = session1.createObject(Book.newBook().toArray(), Book.class);
146             try {
147                 session2.readObjectByOid(oid);
148                 assertTrue("should raise a IdentityException", true);
149             } catch (IdentityException e) {
150             }
151             tx1.commit();
152             session2.readObjectByOid(oid);
153             tx2.commit();
154         }
155
156         {
157             tx1.begin();
158             tx2.begin();
159             Object JavaDoc oid = session1.createObject(Book.newBook().toArray(), Book.class);
160             try {
161                 session2.readObjectByOid(oid);
162                 assertTrue("should raise a IdentityException", true);
163             } catch (IdentityException e) {
164             }
165             tx1.rollback();
166             try {
167                 session2.readObjectByOid(oid);
168                 assertTrue("should raise a IdentityException", true);
169             } catch (IdentityException e) {
170             }
171             tx2.commit();
172         }
173
174         session1.closeSession();
175         session2.closeSession();
176     }
177
178     public void updateTest() {
179         if (!canRun) {
180             System.out.println("can't execute this test with implementation other than 'multi'");
181             return;
182         }
183
184         JalistoProperties properties = JalistoFactory.getInternalFactory().getProperties(getJalistoPropertiesFilename());
185         overrideJalistoProperties(properties);
186         Session session1 = JalistoFactory.getSession(getJalistoPropertiesFilename());
187         session1.openSession();
188         Transaction tx1 = session1.currentTransaction();
189         Session session2 = JalistoFactory.getSession(getJalistoPropertiesFilename());
190         session2.openSession();
191         Transaction tx2 = session2.currentTransaction();
192
193         tx1.begin();
194         Object JavaDoc oid = session1.createObject(Book.newBook().toArray(), Book.class);
195         tx1.commit();
196
197         {
198             tx1.begin();
199             tx2.begin();
200             Object JavaDoc[] o1 = session1.readObjectByOid(oid);
201             Object JavaDoc[] o2 = session2.readObjectByOid(oid);
202             assertTrue("must not be identical", o1 != o2);
203             assertTrue("must be equals", equals(o1, o2));
204             o1[0] = o1[0] + " (update 01 by s1)";
205             session1.updateObjectByOid(oid, o1);
206             tx1.commit();
207             Object JavaDoc[] o3 = session2.readObjectByOid(oid);
208             assertTrue("must not be identical", o2 != o3);
209             assertTrue("must be equals", equals(o2, o3));
210             tx2.commit();
211             tx2.begin();
212             Object JavaDoc[] o4 = session2.readObjectByOid(oid);
213             assertTrue("must be equals", equals(o1, o4));
214             tx2.commit();
215         }
216
217         {
218             tx1.begin();
219             Object JavaDoc[] o1 = session1.readObjectByOid(oid);
220             o1[0] = o1[0] + " (update 02 by s1)";
221             session1.updateObjectByOid(oid, o1);
222             tx2.begin();
223             Object JavaDoc[] o2 = session2.readObjectByOid(oid);
224             assertTrue("must not be identical", o1 != o2);
225             assertTrue("must not be equals", !equals(o1, o2));
226             tx1.commit();
227             tx2.commit();
228         }
229
230         {
231             tx1.begin();
232             tx2.begin();
233             Object JavaDoc[] o1 = session1.readObjectByOid(oid);
234             Object JavaDoc[] o2 = session2.readObjectByOid(oid);
235             assertTrue("must not be identical", o1 != o2);
236             assertTrue("must be equals", equals(o1, o2));
237             o1[0] = o1[0] + " (update 03 by s1)";
238             session1.updateObjectByOid(oid, o1);
239             tx1.commit();
240             o2[0] = o2[0] + " (update 01 by s2)";
241             session2.updateObjectByOid(oid, o2);
242             try {
243                 tx2.commit();
244                 assertTrue("must raise optimistic exception", false);
245             } catch (TransactionException jalistoTransactionExc) {
246                 assertTrue("must be a optimistic/pessimistic exception",
247                            jalistoTransactionExc.isOptimisticException() || jalistoTransactionExc.isPessimisticException());
248             } finally {
249                 if (tx2.isActive()) {
250                     tx2.rollback();
251                 }
252             }
253         }
254
255         {
256             tx1.begin();
257             tx2.begin();
258             Object JavaDoc[] o1 = session1.readObjectByOid(oid);
259             Object JavaDoc[] o2 = session2.readObjectByOid(oid);
260             assertTrue("must not be identical", o1 != o2);
261             assertTrue("must be equals", equals(o1, o2));
262             o1[0] = o1[0] + " (update 04 by s1)";
263             session1.updateObjectByOid(oid, o1);
264             tx1.commit();
265             Object JavaDoc[] o3 = session2.readObjectByOid(oid); // with read before update
266
o3[0] = o3[0] + " (update 02 by s2)";
267             session2.updateObjectByOid(oid, o3);
268             try {
269                 tx2.commit();
270                 assertTrue("must raise optimistic exception", false);
271             } catch (TransactionException jalistoTransactionExc) {
272                 assertTrue("must be a optimistic/pessimistic exception",
273                            jalistoTransactionExc.isOptimisticException() || jalistoTransactionExc.isPessimisticException());
274             } finally {
275                 if (tx2.isActive()) {
276                     tx2.rollback();
277                 }
278             }
279         }
280
281         session1.closeSession();
282         session2.closeSession();
283     }
284
285     public void deleteTest() {
286         if (!canRun) {
287             System.out.println("can't execute this test with implementation other than 'multi'");
288             return;
289         }
290
291         JalistoProperties properties = JalistoFactory.getInternalFactory().getProperties(getJalistoPropertiesFilename());
292         overrideJalistoProperties(properties);
293         Session session1 = JalistoFactory.getSession(getJalistoPropertiesFilename());
294         session1.openSession();
295         Transaction tx1 = session1.currentTransaction();
296         Session session2 = JalistoFactory.getSession(getJalistoPropertiesFilename());
297         session2.openSession();
298         Transaction tx2 = session2.currentTransaction();
299
300         tx1.begin();
301         Object JavaDoc oid = session1.createObject(Book.newBook().toArray(), Book.class);
302         tx1.commit();
303
304         {
305             tx1.begin();
306             tx2.begin();
307             session1.deleteObjectByOid(oid);
308             Object JavaDoc[] o2 = session2.readObjectByOid(oid);
309             assertNotNull(o2);
310             try {
311                 session1.readObjectByOid(oid);
312                 assertTrue("should raise exception", true);
313             } catch (IdentityException e) {
314             }
315             tx1.rollback();
316             tx1.begin();
317             Object JavaDoc[] o1 = session1.readObjectByOid(oid);
318             assertNotNull(o1);
319             tx1.commit();
320             tx2.commit();
321         }
322
323         session1.closeSession();
324         session2.closeSession();
325     }
326
327     public void extentTest(int number) {
328         if (!canRun) {
329             System.out.println("can't execute this test with implementation other than 'multi'");
330             return;
331         }
332
333         JalistoProperties properties = JalistoFactory.getInternalFactory().getProperties(getJalistoPropertiesFilename());
334         overrideJalistoProperties(properties);
335         Session session1 = JalistoFactory.getSession(getJalistoPropertiesFilename());
336         session1.openSession();
337         Transaction tx1 = session1.currentTransaction();
338         Session session2 = JalistoFactory.getSession(getJalistoPropertiesFilename());
339         session2.openSession();
340         Transaction tx2 = session2.currentTransaction();
341         ArrayList JavaDoc oids = new ArrayList JavaDoc();
342
343         {
344             tx1.begin();
345             Iterator JavaDoc extent = session1.getExtent(Book.class).readFully().iterator();
346             while(extent.hasNext()) {
347                 session1.deleteObjectByOid(extent.next());
348             }
349             for (int i = 0; i < number; i++) {
350                 oids.add(session1.createObject(Book.newBook().toArray(), Book.class));
351             }
352             tx1.commit();
353         }
354
355         {
356             tx1.begin();
357             tx2.begin();
358             Object JavaDoc createdOid = session1.createObject(Book.newBook().toArray(), Book.class);
359             Object JavaDoc removedOid = oids.get(0);
360             session2.deleteObjectByOid(removedOid);
361             {
362                 Extent extent1 = session1.getExtent(Book.class).readFully();
363                 assertEquals("must be equals", number + 1, extent1.size());
364                 assertTrue("must contains", extent1.contains(createdOid));
365                 assertTrue("must contains", extent1.contains(removedOid));
366
367                 Extent extent2 = session2.getExtent(Book.class).readFully();
368                 assertEquals("must be equals", number - 1, extent2.size());
369                 assertTrue("must not contains", ! extent2.contains(createdOid));
370                 assertTrue("must not contains", ! extent2.contains(removedOid));
371             }
372             tx1.commit();
373             tx1.begin();
374             {
375                 Extent extent1 = session1.getExtent(Book.class).readFully();
376                 assertEquals("must be equals", number + 1, extent1.size());
377                 assertTrue("must contains", extent1.contains(createdOid));
378                 assertTrue("must contains", extent1.contains(removedOid));
379
380                 Extent extent2 = session2.getExtent(Book.class).readFully();
381                 assertEquals("must be equals", number, extent2.size());
382                 assertTrue("must contains", extent2.contains(createdOid));
383                 assertTrue("must not contains", ! extent2.contains(removedOid));
384             }
385             tx2.commit();
386             tx2.begin();
387             {
388                 Extent extent1 = session1.getExtent(Book.class).readFully();
389                 assertEquals("must be equals", number, extent1.size());
390                 assertTrue("must contains", extent1.contains(createdOid));
391                 assertTrue("must not contains", ! extent1.contains(removedOid));
392
393                 Extent extent2 = session2.getExtent(Book.class).readFully();
394                 assertEquals("must be equals", number, extent2.size());
395                 assertTrue("must contains", extent2.contains(createdOid));
396                 assertTrue("must not contains", ! extent2.contains(removedOid));
397             }
398             tx1.commit();
399             tx2.commit();
400         }
401
402         session1.closeSession();
403         session2.closeSession();
404     }
405
406     public void refreshTest() {
407         if (!canRun) {
408             System.out.println("can't execute this test with implementation other than 'multi'");
409             return;
410         }
411
412         Session session1 = JalistoFactory.getSession(getJalistoPropertiesFilename());
413         session1.openSession();
414         Transaction tx1 = session1.currentTransaction();
415         Session session2 = JalistoFactory.getSession(getJalistoPropertiesFilename());
416         session2.openSession();
417         Transaction tx2 = session2.currentTransaction();
418
419         tx1.begin();
420         Object JavaDoc oid = session1.createObject(Book.newBook().toArray(), Book.class);
421         tx1.commit();
422
423         {
424             tx1.begin();
425             tx2.begin();
426             Object JavaDoc[] o11 = session1.readObjectByOid(oid);
427             Object JavaDoc[] o21 = session2.readObjectByOid(oid);
428             assertNotNull(o11);
429             assertNotNull(o21);
430             assertTrue("should not be same reference", o11 != o21);
431             String JavaDoc title = (String JavaDoc) o11[0];
432             assertEquals("must be equals", title, o21[0]);
433             o11[0] = title + "u1";
434             session1.updateObjectByOid(oid, o11);
435             tx1.commit();
436             tx1.begin();
437             Object JavaDoc[] o12 = session1.readObjectByOid(oid);
438             assertTrue("should not be same reference", o11 != o12);
439             assertEquals("must be equals", title+"u1", o12[0]);
440             Object JavaDoc[] o22 = session2.readObjectByOid(oid);
441             assertEquals("must be equals", title, o22[0]);
442             Object JavaDoc[] o23 = session2.refreshObjectByOid(oid);
443             assertTrue("should not be same reference", o21 != o23);
444             assertEquals("must be equals", title+"u1", o23[0]);
445             Object JavaDoc[] o24 = session2.readObjectByOid(oid);
446             assertEquals("must be equals", title+"u1", o24[0]);
447             tx1.commit();
448             tx2.commit();
449         }
450
451         {
452             tx1.begin();
453             tx2.begin();
454             Object JavaDoc[] o11 = session1.readObjectByOid(oid);
455             Object JavaDoc[] o21 = session2.readObjectByOid(oid);
456             assertNotNull(o11);
457             assertNotNull(o21);
458             assertTrue("should not be same reference", o11 != o21);
459             String JavaDoc title = (String JavaDoc) o11[0];
460             assertEquals("must be equals", title, o21[0]);
461             o11[0] = title + "u1";
462             o21[0] = title + "u2";
463             session1.updateObjectByOid(oid, o11);
464             session2.updateObjectByOid(oid, o21);
465             Object JavaDoc[] o12 = session1.readObjectByOid(oid);
466             Object JavaDoc[] o22 = session2.readObjectByOid(oid);
467             assertEquals("must be equals", title+"u1", o12[0]);
468             assertEquals("must be equals", title+"u2", o22[0]);
469             Object JavaDoc[] o13 = session1.refreshObjectByOid(oid);
470             assertEquals("must be equals", title, o13[0]);
471             o13[0] = title + "u3";
472             session1.updateObjectByOid(oid, o13);
473             tx1.commit();
474             Object JavaDoc[] o23 = session2.readObjectByOid(oid);
475             assertEquals("must be equals", title+"u2", o23[0]);
476             Object JavaDoc[] o24 = session2.refreshObjectByOid(oid);
477             assertEquals("must be equals", title+"u3", o24[0]);
478             tx2.commit();
479         }
480
481         {
482             tx1.begin();
483             tx2.begin();
484             Object JavaDoc[] o11 = session1.readObjectByOid(oid);
485             Object JavaDoc[] o21 = session2.readObjectByOid(oid);
486             assertNotNull(o11);
487             assertNotNull(o21);
488             assertTrue("should not be same reference", o11 != o21);
489             session1.deleteObjectByOid(oid);
490             try {
491                 session1.refreshObjectByOid(oid);
492                 assertTrue("should raise exception", false);
493             } catch (IdentityException fie) {
494             }
495             tx1.commit();
496             tx2.commit();
497         }
498
499         session1.closeSession();
500         session2.closeSession();
501     }
502
503     public void testMakeOidRollback01() {
504         if (!canRun) {
505             System.out.println("can't execute this test with implementation other than 'multi'");
506             return;
507         }
508
509         Session session1 = JalistoFactory.getSession(getJalistoPropertiesFilename());
510         session1.openSession();
511         Transaction tx1 = session1.currentTransaction();
512         Session session2 = JalistoFactory.getSession(getJalistoPropertiesFilename());
513         session2.openSession();
514         Transaction tx2 = session2.currentTransaction();
515
516         int nbr = session1.getInternalSession().getProperties().getOidPageSize();
517         ArrayList JavaDoc oids1 = new ArrayList JavaDoc();
518
519         try {
520             tx1.begin();
521             tx2.begin();
522             for (int i = 0; i < nbr*5; i++) {
523                 oids1.add(session1.makeNewFileOid(Book.class));
524             }
525             tx1.commit();
526             tx2.commit();
527
528             tx2.begin();
529             for (int i = 0; i < oids1.size(); i++) {
530                 session2.deleteObjectByOid(oids1.get(i));
531             }
532             tx2.commit();
533         } finally {
534             if (tx1.isActive()) {
535                 tx1.rollback();
536             }
537             if (tx2.isActive()) {
538                 tx2.rollback();
539             }
540             session1.closeSession();
541             session2.closeSession();
542         }
543     }
544
545     public void testMakeOidRollback02() {
546         if (!canRun) {
547             System.out.println("can't execute this test with implementation other than 'multi'");
548             return;
549         }
550
551         Session session1 = JalistoFactory.getSession(getJalistoPropertiesFilename());
552         session1.openSession();
553         Transaction tx1 = session1.currentTransaction();
554         Session session2 = JalistoFactory.getSession(getJalistoPropertiesFilename());
555         session2.openSession();
556         Transaction tx2 = session2.currentTransaction();
557
558         int nbr = session1.getInternalSession().getProperties().getOidPageSize();
559         ArrayList JavaDoc oids1 = new ArrayList JavaDoc();
560         ArrayList JavaDoc oids2 = new ArrayList JavaDoc();
561
562         try {
563             tx1.begin();
564             tx2.begin();
565             for (int i = 0; i < nbr/2; i++) {
566                 oids1.add(session1.makeNewFileOid(Book.class));
567                 oids2.add(session2.makeNewFileOid(Book.class));
568             }
569             tx1.commit();
570             tx1.begin();
571             tx2.rollback();
572             Object JavaDoc[] o = Book.newBook().toArray();
573             for (int i = 0; i < oids2.size(); i++) {
574                 Object JavaDoc oid = oids2.get(i);
575                 try {
576                     session1.createObject(oid, o);
577                     assertTrue("must raise exception", false);
578                 } catch (IdentityException jalistoIdentityExc) {
579                     String JavaDoc message = "the given oid "+oid+" doesn't exist in this Jalisto datastore";
580                     assertEquals("must be equals", message, jalistoIdentityExc.getMessage());
581                 }
582             }
583             tx1.rollback();
584             tx1.begin();
585             for (int i = 0; i < nbr*4; i++) {
586                 oids1.add(session1.makeNewFileOid(Book.class));
587             }
588             tx1.commit();
589
590             tx2.begin();
591             for (int i = 0; i < oids1.size(); i++) {
592                 session2.deleteObjectByOid(oids1.get(i));
593             }
594             tx2.commit();
595         } finally {
596             if (tx1.isActive()) {
597                 tx1.rollback();
598             }
599             if (tx2.isActive()) {
600                 tx2.rollback();
601             }
602             session1.closeSession();
603             session2.closeSession();
604         }
605     }
606
607     /**
608      * **************************************************************************************
609      */

610
611     private boolean equals(Object JavaDoc[] o1, Object JavaDoc[] o2) {
612         for (short i = 0; i < o1.length; i++) {
613             if (i >= o2.length) {
614                 return false;
615             }
616             if (!o1[i].equals(o2[i])) {
617                 return false;
618             }
619         }
620         return true;
621     }
622
623     public void overrideJalistoProperties(JalistoProperties jalistoProperties) {
624         jalistoProperties.setProperty(JalistoProperties.CONCURRENCY_MODE_KEY, "optimistic");
625     }
626
627     public void define(ClassDescription classDescription) {
628         JalistoProperties properties = JalistoFactory.getInternalFactory().getProperties(getJalistoPropertiesFilename());
629         if (!properties.isMultiImplementation()) {
630             canRun = false;
631             return;
632         }
633         overrideJalistoProperties(properties);
634         Session s = JalistoFactory.getSession(getJalistoPropertiesFilename());
635         s.openSession();
636         s.defineClass(classDescription);
637         s.closeSession();
638     }
639
640     public void finishTests() {
641         super.finishTests();
642     }
643
644
645     private boolean canRun = true;
646 }
647
Popular Tags