1 18 package org.objectweb.speedo.runtime.odis; 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import javax.jdo.Extent; 24 import javax.jdo.PersistenceManager; 25 import javax.jdo.Query; 26 27 import org.objectweb.speedo.SpeedoTestHelper; 28 import org.objectweb.speedo.pobjects.odis.Competition; 29 import org.objectweb.speedo.pobjects.odis.Trackpoint; 30 import org.objectweb.util.monolog.api.BasicLevel; 31 32 36 public class TestTrackpoint extends SpeedoTestHelper { 37 38 private static int competitionId = 2; 39 private static int trackpointId =5; 40 41 public TestTrackpoint(String s) { 42 super(s); 43 } 44 45 protected String getLoggerName() { 46 return LOG_NAME + ".rt.odis.TestTrackpoint"; 47 } 48 49 private void createCompetition(){ 50 Competition competition = new Competition(competitionId++); 52 Trackpoint trackpoint1 = new Trackpoint(trackpointId++); 54 Trackpoint trackpoint2 = new Trackpoint(trackpointId++); 55 Trackpoint trackpoint3 = new Trackpoint(trackpointId++); 56 Trackpoint trackpoint4 = new Trackpoint(trackpointId++); 57 58 competition.addTrackpoint(trackpoint1); 59 competition.addTrackpoint(trackpoint2); 60 competition.addTrackpoint(trackpoint3); 61 competition.addTrackpoint(trackpoint4); 62 63 PersistenceManager pm = pmf.getPersistenceManager(); 64 pm.currentTransaction().begin(); 66 pm.makePersistent(competition); 69 pm.currentTransaction().commit(); 70 } 71 72 public void testGetTrackpoints() { 73 logger.log(BasicLevel.DEBUG, "**************testGetTrackpoints***********"); 74 Competition competition = new Competition(1); 76 Trackpoint trackpoint1 = new Trackpoint(1); 78 Trackpoint trackpoint2 = new Trackpoint(2); 79 Trackpoint trackpoint3 = new Trackpoint(3); 80 Trackpoint trackpoint4 = new Trackpoint(4); 81 82 competition.addTrackpoint(trackpoint1); 83 competition.addTrackpoint(trackpoint2); 84 competition.addTrackpoint(trackpoint3); 85 competition.addTrackpoint(trackpoint4); 86 87 PersistenceManager pm = pmf.getPersistenceManager(); 88 pm.currentTransaction().begin(); 90 pm.makePersistent(competition); 93 pm.currentTransaction().commit(); 94 95 Object idCompetition = pm.getObjectId(competition); 97 98 Competition copyCompetition = (Competition) pm.getObjectById(idCompetition, true); 99 Collection collection = copyCompetition.getTrackpoints(); 101 Iterator it = collection.iterator(); 102 logger.log(BasicLevel.DEBUG, "Collection length=" + collection.size()); 103 while(it.hasNext()){ 104 Trackpoint tp = (Trackpoint) it.next(); 105 logger.log(BasicLevel.DEBUG, "Trackpoint[id_order=" + tp.getId_order() + ",id_competition=" + tp.getId_competition() + "]"); 106 } 107 108 Object idTrackpoint = pm.getObjectId(trackpoint1); 110 Trackpoint copyTrackpoint = (Trackpoint) pm.getObjectById(idTrackpoint, true); 111 logger.log(BasicLevel.DEBUG, "competition: " + copyTrackpoint.getCompetition().getId()); 112 113 pm.close(); 114 } 115 116 117 public void testQueryExtent(){ 118 logger.log(BasicLevel.DEBUG, "****************testQueryExtent****************"); 119 createCompetition(); 121 createCompetition(); 122 123 PersistenceManager pm = pmf.getPersistenceManager(); 124 int idCompetition = 3; 125 Competition comp = null; 126 127 128 Query qCompetition = pm.newQuery(Competition.class); 129 qCompetition.setFilter("(id == " + idCompetition + ")"); 130 131 Collection col = (Collection ) qCompetition.execute(); 132 133 Iterator it = col.iterator(); 134 if (it.hasNext()) { 135 136 comp = (Competition) it.next(); 137 logger.log(BasicLevel.DEBUG, "competition:" + comp.getId() + ", tp=" + comp.getTrackpoints().toString() ); 138 } else { 139 logger.log(BasicLevel.DEBUG, "Competition with id "+ idCompetition + " unfound."); 140 141 qCompetition.closeAll(); 142 return; 143 } 144 qCompetition.closeAll(); 145 146 Iterator itExtent = null; 147 Iterator itTp = null; 148 149 Extent extent = pm.getExtent(Competition.class, false); 150 itExtent = extent.iterator(); 151 while(itExtent.hasNext()) { 152 comp = (Competition)itExtent.next(); 153 logger.log(BasicLevel.DEBUG, "Competition: id=" + comp.getId() + ", tps=" + comp.getTrackpoints().toString()); 154 itTp = comp.getTrackpoints().iterator(); 155 while(itTp.hasNext()){ 156 Trackpoint tp = (Trackpoint) itTp.next(); 157 logger.log(BasicLevel.DEBUG, "Trackpoint: id_order=" + tp.getId_order() + ", id_competition=" + tp.getId_competition() ); 158 } 159 160 } 161 extent.closeAll(); 162 163 pm.close(); 164 } 165 } 166 | Popular Tags |