KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > sequence > basic > TestSequence


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.runtime.sequence.basic;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.jdo.JDODataStoreException;
33 import javax.jdo.JDOException;
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.Query;
36 import javax.jdo.datastore.Sequence;
37
38 import junit.framework.Assert;
39
40 import org.objectweb.speedo.SpeedoTestHelper;
41 import org.objectweb.speedo.api.ExceptionHelper;
42 import org.objectweb.speedo.pobjects.sequence.basic.Cow;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  *
47  * @author Y.Bersihand
48  */

49 public class TestSequence extends SpeedoTestHelper {
50     
51     public TestSequence(String JavaDoc s) {
52         super(s);
53     }
54
55     protected String JavaDoc getLoggerName() {
56         return LOG_NAME + ".rt.sequence.TestSequence";
57     }
58     
59     public static final String JavaDoc COW_SEQ = "org.objectweb.speedo.pobjects.sequence.basic.cowseq";
60     
61     public static final String JavaDoc COW_SEQ_TRANSACTIONAL = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_transactional";
62     
63     public static final String JavaDoc COW_SEQ_DATASTORE = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_datastore";
64     
65     public static final int ADDITIONAL = 5;
66     
67     public static final int COMPARE_NUMBER = 100;
68     /**
69      * Get a sequence and use it to make objects persistent.
70      */

71     public void testSequence() {
72         logger.log(BasicLevel.DEBUG, "***************testSequence*****************");
73         PersistenceManager pm = pmf.getPersistenceManager();
74         pm.getObjectIdClass(Cow.class);
75         //get the sequence
76
Sequence s = pm.getSequence(COW_SEQ);
77         assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
78         Cow c1 = new Cow("cowSeq1", ((Long JavaDoc)s.next()).longValue());
79         Cow c2 = new Cow("cowSeq2", ((Long JavaDoc)s.next()).longValue());
80         Cow c3 = new Cow("cowSeq3", ((Long JavaDoc)s.next()).longValue());
81         assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
82         assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
83         Collection JavaDoc c = new ArrayList JavaDoc();
84         c.add(c1);
85         c.add(c2);
86         c.add(c3);
87         //make persistent
88
pm.currentTransaction().begin();
89         pm.makePersistentAll(c);
90         pm.currentTransaction().commit();
91         logger.log(BasicLevel.DEBUG, "c1: " + c1.getNbOfLegs());
92         logger.log(BasicLevel.DEBUG, "c2: " + c2.getNbOfLegs());
93         logger.log(BasicLevel.DEBUG, "c3: " + c3.getNbOfLegs());
94         pm.close();
95     }
96     
97     /**
98      * Test the allocate method.
99      */

100     public void testAllocate() {
101         logger.log(BasicLevel.DEBUG, "***************testAllocate*****************");
102         PersistenceManager pm = pmf.getPersistenceManager();
103         pm.getObjectIdClass(Cow.class);
104         //get the sequence
105
Sequence s = pm.getSequence(COW_SEQ);
106         assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
107         long beforeAllocate = ((Long JavaDoc) s.current()).longValue();
108         Collection JavaDoc col = new ArrayList JavaDoc();
109         Cow cow = null;
110         for (int i = 1; i <= ADDITIONAL; i++) {
111             cow = new Cow("cowAllocate" + i, ((Long JavaDoc)s.next()).longValue());
112             col.add(cow);
113         }
114         assertEquals(beforeAllocate + ADDITIONAL, ((Long JavaDoc) s.current()).longValue());
115         cow = new Cow("cowAfterAllocate", ((Long JavaDoc)s.next()).longValue());
116         col.add(cow);
117         pm.currentTransaction().begin();
118         //make persistent
119
pm.makePersistentAll(col);
120         s.current();
121         pm.currentTransaction().commit();
122         Iterator JavaDoc it = col.iterator();
123         while (it.hasNext()) {
124             Cow c = (Cow) it.next();
125             logger.log(BasicLevel.DEBUG, "cow " + c.getName() + ": " + c.getNbOfLegs());
126         }
127         pm.close();
128     }
129     
130     /**
131      * Compare the allocate method to the next mathod in terms of time elapsed.
132      * The allocate should be faster than next.
133      *
134      */

135     public void testCompareNextToAllocate() {
136         logger.log(BasicLevel.DEBUG, "***************testCompareNextToAllocate*****************");
137         PersistenceManager pm = pmf.getPersistenceManager();
138         pm.getObjectIdClass(Cow.class);
139         //get the sequence
140
Sequence s = pm.getSequence(COW_SEQ);
141         assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
142         long timeToGetManyNext = System.currentTimeMillis();
143         for (int i = 0; i< COMPARE_NUMBER; i++) {
144             s.next();
145         }
146         timeToGetManyNext = System.currentTimeMillis() - timeToGetManyNext;
147         long timeToGetManyAllocate = System.currentTimeMillis();
148         s.allocate(COMPARE_NUMBER);
149         for (int i = 0; i< COMPARE_NUMBER; i++) {
150             s.next();
151         }
152         timeToGetManyAllocate = System.currentTimeMillis() - timeToGetManyAllocate;
153         logger.log(BasicLevel.DEBUG, COMPARE_NUMBER + " ids generated:\nwith allocate: " + timeToGetManyAllocate
154                 + "\nwith next: " + timeToGetManyNext);
155         assertTrue("The time to get many ids with an allocate should be inferior to the time to get many ids with next()",
156                 timeToGetManyAllocate<timeToGetManyNext);
157     }
158     
159     /**
160      * Get a transactional sequence and use it outside a transaction.
161      */

162     public void testSequenceTransactional1() {
163         logger.log(BasicLevel.DEBUG, "***************testSequenceTransactional1*****************");
164         PersistenceManager pm = pmf.getPersistenceManager();
165         pm.getObjectIdClass(Cow.class);
166         //get the sequence
167
Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL);
168         assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL + " should not be null.", s);
169         try {
170             //use the sequence outside a tx
171
Cow c1 = new Cow("cowSeqNC1", ((Long JavaDoc)s.next()).longValue());
172             fail("An exception should be caught: a transactional sequence is used outside a transaction.");
173         } catch (Exception JavaDoc e) {
174             assertEquals("Should be a JDODatastoreException due to the fact the transactional"
175                     + "sequence has been used outside of a transaction. " + e.getMessage(), JDODataStoreException.class, e.getClass());
176         } finally {
177             if (pm.currentTransaction().isActive())
178                 pm.currentTransaction().rollback();
179             pm.close();
180         }
181     }
182     
183     /**
184      * Get a sequence and use it to make objects persistent.
185      */

186     public void testSequenceTransactional2() {
187         logger.log(BasicLevel.DEBUG, "***************testSequenceTransactional2*****************");
188         PersistenceManager pm = pmf.getPersistenceManager();
189         pm.getObjectIdClass(Cow.class);
190         //get the sequence
191
Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL);
192         assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL + " should not be null.", s);
193         //begin the tx
194
pm.currentTransaction().begin();
195         Cow c1 = new Cow("cowSeqNC1", ((Long JavaDoc)s.next()).longValue());
196         Cow c2 = new Cow("cowSeqNC2", ((Long JavaDoc)s.next()).longValue());
197         Cow c3 = new Cow("cowSeqNC3", ((Long JavaDoc)s.next()).longValue());
198         assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
199         assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
200         Collection JavaDoc c = new ArrayList JavaDoc();
201         c.add(c1);
202         c.add(c2);
203         c.add(c3);
204         //make persistent
205
pm.makePersistentAll(c);
206         pm.currentTransaction().commit();
207         //begin a tx
208
pm.currentTransaction().begin();
209         Cow c4 = new Cow("cowSeqNC4", ((Long JavaDoc)s.next()).longValue());
210         //keep the number
211
long index = c4.getNbOfLegs();
212         pm.makePersistent(c4);
213         //rollback
214
pm.currentTransaction().rollback();
215         //begin a tx
216
pm.currentTransaction().begin();
217         //check the next number generated is not the one used for the rollback : gap
218
assertTrue(((Long JavaDoc)s.next()).longValue() != index);
219         pm.currentTransaction().commit();
220         pm.close();
221     }
222     
223     /**
224      * Get a sequence linked to a datastore and use it to make objects persistent.
225      */

226     public void testSequenceDatastore() {
227         logger.log(BasicLevel.DEBUG, "***************testSequenceDatastore*****************");
228         PersistenceManager pm = pmf.getPersistenceManager();
229         pm.getObjectIdClass(Cow.class);
230         //get the sequence
231
Sequence s = pm.getSequence(COW_SEQ_DATASTORE);
232         assertNotNull("Sequence " + COW_SEQ_DATASTORE + " should not be null.", s);
233         //begin the tx
234
pm.currentTransaction().begin();
235         Cow c1 = new Cow("cowSeqDS1", ((Long JavaDoc)s.next()).longValue());
236         Cow c2 = new Cow("cowSeqDS2", ((Long JavaDoc)s.next()).longValue());
237         Cow c3 = new Cow("cowSeqDS3", ((Long JavaDoc)s.next()).longValue());
238         assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
239         assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
240         Collection JavaDoc c = new ArrayList JavaDoc();
241         c.add(c1);
242         c.add(c2);
243         c.add(c3);
244         //make persistent
245
pm.makePersistentAll(c);
246         pm.currentTransaction().commit();
247         pm.close();
248     }
249     /**
250      * Remove all the persistence instances.
251      */

252     public void testRemovingOfPersistentObject() {
253         PersistenceManager pm = pmf.getPersistenceManager();
254         try {
255             Class JavaDoc[] cs = new Class JavaDoc[]{Cow.class};
256             pm.currentTransaction().begin();
257             for(int i=0; i<cs.length; i++) {
258                 Query query = pm.newQuery(cs[i]);
259                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
260                 Iterator JavaDoc it = col.iterator();
261                 while(it.hasNext()) {
262                     Object JavaDoc o = it.next();
263                     Assert.assertNotNull("null object in the query result"
264                         + cs[i].getName(), o);
265                     pm.deletePersistent(o);
266
267                 }
268                 query.close(col);
269             }
270             pm.currentTransaction().commit();
271         } catch (JDOException e) {
272             Exception JavaDoc ie = ExceptionHelper.getNested(e);
273             logger.log(BasicLevel.ERROR, "", ie);
274             fail(ie.getMessage());
275         } finally {
276             pm.close();
277         }
278     }
279 }
280
Popular Tags