Package org.objectweb.speedo.pobjects.detach

Examples of org.objectweb.speedo.pobjects.detach.Team


   *     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()) {
View Full Code Here


  /**
   * Test the detach method: make an object persistent, and detach it out of an active transaction.
   */
  public void testDetachCopy() {
    logger.log(BasicLevel.DEBUG, "***************testDetachCopy*****************");
    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();
   
    t = null;
    p1 = null;
    p2 = null;
    c = null;
   
    pm.evictAll();
    pm.close();
   
   
    pm = pmf.getPersistenceManager();
    Query query = pm.newQuery(Team.class, "players.contains(player) & player.age<12");
    query.declareVariables("Player player");
    Collection results = (Collection)query.execute();
    assertTrue("The result of the query should be empty.", results.isEmpty());
    query.closeAll();
   
    query = pm.newQuery(Team.class, "players.contains(player) & player.age>12");
    query.declareVariables("Player player");
    results = (Collection)query.execute();
    assertTrue("The result of the query shouldn't be empty.", !results.isEmpty());
    Team res = (Team) results.iterator().next();
    Collection cpl = res.getPlayers();
    Iterator itPlayer = cpl.iterator();
    while(itPlayer.hasNext()) {
      Player pl = (Player) itPlayer.next();
      assertTrue("The age of the player should be over 12. It is " + pl.getAge() + ".", pl.getAge()>12);
    }
    query.closeAll();
   
    query = pm.newQuery(Team.class, "town==\"Bordeaux\"");
    results = (Collection)query.execute();
    assertEquals("The result of the query shouldn't be empty.", false, results.isEmpty());
    Team toDetach = (Team) results.iterator().next();
    query.closeAll();
   
    //detach the team t
    Team copyOfT = (Team) pm.detachCopy(toDetach);
    try {
      assertNotNull(copyOfT);
      assertEquals("Town of team and detached team are not the same", toDetach.getTown(), copyOfT.getTown());
      assertEquals("Coach experience of team and detached team are not the same",toDetach.getCoach().getExperience(), copyOfT.getCoach().getExperience());
      assertEquals("Coach name of team and detached team are not the same",toDetach.getCoach().getName(), copyOfT.getCoach().getName());
      //print the team out
      logger.log(BasicLevel.DEBUG, copyOfT.toString());
    } catch (Exception e) {
      fail(e.getMessage());
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
View Full Code Here

  /**
    * Test the detach method with a non persistent object: the object is made persistent before being detached.
    */
  public void testDetachNonPersistentCopy() {
    logger.log(BasicLevel.DEBUG, "*************testDetachNonPersistentCopy*****************");
    Team t = new Team("Marseille",null,null);
    Coach c = new Coach("c2", 5, t);
    t.setCoach(c);
    Player p = new Player("p3", t, 25);
    t.addPlayer(p);
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.currentTransaction().begin();
    //detach the team t while it is not persistent
    Team copyOfT = (Team) pm.detachCopy(t);
    pm.currentTransaction().commit();
    try {
      assertNotNull(copyOfT);
      assertTrue(((JDOPersistentObjectItf )t).jdoIsPersistent());
      //  print the team out
      logger.log(BasicLevel.DEBUG, copyOfT.toString());
    } catch(Exception e) {
      fail(e.getMessage());
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
View Full Code Here

  /**
    * Test the detach method with a persistent-new object: the object is flushed before being detached.
    */
  public void testDetachPersistentNew() {
    logger.log(BasicLevel.DEBUG, "******************testDetachPersistentNew****************");
    Team t = new Team("Monaco",null,null);
    Coach c = new Coach("c3", 5, t);
    t.setCoach(c);
    Player p = new Player("p4", t, 25);
    t.addPlayer(p);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.currentTransaction().begin();
    pm.makePersistent(c);
    //detach the team while it is persistent-new: the object must be flushed before being detached
    Team copyOfT = (Team) pm.detachCopy(t);
    try {
      assertNotNull(copyOfT);
      assertEquals("Town of team and detached team are not the same", t.getTown(), copyOfT.getTown());
      assertEquals("Coach experience of team and detached team are not the same",t.getCoach().getExperience(), copyOfT.getCoach().getExperience());
      assertEquals("Coach name of team and detached team are not the same",t.getCoach().getName(), copyOfT.getCoach().getName());
      pm.currentTransaction().commit();
      //  print the team out
      logger.log(BasicLevel.DEBUG, copyOfT.toString());
    } catch(Exception e) {
      fail(e.getMessage());
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
View Full Code Here

  /**
    * Test the detach method with a persistent-dirty object: the object is flushed before being detached.
    */
  public void testDetachPersistentDirty() {
    logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDirty****************");
    Team t = new Team("Nantes",null,null);
    Coach c = new Coach("c4", 5, t);
    t.setCoach(c);
    Player p = new Player("p5", t, 28);
    t.addPlayer(p);
       
    PersistenceManager pm = pmf.getPersistenceManager();
    //make persistent
    pm.currentTransaction().begin();
    pm.makePersistent(c);
    pm.currentTransaction().commit();
    //update and detach
    pm.currentTransaction().begin();
    c.setExperience(10);
    //detach the team while it is persistent-dirty: the object must be flushed before being detached
    Team copyOfT = (Team) pm.detachCopy(t);
    pm.currentTransaction().commit();
    try {
      assertEquals(10, copyOfT.getCoach().getExperience());
      logger.log(BasicLevel.DEBUG, copyOfT.toString());
    } catch(Exception e) {
      fail(e.getMessage());
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
View Full Code Here

  /**
    * Test the detach method with a persistent-deleted object: an exception must be thrown.
    */
  public void testDetachPersistentDeleted() {
    logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDeleted****************");
    Team t = new Team("Paris",null, null);
    Coach c = new Coach("c5", 5, t);
    t.setCoach(c);
    Player p = new Player("p6", t, 28);
    t.addPlayer(p);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    //make persistent
    pm.currentTransaction().begin();
    pm.makePersistent(c);
View Full Code Here

  /**
    * Test the detach method with a persistent-deleted object within a collection: an exception must be thrown.
    */
  public void testDetachPersistentDeletedCollection() {
    logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDeletedCollection****************");
    Team t = new Team("Auxerre",null, null);
    Coach c = new Coach("c6", 10, t);
    t.setCoach(c);
    Player p = new Player("p7", t, 28);
    t.addPlayer(p);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    //make persistent
    pm.currentTransaction().begin();
    pm.makePersistent(c);
View Full Code Here

  /**
   * Test the detach method: make an object persistent with a null refrence, and detach it out of an active transaction.
   */
  public void testDetachNullReference() {
    logger.log(BasicLevel.DEBUG, "***************testDetachNullReference*****************");
    Team t = new Team("Niort",null,null);
    Collection players = new ArrayList();
    Player p1 = new Player("p8", t, 25);
    players.add(p1);
    Player p2 = new Player("p9", t, 32);
    players.add(p2);
    t.setPlayers(players);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.currentTransaction().begin();
    logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
    pm.makePersistent(t);
    pm.currentTransaction().commit();
   
    try {
        Extent extent = pm.getExtent(Team.class, false);
        Query query = pm.newQuery(extent, "town == townName");
        query.declareParameters("String townName");
        Collection collection = (Collection) query.execute(t.getTown());
        assertTrue("The result should not be empty", !collection.isEmpty());
        Iterator itr = collection.iterator();
        Team copyOfT = null;
        while (itr.hasNext()) {
          Team team = (Team) itr.next();
          assertEquals("The town should be " + t.getTown(), t.getTown(), team.getTown());
          //detach the team t
          copyOfT = (Team) pm.detachCopy(team);
        }
        query.close(collection);
        extent.closeAll();
View Full Code Here

      pm.makePersistent(c);
      pm.currentTransaction().commit();
      //detach a copy
      Coach copyOfC = (Coach) pm.detachCopy(c);
      //create a team referencing the detached coach
      Team t = new Team("LeMans",null, null);
      t.setCoach(copyOfC);
      //make persistent the team
      pm.currentTransaction().begin();
      pm.makePersistent(t);
      pm.currentTransaction().commit();
      assertNotNull(t.getCoach());
      assertEquals(t.getCoach().getName(), c.getName());
    } catch(Exception e) {
      fail(e.getMessage());
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
View Full Code Here

   *   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();
View Full Code Here

TOP

Related Classes of org.objectweb.speedo.pobjects.detach.Team

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.