/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
*/
package org.objectweb.speedo.runtime.detach;
import java.util.Collection;
import java.util.Iterator;
import javax.jdo.FetchPlan;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import junit.framework.Assert;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.detach.groupama.CommuneHelper;
import org.objectweb.speedo.pobjects.detach.groupama.ConventionHelper;
import org.objectweb.speedo.pobjects.detach.groupama.DomaineHelper;
import org.objectweb.speedo.pobjects.detach.groupama.IntervenantHelper;
import org.objectweb.speedo.pobjects.detach.groupama.MarqueHelper;
import org.objectweb.speedo.pobjects.detach.groupama.SpecialiteMercureHelper;
import org.objectweb.speedo.pobjects.detach.groupama.ZoneGeographiqueHelper;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* Test the detach method on 1<->N<->1 relationships
* @author Y.Bersihand
*/
public class TestDetach1N1 extends SpeedoTestHelper {
public TestDetach1N1(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.detach.TestDetach1N1";
}
/**
* Test the detach method: make an object persistent, and detach it out of an active transaction.
*/
public void testCreateObjects() {
logger.log(BasicLevel.DEBUG, "***************testDetach1N1*****************");
//create 2 marques
MarqueHelper marque1 = new MarqueHelper();
marque1.setCdmem("1");
marque1.setLimem("marque1");
MarqueHelper marque2 = new MarqueHelper();
marque2.setCdmem("2");
marque2.setLimem("marque2");
//create 1 Intervenant
IntervenantHelper intervenant1 = new IntervenantHelper();
intervenant1.setNomep(1);
//create 2 Conventions
ConventionHelper convention1 = new ConventionHelper();
// marque 1 <-> * convention
// intervenant 1 <-> * convention
convention1.setIntervenant(intervenant1);
convention1.setNomep(intervenant1.getNomep());
convention1.setMarque(marque1);
convention1.setCdmem(marque1.getCdmem());
marque1.addConvention(convention1);
intervenant1.addConvention(convention1);
ConventionHelper convention2 = new ConventionHelper();
convention2.setIntervenant(intervenant1);
convention2.setNomep(intervenant1.getNomep());
convention2.setMarque(marque2);
convention2.setCdmem(marque2.getCdmem());
marque2.addConvention(convention2);
intervenant1.addConvention(convention2);
//create 2 communes
CommuneHelper commune1 = new CommuneHelper();
commune1.setId(1);
commune1.setDummy("commune1");
CommuneHelper commune2 = new CommuneHelper();
commune2.setId(2);
commune2.setDummy("commune2");
//create 2 zg
ZoneGeographiqueHelper zg1 = new ZoneGeographiqueHelper();
zg1.setId(1);
// commune 1 <-> * zonegeographique
zg1.setCommune(commune1);
commune1.addZoneGeographique(zg1);
ZoneGeographiqueHelper zg2 = new ZoneGeographiqueHelper();
zg2.setId(2);
// commune 1 <-> * zonegeographique
zg2.setCommune(commune2);
commune2.addZoneGeographique(zg2);
// convention 1 <-> * zonegeographique
convention1.addZoneGeographique(zg1);
zg1.setConvention(convention1);
convention1.addZoneGeographique(zg2);
zg2.setConvention(convention2);
//create a domaine
DomaineHelper domaine1 = new DomaineHelper();
domaine1.setCdmed(1);
domaine1.setLimed(1);
SpecialiteMercureHelper specialiteMercure1 = new SpecialiteMercureHelper();
specialiteMercure1.setCdmec(1);
specialiteMercure1.setCdmed(1);
specialiteMercure1.setCdmese(1);
// spcecialiteMercure * <-> 1 domaine
specialiteMercure1.setDomaine(domaine1);
domaine1.addSpecialiteMercure(specialiteMercure1);
// spcecialiteMercure 1 <-> * convention
specialiteMercure1.addConvention(convention1);
convention1.setSpecialiteMercure(specialiteMercure1);
SpecialiteMercureHelper specialiteMercure2 = new SpecialiteMercureHelper();
specialiteMercure1.setCdmec(2);
specialiteMercure1.setCdmed(1);
specialiteMercure1.setCdmese(2);
// spcecialiteMercure * <-> 1 domaine
specialiteMercure2.setDomaine(domaine1);
domaine1.addSpecialiteMercure(specialiteMercure2);
// spcecialiteMercure 1 <-> * convention
specialiteMercure2.addConvention(convention2);
convention2.setSpecialiteMercure(specialiteMercure2);
PersistenceManager pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the 2 conventions");
pm.makePersistent(convention1);
pm.makePersistent(convention2);
pm.currentTransaction().commit();
//set the fetch group to listeIntervenants
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups().addGroup("listeIntervenants");
//detach the intervenant
IntervenantHelper copyOfIntervenant1 = (IntervenantHelper) pm.detachCopy(intervenant1);
assertNotNull(copyOfIntervenant1);
assertEquals("Nomep of intervenant1 and its detached copy are not the same.", intervenant1.getNomep(), copyOfIntervenant1.getNomep());
Collection conv = intervenant1.getConventions();
Collection copyConv = copyOfIntervenant1.getConventions();
assertEquals("Size of conventions not the same for intervenant1 and its detached copy.", conv.size(), copyConv.size());
Iterator itConv = conv.iterator();
Iterator itCopyConv = copyConv.iterator();
while (itConv.hasNext() && itCopyConv.hasNext()) {
ConventionHelper convention = (ConventionHelper) itConv.next();
ConventionHelper copyConvention = (ConventionHelper) itCopyConv.next();
assertEquals("cdmem is not the same for the convention and its detached copy", convention.getCdmem(), copyConvention.getCdmem());
assertEquals("nomep is not the same for the convention and its detached copy", convention.getNomep(), copyConvention.getNomep());
SpecialiteMercureHelper specialiteMercure = convention.getSpecialiteMercure();
SpecialiteMercureHelper copySpecialiteMercure = copyConvention.getSpecialiteMercure();
assertEquals("cdmed not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmed(), copySpecialiteMercure.getCdmed());
assertEquals("cdmec not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmec(), copySpecialiteMercure.getCdmec());
assertEquals("cdmese not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmese(), copySpecialiteMercure.getCdmese());
DomaineHelper domaine = specialiteMercure.getDomaine();
DomaineHelper copyDomaine = copySpecialiteMercure.getDomaine();
assertEquals("cdmed not the same for the domaine and its detached copy", domaine.getCdmed(), copyDomaine.getCdmed());
assertEquals("limed not the same for the domaine and its detached copy", domaine.getLimed(), copyDomaine.getLimed());
MarqueHelper marque = convention.getMarque();
MarqueHelper copyMarque = copyConvention.getMarque();
assertEquals("cdmem is not the same for the marque and its detached copy", marque.getCdmem(), copyMarque.getCdmem());
assertEquals("limem is not the same for the marque and its detached copy", marque.getLimem(), copyMarque.getLimem());
}
//query
Query query = pm.newQuery(ConventionHelper.class);
StringBuffer filter = new StringBuffer();
filter.append("(intervenant.nomep == (\"" +
intervenant1.getNomep() + "\")) && ");
filter.append("(cdmem == (\"" + marque1.getCdmem() + "\"))");
query.setFilter(filter.toString());
//fetchplan
fp = pm.getFetchPlan().clearGroups();
fp.addGroup("listeConventions");
//execute
Collection results = (Collection) query.execute();
//detach all the conventions retrieved
Collection detachedConv = pm.detachCopyAll(results);
Iterator itDC = detachedConv.iterator();
while (itDC.hasNext()) {
ConventionHelper dConv = (ConventionHelper) itDC.next();
Collection zonesGeo = dConv.getZoneGeographiques();
assertEquals("Size of zoneGeo for convention1 and its detached copy si not the same.", convention1.getZoneGeographiques().size(), zonesGeo.size());
Iterator itZg = zonesGeo.iterator();
while (itZg.hasNext()) {
ZoneGeographiqueHelper zoneGeo = (ZoneGeographiqueHelper) itZg.next();
assertNotNull("Commune null for zoneGeo " + zoneGeo.getId() + ".", zoneGeo.getCommune());
logger.log(BasicLevel.DEBUG, "Commune of zg" + zoneGeo.getId() + ": " + zoneGeo.getCommune().getDummy());
}
SpecialiteMercureHelper dSpecialiteMercure = dConv.getSpecialiteMercure();
assertEquals("cdmed not the same for the specialiteMercure and its detached copy", specialiteMercure1.getCdmed(), dSpecialiteMercure.getCdmed());
assertEquals("cdmec not the same for the specialiteMercure and its detached copy", specialiteMercure1.getCdmec(), dSpecialiteMercure.getCdmec());
assertEquals("cdmese not the same for the specialiteMercure and its detached copy", specialiteMercure1.getCdmese(), dSpecialiteMercure.getCdmese());
DomaineHelper dDomaine = dSpecialiteMercure.getDomaine();
assertEquals("cdmed not the same for the domaine and its detached copy", domaine1.getCdmed(), dDomaine.getCdmed());
assertEquals("limed not the same for the domaine and its detached copy", domaine1.getLimed(), dDomaine.getLimed());
}
query.closeAll();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
public void testRemovingOfPersistentObject() {
PersistenceManager pm = pmf.getPersistenceManager();
try {
Class[] cs = new Class[]{ConventionHelper.class, IntervenantHelper.class, MarqueHelper.class
, CommuneHelper.class, ZoneGeographiqueHelper.class, DomaineHelper.class, SpecialiteMercureHelper.class};
pm.currentTransaction().begin();
for(int i=0; i<cs.length; i++) {
Query query = pm.newQuery(cs[i]);
Collection col = (Collection) query.execute();
Iterator it = col.iterator();
while(it.hasNext()) {
Object o = it.next();
Assert.assertNotNull("null object in the query result"
+ cs[i].getName(), o);
pm.deletePersistent(o);
}
query.close(col);
}
pm.currentTransaction().commit();
} catch (JDOException e) {
Exception ie = ExceptionHelper.getNested(e);
logger.log(BasicLevel.ERROR, "", ie);
fail(ie.getMessage());
} finally {
pm.close();
}
}
}