KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > inheritance > TestIndex


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.inheritance;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.jdo.Extent;
33 import javax.jdo.JDOException;
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.Query;
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.inheritance.index.Daughter;
42 import org.objectweb.speedo.pobjects.inheritance.index.Mother;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  * Test the queries and extent with prefetch in case of inheritance.
47  * The aim is to have coherent order between java fields and sql columns.
48  * @author Y.Bersihand
49  */

50 public class TestIndex extends SpeedoTestHelper {
51     
52     public TestIndex(String JavaDoc s) {
53         super(s);
54     }
55
56     protected String JavaDoc getLoggerName() {
57         return LOG_NAME + ".rt.inheritance.TestIndex";
58     }
59
60     /**
61      * create the objects
62      */

63     public void testCreate() {
64         PersistenceManager pm = pmf.getPersistenceManager();
65         try {
66             Mother m1 = new Mother(1,"mother1", true);
67             Mother m2 = new Mother(2,"mother2", true);
68             Daughter d1 = new Daughter(3,"daughter1", false, new Long JavaDoc(3));
69             Daughter d2 = new Daughter(4,"daughter2", false, new Long JavaDoc(4));
70             Collection JavaDoc col = new ArrayList JavaDoc();
71             col.add(m1);
72             col.add(m2);
73             col.add(d1);
74             col.add(d2);
75             // make persistent all the objects
76
pm.currentTransaction().begin();
77             pm.makePersistentAll(col);
78             pm.currentTransaction().commit();
79         } catch (Exception JavaDoc e) {
80             fail(e.getMessage());
81         } finally {
82             if (pm.currentTransaction().isActive())
83                 pm.currentTransaction().rollback();
84             // remove all the objects from the cache
85
pm.evictAll();
86             pm.close();
87         }
88     }
89
90     /**
91      * query: select super
92      */

93     public void testSelectSuper() {
94         PersistenceManager pm = pmf.getPersistenceManager();
95         try {
96             Query query = pm.newQuery(Mother.class);
97             String JavaDoc filter = "java1 > 0";
98             query.setFilter(filter);
99             Collection JavaDoc result = (Collection JavaDoc) query.execute();
100             assertNotNull(result);
101             Iterator JavaDoc it = result.iterator();
102             while (it.hasNext()) {
103                 Mother m = (Mother) it.next();
104                 assertTrue("Field java1 should be superior to 0.", m.getJava1()>0);
105             }
106         } catch (Exception JavaDoc e) {
107             fail(e.getMessage());
108         } finally {
109             pm.evictAll();
110             pm.close();
111         }
112     }
113     
114     /**
115      * query: select subclass
116      */

117     public void testSelectSubclass() {
118         PersistenceManager pm = pmf.getPersistenceManager();
119         try {
120             Query query = pm.newQuery(Daughter.class);
121             String JavaDoc filter = "java1 > 0";
122             query.setFilter(filter);
123             Collection JavaDoc result = (Collection JavaDoc) query.execute();
124             assertNotNull(result);
125             Iterator JavaDoc it = result.iterator();
126             while (it.hasNext()) {
127                 Mother m = (Mother) it.next();
128                 assertTrue("Field java1 should be superior to 0.", m.getJava1()>0);
129             }
130         } catch (Exception JavaDoc e) {
131             fail(e.getMessage());
132         } finally {
133             pm.evictAll();
134             pm.close();
135         }
136     }
137     
138     /**
139      * extent: super with subclasses
140      */

141     public void testExtentSuperWithSubclasses() {
142         computeExtent(Mother.class, true);
143     }
144     
145     /**
146      * extent: super without subclasses
147      */

148     public void testExtentSuperWithoutSubclasses() {
149         computeExtent(Mother.class, false);
150     }
151     
152     /**
153      * extent: subclass with subclasses
154      */

155     public void testExtentSubWithSubclasses() {
156         computeExtent(Daughter.class, true);
157     }
158     
159     /**
160      * extent: subclass without subclasses
161      */

162     public void testExtentSubWithoutSubclasses() {
163         computeExtent(Daughter.class, false);
164     }
165     
166     /**
167      * extent
168      */

169     public void computeExtent(Class JavaDoc cl, boolean withSubclasses) {
170         PersistenceManager pm = pmf.getPersistenceManager();
171         try {
172             Extent extent = pm.getExtent(cl, withSubclasses);
173             Iterator JavaDoc it = extent.iterator();
174             while(it.hasNext()) {
175                 Mother m = (Mother) it.next();
176                 assertTrue(m.getJava1()>0);
177             }
178         } catch (Exception JavaDoc e) {
179             fail(e.getMessage());
180         } finally {
181             pm.evictAll();
182             pm.close();
183         }
184     }
185     /**
186      * Remove the objects from the database.
187      *
188      */

189     public void testRemovingOfPersistentObject() {
190         PersistenceManager pm = pmf.getPersistenceManager();
191         try {
192             Class JavaDoc[] cs = new Class JavaDoc[]{Mother.class};
193             pm.currentTransaction().begin();
194             for(int i=0; i<cs.length; i++) {
195                 Query query = pm.newQuery(cs[i]);
196                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
197                 Iterator JavaDoc it = col.iterator();
198                 while(it.hasNext()) {
199                     Object JavaDoc o = it.next();
200                     Assert.assertNotNull("null object in the query result"
201                         + cs[i].getName(), o);
202                     pm.deletePersistent(o);
203
204                 }
205                 query.close(col);
206             }
207             pm.currentTransaction().commit();
208         } catch (JDOException e) {
209             Exception JavaDoc ie = ExceptionHelper.getNested(e);
210             logger.log(BasicLevel.ERROR, "", ie);
211             fail(ie.getMessage());
212         } finally {
213             pm.close();
214         }
215     }
216 }
217
Popular Tags