KickJava   Java API By Example, From Geeks To Geeks.

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


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.single.BookForm;
42 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.ContactDetails;
43 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.CreditCard;
44 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.Form;
45 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.PurchaseForm;
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 TestFilterOutOfPK extends SpeedoTestHelper {
53     
54     public TestFilterOutOfPK(String JavaDoc s) {
55         super(s);
56     }
57
58     protected String JavaDoc getLoggerName() {
59         return LOG_NAME + ".rt.inheritance.TestFilterOutOfPK";
60     }
61     
62     protected int nbToRetrieve = 0;
63     protected String JavaDoc phone = "123456789";
64     
65     /**This steps describes how to:
66      * make persistent inherited objects
67      *
68      */

69     public void testFilterOutOfPK() {
70         logger.log(BasicLevel.DEBUG, "***************testFilterOutOfPK*****************");
71         //create the forms
72
createInheritedObjects();
73         //get all the forms
74
iterateExtent(Form.class);
75         //get all the book forms
76
iterateExtent(BookForm.class);
77         //get all the purchase forms
78
iterateExtent(PurchaseForm.class);
79         retrieveAllXXForms(phone);
80         queryTruble("1");
81     }
82     
83     /**
84      * Create persistent objects
85      */

86     public void createInheritedObjects(){
87         PersistenceManager pm = pmf.getPersistenceManager();
88         try {
89             ContactDetails contactDetails1 = new ContactDetails("Jerry", "Rice", phone);
90             ContactDetails contactDetails2 = new ContactDetails("Ray", "Allen", "987654321");
91         
92             CreditCard card1 = new CreditCard(1, "visa");
93             CreditCard card2 = new CreditCard(2, "american express");
94         
95             BookForm bookForm1 = new BookForm(1, contactDetails1, 1);
96             BookForm bookForm2 = new BookForm(2, contactDetails1, 2);
97             BookForm bookForm3 = new BookForm(3, contactDetails1, 3);
98             PurchaseForm purchaseForm1 = new PurchaseForm(4, contactDetails1, 400, card1);
99             PurchaseForm purchaseForm2 = new PurchaseForm(5, contactDetails1, 500, card2);
100         
101             BookForm bookForm4 = new BookForm(6, contactDetails2, 6);
102             BookForm bookForm5 = new BookForm(7, contactDetails2, 7);
103         
104             Collection JavaDoc forms = new ArrayList JavaDoc();
105             forms.add(bookForm1);
106             forms.add(bookForm2);
107             forms.add(bookForm3);
108             forms.add(bookForm4);
109             forms.add(bookForm5);
110             forms.add(purchaseForm1);
111             forms.add(purchaseForm2);
112         
113             Iterator JavaDoc it = forms.iterator();
114             nbToRetrieve = 0;
115             while (it.hasNext()) {
116                 Form f = (Form) it.next();
117                 if (f.getContactDetails().getPhoneNumber().equals(phone))
118                     nbToRetrieve++;
119             }
120             // make persistent all the forms
121
pm.currentTransaction().begin();
122             pm.makePersistentAll(forms);
123             pm.currentTransaction().commit();
124         } catch (Exception JavaDoc e) {
125             fail(e.getMessage());
126         } finally {
127             if (pm.currentTransaction().isActive())
128                 pm.currentTransaction().rollback();
129             //remove all the objects from the cache
130
pm.evictAll();
131             pm.close();
132         }
133     }
134
135     //iterate over all the instances of a class
136
public void iterateExtent(Class JavaDoc cl){
137         PersistenceManager pm = pmf.getPersistenceManager();
138         try {
139             pm.currentTransaction().begin();
140             Extent extent = pm.getExtent(cl, true);
141             Iterator JavaDoc it = extent.iterator();
142             String JavaDoc className = cl.getName().substring(cl.getName().lastIndexOf("."));
143             logger.log(BasicLevel.DEBUG, "All " + cl.getName() + " instances:");
144             while(it.hasNext()){
145                 Form f = (Form) it.next();
146                 assertNotNull("The form should not be null", f);
147                 //logger.log(BasicLevel.DEBUG, f.toString());
148
}
149             extent.close(it);
150             pm.currentTransaction().commit();
151         } catch (Exception JavaDoc e) {
152             fail(e.getMessage());
153         } finally {
154             if (pm.currentTransaction().isActive())
155                 pm.currentTransaction().rollback();
156             pm.evictAll();
157             pm.close();
158         }
159     }
160     
161     public void retrieveAllXXForms(String JavaDoc phoneNumber){
162         PersistenceManager pm = pmf.getPersistenceManager();
163         try {
164             Query query = pm.newQuery(Form.class,"contactDetails.phoneNumber==" + phoneNumber);
165             Collection JavaDoc results = (Collection JavaDoc)query.execute();
166             assertEquals(nbToRetrieve, results.size());
167             logger.log(BasicLevel.DEBUG, "All " + phoneNumber + " forms:");
168             Iterator JavaDoc it = results.iterator();
169             while(it.hasNext()){
170                 Form f = (Form) it.next();
171                 assertEquals(phoneNumber, f.getContactDetails().getPhoneNumber());
172             }
173             query.closeAll();
174         } catch (Exception JavaDoc e) {
175             fail(e.getMessage());
176         } finally {
177             pm.evictAll();
178             pm.close();
179         }
180     }
181     
182     //problem sent by a user
183
public void queryTruble(String JavaDoc myId) {
184         PersistenceManager pm = pmf.getPersistenceManager();
185         try {
186             pm.currentTransaction().begin();
187         
188             BookForm b = null;
189             Query query = pm.newQuery(BookForm.class, "(id == argId)");
190             query.declareParameters("int argId");
191             Integer JavaDoc id = Integer.valueOf(myId);
192             Collection JavaDoc results = (Collection JavaDoc)query.execute(id);
193             Iterator JavaDoc it = results.iterator();
194             if(it.hasNext()){
195                 b = (BookForm) it.next();
196                 logger.log(BasicLevel.DEBUG, b.getTitle());
197             }
198             query.closeAll();
199             pm.currentTransaction().commit();
200         } catch (Exception JavaDoc e) {
201             if (pm.currentTransaction().isActive())
202                 pm.currentTransaction().rollback();
203             fail(e.getMessage());
204         } finally {
205             pm.close();
206         }
207     }
208     
209     public void testRemovingOfPersistentObject() {
210         PersistenceManager pm = pmf.getPersistenceManager();
211         try {
212             Class JavaDoc[] cs = new Class JavaDoc[]{Form.class, CreditCard.class, ContactDetails.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