001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.odis;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import javax.jdo.Extent;
023: import javax.jdo.PersistenceManager;
024: import javax.jdo.Query;
025:
026: import org.objectweb.speedo.SpeedoTestHelper;
027: import org.objectweb.speedo.pobjects.odis.Competition;
028: import org.objectweb.speedo.pobjects.odis.Trackpoint;
029: import org.objectweb.util.monolog.api.BasicLevel;
030:
031: /**
032: *
033: * @author S.Chassande-Barrioz
034: */
035: public class TestTrackpoint extends SpeedoTestHelper {
036:
037: private static int competitionId = 2;
038: private static int trackpointId = 5;
039:
040: public TestTrackpoint(String s) {
041: super (s);
042: }
043:
044: protected String getLoggerName() {
045: return LOG_NAME + ".rt.odis.TestTrackpoint";
046: }
047:
048: private void createCompetition() {
049: //create the competition
050: Competition competition = new Competition(competitionId++);
051: //create trackpoints
052: Trackpoint trackpoint1 = new Trackpoint(trackpointId++);
053: Trackpoint trackpoint2 = new Trackpoint(trackpointId++);
054: Trackpoint trackpoint3 = new Trackpoint(trackpointId++);
055: Trackpoint trackpoint4 = new Trackpoint(trackpointId++);
056:
057: competition.addTrackpoint(trackpoint1);
058: competition.addTrackpoint(trackpoint2);
059: competition.addTrackpoint(trackpoint3);
060: competition.addTrackpoint(trackpoint4);
061:
062: PersistenceManager pm = pmf.getPersistenceManager();
063: //store the graph defined above in the datastore
064: pm.currentTransaction().begin();
065: //make persistent the competition
066: // all the references reachable from this object will be made persistent
067: pm.makePersistent(competition);
068: pm.currentTransaction().commit();
069: }
070:
071: public void testGetTrackpoints() {
072: logger.log(BasicLevel.DEBUG,
073: "**************testGetTrackpoints***********");
074: //create the competition
075: Competition competition = new Competition(1);
076: //create trackpoints
077: Trackpoint trackpoint1 = new Trackpoint(1);
078: Trackpoint trackpoint2 = new Trackpoint(2);
079: Trackpoint trackpoint3 = new Trackpoint(3);
080: Trackpoint trackpoint4 = new Trackpoint(4);
081:
082: competition.addTrackpoint(trackpoint1);
083: competition.addTrackpoint(trackpoint2);
084: competition.addTrackpoint(trackpoint3);
085: competition.addTrackpoint(trackpoint4);
086:
087: PersistenceManager pm = pmf.getPersistenceManager();
088: //store the graph defined above in the datastore
089: pm.currentTransaction().begin();
090: //make persistent the competition
091: // all the references reachable from this object will be made persistent
092: pm.makePersistent(competition);
093: pm.currentTransaction().commit();
094:
095: //get the id of competition
096: Object idCompetition = pm.getObjectId(competition);
097:
098: Competition copyCompetition = (Competition) pm.getObjectById(
099: idCompetition, true);
100: //print the trackpoints
101: Collection collection = copyCompetition.getTrackpoints();
102: Iterator it = collection.iterator();
103: logger.log(BasicLevel.DEBUG, "Collection length="
104: + collection.size());
105: while (it.hasNext()) {
106: Trackpoint tp = (Trackpoint) it.next();
107: logger.log(BasicLevel.DEBUG, "Trackpoint[id_order="
108: + tp.getId_order() + ",id_competition="
109: + tp.getId_competition() + "]");
110: }
111:
112: //get the id of trackpoint1
113: Object idTrackpoint = pm.getObjectId(trackpoint1);
114: Trackpoint copyTrackpoint = (Trackpoint) pm.getObjectById(
115: idTrackpoint, true);
116: logger.log(BasicLevel.DEBUG, "competition: "
117: + copyTrackpoint.getCompetition().getId());
118:
119: pm.close();
120: }
121:
122: public void testQueryExtent() {
123: logger.log(BasicLevel.DEBUG,
124: "****************testQueryExtent****************");
125: //create 2 other competitions
126: createCompetition();
127: createCompetition();
128:
129: PersistenceManager pm = pmf.getPersistenceManager();
130: int idCompetition = 3;
131: Competition comp = null;
132:
133: /* create query */
134: Query qCompetition = pm.newQuery(Competition.class);
135: qCompetition.setFilter("(id == " + idCompetition + ")");
136: /* get the answer */
137: Collection col = (Collection) qCompetition.execute();
138: /* pass through the answer: should be at most one */
139: Iterator it = col.iterator();
140: if (it.hasNext()) {
141: /* get the competition */
142: comp = (Competition) it.next();
143: logger.log(BasicLevel.DEBUG, "competition:" + comp.getId()
144: + ", tp=" + comp.getTrackpoints().toString());
145: } else {
146: logger.log(BasicLevel.DEBUG, "Competition with id "
147: + idCompetition + " unfound.");
148: /* close persistence context */
149: qCompetition.closeAll();
150: return;
151: }
152: qCompetition.closeAll();
153:
154: Iterator itExtent = null;
155: Iterator itTp = null;
156:
157: Extent extent = pm.getExtent(Competition.class, false);
158: itExtent = extent.iterator();
159: while (itExtent.hasNext()) {
160: comp = (Competition) itExtent.next();
161: logger.log(BasicLevel.DEBUG, "Competition: id="
162: + comp.getId() + ", tps="
163: + comp.getTrackpoints().toString());
164: itTp = comp.getTrackpoints().iterator();
165: while (itTp.hasNext()) {
166: Trackpoint tp = (Trackpoint) itTp.next();
167: logger.log(BasicLevel.DEBUG, "Trackpoint: id_order="
168: + tp.getId_order() + ", id_competition="
169: + tp.getId_competition());
170: }
171:
172: }
173: extent.closeAll();
174:
175: pm.close();
176: }
177: }
|