KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > sequence > id > inheritance > TestSequenceInheritance


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.inheritance;
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.inheritance.Article;
41 import org.objectweb.speedo.pobjects.sequence.id.inheritance.Materiel;
42 import org.objectweb.speedo.pobjects.sequence.id.inheritance.Service;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  * @author Y.Bersihand
47  */

48 public class TestSequenceInheritance extends SpeedoTestHelper {
49     
50     public static final int ADDITIONAL = 5;
51     
52     public TestSequenceInheritance(String JavaDoc s) {
53         super(s);
54     }
55
56     protected String JavaDoc getLoggerName() {
57         return LOG_NAME + ".rt.sequence.TestSequence";
58     }
59     
60     public static final String JavaDoc ARTICLE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.inheritance.article_seq";
61     
62     /**
63      * Get a sequence and use it to make objects persistent.
64      * A field has a value-strategy set to sequence.
65      */

66     public void testSequenceInheritance1() {
67         logger.log(BasicLevel.DEBUG, "***************testSequenceInheritance1*****************");
68         PersistenceManager pm = pmf.getPersistenceManager();
69         try {
70             pm.getObjectIdClass(Article.class);
71             // get the sequence
72
Sequence s = pm.getSequence(ARTICLE_SEQ);
73             assertNotNull("Sequence " + ARTICLE_SEQ + " should not be null.", s);
74             s.allocate(ADDITIONAL);
75             Article articles[] = new Article[ADDITIONAL];
76             for (int i = 0; i < ADDITIONAL; i++) {
77                 if (i%2 == 0) {
78                     articles[i] = new Service();
79                 } else {
80                     articles[i] = new Materiel();
81                 }
82             }
83             // make persistent
84
pm.currentTransaction().begin();
85             pm.makePersistentAll(articles);
86             pm.currentTransaction().commit();
87             for (int i = 0; i < ADDITIONAL-1; i++) {
88                 assertTrue(articles[i].getId() < articles[i+1].getId());
89             }
90         } catch (Exception JavaDoc e) {
91             fail(e.getMessage());
92         } finally {
93             if (pm.currentTransaction().isActive())
94                 pm.currentTransaction().rollback();
95             pm.close();
96         }
97     }
98     
99     /**
100      * Remove all the persistent instances.
101      */

102     public void testRemovingOfPersistentObject() {
103         PersistenceManager pm = pmf.getPersistenceManager();
104         try {
105             Class JavaDoc[] cs = new Class JavaDoc[]{Article.class};
106             pm.currentTransaction().begin();
107             for(int i=0; i<cs.length; i++) {
108                 Query query = pm.newQuery(cs[i]);
109                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
110                 Iterator JavaDoc it = col.iterator();
111                 while(it.hasNext()) {
112                     Object JavaDoc o = it.next();
113                     Assert.assertNotNull("null object in the query result"
114                         + cs[i].getName(), o);
115                     pm.deletePersistent(o);
116
117                 }
118                 query.close(col);
119             }
120             pm.currentTransaction().commit();
121         } catch (JDOException e) {
122             Exception JavaDoc ie = ExceptionHelper.getNested(e);
123             logger.log(BasicLevel.ERROR, "", ie);
124             fail(ie.getMessage());
125         } finally {
126             pm.close();
127         }
128     }
129 }
130
Popular Tags