/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
*/
package org.objectweb.speedo.runtime.inheritance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.jdo.Extent;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import junit.framework.Assert;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.BookForm;
import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.ContactDetails;
import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.CreditCard;
import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.Form;
import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.PurchaseForm;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* Test the inheritance with a single user id and a filter out of the id.
* @author Y.Bersihand
*/
public class TestFilterOutOfPK extends SpeedoTestHelper {
public TestFilterOutOfPK(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.inheritance.TestFilterOutOfPK";
}
protected int nbToRetrieve = 0;
protected String phone = "123456789";
/**This steps describes how to:
* make persistent inherited objects
*
*/
public void testFilterOutOfPK() {
logger.log(BasicLevel.DEBUG, "***************testFilterOutOfPK*****************");
//create the forms
createInheritedObjects();
//get all the forms
iterateExtent(Form.class);
//get all the book forms
iterateExtent(BookForm.class);
//get all the purchase forms
iterateExtent(PurchaseForm.class);
retrieveAllXXForms(phone);
queryTruble("1");
}
/**
* Create persistent objects
*/
public void createInheritedObjects(){
PersistenceManager pm = pmf.getPersistenceManager();
try {
ContactDetails contactDetails1 = new ContactDetails("Jerry", "Rice", phone);
ContactDetails contactDetails2 = new ContactDetails("Ray", "Allen", "987654321");
CreditCard card1 = new CreditCard(1, "visa");
CreditCard card2 = new CreditCard(2, "american express");
BookForm bookForm1 = new BookForm(1, contactDetails1, 1);
BookForm bookForm2 = new BookForm(2, contactDetails1, 2);
BookForm bookForm3 = new BookForm(3, contactDetails1, 3);
PurchaseForm purchaseForm1 = new PurchaseForm(4, contactDetails1, 400, card1);
PurchaseForm purchaseForm2 = new PurchaseForm(5, contactDetails1, 500, card2);
BookForm bookForm4 = new BookForm(6, contactDetails2, 6);
BookForm bookForm5 = new BookForm(7, contactDetails2, 7);
Collection forms = new ArrayList();
forms.add(bookForm1);
forms.add(bookForm2);
forms.add(bookForm3);
forms.add(bookForm4);
forms.add(bookForm5);
forms.add(purchaseForm1);
forms.add(purchaseForm2);
Iterator it = forms.iterator();
nbToRetrieve = 0;
while (it.hasNext()) {
Form f = (Form) it.next();
if (f.getContactDetails().getPhoneNumber().equals(phone))
nbToRetrieve++;
}
// make persistent all the forms
pm.currentTransaction().begin();
pm.makePersistentAll(forms);
pm.currentTransaction().commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
//remove all the objects from the cache
pm.evictAll();
pm.close();
}
}
//iterate over all the instances of a class
public void iterateExtent(Class cl){
PersistenceManager pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
Extent extent = pm.getExtent(cl, true);
Iterator it = extent.iterator();
String className = cl.getName().substring(cl.getName().lastIndexOf("."));
logger.log(BasicLevel.DEBUG, "All " + cl.getName() + " instances:");
while(it.hasNext()){
Form f = (Form) it.next();
assertNotNull("The form should not be null", f);
//logger.log(BasicLevel.DEBUG, f.toString());
}
extent.close(it);
pm.currentTransaction().commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.evictAll();
pm.close();
}
}
public void retrieveAllXXForms(String phoneNumber){
PersistenceManager pm = pmf.getPersistenceManager();
try {
Query query = pm.newQuery(Form.class,"contactDetails.phoneNumber==" + phoneNumber);
Collection results = (Collection)query.execute();
assertEquals(nbToRetrieve, results.size());
logger.log(BasicLevel.DEBUG, "All " + phoneNumber + " forms:");
Iterator it = results.iterator();
while(it.hasNext()){
Form f = (Form) it.next();
assertEquals(phoneNumber, f.getContactDetails().getPhoneNumber());
}
query.closeAll();
} catch (Exception e) {
fail(e.getMessage());
} finally {
pm.evictAll();
pm.close();
}
}
//problem sent by a user
public void queryTruble(String myId) {
PersistenceManager pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
BookForm b = null;
Query query = pm.newQuery(BookForm.class, "(id == argId)");
query.declareParameters("int argId");
Integer id = Integer.valueOf(myId);
Collection results = (Collection)query.execute(id);
Iterator it = results.iterator();
if(it.hasNext()){
b = (BookForm) it.next();
logger.log(BasicLevel.DEBUG, b.getTitle());
}
query.closeAll();
pm.currentTransaction().commit();
} catch (Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
public void testRemovingOfPersistentObject() {
PersistenceManager pm = pmf.getPersistenceManager();
try {
Class[] cs = new Class[]{Form.class, CreditCard.class, ContactDetails.class};
pm.currentTransaction().begin();
for(int i=0; i<cs.length; i++) {
Query query = pm.newQuery(cs[i]);
Collection col = (Collection) query.execute();
Iterator it = col.iterator();
while(it.hasNext()) {
Object o = it.next();
Assert.assertNotNull("null object in the query result"
+ cs[i].getName(), o);
pm.deletePersistent(o);
}
query.close(col);
}
pm.currentTransaction().commit();
} catch (JDOException e) {
Exception ie = ExceptionHelper.getNested(e);
logger.log(BasicLevel.ERROR, "", ie);
fail(ie.getMessage());
} finally {
pm.close();
}
}
}