Package org.objectweb.speedo.runtime.odis

Source Code of org.objectweb.speedo.runtime.odis.TestTrackpoint

/**
* 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
*/
package org.objectweb.speedo.runtime.odis;

import java.util.Collection;
import java.util.Iterator;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.odis.Competition;
import org.objectweb.speedo.pobjects.odis.Trackpoint;
import org.objectweb.util.monolog.api.BasicLevel;

/**
*
* @author S.Chassande-Barrioz
*/
public class TestTrackpoint extends SpeedoTestHelper {

  private static int competitionId = 2;
  private static int trackpointId =5;
 
  public TestTrackpoint(String s) {
    super(s);
  }

  protected String getLoggerName() {
    return LOG_NAME + ".rt.odis.TestTrackpoint";
  }

  private void createCompetition(){
    //create the competition
    Competition competition = new Competition(competitionId++);
    //create trackpoints
    Trackpoint trackpoint1 = new Trackpoint(trackpointId++);
    Trackpoint trackpoint2 = new Trackpoint(trackpointId++);
    Trackpoint trackpoint3 = new Trackpoint(trackpointId++);
    Trackpoint trackpoint4 = new Trackpoint(trackpointId++);
   
    competition.addTrackpoint(trackpoint1);
    competition.addTrackpoint(trackpoint2);
    competition.addTrackpoint(trackpoint3);
    competition.addTrackpoint(trackpoint4);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    //store the graph defined above in the datastore
    pm.currentTransaction().begin();
    //make persistent the competition
    // all the references reachable from this object will be made persistent
        pm.makePersistent(competition);
        pm.currentTransaction().commit();
  }
 
  public void testGetTrackpoints() {
    logger.log(BasicLevel.DEBUG, "**************testGetTrackpoints***********");
    //create the competition
    Competition competition = new Competition(1);
    //create trackpoints
    Trackpoint trackpoint1 = new Trackpoint(1);
    Trackpoint trackpoint2 = new Trackpoint(2);
    Trackpoint trackpoint3 = new Trackpoint(3);
    Trackpoint trackpoint4 = new Trackpoint(4);
   
    competition.addTrackpoint(trackpoint1);
    competition.addTrackpoint(trackpoint2);
    competition.addTrackpoint(trackpoint3);
    competition.addTrackpoint(trackpoint4);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    //store the graph defined above in the datastore
    pm.currentTransaction().begin();
    //make persistent the competition
    // all the references reachable from this object will be made persistent
        pm.makePersistent(competition);
        pm.currentTransaction().commit();
       
        //get the id of competition
        Object idCompetition = pm.getObjectId(competition);
       
        Competition copyCompetition = (Competition) pm.getObjectById(idCompetition, true);
        //print the trackpoints
        Collection collection = copyCompetition.getTrackpoints();
        Iterator it = collection.iterator();
        logger.log(BasicLevel.DEBUG, "Collection length=" + collection.size());
        while(it.hasNext()){
          Trackpoint tp = (Trackpoint) it.next();
          logger.log(BasicLevel.DEBUG, "Trackpoint[id_order=" + tp.getId_order() + ",id_competition=" + tp.getId_competition() + "]");
        }
       
        //get the id of trackpoint1
        Object idTrackpoint = pm.getObjectId(trackpoint1);
        Trackpoint copyTrackpoint = (Trackpoint) pm.getObjectById(idTrackpoint, true);
        logger.log(BasicLevel.DEBUG, "competition: " + copyTrackpoint.getCompetition().getId());
       
        pm.close();       
  }

 
  public void testQueryExtent(){
    logger.log(BasicLevel.DEBUG, "****************testQueryExtent****************");
    //create 2 other competitions
    createCompetition();
    createCompetition();
   
    PersistenceManager pm = pmf.getPersistenceManager();
    int idCompetition = 3;
    Competition comp = null;
   
    /* create query */
    Query qCompetition = pm.newQuery(Competition.class);
    qCompetition.setFilter("(id == " + idCompetition + ")");
    /* get the answer */
    Collection col = (Collection) qCompetition.execute();
    /* pass through the answer: should be at most one */
    Iterator it = col.iterator();
    if (it.hasNext()) {
      /* get the competition */
      comp = (Competition) it.next();
      logger.log(BasicLevel.DEBUG, "competition:" + comp.getId() + ", tp=" + comp.getTrackpoints().toString() );
    } else {
      logger.log(BasicLevel.DEBUG, "Competition with id "+ idCompetition + " unfound.");
      /* close persistence context */
      qCompetition.closeAll();
      return;
    }
    qCompetition.closeAll();
   
    Iterator    itExtent  = null;
    Iterator itTp = null;
     
    Extent extent = pm.getExtent(Competition.class, false);
    itExtent = extent.iterator();
    while(itExtent.hasNext()) {
      comp =  (Competition)itExtent.next();
      logger.log(BasicLevel.DEBUG, "Competition: id=" + comp.getId() + ", tps=" + comp.getTrackpoints().toString());
      itTp = comp.getTrackpoints().iterator();
      while(itTp.hasNext()){
        Trackpoint tp = (Trackpoint) itTp.next();
        logger.log(BasicLevel.DEBUG, "Trackpoint: id_order=" + tp.getId_order() + ", id_competition=" + tp.getId_competition() );
      }
     
    }
    extent.closeAll();
   
    pm.close();
  }
}
TOP

Related Classes of org.objectweb.speedo.runtime.odis.TestTrackpoint

TOP
Copyright © 2018 www.massapi.com. 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.