KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > sequence > id > speedoExtension > TestSequenceSE


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.speedoExtension;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.jdo.JDOException;
33 import javax.jdo.PersistenceManager;
34 import javax.jdo.Query;
35 import javax.jdo.datastore.Sequence;
36
37 import junit.framework.Assert;
38
39 import org.objectweb.speedo.SpeedoTestHelper;
40 import org.objectweb.speedo.api.ExceptionHelper;
41 import org.objectweb.speedo.pobjects.sequence.id.speedoExtension.Card;
42 import org.objectweb.speedo.pobjects.sequence.id.speedoExtension.Maps;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  * Test the sequence defined as speedo extension.
47  * 2 cases:
48  * Card: id field to map the sequence
49  * Map: the id is an hidden field
50  * @author Y.Bersihand
51  */

52 public class TestSequenceSE extends SpeedoTestHelper {
53     
54     public TestSequenceSE(String JavaDoc s) {
55         super(s);
56     }
57
58     protected String JavaDoc getLoggerName() {
59         return LOG_NAME + ".rt.sequence.TestSequence";
60     }
61     
62     public static final String JavaDoc CARD_SEQ = "org.objectweb.speedo.pobjects.sequence.id.speedoExtension.card_sequence";
63     public static final String JavaDoc MAP_SEQ = "org.objectweb.speedo.pobjects.sequence.id.speedoExtension.map_sequence";
64     
65     /**
66      * Get a sequence and use it to make objects persistent.
67      */

68     public void testSequenceId() {
69         logger.log(BasicLevel.DEBUG, "***************testSequenceId*****************");
70         PersistenceManager pm = pmf.getPersistenceManager();
71         pm.getObjectIdClass(Card.class);
72         //get the sequence
73
Sequence s = pm.getSequence(CARD_SEQ);
74         assertNotNull("Sequence " + CARD_SEQ + " should not be null.", s);
75         Card c1 = new Card();
76         c1.setName("card 1");
77         Card c2 = new Card();
78         c2.setName("card 2");
79         
80         Collection JavaDoc c = new ArrayList JavaDoc();
81         c.add(c1);
82         c.add(c2);
83         //make persistent
84
pm.currentTransaction().begin();
85         pm.makePersistentAll(c);
86         pm.currentTransaction().commit();
87         assertTrue(c1.getId() < c2.getId());
88         pm.close();
89     }
90     
91     /**
92      * Get a sequence and use it to make objects persistent.
93      */

94     public void testSequenceHiddenField() {
95         logger.log(BasicLevel.DEBUG, "***************testSequenceHiddenField*****************");
96         PersistenceManager pm = pmf.getPersistenceManager();
97         pm.getObjectIdClass(Maps.class);
98         //get the sequence
99
Sequence s = pm.getSequence(MAP_SEQ);
100         assertNotNull("Sequence " + MAP_SEQ + " should not be null.", s);
101         Maps m1 = new Maps();
102         m1.setName("map 1");
103         Maps m2 = new Maps();
104         m2.setName("map 2");
105         
106         Collection JavaDoc c = new ArrayList JavaDoc();
107         c.add(m1);
108         c.add(m2);
109         //make persistent
110
pm.currentTransaction().begin();
111         pm.makePersistentAll(c);
112         pm.currentTransaction().commit();
113         pm.close();
114     }
115     
116     /**
117      * Remove all the persistent instances.
118      */

119     public void _testRemovingOfPersistentObject() {
120         PersistenceManager pm = pmf.getPersistenceManager();
121         try {
122             Class JavaDoc[] cs = new Class JavaDoc[]{Maps.class, Card.class};
123             pm.currentTransaction().begin();
124             for(int i=0; i<cs.length; i++) {
125                 Query query = pm.newQuery(cs[i]);
126                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
127                 Iterator JavaDoc it = col.iterator();
128                 while(it.hasNext()) {
129                     Object JavaDoc o = it.next();
130                     Assert.assertNotNull("null object in the query result"
131                         + cs[i].getName(), o);
132                     pm.deletePersistent(o);
133
134                 }
135                 query.close(col);
136             }
137             pm.currentTransaction().commit();
138         } catch (JDOException e) {
139             Exception JavaDoc ie = ExceptionHelper.getNested(e);
140             logger.log(BasicLevel.ERROR, "", ie);
141             fail(ie.getMessage());
142         } finally {
143             pm.close();
144         }
145     }
146 }
147
Popular Tags