/**
* 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.pobjects.detach.Coach;
import org.objectweb.speedo.pobjects.detach.Player;
import org.objectweb.speedo.pobjects.detach.Team;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* @author Y.Bersihand
*/
public class TestVersion extends SpeedoTestHelper {
public TestVersion(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.detach.TestVersion";
}
/**
* Test the attach method with versioning:
* make an object persistent
* tx.begin
* detach it
* tx.commit
* attach => OK
*/
public void testVersion1() {
logger.log(BasicLevel.DEBUG, "***************testVersion1*****************");
Team t = new Team("T1",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p1", t, 20);
players.add(p1);
Player p2 = new Player("p2", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c1", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(t);
pm.currentTransaction().commit();
pm.currentTransaction().begin();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//commit the tx
pm.currentTransaction().commit();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "ATTACH BEGIN");
try{
//attach the "invalid" team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
logger.log(BasicLevel.DEBUG, "ATTACH END");
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
}catch(Exception e){
fail("The attached is supposed to be ok");
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* tx.begin
* write
* detach
* tx.commit
* attach => OK
*/
public void testVersion2() {
logger.log(BasicLevel.DEBUG, "***************testVersion2*****************");
Team t = new Team("T2",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();
pm.makePersistent(t);
pm.currentTransaction().commit();
//begin
pm.currentTransaction().begin();
//modify the coach
t.getCoach().setExperience(99);
Iterator it = t.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//commit
pm.currentTransaction().commit();
pm.currentTransaction().begin();
try{
//attach the team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
}catch(Exception e){
fail("The attached is supposed to be ok");
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* tx.begin
* detach
* write
* tx.commit
* attach => FAIL
*/
public void testVersion3() {
logger.log(BasicLevel.DEBUG, "***************testVersion3*****************");
Team t = new Team("T3",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();
pm.makePersistent(t);
pm.currentTransaction().commit();
//begin
pm.currentTransaction().begin();
//detach the team t
Team copyOfT = null;
try {
copyOfT = (Team) pm.detachCopy(t);
// modify the coach
t.setCoach(new Coach("c31", 10, t));
Iterator it = t.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
//commit
pm.currentTransaction().commit();
} catch (Exception e) {
fail("Detach error: " + e.getMessage());
pm.currentTransaction().rollback();
pm.close();
return;
}
pm.currentTransaction().begin();
try{
//attach the team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
fail("The attached is supposed to fail");
}catch(Exception e){
assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* tx.begin
* detach it
* tx.rollback
* attach => OK
*/
public void testVersion4() {
logger.log(BasicLevel.DEBUG, "***************testVersion4*****************");
Team t = new Team("T4",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();
pm.makePersistent(t);
pm.currentTransaction().commit();
pm.currentTransaction().begin();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//rollback the tx
pm.currentTransaction().rollback();
pm.currentTransaction().begin();
try{
//attach the "invalid" team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
}catch(Exception e){
fail("The attached is supposed to be ok");
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* tx.begin
* write
* detach
* tx.rollback
* attach => FAIL
*/
public void testVersion5() {
logger.log(BasicLevel.DEBUG, "***************testVersion5*****************");
Team t = new Team("T5",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();
pm.makePersistent(t);
pm.currentTransaction().commit();
//begin
pm.currentTransaction().begin();
//modify the coach
t.setCoach(new Coach("c51", 10, t));
Iterator it = t.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//rollback
pm.currentTransaction().rollback();
pm.currentTransaction().begin();
try{
//attach the team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
fail("The attached is supposed to fail.");
}catch(Exception e){
assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* tx.begin
* detach
* write
* tx.rollback
* attach => OK
*/
public void testVersion6() {
logger.log(BasicLevel.DEBUG, "***************testVersion6*****************");
Team t = new Team("T6",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();
pm.makePersistent(t);
pm.currentTransaction().commit();
//begin
pm.currentTransaction().begin();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//modify the coach
t.getCoach().setExperience(99);
Iterator it = t.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
//rollback
pm.currentTransaction().rollback();
pm.currentTransaction().begin();
try{
//attach the team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
}catch(Exception e){
fail("The attached is supposed to be ok.");
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* tx.begin
* detach(d1)
* detach(d2)
* tx.commit
* write d1
* write d2
* tx.begin
* attach(d1) => OK
* tx.commit
* tx.begin
* attach(d2) => FAIL
* tx.commit
*/
public void testVersion7() {
logger.log(BasicLevel.DEBUG, "***************testVersion7*****************");
Team t = new Team("T7",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p13", t, 20);
players.add(p1);
Player p2 = new Player("p14", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c7", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(t);
pm.currentTransaction().commit();
//begin
pm.currentTransaction().begin();
//detach the team t
Team copyOfT1 = (Team) pm.detachCopy(t);
//detach the team t
Team copyOfT2 = (Team) pm.detachCopy(t);
//commit
pm.currentTransaction().commit();
//modify the copy1
copyOfT1.getCoach().setExperience(99);
Iterator it = copyOfT1.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
//modify the copy2
copyOfT2.getCoach().setExperience(99);
it = copyOfT2.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
boolean attach1OK = false;
try{
pm.currentTransaction().begin();
//attach the copy1
Team attachedTeam1 = (Team) pm.makePersistent(copyOfT1);
pm.currentTransaction().commit();
attach1OK = true;
pm.currentTransaction().begin();
//attach the copy2
Team attachedTeam2 = (Team) pm.makePersistent(copyOfT2);
pm.currentTransaction().commit();
fail("The second attach is supposed to fail");
}catch(Exception e){
assertTrue("The first attach is supposed to be ok.", attach1OK);
assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* tx.begin
* detach(d1)
* detach(d2)
* tx.commit
* write d1
* write d2
* tx.begin
* attach(d2) => OK
* tx.commit
* tx.begin
* attach(d1) => FAIL
* tx.commit
*/
public void testVersion8() {
logger.log(BasicLevel.DEBUG, "***************testVersion8*****************");
Team t = new Team("T8",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p15", t, 20);
players.add(p1);
Player p2 = new Player("p16", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c8", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(t);
pm.currentTransaction().commit();
//begin
pm.currentTransaction().begin();
//detach the team t
Team copyOfT1 = (Team) pm.detachCopy(t);
//detach the team t
Team copyOfT2 = (Team) pm.detachCopy(t);
//commit
pm.currentTransaction().commit();
//modify the copy1
copyOfT1.getCoach().setExperience(99);
Iterator it = copyOfT1.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
//modify the copy2
copyOfT2.getCoach().setExperience(99);
it = copyOfT2.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
boolean attach2OK = false;
try{
pm.currentTransaction().begin();
//attach the copy2
Team attachedTeam2 = (Team) pm.makePersistent(copyOfT2);
pm.currentTransaction().commit();
attach2OK = true;
pm.currentTransaction().begin();
//attach the copy1
Team attachedTeam1 = (Team) pm.makePersistent(copyOfT1);
pm.currentTransaction().commit();
fail("The attach of copy1 is supposed to fail");
}catch(Exception e){
assertTrue("The attach of copy2 is supposed to be ok.", attach2OK);
assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* detach
* tx.begin
* write
* attach => FAIL
* tx.commit
*/
public void testVersion9() {
logger.log(BasicLevel.DEBUG, "***************testVersion9*****************");
Team t = new Team("T9",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p17", t, 20);
players.add(p1);
Player p2 = new Player("p18", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c9", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(t);
pm.currentTransaction().commit();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//begin
pm.currentTransaction().begin();
//modify the coach
t.getCoach().setExperience(99);
Iterator it = t.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
try{
//attach the team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
fail("The attached is supposed to fail.");
}catch(Exception e){
assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* detach
* tx.begin
* write
* tx.commit
* tx.begin
* attach => FAIL
* tx.commit
*/
public void testVersion10() {
logger.log(BasicLevel.DEBUG, "***************testVersion10*****************");
Team t = new Team("T10",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p19", t, 20);
players.add(p1);
Player p2 = new Player("p20", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c10", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(t);
pm.currentTransaction().commit();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//begin
pm.currentTransaction().begin();
//modify the coach
t.getCoach().setExperience(99);
Iterator it = t.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
//commit
pm.currentTransaction().commit();
//begin
pm.currentTransaction().begin();
try{
//attach the team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
fail("The attached is supposed to fail.");
}catch(Exception e){
assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
/**
* Test the attach method with versioning:
* make an object persistent
* detach
* tx.begin
* write
* tx.rollback
* tx.begin
* attach => OK
* tx.commit
*/
public void testVersion11() {
logger.log(BasicLevel.DEBUG, "***************testVersion11*****************");
Team t = new Team("T11",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("c11", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(t);
pm.currentTransaction().commit();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
//begin
pm.currentTransaction().begin();
//modify the coach
t.getCoach().setExperience(99);
Iterator it = t.getPlayers().iterator();
//modify the players
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
//rollback
pm.currentTransaction().rollback();
//begin
pm.currentTransaction().begin();
try{
//attach the team
Team attachedTeam = (Team) pm.makePersistent(copyOfT);
pm.currentTransaction().commit();
}catch(Exception e){
fail("The attached is supposed to be ok.");
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
public void testRemovingOfPersistentObject() {
PersistenceManager pm = pmf.getPersistenceManager();
try {
Class[] cs = new Class[]{Coach.class, Player.class, Team.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();
}
}
}