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.id;
025:
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.jdo.JDOException;
030: import javax.jdo.PersistenceManager;
031: import javax.jdo.Query;
032: import javax.jdo.datastore.Sequence;
033:
034: import junit.framework.Assert;
035:
036: import org.objectweb.speedo.SpeedoTestHelper;
037: import org.objectweb.speedo.api.ExceptionHelper;
038: import org.objectweb.speedo.pobjects.sequence.id.CompactDisc;
039: import org.objectweb.speedo.pobjects.sequence.id.Phone;
040: import org.objectweb.speedo.pobjects.sequence.id.Product;
041: import org.objectweb.speedo.pobjects.sequence.id.Suitcase;
042: import org.objectweb.util.monolog.api.BasicLevel;
043:
044: /**
045: * @author Y.Bersihand
046: */
047: public class TestSequenceId extends SpeedoTestHelper {
048:
049: public static final int ADDITIONAL = 100;
050:
051: public TestSequenceId(String s) {
052: super (s);
053: }
054:
055: protected String getLoggerName() {
056: return LOG_NAME + ".rt.sequence.TestSequence";
057: }
058:
059: public static final String PRODUCT_SEQ = "org.objectweb.speedo.pobjects.sequence.id.product_seq";
060: public static final String PHONE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.phone_seq";
061: public static final String SUITCASE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.suitcase_seq";
062: public static final String CD_SEQ = "org.objectweb.speedo.pobjects.sequence.id.cd_seq";
063:
064: /**
065: * Get a sequence and use it to make objects persistent.
066: * A field has a value-strategy set to sequence.
067: */
068: public void testSequenceId1() {
069: logger.log(BasicLevel.DEBUG,
070: "***************testSequenceId1*****************");
071: PersistenceManager pm = pmf.getPersistenceManager();
072: pm.getObjectIdClass(Product.class);
073: //get the sequence
074: Sequence s = pm.getSequence(PRODUCT_SEQ);
075: assertNotNull("Sequence " + PRODUCT_SEQ
076: + " should not be null.", s);
077: Product p1 = new Product();
078: p1.setName("product 1");
079: Product p2 = new Product();
080: p2.setName("product 2");
081: //make persistent
082: pm.currentTransaction().begin();
083: pm.makePersistent(p1);
084: pm.makePersistent(p2);
085: pm.currentTransaction().commit();
086: assertTrue(p1.getReference() < p2.getReference());
087: pm.close();
088: }
089:
090: /**
091: * Get a sequence and use it to make objects persistent.
092: * A datastore-identity has a strategy set to sequence.
093: */
094: public void testSequenceId2() {
095: logger.log(BasicLevel.DEBUG,
096: "***************testSequenceId2*****************");
097: PersistenceManager pm = pmf.getPersistenceManager();
098: pm.getObjectIdClass(Phone.class);
099: //get the sequence
100: Sequence s = pm.getSequence(PHONE_SEQ);
101: assertNotNull("Sequence " + PHONE_SEQ + " should not be null.",
102: s);
103: Phone p1 = new Phone();
104: p1.setName("phone 1");
105: Phone p2 = new Phone();
106: p2.setName("phone 2");
107: //make persistent
108: pm.currentTransaction().begin();
109: pm.makePersistent(p1);
110: pm.makePersistent(p2);
111: pm.currentTransaction().commit();
112: pm.close();
113: }
114:
115: public void testAllocateRdbSequence() {
116: logger
117: .log(BasicLevel.DEBUG,
118: "***************testAllocateRdbSequence*****************");
119: PersistenceManager pm = pmf.getPersistenceManager();
120: try {
121: pm.getObjectIdClass(Phone.class);
122: //get the sequence
123: Sequence s = pm.getSequence(PHONE_SEQ);
124: assertNotNull("Sequence " + PHONE_SEQ
125: + " should not be null.", s);
126: Phone[] phones = new Phone[ADDITIONAL];
127: long timeAllocate = System.currentTimeMillis();
128: pm.currentTransaction().begin();
129: //allocate
130: s.allocate(ADDITIONAL);
131: for (int i = 0; i < ADDITIONAL; i++) {
132: phones[i] = new Phone();
133: phones[i].setName("phone " + i);
134: }
135: //make persistent
136: pm.makePersistentAll(phones);
137: pm.currentTransaction().commit();
138: timeAllocate = System.currentTimeMillis() - timeAllocate;
139: long timeNext = System.currentTimeMillis();
140: pm.currentTransaction().begin();
141: for (int i = ADDITIONAL; i < ADDITIONAL * 2; i++) {
142: phones[i - ADDITIONAL] = new Phone();
143: phones[i - ADDITIONAL].setName("phone " + i);
144: }
145: //make persistent
146: pm.makePersistentAll(phones);
147: pm.currentTransaction().commit();
148: timeNext = System.currentTimeMillis() - timeNext;
149: assertTrue(
150: "Time with allocate should be <= to time with next. \nAllocate: "
151: + timeAllocate + "\nNext: " + timeNext,
152: timeAllocate <= timeNext);
153: } catch (Exception e) {
154: fail(e.getMessage());
155: } finally {
156: if (pm.currentTransaction().isActive())
157: pm.currentTransaction().rollback();
158: pm.close();
159: }
160: }
161:
162: /**
163: * Test a sequence which is not rdb.
164: * The id of the class is application.
165: *
166: */
167: public void testApplicationId1() {
168: logger.log(BasicLevel.DEBUG,
169: "***************testApplicationId1*****************");
170: PersistenceManager pm = pmf.getPersistenceManager();
171: pm.getObjectIdClass(Suitcase.class);
172: //get the sequence
173: Sequence s = pm.getSequence(SUITCASE_SEQ);
174: assertNotNull("Sequence " + SUITCASE_SEQ
175: + " should not be null.", s);
176: Suitcase s1 = new Suitcase();
177: s1.setColor("black");
178: Suitcase s2 = new Suitcase();
179: s2.setColor("grey");
180: logger.log(BasicLevel.DEBUG, "Value is " + s.currentValue());
181: //make persistent
182: pm.currentTransaction().begin();
183: pm.makePersistent(s1);
184: pm.makePersistent(s2);
185: pm.currentTransaction().commit();
186: assertTrue(s1.getId() < s2.getId());
187: logger.log(BasicLevel.DEBUG, "After commit, value is "
188: + s.currentValue());
189: pm.close();
190: }
191:
192: /**
193: * Get a non-rdb sequence and use it to make objects persistent.
194: * A datastore-identity has a strategy set to sequence.
195: */
196: public void testApplicationId2() {
197: logger.log(BasicLevel.DEBUG,
198: "***************testApplicationId2*****************");
199: PersistenceManager pm = pmf.getPersistenceManager();
200: pm.getObjectIdClass(CompactDisc.class);
201: //get the sequence
202: Sequence s = pm.getSequence(CD_SEQ);
203: assertNotNull("Sequence " + CD_SEQ + " should not be null.", s);
204: CompactDisc cd1 = new CompactDisc();
205: cd1.setTitle("cd1");
206: CompactDisc cd2 = new CompactDisc();
207: cd1.setTitle("cd2");
208: //make persistent
209: pm.currentTransaction().begin();
210: pm.makePersistent(cd1);
211: pm.makePersistent(cd2);
212: pm.currentTransaction().commit();
213: pm.close();
214: }
215:
216: /**
217: * Remove all the persistent instances.
218: */
219: public void testRemovingOfPersistentObject() {
220: PersistenceManager pm = pmf.getPersistenceManager();
221: try {
222: Class[] cs = new Class[] { Product.class, Phone.class,
223: Suitcase.class, CompactDisc.class };
224: pm.currentTransaction().begin();
225: for (int i = 0; i < cs.length; i++) {
226: Query query = pm.newQuery(cs[i]);
227: Collection col = (Collection) query.execute();
228: Iterator it = col.iterator();
229: while (it.hasNext()) {
230: Object o = it.next();
231: Assert.assertNotNull(
232: "null object in the query result"
233: + cs[i].getName(), o);
234: pm.deletePersistent(o);
235:
236: }
237: query.close(col);
238: }
239: pm.currentTransaction().commit();
240: } catch (JDOException e) {
241: Exception ie = ExceptionHelper.getNested(e);
242: logger.log(BasicLevel.ERROR, "", ie);
243: fail(ie.getMessage());
244: } finally {
245: pm.close();
246: }
247: }
248: }
|