001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.runtime.detach;
025:
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.jdo.FetchPlan;
030: import javax.jdo.JDOException;
031: import javax.jdo.PersistenceManager;
032: import javax.jdo.Query;
033:
034: import junit.framework.Assert;
035:
036: import org.objectweb.speedo.SpeedoTestHelper;
037: import org.objectweb.speedo.api.ExceptionHelper;
038: import org.objectweb.speedo.pobjects.detach.groupama.CommuneHelper;
039: import org.objectweb.speedo.pobjects.detach.groupama.ConventionHelper;
040: import org.objectweb.speedo.pobjects.detach.groupama.DomaineHelper;
041: import org.objectweb.speedo.pobjects.detach.groupama.IntervenantHelper;
042: import org.objectweb.speedo.pobjects.detach.groupama.MarqueHelper;
043: import org.objectweb.speedo.pobjects.detach.groupama.SpecialiteMercureHelper;
044: import org.objectweb.speedo.pobjects.detach.groupama.ZoneGeographiqueHelper;
045: import org.objectweb.util.monolog.api.BasicLevel;
046:
047: /**
048: * Test the detach method on 1<->N<->1 relationships
049: * @author Y.Bersihand
050: */
051: public class TestDetach1N1 extends SpeedoTestHelper {
052:
053: public TestDetach1N1(String s) {
054: super (s);
055: }
056:
057: protected String getLoggerName() {
058: return LOG_NAME + ".rt.detach.TestDetach1N1";
059: }
060:
061: /**
062: * Test the detach method: make an object persistent, and detach it out of an active transaction.
063: */
064: public void testCreateObjects() {
065: logger.log(BasicLevel.DEBUG,
066: "***************testDetach1N1*****************");
067: //create 2 marques
068: MarqueHelper marque1 = new MarqueHelper();
069: marque1.setCdmem("1");
070: marque1.setLimem("marque1");
071:
072: MarqueHelper marque2 = new MarqueHelper();
073: marque2.setCdmem("2");
074: marque2.setLimem("marque2");
075:
076: //create 1 Intervenant
077: IntervenantHelper intervenant1 = new IntervenantHelper();
078: intervenant1.setNomep(1);
079:
080: //create 2 Conventions
081: ConventionHelper convention1 = new ConventionHelper();
082: // marque 1 <-> * convention
083: // intervenant 1 <-> * convention
084: convention1.setIntervenant(intervenant1);
085: convention1.setNomep(intervenant1.getNomep());
086: convention1.setMarque(marque1);
087: convention1.setCdmem(marque1.getCdmem());
088: marque1.addConvention(convention1);
089: intervenant1.addConvention(convention1);
090:
091: ConventionHelper convention2 = new ConventionHelper();
092: convention2.setIntervenant(intervenant1);
093: convention2.setNomep(intervenant1.getNomep());
094: convention2.setMarque(marque2);
095: convention2.setCdmem(marque2.getCdmem());
096: marque2.addConvention(convention2);
097: intervenant1.addConvention(convention2);
098:
099: //create 2 communes
100: CommuneHelper commune1 = new CommuneHelper();
101: commune1.setId(1);
102: commune1.setDummy("commune1");
103: CommuneHelper commune2 = new CommuneHelper();
104: commune2.setId(2);
105: commune2.setDummy("commune2");
106:
107: //create 2 zg
108: ZoneGeographiqueHelper zg1 = new ZoneGeographiqueHelper();
109: zg1.setId(1);
110: // commune 1 <-> * zonegeographique
111: zg1.setCommune(commune1);
112: commune1.addZoneGeographique(zg1);
113: ZoneGeographiqueHelper zg2 = new ZoneGeographiqueHelper();
114: zg2.setId(2);
115: // commune 1 <-> * zonegeographique
116: zg2.setCommune(commune2);
117: commune2.addZoneGeographique(zg2);
118:
119: // convention 1 <-> * zonegeographique
120: convention1.addZoneGeographique(zg1);
121: zg1.setConvention(convention1);
122: convention1.addZoneGeographique(zg2);
123: zg2.setConvention(convention2);
124:
125: //create a domaine
126: DomaineHelper domaine1 = new DomaineHelper();
127: domaine1.setCdmed(1);
128: domaine1.setLimed(1);
129:
130: SpecialiteMercureHelper specialiteMercure1 = new SpecialiteMercureHelper();
131: specialiteMercure1.setCdmec(1);
132: specialiteMercure1.setCdmed(1);
133: specialiteMercure1.setCdmese(1);
134: // spcecialiteMercure * <-> 1 domaine
135: specialiteMercure1.setDomaine(domaine1);
136: domaine1.addSpecialiteMercure(specialiteMercure1);
137: // spcecialiteMercure 1 <-> * convention
138: specialiteMercure1.addConvention(convention1);
139: convention1.setSpecialiteMercure(specialiteMercure1);
140:
141: SpecialiteMercureHelper specialiteMercure2 = new SpecialiteMercureHelper();
142: specialiteMercure1.setCdmec(2);
143: specialiteMercure1.setCdmed(1);
144: specialiteMercure1.setCdmese(2);
145: // spcecialiteMercure * <-> 1 domaine
146: specialiteMercure2.setDomaine(domaine1);
147: domaine1.addSpecialiteMercure(specialiteMercure2);
148: // spcecialiteMercure 1 <-> * convention
149: specialiteMercure2.addConvention(convention2);
150: convention2.setSpecialiteMercure(specialiteMercure2);
151:
152: PersistenceManager pm = pmf.getPersistenceManager();
153:
154: try {
155: pm.currentTransaction().begin();
156: logger.log(BasicLevel.DEBUG,
157: "make persistent the 2 conventions");
158: pm.makePersistent(convention1);
159: pm.makePersistent(convention2);
160:
161: pm.currentTransaction().commit();
162:
163: //set the fetch group to listeIntervenants
164: FetchPlan fp = pm.getFetchPlan();
165: fp.clearGroups().addGroup("listeIntervenants");
166: //detach the intervenant
167: IntervenantHelper copyOfIntervenant1 = (IntervenantHelper) pm
168: .detachCopy(intervenant1);
169: assertNotNull(copyOfIntervenant1);
170: assertEquals(
171: "Nomep of intervenant1 and its detached copy are not the same.",
172: intervenant1.getNomep(), copyOfIntervenant1
173: .getNomep());
174: Collection conv = intervenant1.getConventions();
175: Collection copyConv = copyOfIntervenant1.getConventions();
176: assertEquals(
177: "Size of conventions not the same for intervenant1 and its detached copy.",
178: conv.size(), copyConv.size());
179: Iterator itConv = conv.iterator();
180: Iterator itCopyConv = copyConv.iterator();
181: while (itConv.hasNext() && itCopyConv.hasNext()) {
182: ConventionHelper convention = (ConventionHelper) itConv
183: .next();
184: ConventionHelper copyConvention = (ConventionHelper) itCopyConv
185: .next();
186: assertEquals(
187: "cdmem is not the same for the convention and its detached copy",
188: convention.getCdmem(), copyConvention
189: .getCdmem());
190: assertEquals(
191: "nomep is not the same for the convention and its detached copy",
192: convention.getNomep(), copyConvention
193: .getNomep());
194: SpecialiteMercureHelper specialiteMercure = convention
195: .getSpecialiteMercure();
196: SpecialiteMercureHelper copySpecialiteMercure = copyConvention
197: .getSpecialiteMercure();
198: assertEquals(
199: "cdmed not the same for the specialiteMercure and its detached copy",
200: specialiteMercure.getCdmed(),
201: copySpecialiteMercure.getCdmed());
202: assertEquals(
203: "cdmec not the same for the specialiteMercure and its detached copy",
204: specialiteMercure.getCdmec(),
205: copySpecialiteMercure.getCdmec());
206: assertEquals(
207: "cdmese not the same for the specialiteMercure and its detached copy",
208: specialiteMercure.getCdmese(),
209: copySpecialiteMercure.getCdmese());
210: DomaineHelper domaine = specialiteMercure.getDomaine();
211: DomaineHelper copyDomaine = copySpecialiteMercure
212: .getDomaine();
213: assertEquals(
214: "cdmed not the same for the domaine and its detached copy",
215: domaine.getCdmed(), copyDomaine.getCdmed());
216: assertEquals(
217: "limed not the same for the domaine and its detached copy",
218: domaine.getLimed(), copyDomaine.getLimed());
219: MarqueHelper marque = convention.getMarque();
220: MarqueHelper copyMarque = copyConvention.getMarque();
221: assertEquals(
222: "cdmem is not the same for the marque and its detached copy",
223: marque.getCdmem(), copyMarque.getCdmem());
224: assertEquals(
225: "limem is not the same for the marque and its detached copy",
226: marque.getLimem(), copyMarque.getLimem());
227: }
228:
229: //query
230: Query query = pm.newQuery(ConventionHelper.class);
231: StringBuffer filter = new StringBuffer();
232: filter.append("(intervenant.nomep == (\""
233: + intervenant1.getNomep() + "\")) && ");
234: filter
235: .append("(cdmem == (\"" + marque1.getCdmem()
236: + "\"))");
237: query.setFilter(filter.toString());
238: //fetchplan
239: fp = pm.getFetchPlan().clearGroups();
240: fp.addGroup("listeConventions");
241: //execute
242: Collection results = (Collection) query.execute();
243: //detach all the conventions retrieved
244: Collection detachedConv = pm.detachCopyAll(results);
245: Iterator itDC = detachedConv.iterator();
246: while (itDC.hasNext()) {
247: ConventionHelper dConv = (ConventionHelper) itDC.next();
248: Collection zonesGeo = dConv.getZoneGeographiques();
249: assertEquals(
250: "Size of zoneGeo for convention1 and its detached copy si not the same.",
251: convention1.getZoneGeographiques().size(),
252: zonesGeo.size());
253: Iterator itZg = zonesGeo.iterator();
254: while (itZg.hasNext()) {
255: ZoneGeographiqueHelper zoneGeo = (ZoneGeographiqueHelper) itZg
256: .next();
257: assertNotNull("Commune null for zoneGeo "
258: + zoneGeo.getId() + ".", zoneGeo
259: .getCommune());
260: logger.log(BasicLevel.DEBUG, "Commune of zg"
261: + zoneGeo.getId() + ": "
262: + zoneGeo.getCommune().getDummy());
263: }
264: SpecialiteMercureHelper dSpecialiteMercure = dConv
265: .getSpecialiteMercure();
266: assertEquals(
267: "cdmed not the same for the specialiteMercure and its detached copy",
268: specialiteMercure1.getCdmed(),
269: dSpecialiteMercure.getCdmed());
270: assertEquals(
271: "cdmec not the same for the specialiteMercure and its detached copy",
272: specialiteMercure1.getCdmec(),
273: dSpecialiteMercure.getCdmec());
274: assertEquals(
275: "cdmese not the same for the specialiteMercure and its detached copy",
276: specialiteMercure1.getCdmese(),
277: dSpecialiteMercure.getCdmese());
278: DomaineHelper dDomaine = dSpecialiteMercure
279: .getDomaine();
280: assertEquals(
281: "cdmed not the same for the domaine and its detached copy",
282: domaine1.getCdmed(), dDomaine.getCdmed());
283: assertEquals(
284: "limed not the same for the domaine and its detached copy",
285: domaine1.getLimed(), dDomaine.getLimed());
286: }
287: query.closeAll();
288: } catch (Exception e) {
289: e.printStackTrace();
290: fail(e.getMessage());
291: } finally {
292: if (pm.currentTransaction().isActive())
293: pm.currentTransaction().rollback();
294: pm.close();
295: }
296: }
297:
298: public void testRemovingOfPersistentObject() {
299: PersistenceManager pm = pmf.getPersistenceManager();
300: try {
301: Class[] cs = new Class[] { ConventionHelper.class,
302: IntervenantHelper.class, MarqueHelper.class,
303: CommuneHelper.class, ZoneGeographiqueHelper.class,
304: DomaineHelper.class, SpecialiteMercureHelper.class };
305: pm.currentTransaction().begin();
306: for (int i = 0; i < cs.length; i++) {
307: Query query = pm.newQuery(cs[i]);
308: Collection col = (Collection) query.execute();
309: Iterator it = col.iterator();
310: while (it.hasNext()) {
311: Object o = it.next();
312: Assert.assertNotNull(
313: "null object in the query result"
314: + cs[i].getName(), o);
315: pm.deletePersistent(o);
316:
317: }
318: query.close(col);
319: }
320: pm.currentTransaction().commit();
321: } catch (JDOException e) {
322: Exception ie = ExceptionHelper.getNested(e);
323: logger.log(BasicLevel.ERROR, "", ie);
324: fail(ie.getMessage());
325: } finally {
326: pm.close();
327: }
328: }
329: }
|