KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > sequence > id > TestSequenceId


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.id;
27
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.jdo.JDOException;
32 import javax.jdo.PersistenceManager;
33 import javax.jdo.Query;
34 import javax.jdo.datastore.Sequence;
35
36 import junit.framework.Assert;
37
38 import org.objectweb.speedo.SpeedoTestHelper;
39 import org.objectweb.speedo.api.ExceptionHelper;
40 import org.objectweb.speedo.pobjects.sequence.id.CompactDisc;
41 import org.objectweb.speedo.pobjects.sequence.id.Phone;
42 import org.objectweb.speedo.pobjects.sequence.id.Product;
43 import org.objectweb.speedo.pobjects.sequence.id.Suitcase;
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 /**
47  * @author Y.Bersihand
48  */

49 public class TestSequenceId extends SpeedoTestHelper {
50     
51     public static final int ADDITIONAL = 100;
52     
53     public TestSequenceId(String JavaDoc s) {
54         super(s);
55     }
56
57     protected String JavaDoc getLoggerName() {
58         return LOG_NAME + ".rt.sequence.TestSequence";
59     }
60     
61     public static final String JavaDoc PRODUCT_SEQ = "org.objectweb.speedo.pobjects.sequence.id.product_seq";
62     public static final String JavaDoc PHONE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.phone_seq";
63     public static final String JavaDoc SUITCASE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.suitcase_seq";
64     public static final String JavaDoc CD_SEQ = "org.objectweb.speedo.pobjects.sequence.id.cd_seq";
65     
66     /**
67      * Get a sequence and use it to make objects persistent.
68      * A field has a value-strategy set to sequence.
69      */

70     public void testSequenceId1() {
71         logger.log(BasicLevel.DEBUG, "***************testSequenceId1*****************");
72         PersistenceManager pm = pmf.getPersistenceManager();
73         pm.getObjectIdClass(Product.class);
74         //get the sequence
75
Sequence s = pm.getSequence(PRODUCT_SEQ);
76         assertNotNull("Sequence " + PRODUCT_SEQ + " should not be null.", s);
77         Product p1 = new Product();
78         p1.setName("product 1");
79         Product p2 = new Product();
80         p2.setName("product 2");
81         //make persistent
82
pm.currentTransaction().begin();
83         pm.makePersistent(p1);
84         pm.makePersistent(p2);
85         pm.currentTransaction().commit();
86         assertTrue(p1.getReference() < p2.getReference());
87         pm.close();
88     }
89     
90     /**
91      * Get a sequence and use it to make objects persistent.
92      * A datastore-identity has a strategy set to sequence.
93      */

94     public void testSequenceId2() {
95         logger.log(BasicLevel.DEBUG, "***************testSequenceId2*****************");
96         PersistenceManager pm = pmf.getPersistenceManager();
97         pm.getObjectIdClass(Phone.class);
98         //get the sequence
99
Sequence s = pm.getSequence(PHONE_SEQ);
100         assertNotNull("Sequence " + PHONE_SEQ + " should not be null.", s);
101         Phone p1 = new Phone();
102         p1.setName("phone 1");
103         Phone p2 = new Phone();
104         p2.setName("phone 2");
105         //make persistent
106
pm.currentTransaction().begin();
107         pm.makePersistent(p1);
108         pm.makePersistent(p2);
109         pm.currentTransaction().commit();
110         pm.close();
111     }
112     
113     public void testAllocateRdbSequence() {
114         logger.log(BasicLevel.DEBUG, "***************testAllocateRdbSequence*****************");
115         PersistenceManager pm = pmf.getPersistenceManager();
116         try {
117             pm.getObjectIdClass(Phone.class);
118             //get the sequence
119
Sequence s = pm.getSequence(PHONE_SEQ);
120             assertNotNull("Sequence " + PHONE_SEQ + " should not be null.", s);
121             Phone[] phones = new Phone[ADDITIONAL];
122             long timeAllocate = System.currentTimeMillis();
123             pm.currentTransaction().begin();
124             //allocate
125
s.allocate(ADDITIONAL);
126             for (int i = 0; i < ADDITIONAL ; i++) {
127                 phones[i] = new Phone();
128                 phones[i].setName("phone " + i);
129             }
130             //make persistent
131
pm.makePersistentAll(phones);
132             pm.currentTransaction().commit();
133             timeAllocate = System.currentTimeMillis() - timeAllocate;
134             long timeNext = System.currentTimeMillis();
135             pm.currentTransaction().begin();
136             for (int i = ADDITIONAL; i < ADDITIONAL*2 ; i++) {
137                 phones[i - ADDITIONAL] = new Phone();
138                 phones[i - ADDITIONAL].setName("phone " + i);
139             }
140             //make persistent
141
pm.makePersistentAll(phones);
142             pm.currentTransaction().commit();
143             timeNext = System.currentTimeMillis() - timeNext;
144             assertTrue("Time with allocate should be <= to time with next. \nAllocate: " + timeAllocate
145                     + "\nNext: " + timeNext, timeAllocate <= timeNext);
146         } catch (Exception JavaDoc e) {
147             fail(e.getMessage());
148         } finally {
149             if (pm.currentTransaction().isActive())
150                 pm.currentTransaction().rollback();
151             pm.close();
152         }
153     }
154     
155     /**
156      * Test a sequence which is not rdb.
157      * The id of the class is application.
158      *
159      */

160     public void testApplicationId1() {
161         logger.log(BasicLevel.DEBUG, "***************testApplicationId1*****************");
162         PersistenceManager pm = pmf.getPersistenceManager();
163         pm.getObjectIdClass(Suitcase.class);
164         //get the sequence
165
Sequence s = pm.getSequence(SUITCASE_SEQ);
166         assertNotNull("Sequence " + SUITCASE_SEQ + " should not be null.", s);
167         Suitcase s1 = new Suitcase();
168         s1.setColor("black");
169         Suitcase s2 = new Suitcase();
170         s2.setColor("grey");
171         //make persistent
172
pm.currentTransaction().begin();
173         pm.makePersistent(s1);
174         pm.makePersistent(s2);
175         pm.currentTransaction().commit();
176         assertTrue(s1.getId() < s2.getId());
177         pm.close();
178     }
179     
180     /**
181      * Get a non-rdb sequence and use it to make objects persistent.
182      * A datastore-identity has a strategy set to sequence.
183      */

184     public void testApplicationId2() {
185         logger.log(BasicLevel.DEBUG, "***************testApplicationId2*****************");
186         PersistenceManager pm = pmf.getPersistenceManager();
187         pm.getObjectIdClass(CompactDisc.class);
188         //get the sequence
189
Sequence s = pm.getSequence(CD_SEQ);
190         assertNotNull("Sequence " + CD_SEQ + " should not be null.", s);
191         CompactDisc cd1 = new CompactDisc();
192         cd1.setTitle("cd1");
193         CompactDisc cd2 = new CompactDisc();
194         cd1.setTitle("cd2");
195         //make persistent
196
pm.currentTransaction().begin();
197         pm.makePersistent(cd1);
198         pm.makePersistent(cd2);
199         pm.currentTransaction().commit();
200         pm.close();
201     }
202     
203     /**
204      * Remove all the persistent instances.
205      */

206     public void testRemovingOfPersistentObject() {
207         PersistenceManager pm = pmf.getPersistenceManager();
208         try {
209             Class JavaDoc[] cs = new Class JavaDoc[]{Product.class,
210                                     Phone.class,
211                                     Suitcase.class,
212                                     CompactDisc.class};
213             pm.currentTransaction().begin();
214             for(int i=0; i<cs.length; i++) {
215                 Query query = pm.newQuery(cs[i]);
216                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
217                 Iterator JavaDoc it = col.iterator();
218                 while(it.hasNext()) {
219                     Object JavaDoc o = it.next();
220                     Assert.assertNotNull("null object in the query result"
221                         + cs[i].getName(), o);
222                     pm.deletePersistent(o);
223
224                 }
225                 query.close(col);
226             }
227             pm.currentTransaction().commit();
228         } catch (JDOException e) {
229             Exception JavaDoc ie = ExceptionHelper.getNested(e);
230             logger.log(BasicLevel.ERROR, "", ie);
231             fail(ie.getMessage());
232         } finally {
233             pm.close();
234         }
235     }
236 }
237
Popular Tags