001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.runtime.sequence.basic;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.jdo.JDODataStoreException;
031: import javax.jdo.JDOException;
032: import javax.jdo.PersistenceManager;
033: import javax.jdo.Query;
034: import javax.jdo.datastore.Sequence;
035:
036: import junit.framework.Assert;
037:
038: import org.objectweb.speedo.SpeedoTestHelper;
039: import org.objectweb.speedo.api.ExceptionHelper;
040: import org.objectweb.speedo.pobjects.sequence.basic.Cow;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: *
045: * @author Y.Bersihand
046: */
047: public class TestSequence extends SpeedoTestHelper {
048:
049: public TestSequence(String s) {
050: super (s);
051: }
052:
053: protected String getLoggerName() {
054: return LOG_NAME + ".rt.sequence.TestSequence";
055: }
056:
057: public static final String COW_SEQ = "org.objectweb.speedo.pobjects.sequence.basic.cowseq";
058:
059: public static final String COW_SEQ_TRANSACTIONAL = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_transactional";
060:
061: public static final String COW_SEQ_DATASTORE = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_datastore";
062:
063: public static final int ADDITIONAL = 5;
064:
065: public static final int COMPARE_NUMBER = 100;
066:
067: /**
068: * Get a sequence and use it to make objects persistent.
069: */
070: public void testSequence() {
071: logger.log(BasicLevel.DEBUG,
072: "***************testSequence*****************");
073: PersistenceManager pm = pmf.getPersistenceManager();
074: pm.getObjectIdClass(Cow.class);
075: //get the sequence
076: Sequence s = pm.getSequence(COW_SEQ);
077: assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
078: Cow c1 = new Cow("cowSeq1", ((Long) s.next()).longValue());
079: Cow c2 = new Cow("cowSeq2", ((Long) s.next()).longValue());
080: Cow c3 = new Cow("cowSeq3", ((Long) s.next()).longValue());
081: assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
082: assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
083: Collection c = new ArrayList();
084: c.add(c1);
085: c.add(c2);
086: c.add(c3);
087: //make persistent
088: pm.currentTransaction().begin();
089: pm.makePersistentAll(c);
090: pm.currentTransaction().commit();
091: logger.log(BasicLevel.DEBUG, "c1: " + c1.getNbOfLegs());
092: logger.log(BasicLevel.DEBUG, "c2: " + c2.getNbOfLegs());
093: logger.log(BasicLevel.DEBUG, "c3: " + c3.getNbOfLegs());
094: pm.close();
095: }
096:
097: /**
098: * Test the allocate method.
099: */
100: public void testAllocate() {
101: logger.log(BasicLevel.DEBUG,
102: "***************testAllocate*****************");
103: PersistenceManager pm = pmf.getPersistenceManager();
104: pm.getObjectIdClass(Cow.class);
105: //get the sequence
106: Sequence s = pm.getSequence(COW_SEQ);
107: assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
108: long beforeAllocate = ((Long) s.current()).longValue();
109: Collection col = new ArrayList();
110: Cow cow = null;
111: for (int i = 1; i <= ADDITIONAL; i++) {
112: cow = new Cow("cowAllocate" + i, ((Long) s.next())
113: .longValue());
114: col.add(cow);
115: }
116: assertEquals(beforeAllocate + ADDITIONAL, ((Long) s.current())
117: .longValue());
118: cow = new Cow("cowAfterAllocate", ((Long) s.next()).longValue());
119: col.add(cow);
120: pm.currentTransaction().begin();
121: //make persistent
122: pm.makePersistentAll(col);
123: s.current();
124: pm.currentTransaction().commit();
125: Iterator it = col.iterator();
126: while (it.hasNext()) {
127: Cow c = (Cow) it.next();
128: logger.log(BasicLevel.DEBUG, "cow " + c.getName() + ": "
129: + c.getNbOfLegs());
130: }
131: pm.close();
132: }
133:
134: /**
135: * Compare the allocate method to the next mathod in terms of time elapsed.
136: * The allocate should be faster than next.
137: *
138: */
139: public void testCompareNextToAllocate() {
140: logger
141: .log(BasicLevel.DEBUG,
142: "***************testCompareNextToAllocate*****************");
143: PersistenceManager pm = pmf.getPersistenceManager();
144: pm.getObjectIdClass(Cow.class);
145: //get the sequence
146: Sequence s = pm.getSequence(COW_SEQ);
147: assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
148: long timeToGetManyNext = System.currentTimeMillis();
149: for (int i = 0; i < COMPARE_NUMBER; i++) {
150: s.next();
151: }
152: timeToGetManyNext = System.currentTimeMillis()
153: - timeToGetManyNext;
154: long timeToGetManyAllocate = System.currentTimeMillis();
155: s.allocate(COMPARE_NUMBER);
156: for (int i = 0; i < COMPARE_NUMBER; i++) {
157: s.next();
158: }
159: timeToGetManyAllocate = System.currentTimeMillis()
160: - timeToGetManyAllocate;
161: logger.log(BasicLevel.DEBUG, COMPARE_NUMBER
162: + " ids generated:\nwith allocate: "
163: + timeToGetManyAllocate + "\nwith next: "
164: + timeToGetManyNext);
165: assertTrue(
166: "The time to get many ids with an allocate should be inferior to the time to get many ids with next()",
167: timeToGetManyAllocate < timeToGetManyNext);
168: }
169:
170: /**
171: * Get a transactional sequence and use it outside a transaction.
172: */
173: public void testSequenceTransactional1() {
174: logger
175: .log(BasicLevel.DEBUG,
176: "***************testSequenceTransactional1*****************");
177: PersistenceManager pm = pmf.getPersistenceManager();
178: pm.getObjectIdClass(Cow.class);
179: //get the sequence
180: Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL);
181: assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL
182: + " should not be null.", s);
183: try {
184: //use the sequence outside a tx
185: Cow c1 = new Cow("cowSeqNC1", ((Long) s.next()).longValue());
186: fail("An exception should be caught: a transactional sequence is used outside a transaction.");
187: } catch (Exception e) {
188: if (!JDODataStoreException.class.equals(e.getClass())) {
189: logger.log(BasicLevel.ERROR, "Found exception: ", e);
190: }
191: assertEquals(
192: "Should be a JDODatastoreException due to the fact the transactional"
193: + "sequence has been used outside of a transaction. "
194: + e.getMessage(),
195: JDODataStoreException.class, e.getClass());
196: } finally {
197: if (pm.currentTransaction().isActive())
198: pm.currentTransaction().rollback();
199: pm.close();
200: }
201: }
202:
203: /**
204: * Get a sequence and use it to make objects persistent.
205: */
206: public void testSequenceTransactional2() {
207: logger
208: .log(BasicLevel.DEBUG,
209: "***************testSequenceTransactional2*****************");
210: PersistenceManager pm = pmf.getPersistenceManager();
211: pm.getObjectIdClass(Cow.class);
212: //get the sequence
213: Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL);
214: assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL
215: + " should not be null.", s);
216: //begin the tx
217: pm.currentTransaction().begin();
218: Cow c1 = new Cow("cowSeqNC1", ((Long) s.next()).longValue());
219: Cow c2 = new Cow("cowSeqNC2", ((Long) s.next()).longValue());
220: Cow c3 = new Cow("cowSeqNC3", ((Long) s.next()).longValue());
221: assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
222: assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
223: Collection c = new ArrayList();
224: c.add(c1);
225: c.add(c2);
226: c.add(c3);
227: //make persistent
228: pm.makePersistentAll(c);
229: pm.currentTransaction().commit();
230: //begin a tx
231: pm.currentTransaction().begin();
232: Cow c4 = new Cow("cowSeqNC4", ((Long) s.next()).longValue());
233: //keep the number
234: long index = c4.getNbOfLegs();
235: pm.makePersistent(c4);
236: //rollback
237: pm.currentTransaction().rollback();
238: //begin a tx
239: pm.currentTransaction().begin();
240: //check the next number generated is not the one used for the rollback : gap
241: assertTrue(((Long) s.next()).longValue() != index);
242: pm.currentTransaction().commit();
243: pm.close();
244: }
245:
246: /**
247: * Get a sequence linked to a datastore and use it to make objects persistent.
248: */
249: public void testSequenceDatastore() {
250: logger
251: .log(BasicLevel.DEBUG,
252: "***************testSequenceDatastore*****************");
253: PersistenceManager pm = pmf.getPersistenceManager();
254: pm.getObjectIdClass(Cow.class);
255: //get the sequence
256: Sequence s = pm.getSequence(COW_SEQ_DATASTORE);
257: assertNotNull("Sequence " + COW_SEQ_DATASTORE
258: + " should not be null.", s);
259: //begin the tx
260: pm.currentTransaction().begin();
261: Cow c1 = new Cow("cowSeqDS1", ((Long) s.next()).longValue());
262: Cow c2 = new Cow("cowSeqDS2", ((Long) s.next()).longValue());
263: Cow c3 = new Cow("cowSeqDS3", ((Long) s.next()).longValue());
264: assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
265: assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
266: Collection c = new ArrayList();
267: c.add(c1);
268: c.add(c2);
269: c.add(c3);
270: //make persistent
271: pm.makePersistentAll(c);
272: pm.currentTransaction().commit();
273: pm.close();
274: }
275:
276: /**
277: * Remove all the persistence instances.
278: */
279: public void testRemovingOfPersistentObject() {
280: PersistenceManager pm = pmf.getPersistenceManager();
281: try {
282: Class[] cs = new Class[] { Cow.class };
283: pm.currentTransaction().begin();
284: for (int i = 0; i < cs.length; i++) {
285: Query query = pm.newQuery(cs[i]);
286: Collection col = (Collection) query.execute();
287: Iterator it = col.iterator();
288: while (it.hasNext()) {
289: Object o = it.next();
290: Assert.assertNotNull(
291: "null object in the query result"
292: + cs[i].getName(), o);
293: pm.deletePersistent(o);
294:
295: }
296: query.close(col);
297: }
298: pm.currentTransaction().commit();
299: } catch (JDOException e) {
300: Exception ie = ExceptionHelper.getNested(e);
301: logger.log(BasicLevel.ERROR, "", ie);
302: fail(ie.getMessage());
303: } finally {
304: pm.close();
305: }
306: }
307: }
|