/**
* 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.detach;
import java.util.Collection;
import java.util.Iterator;
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.detach.Address;
import org.objectweb.speedo.pobjects.detach.Person;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* @author Y.Bersihand
*/
public class TestSwiss extends SpeedoTestHelper {
public TestSwiss(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.detach.TestSwiss";
}
/**
* Test the detach method
*/
public void testDetachManyTimes() {
try {
long personId = createPersonLong();
Person detached1 = getPersonById(personId);
logger.log(BasicLevel.DEBUG, "detached1 address: " + detached1.getAddress().getType());
Person detached2 = getPersonById(personId);
logger.log(BasicLevel.DEBUG, "detached2 address: " + detached2.getAddress().getType());
assertEquals(detached1.getId(), detached2.getId());
assertEquals(detached1.getAddress().getType(), detached2.getAddress().getType());
} catch (Exception e) {
e.printStackTrace();
}
}
public void testRemovingOfPersistentObject() {
PersistenceManager pm = pmf.getPersistenceManager();
try {
Class[] cs = new Class[]{Address.class, Person.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();
}
}
/*----------------- PRIVATE METHODS----------------------------*/
private Object createPerson(){
logger.log(BasicLevel.DEBUG, "***************createPerson Object*****************");
Address address = new Address("mail");
Person person = new Person(address);
PersistenceManager pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
pm.makePersistent(person);
pm.currentTransaction().commit();
return pm.getObjectId(person);
} catch (Exception e) {
fail(e.getMessage());
return null;
} finally {
pm.close();
}
}
private long createPersonLong(){
logger.log(BasicLevel.DEBUG, "***************createPersonLong*****************");
Address address = new Address("mail");
Person person = new Person(address);
PersistenceManager pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
pm.makePersistent(person);
pm.currentTransaction().commit();
return person.getId();
} catch (Exception e) {
fail(e.getMessage());
return -1;
} finally {
pm.close();
}
}
private Person getPersonById(Object personId) {
logger.log(BasicLevel.DEBUG, "***************getPersonById*****************");
Person result = null;
PersistenceManager pm = pmf.getPersistenceManager();
try {
//Object oid = pm.newObjectIdInstance(Person.class, "" + personId);
Person p = (Person) pm.getObjectById(personId, false);
if (p != null) {
p.getAddress();
result = (Person) pm.detachCopy(p);
}
} catch (Exception e) {
logger.log(BasicLevel.DEBUG, "Person with Key=" + personId + " not found.");
} finally {
pm.close();
}
return result;
}
private Person getPersonById(long personId) {
logger.log(BasicLevel.DEBUG, "***************getPersonById*****************");
Person result = null;
PersistenceManager pm = pmf.getPersistenceManager();
try {
Object oid = pm.newObjectIdInstance(Person.class, "" + personId);
Person p = (Person) pm.getObjectById(oid, false);
if (p != null) {
p.getAddress();
result = (Person) pm.detachCopy(p);
}
} catch (Exception e) {
logger.log(BasicLevel.DEBUG, "Person with Key=" + personId + " not found.");
} finally {
pm.close();
}
return result;
}
}