KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > detach > TestDetach1N1


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

25
26 package org.objectweb.speedo.runtime.detach;
27
28
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.jdo.FetchPlan;
33 import javax.jdo.JDOException;
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.Query;
36
37 import junit.framework.Assert;
38
39 import org.objectweb.speedo.SpeedoTestHelper;
40 import org.objectweb.speedo.api.ExceptionHelper;
41 import org.objectweb.speedo.pobjects.detach.groupama.CommuneHelper;
42 import org.objectweb.speedo.pobjects.detach.groupama.ConventionHelper;
43 import org.objectweb.speedo.pobjects.detach.groupama.DomaineHelper;
44 import org.objectweb.speedo.pobjects.detach.groupama.IntervenantHelper;
45 import org.objectweb.speedo.pobjects.detach.groupama.MarqueHelper;
46 import org.objectweb.speedo.pobjects.detach.groupama.SpecialiteMercureHelper;
47 import org.objectweb.speedo.pobjects.detach.groupama.ZoneGeographiqueHelper;
48 import org.objectweb.util.monolog.api.BasicLevel;
49
50 /**
51  * Test the detach method on 1<->N<->1 relationships
52  * @author Y.Bersihand
53  */

54 public class TestDetach1N1 extends SpeedoTestHelper {
55
56     public TestDetach1N1(String JavaDoc s) {
57         super(s);
58     }
59     
60     protected String JavaDoc getLoggerName() {
61         return LOG_NAME + ".rt.detach.TestDetach1N1";
62     }
63     
64     /**
65      * Test the detach method: make an object persistent, and detach it out of an active transaction.
66      */

67     public void testCreateObjects() {
68         logger.log(BasicLevel.DEBUG, "***************testDetach1N1*****************");
69         //create 2 marques
70
MarqueHelper marque1 = new MarqueHelper();
71         marque1.setCdmem("1");
72         marque1.setLimem("marque1");
73         
74         MarqueHelper marque2 = new MarqueHelper();
75         marque2.setCdmem("2");
76         marque2.setLimem("marque2");
77         
78         //create 1 Intervenant
79
IntervenantHelper intervenant1 = new IntervenantHelper();
80         intervenant1.setNomep(1);
81         
82         //create 2 Conventions
83
ConventionHelper convention1 = new ConventionHelper();
84         // marque 1 <-> * convention
85
// intervenant 1 <-> * convention
86
convention1.setIntervenant(intervenant1);
87         convention1.setNomep(intervenant1.getNomep());
88         convention1.setMarque(marque1);
89         convention1.setCdmem(marque1.getCdmem());
90         marque1.addConvention(convention1);
91         intervenant1.addConvention(convention1);
92     
93         ConventionHelper convention2 = new ConventionHelper();
94         convention2.setIntervenant(intervenant1);
95         convention2.setNomep(intervenant1.getNomep());
96         convention2.setMarque(marque2);
97         convention2.setCdmem(marque2.getCdmem());
98         marque2.addConvention(convention2);
99         intervenant1.addConvention(convention2);
100     
101         //create 2 communes
102
CommuneHelper commune1 = new CommuneHelper();
103         commune1.setId(1);
104         commune1.setDummy("commune1");
105         CommuneHelper commune2 = new CommuneHelper();
106         commune2.setId(2);
107         commune2.setDummy("commune2");
108         
109         //create 2 zg
110
ZoneGeographiqueHelper zg1 = new ZoneGeographiqueHelper();
111         zg1.setId(1);
112         // commune 1 <-> * zonegeographique
113
zg1.setCommune(commune1);
114         commune1.addZoneGeographique(zg1);
115         ZoneGeographiqueHelper zg2 = new ZoneGeographiqueHelper();
116         zg2.setId(2);
117         // commune 1 <-> * zonegeographique
118
zg2.setCommune(commune2);
119         commune2.addZoneGeographique(zg2);
120         
121         // convention 1 <-> * zonegeographique
122
convention1.addZoneGeographique(zg1);
123         zg1.setConvention(convention1);
124         convention1.addZoneGeographique(zg2);
125         zg2.setConvention(convention2);
126         
127         //create a domaine
128
DomaineHelper domaine1 = new DomaineHelper();
129         domaine1.setCdmed(1);
130         domaine1.setLimed(1);
131         
132         SpecialiteMercureHelper specialiteMercure1 = new SpecialiteMercureHelper();
133         specialiteMercure1.setCdmec(1);
134         specialiteMercure1.setCdmed(1);
135         specialiteMercure1.setCdmese(1);
136         // spcecialiteMercure * <-> 1 domaine
137
specialiteMercure1.setDomaine(domaine1);
138         domaine1.addSpecialiteMercure(specialiteMercure1);
139         // spcecialiteMercure 1 <-> * convention
140
specialiteMercure1.addConvention(convention1);
141         convention1.setSpecialiteMercure(specialiteMercure1);
142         
143         SpecialiteMercureHelper specialiteMercure2 = new SpecialiteMercureHelper();
144         specialiteMercure1.setCdmec(2);
145         specialiteMercure1.setCdmed(1);
146         specialiteMercure1.setCdmese(2);
147         // spcecialiteMercure * <-> 1 domaine
148
specialiteMercure2.setDomaine(domaine1);
149         domaine1.addSpecialiteMercure(specialiteMercure2);
150         // spcecialiteMercure 1 <-> * convention
151
specialiteMercure2.addConvention(convention2);
152         convention2.setSpecialiteMercure(specialiteMercure2);
153         
154         PersistenceManager pm = pmf.getPersistenceManager();
155         
156         try {
157             pm.currentTransaction().begin();
158             logger.log(BasicLevel.DEBUG, "make persistent the 2 conventions");
159             pm.makePersistent(convention1);
160             pm.makePersistent(convention2);
161             
162             pm.currentTransaction().commit();
163         
164             //set the fetch group to listeIntervenants
165
FetchPlan fp = pm.getFetchPlan();
166             fp.clearGroups().addGroup("listeIntervenants");
167             //detach the intervenant
168
IntervenantHelper copyOfIntervenant1 = (IntervenantHelper) pm.detachCopy(intervenant1);
169             assertNotNull(copyOfIntervenant1);
170             assertEquals("Nomep of intervenant1 and its detached copy are not the same.", intervenant1.getNomep(), copyOfIntervenant1.getNomep());
171             Collection JavaDoc conv = intervenant1.getConventions();
172             Collection JavaDoc copyConv = copyOfIntervenant1.getConventions();
173             assertEquals("Size of conventions not the same for intervenant1 and its detached copy.", conv.size(), copyConv.size());
174             Iterator JavaDoc itConv = conv.iterator();
175             Iterator JavaDoc itCopyConv = copyConv.iterator();
176             while (itConv.hasNext() && itCopyConv.hasNext()) {
177                 ConventionHelper convention = (ConventionHelper) itConv.next();
178                 ConventionHelper copyConvention = (ConventionHelper) itCopyConv.next();
179                 assertEquals("cdmem is not the same for the convention and its detached copy", convention.getCdmem(), copyConvention.getCdmem());
180                 assertEquals("nomep is not the same for the convention and its detached copy", convention.getNomep(), copyConvention.getNomep());
181                 SpecialiteMercureHelper specialiteMercure = convention.getSpecialiteMercure();
182                 SpecialiteMercureHelper copySpecialiteMercure = copyConvention.getSpecialiteMercure();
183                 assertEquals("cdmed not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmed(), copySpecialiteMercure.getCdmed());
184                 assertEquals("cdmec not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmec(), copySpecialiteMercure.getCdmec());
185                 assertEquals("cdmese not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmese(), copySpecialiteMercure.getCdmese());
186                 DomaineHelper domaine = specialiteMercure.getDomaine();
187                 DomaineHelper copyDomaine = copySpecialiteMercure.getDomaine();
188                 assertEquals("cdmed not the same for the domaine and its detached copy", domaine.getCdmed(), copyDomaine.getCdmed());
189                 assertEquals("limed not the same for the domaine and its detached copy", domaine.getLimed(), copyDomaine.getLimed());
190                 MarqueHelper marque = convention.getMarque();
191                 MarqueHelper copyMarque = copyConvention.getMarque();
192                 assertEquals("cdmem is not the same for the marque and its detached copy", marque.getCdmem(), copyMarque.getCdmem());
193                 assertEquals("limem is not the same for the marque and its detached copy", marque.getLimem(), copyMarque.getLimem());
194             }
195             
196             //query
197
Query query = pm.newQuery(ConventionHelper.class);
198             StringBuffer JavaDoc filter = new StringBuffer JavaDoc();
199             filter.append("(intervenant.nomep == (\"" +
200                              intervenant1.getNomep() + "\")) && ");
201             filter.append("(cdmem == (\"" + marque1.getCdmem() + "\"))");
202             query.setFilter(filter.toString());
203             //fetchplan
204
fp = pm.getFetchPlan().clearGroups();
205             fp.addGroup("listeConventions");
206             //execute
207
Collection JavaDoc results = (Collection JavaDoc) query.execute();
208             //detach all the conventions retrieved
209
Collection JavaDoc detachedConv = pm.detachCopyAll(results);
210             Iterator JavaDoc itDC = detachedConv.iterator();
211             while (itDC.hasNext()) {
212                 ConventionHelper dConv = (ConventionHelper) itDC.next();
213                 Collection JavaDoc zonesGeo = dConv.getZoneGeographiques();
214                 assertEquals("Size of zoneGeo for convention1 and its detached copy si not the same.", convention1.getZoneGeographiques().size(), zonesGeo.size());
215                 Iterator JavaDoc itZg = zonesGeo.iterator();
216                 while (itZg.hasNext()) {
217                     ZoneGeographiqueHelper zoneGeo = (ZoneGeographiqueHelper) itZg.next();
218                     assertNotNull("Commune null for zoneGeo " + zoneGeo.getId() + ".", zoneGeo.getCommune());
219                     logger.log(BasicLevel.DEBUG, "Commune of zg" + zoneGeo.getId() + ": " + zoneGeo.getCommune().getDummy());
220                 }
221                 SpecialiteMercureHelper dSpecialiteMercure = dConv.getSpecialiteMercure();
222                 assertEquals("cdmed not the same for the specialiteMercure and its detached copy", specialiteMercure1.getCdmed(), dSpecialiteMercure.getCdmed());
223                 assertEquals("cdmec not the same for the specialiteMercure and its detached copy", specialiteMercure1.getCdmec(), dSpecialiteMercure.getCdmec());
224                 assertEquals("cdmese not the same for the specialiteMercure and its detached copy", specialiteMercure1.getCdmese(), dSpecialiteMercure.getCdmese());
225                 DomaineHelper dDomaine = dSpecialiteMercure.getDomaine();
226                 assertEquals("cdmed not the same for the domaine and its detached copy", domaine1.getCdmed(), dDomaine.getCdmed());
227                 assertEquals("limed not the same for the domaine and its detached copy", domaine1.getLimed(), dDomaine.getLimed());
228             }
229             query.closeAll();
230         } catch (Exception JavaDoc e) {
231             e.printStackTrace();
232             fail(e.getMessage());
233         } finally {
234             if (pm.currentTransaction().isActive())
235                 pm.currentTransaction().rollback();
236             pm.close();
237         }
238     }
239     
240     
241     public void testRemovingOfPersistentObject() {
242         PersistenceManager pm = pmf.getPersistenceManager();
243         try {
244             Class JavaDoc[] cs = new Class JavaDoc[]{ConventionHelper.class, IntervenantHelper.class, MarqueHelper.class
245                     , CommuneHelper.class, ZoneGeographiqueHelper.class, DomaineHelper.class, SpecialiteMercureHelper.class};
246             pm.currentTransaction().begin();
247             for(int i=0; i<cs.length; i++) {
248                 Query query = pm.newQuery(cs[i]);
249                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
250                 Iterator JavaDoc it = col.iterator();
251                 while(it.hasNext()) {
252                     Object JavaDoc o = it.next();
253                     Assert.assertNotNull("null object in the query result"
254                         + cs[i].getName(), o);
255                     pm.deletePersistent(o);
256
257                 }
258                 query.close(col);
259             }
260             pm.currentTransaction().commit();
261         } catch (JDOException e) {
262             Exception JavaDoc ie = ExceptionHelper.getNested(e);
263             logger.log(BasicLevel.ERROR, "", ie);
264             fail(ie.getMessage());
265         } finally {
266             pm.close();
267         }
268     }
269 }
270
Popular Tags