KickJava   Java API By Example, From Geeks To Geeks.

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


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.filterOutOfPK.composite.A1;
42 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A21;
43 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A22;
44 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A3;
45 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.B;
46 import org.objectweb.util.monolog.api.BasicLevel;
47
48 /**
49  * Test the inheritance with a single user id and a filter out of the id.
50  * @author Y.Bersihand
51  */

52 public class TestCompositeFilterOutOfPK extends SpeedoTestHelper {
53     
54     public TestCompositeFilterOutOfPK(String JavaDoc s) {
55         super(s);
56     }
57
58     protected String JavaDoc getLoggerName() {
59         return LOG_NAME + ".rt.inheritance.TestCompositeFilterOutOfPK";
60     }
61     
62     int nbOfA1 = 3;
63     int nbOfA22 = 2;
64     int nbOfA3 = 2;
65     int nbToRetrieve = 0;
66     
67     public void testCompositeFilterOutOfPK() {
68         logger.log(BasicLevel.DEBUG, "***************testCompositeFilterOutOfPK*****************");
69         //create the forms
70
createInheritedObjects();
71         //get all the A1
72
iterateExtent(A1.class);
73         //get all the A21
74
iterateExtent(A21.class);
75         //get all the A22
76
iterateExtent(A22.class);
77         //get all the A3
78
iterateExtent(A3.class);
79         //query
80
retrieveAllXXA(1000);
81     }
82     
83     /**
84      * Create persistent objects
85      */

86     public void createInheritedObjects(){
87         PersistenceManager pm = pmf.getPersistenceManager();
88         try {
89             Collection JavaDoc col = new ArrayList JavaDoc();
90             
91             B b1 = new B(1000, "1000");
92             col.add(b1);
93             B b2 = new B(2000, "2000");
94             col.add(b2);
95             //create A1
96
A1[] a1 = new A1[nbOfA1];
97             for (int i = 0; i < a1.length; i++) {
98                 a1[i] = new A1(i+1, ""+(i+1), 1, b1);
99                 col.add(a1[i]);
100                 nbToRetrieve++;
101             }
102             //create A22
103
A22[] a22 = new A22[nbOfA22];
104             for (int i = 0; i < a22.length; i++) {
105                 a22[i] = new A22(i+1+nbOfA1, ""+(i+1+nbOfA1), 22, b1, "this is an A22");
106                 col.add(a22[i]);
107                 nbToRetrieve++;
108             }
109             //create A3
110
A3[] a3 = new A3[nbOfA3];
111             for (int i = 0; i < a3.length; i++) {
112                 a3[i] = new A3(i+1+nbOfA1+nbOfA22, ""+(i+1+nbOfA1+nbOfA22), 3, b2, true, 9);
113                 col.add(a3[i]);
114             }
115             
116             
117             // make persistent all the forms
118
pm.currentTransaction().begin();
119             pm.makePersistentAll(col);
120             pm.currentTransaction().commit();
121         } catch (Exception JavaDoc e) {
122             fail(e.getMessage());
123         } finally {
124             if (pm.currentTransaction().isActive())
125                 pm.currentTransaction().rollback();
126             //remove all the objects from the cache
127
pm.evictAll();
128             pm.close();
129         }
130     }
131
132     //iterate over all the instances of a class
133
public void iterateExtent(Class JavaDoc cl){
134         PersistenceManager pm = pmf.getPersistenceManager();
135         try {
136             int nb = 0;
137             int nbExpected = 0;
138             if (cl == A1.class) {
139                 nbExpected = nbOfA1 + nbOfA22+ nbOfA3;
140             } else if (cl == A21.class) {
141                 nbExpected = nbOfA22;
142             } else if (cl == A22.class) {
143                 nbExpected = nbOfA22;
144             } else if (cl == A3.class) {
145                 nbExpected = nbOfA3;
146             }
147             
148             pm.currentTransaction().begin();
149             Extent extent = pm.getExtent(cl, true);
150             Iterator JavaDoc it = extent.iterator();
151             String JavaDoc className = cl.getName().substring(cl.getName().lastIndexOf("."));
152             logger.log(BasicLevel.DEBUG, "All " + cl.getName() + " instances:");
153             while(it.hasNext()){
154                 A1 a = (A1) it.next();
155                 assertNotNull("The A should not be null", a);
156                 nb++;
157             }
158             extent.close(it);
159             pm.currentTransaction().commit();
160             assertEquals("The size of elements retrieved is not right.", nbExpected, nb);
161         } catch (Exception JavaDoc e) {
162             fail(e.getMessage());
163         } finally {
164             if (pm.currentTransaction().isActive())
165                 pm.currentTransaction().rollback();
166             pm.evictAll();
167             pm.close();
168         }
169     }
170     
171     public void retrieveAllXXA(int bid){
172         PersistenceManager pm = pmf.getPersistenceManager();
173         try {
174             Query query = pm.newQuery(A1.class,"b.b1==" + bid);
175             Collection JavaDoc results = (Collection JavaDoc)query.execute();
176             assertEquals(nbToRetrieve, results.size());
177             logger.log(BasicLevel.DEBUG, "All A1 linked to the " + bid + " B:");
178             Iterator JavaDoc it = results.iterator();
179             while(it.hasNext()){
180                 A1 a = (A1) it.next();
181                 assertEquals(bid, a.getB().getB1());
182             }
183             query.closeAll();
184         } catch (Exception JavaDoc e) {
185             fail(e.getMessage());
186         } finally {
187             pm.evictAll();
188             pm.close();
189         }
190     }
191     public void testRemovingOfPersistentObject() {
192         PersistenceManager pm = pmf.getPersistenceManager();
193         try {
194             Class JavaDoc[] cs = new Class JavaDoc[]{A1.class, B.class};
195             pm.currentTransaction().begin();
196             for(int i=0; i<cs.length; i++) {
197                 Query query = pm.newQuery(cs[i]);
198                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
199                 Iterator JavaDoc it = col.iterator();
200                 while(it.hasNext()) {
201                     Object JavaDoc o = it.next();
202                     Assert.assertNotNull("null object in the query result"
203                         + cs[i].getName(), o);
204                     pm.deletePersistent(o);
205
206                 }
207                 query.close(col);
208             }
209             pm.currentTransaction().commit();
210         } catch (JDOException e) {
211             Exception JavaDoc ie = ExceptionHelper.getNested(e);
212             logger.log(BasicLevel.ERROR, "", ie);
213             fail(ie.getMessage());
214         } finally {
215             pm.close();
216         }
217     }
218 }
219
Popular Tags