/**
* 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.ArrayList;
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.mim.jdo.api.JDOPersistentObjectItf;
import org.objectweb.speedo.pobjects.detach.Car;
import org.objectweb.speedo.pobjects.detach.Coach;
import org.objectweb.speedo.pobjects.detach.FormulaOne;
import org.objectweb.speedo.pobjects.detach.Player;
import org.objectweb.speedo.pobjects.detach.Team;
import org.objectweb.speedo.pobjects.detach.Vehicle;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* @author Y.Bersihand
*/
public class TestAttach extends SpeedoTestHelper {
public TestAttach(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.detach.TestAttach";
}
/**
* Test the attach method: make an object persistent, detach it then attach it.
*/
public void testAttachCopy() {
logger.log(BasicLevel.DEBUG, "***************testAttachCopy*****************");
Team t = new Team("Bordeaux",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p1", t, 25);
players.add(p1);
Player p2 = new Player("p2", t, 32);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c1", 5, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
pm.makePersistent(t);
pm.currentTransaction().commit();
try {
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
assertNotNull(copyOfT);
//print the team out
logger.log(BasicLevel.DEBUG, copyOfT.toString());
pm.currentTransaction().begin();
//attach the team t
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
assertEquals(copyOfT.getTown(), attachedTeam.getTown());
assertEquals(copyOfT.getCoach().getExperience(), attachedTeam.getCoach().getExperience());
assertEquals(copyOfT.getCoach().getName(), attachedTeam.getCoach().getName());
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method with update: make an object persistent, detach it , modify it and then attach it.
*/
public void testAttachModifiedCopy() {
logger.log(BasicLevel.DEBUG, "***************testAttachModifiedCopy*****************");
Team t = new Team("Paris",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p3", t, 20);
players.add(p1);
Player p2 = new Player("p4", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c2", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
pm.makePersistent(t);
pm.currentTransaction().commit();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//modify the team
copyOfT.getCoach().setExperience(99);
//print the team out
logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString());
pm.currentTransaction().begin();
//attach the team t
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
try {
assertNotNull(attachedTeam);
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
assertEquals(99, attachedTeam.getCoach().getExperience());
pm.currentTransaction().commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method with an update on a reference: make an object persistent, detach it , modify it and then attach it.
*/
public void testAttachModifiedReference() {
logger.log(BasicLevel.DEBUG, "***************testAttachModifiedReference*****************");
Team t = new Team("Lens",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p21", t, 20);
players.add(p1);
Player p2 = new Player("p22", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c23", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
pm.makePersistent(t);
pm.currentTransaction().commit();
try {
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
Coach newCoach = new Coach("c33", 15, new Team("DummyTeam",null,null));
// update the reference while detached
copyOfT.setCoach(newCoach);
//print the team out
logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString());
pm.currentTransaction().begin();
// attach the team t
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
assertNotNull("attachedTeam should not be null", attachedTeam);
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
assertEquals("Coach of the attached team is not the good one", newCoach, attachedTeam.getCoach());
pm.currentTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method with update on a collection: make an object persistent, detach it , modify it and then attach it.
*/
public void testAttachModifiedCollectionCopy() {
logger.log(BasicLevel.DEBUG, "***************testAttachModifiedCollectionCopy*****************");
Team t = new Team("Nantes",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p5", t, 20);
players.add(p1);
Player p2 = new Player("p6", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c3", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
pm.makePersistent(t);
pm.currentTransaction().commit();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
pm.close();
//modify the coach
copyOfT.getCoach().setExperience(99);
//and modify the collection
Iterator it = copyOfT.getPlayers().iterator();
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
// print the team out
logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString());
PersistenceManager pm2 = pmf.getPersistenceManager();
try {
pm2.currentTransaction().begin();
//attach the team t
Team attachedTeam = (Team) pm2.makePersistent(copyOfT);
Iterator itP = attachedTeam.getPlayers().iterator();
while(itP.hasNext()){
Player p = (Player) itP.next();
assertEquals(99, p.getAge());
}
pm2.currentTransaction().commit();
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
if (pm2.currentTransaction().isActive())
pm2.currentTransaction().rollback();
pm2.close();
}
}
/**
* Test the attach method with a new object: the object is made persistent.
*/
public void testAttachNewObject() {
logger.log(BasicLevel.DEBUG, "***************testAttachNewObject*****************");
Team t = new Team("Bastia",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p7", t, 20);
players.add(p1);
Player p2 = new Player("p8", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c4", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "attach the team " + t.toString());
Team attachedTeam = (Team) pm.makePersistent(t);
pm.currentTransaction().commit();
try {
assertTrue( ((JDOPersistentObjectItf) attachedTeam).jdoIsPersistent());
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method with an object having a null reference.
*/
public void testAttachNullRef() {
logger.log(BasicLevel.DEBUG, "***************testAttachNullRef*****************");
Team t = new Team("Istres",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p9", t, 20);
players.add(p1);
Player p2 = new Player("p10", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c5", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
pm.makePersistent(t);
pm.currentTransaction().commit();
try {
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
assertNotNull(copyOfT);
assertNotNull("Coach of detached team should not be null.", copyOfT.getCoach());
//set null for the coach
copyOfT.setCoach(null);
assertNull(copyOfT.getCoach());
//print the team out
logger.log(BasicLevel.DEBUG, copyOfT.toString());
pm.currentTransaction().begin();
//attach the team t
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
assertEquals(copyOfT.getTown(), attachedTeam.getTown());
assertNull("Coach of attached team should be null", attachedTeam.getCoach());
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method with a new object in a Collection : the object is made persistent.
*/
public void testAttachNewObjectInCollection() {
logger.log(BasicLevel.DEBUG, "***************testAttachNewObjectInCollection*****************");
Team t = new Team("Le Mans",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p11", t, 20);
players.add(p1);
Player p2 = new Player("p12", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c6", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "Make the team persistent " + t.toString());
pm.makePersistent(t);
pm.currentTransaction().commit();
try {
//detach the team
Team copyOfT = (Team) pm.detachCopy(t);
t = null;
//create a new player
String newPlayerName = "pXX";
Player newPlayer = new Player(newPlayerName, copyOfT, 35);
copyOfT.addPlayer(newPlayer);
//attach the team
pm.currentTransaction().begin();
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
Iterator it = attachedTeam.getPlayers().iterator();
boolean newPlayerFound = false;
while(!newPlayerFound && it.hasNext()) {
Player p = (Player) it.next();
if (p.getName().equals(newPlayerName)) {
assertTrue("The new player is not a persistent object", ((JDOPersistentObjectItf) p).jdoIsPersistent());
newPlayerFound = true;
}
}
assertTrue("The new player should have been made persistent", newPlayerFound);
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method: make an inherited object persistent, detach it then attach it.
*/
public void testAttachInherited() {
logger.log(BasicLevel.DEBUG, "***************testAttachInherited*****************");
Car c = new Car("r5", 4, "red");
FormulaOne f = new FormulaOne("williams", 4, "green", 262);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the car " + c.toString() );
logger.log(BasicLevel.DEBUG, "make persistent the formula one " + f.toString());
pm.makePersistent(c);
pm.makePersistent(f);
pm.currentTransaction().commit();
//detach the car c
Car copyOfC = (Car) pm.detachCopy(c);
//print the car out
logger.log(BasicLevel.DEBUG, copyOfC.toString());
//detach the formula one f
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
//print the formula one out
logger.log(BasicLevel.DEBUG, copyOfF.toString());
pm.currentTransaction().begin();
//attach the copied car
Car attachedCar = (Car) pm.makePersistent(copyOfC);
//attach the copied formula one
FormulaOne attachedF = (FormulaOne) pm.makePersistent(copyOfF);
pm.currentTransaction().commit();
try {
assertNotNull(attachedCar);
assertEquals(copyOfC.getColor(), attachedCar.getColor());
assertEquals(copyOfC.getName(), attachedCar.getName());
assertEquals(copyOfC.getNbOfWheels(), attachedCar.getNbOfWheels());
assertEquals(copyOfC.getType(), attachedCar.getType());
assertNotNull(attachedF);
assertEquals(copyOfF.getColor(), attachedF.getColor());
assertEquals(copyOfF.getName(), attachedF.getName());
assertEquals(copyOfF.getNbOfWheels(), attachedF.getNbOfWheels());
assertEquals(copyOfF.getType(), attachedF.getType());
assertEquals(copyOfF.getSpeedMax(), attachedF.getSpeedMax());
logger.log(BasicLevel.DEBUG,"The attached version of the car is as follows:\n " + attachedCar.toString());
logger.log(BasicLevel.DEBUG,"The attached version of the formula one is as follows:\n " + attachedF.toString());
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method with update: make an inherited object persistent, detach it , modify it and then attach it.
*/
public void testAttachModifiedInherited() {
logger.log(BasicLevel.DEBUG, "***************testAttachModifiedInherited*****************");
FormulaOne f = new FormulaOne("renault", 4, "yellow", 262);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the formula one " + f.toString());
pm.makePersistent(f);
pm.currentTransaction().commit();
//detach
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
//modify
copyOfF.setNbOfWheels(2);
copyOfF.setSpeedMax(320);
//print out
logger.log(BasicLevel.DEBUG, "Copy of formula one " + copyOfF.toString());
pm.currentTransaction().begin();
//attach
FormulaOne attachedF = (FormulaOne) pm.makePersistent(copyOfF);
try {
assertEquals(2, attachedF.getNbOfWheels());
assertEquals(320, attachedF.getSpeedMax());
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG,"The attached version of the formula one is as follows:\n " + attachedF.toString());
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method with a new inherited object: the object is made persistent.
*/
public void testAttachNewInherited() {
logger.log(BasicLevel.DEBUG, "***************testAttachNewInherited*****************");
FormulaOne f = new FormulaOne("sauber", 4, "red", 262);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "attach the f1 " + f.toString());
FormulaOne attachedF = (FormulaOne) pm.makePersistent(f);
try {
assertTrue(((JDOPersistentObjectItf)attachedF).jdoIsPersistent());
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedF.toString());
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
/**
* Test the attach method with an invalid copy: make an inherited object persistent, modify it, detach it , rollback the transaction and then attach it.
*/
public void testAttachInvalidInherited() {
logger.log(BasicLevel.DEBUG, "***************testAttachInvalidInherited*****************");
FormulaOne f = new FormulaOne("ferrari", 4, "red", 262);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the f1 " + f.toString());
pm.makePersistent(f);
pm.currentTransaction().commit();
pm.currentTransaction().begin();
//modify
f.setNbOfWheels(1);
//detach the team t
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
//rollback the tx : it invalids the copy
pm.currentTransaction().rollback();
// print out
logger.log(BasicLevel.DEBUG, "Copy of f1 " + copyOfF.toString());
logger.log(BasicLevel.DEBUG, "rollback of the tx: the attach shouldn't work.");
try{
//attach the "invalid" f1
pm.currentTransaction().begin();
FormulaOne attachedF = (FormulaOne) pm.makePersistent(copyOfF);
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedF.toString());
} catch(Exception e){
assertEquals("Wrong exception thrown: " + e.getMessage(), JDOException.class, e.getClass());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
public void testRemovingOfPersistentObject() {
PersistenceManager pm = pmf.getPersistenceManager();
try {
Class[] cs = new Class[]{Car.class, Coach.class, FormulaOne.class,
Player.class, Team.class, Vehicle.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();
}
}
}