KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > odis > TestTrackpoint


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.runtime.odis;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
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 /**
33  *
34  * @author S.Chassande-Barrioz
35  */

36 public class TestTrackpoint extends SpeedoTestHelper {
37
38     private static int competitionId = 2;
39     private static int trackpointId =5;
40     
41     public TestTrackpoint(String JavaDoc s) {
42         super(s);
43     }
44
45     protected String JavaDoc getLoggerName() {
46         return LOG_NAME + ".rt.odis.TestTrackpoint";
47     }
48
49     private void createCompetition(){
50         //create the competition
51
Competition competition = new Competition(competitionId++);
52         //create trackpoints
53
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         //store the graph defined above in the datastore
65
pm.currentTransaction().begin();
66         //make persistent the competition
67
// all the references reachable from this object will be made persistent
68
pm.makePersistent(competition);
69         pm.currentTransaction().commit();
70     }
71     
72     public void testGetTrackpoints() {
73         logger.log(BasicLevel.DEBUG, "**************testGetTrackpoints***********");
74         //create the competition
75
Competition competition = new Competition(1);
76         //create trackpoints
77
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         //store the graph defined above in the datastore
89
pm.currentTransaction().begin();
90         //make persistent the competition
91
// all the references reachable from this object will be made persistent
92
pm.makePersistent(competition);
93         pm.currentTransaction().commit();
94         
95         //get the id of competition
96
Object JavaDoc idCompetition = pm.getObjectId(competition);
97         
98         Competition copyCompetition = (Competition) pm.getObjectById(idCompetition, true);
99         //print the trackpoints
100
Collection JavaDoc collection = copyCompetition.getTrackpoints();
101         Iterator JavaDoc 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         //get the id of trackpoint1
109
Object JavaDoc 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         //create 2 other competitions
120
createCompetition();
121         createCompetition();
122         
123         PersistenceManager pm = pmf.getPersistenceManager();
124         int idCompetition = 3;
125         Competition comp = null;
126         
127         /* create query */
128         Query qCompetition = pm.newQuery(Competition.class);
129         qCompetition.setFilter("(id == " + idCompetition + ")");
130         /* get the answer */
131         Collection JavaDoc col = (Collection JavaDoc) qCompetition.execute();
132         /* pass through the answer: should be at most one */
133         Iterator JavaDoc it = col.iterator();
134         if (it.hasNext()) {
135             /* get the competition */
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             /* close persistence context */
141             qCompetition.closeAll();
142             return;
143         }
144         qCompetition.closeAll();
145         
146         Iterator JavaDoc itExtent = null;
147         Iterator JavaDoc 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