001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.runtime.inheritance;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.jdo.Extent;
031: import javax.jdo.JDOException;
032: import javax.jdo.PersistenceManager;
033: import javax.jdo.Query;
034:
035: import junit.framework.Assert;
036:
037: import org.objectweb.speedo.SpeedoTestHelper;
038: import org.objectweb.speedo.api.ExceptionHelper;
039: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.BookForm;
040: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.ContactDetails;
041: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.CreditCard;
042: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.Form;
043: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.PurchaseForm;
044: import org.objectweb.util.monolog.api.BasicLevel;
045:
046: /**
047: * Test the inheritance with a single user id and a filter out of the id.
048: * @author Y.Bersihand
049: */
050: public class TestFilterOutOfPK extends SpeedoTestHelper {
051:
052: public TestFilterOutOfPK(String s) {
053: super (s);
054: }
055:
056: protected String getLoggerName() {
057: return LOG_NAME + ".rt.inheritance.TestFilterOutOfPK";
058: }
059:
060: protected int nbToRetrieve = 0;
061: protected String phone = "123456789";
062:
063: /**This steps describes how to:
064: * make persistent inherited objects
065: *
066: */
067: public void testFilterOutOfPK() {
068: logger.log(BasicLevel.DEBUG,
069: "***************testFilterOutOfPK*****************");
070: //create the forms
071: createInheritedObjects();
072: //get all the forms
073: iterateExtent(Form.class);
074: //get all the book forms
075: iterateExtent(BookForm.class);
076: //get all the purchase forms
077: iterateExtent(PurchaseForm.class);
078: retrieveAllXXForms(phone);
079: queryTruble("1");
080: }
081:
082: /**
083: * Create persistent objects
084: */
085: public void createInheritedObjects() {
086: PersistenceManager pm = pmf.getPersistenceManager();
087: try {
088: ContactDetails contactDetails1 = new ContactDetails(
089: "Jerry", "Rice", phone);
090: ContactDetails contactDetails2 = new ContactDetails("Ray",
091: "Allen", "987654321");
092:
093: CreditCard card1 = new CreditCard(1, "visa");
094: CreditCard card2 = new CreditCard(2, "american express");
095:
096: BookForm bookForm1 = new BookForm(1, contactDetails1, 1);
097: BookForm bookForm2 = new BookForm(2, contactDetails1, 2);
098: BookForm bookForm3 = new BookForm(3, contactDetails1, 3);
099: PurchaseForm purchaseForm1 = new PurchaseForm(4,
100: contactDetails1, 400, card1);
101: PurchaseForm purchaseForm2 = new PurchaseForm(5,
102: contactDetails1, 500, card2);
103:
104: BookForm bookForm4 = new BookForm(6, contactDetails2, 6);
105: BookForm bookForm5 = new BookForm(7, contactDetails2, 7);
106:
107: Collection forms = new ArrayList();
108: forms.add(bookForm1);
109: forms.add(bookForm2);
110: forms.add(bookForm3);
111: forms.add(bookForm4);
112: forms.add(bookForm5);
113: forms.add(purchaseForm1);
114: forms.add(purchaseForm2);
115:
116: Iterator it = forms.iterator();
117: nbToRetrieve = 0;
118: while (it.hasNext()) {
119: Form f = (Form) it.next();
120: if (f.getContactDetails().getPhoneNumber()
121: .equals(phone))
122: nbToRetrieve++;
123: }
124: // make persistent all the forms
125: pm.currentTransaction().begin();
126: pm.makePersistentAll(forms);
127: pm.currentTransaction().commit();
128: } catch (Exception e) {
129: fail(e.getMessage());
130: } finally {
131: if (pm.currentTransaction().isActive())
132: pm.currentTransaction().rollback();
133: //remove all the objects from the cache
134: pm.evictAll();
135: pm.close();
136: }
137: }
138:
139: //iterate over all the instances of a class
140: public void iterateExtent(Class cl) {
141: PersistenceManager pm = pmf.getPersistenceManager();
142: try {
143: pm.currentTransaction().begin();
144: Extent extent = pm.getExtent(cl, true);
145: Iterator it = extent.iterator();
146: String className = cl.getName().substring(
147: cl.getName().lastIndexOf("."));
148: logger.log(BasicLevel.DEBUG, "All " + cl.getName()
149: + " instances:");
150: while (it.hasNext()) {
151: Form f = (Form) it.next();
152: assertNotNull("The form should not be null", f);
153: //logger.log(BasicLevel.DEBUG, f.toString());
154: }
155: extent.close(it);
156: pm.currentTransaction().commit();
157: } catch (Exception e) {
158: fail(e.getMessage());
159: } finally {
160: if (pm.currentTransaction().isActive())
161: pm.currentTransaction().rollback();
162: pm.evictAll();
163: pm.close();
164: }
165: }
166:
167: public void retrieveAllXXForms(String phoneNumber) {
168: PersistenceManager pm = pmf.getPersistenceManager();
169: try {
170: Query query = pm.newQuery(Form.class,
171: "contactDetails.phoneNumber==" + phoneNumber);
172: Collection results = (Collection) query.execute();
173: assertEquals(nbToRetrieve, results.size());
174: logger.log(BasicLevel.DEBUG, "All " + phoneNumber
175: + " forms:");
176: Iterator it = results.iterator();
177: while (it.hasNext()) {
178: Form f = (Form) it.next();
179: assertEquals(phoneNumber, f.getContactDetails()
180: .getPhoneNumber());
181: }
182: query.closeAll();
183: } catch (Exception e) {
184: fail(e.getMessage());
185: } finally {
186: pm.evictAll();
187: pm.close();
188: }
189: }
190:
191: //problem sent by a user
192: public void queryTruble(String myId) {
193: PersistenceManager pm = pmf.getPersistenceManager();
194: try {
195: pm.currentTransaction().begin();
196:
197: BookForm b = null;
198: Query query = pm.newQuery(BookForm.class, "(id == argId)");
199: query.declareParameters("int argId");
200: Integer id = Integer.valueOf(myId);
201: Collection results = (Collection) query.execute(id);
202: Iterator it = results.iterator();
203: if (it.hasNext()) {
204: b = (BookForm) it.next();
205: logger.log(BasicLevel.DEBUG, b.getTitle());
206: }
207: query.closeAll();
208: pm.currentTransaction().commit();
209: } catch (Exception e) {
210: if (pm.currentTransaction().isActive())
211: pm.currentTransaction().rollback();
212: fail(e.getMessage());
213: } finally {
214: pm.close();
215: }
216: }
217:
218: public void testRemovingOfPersistentObject() {
219: PersistenceManager pm = pmf.getPersistenceManager();
220: try {
221: Class[] cs = new Class[] { Form.class, CreditCard.class,
222: ContactDetails.class };
223: pm.currentTransaction().begin();
224: for (int i = 0; i < cs.length; i++) {
225: Query query = pm.newQuery(cs[i]);
226: Collection col = (Collection) query.execute();
227: Iterator it = col.iterator();
228: while (it.hasNext()) {
229: Object o = it.next();
230: Assert.assertNotNull(
231: "null object in the query result"
232: + cs[i].getName(), o);
233: pm.deletePersistent(o);
234:
235: }
236: query.close(col);
237: }
238: pm.currentTransaction().commit();
239: } catch (JDOException e) {
240: Exception ie = ExceptionHelper.getNested(e);
241: logger.log(BasicLevel.ERROR, "", ie);
242: fail(ie.getMessage());
243: } finally {
244: pm.close();
245: }
246: }
247: }
|