1 25 26 package org.objectweb.speedo.runtime.detach; 27 28 29 import java.util.Collection ; 30 import java.util.Iterator ; 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 54 public class TestDetach1N1 extends SpeedoTestHelper { 55 56 public TestDetach1N1(String s) { 57 super(s); 58 } 59 60 protected String getLoggerName() { 61 return LOG_NAME + ".rt.detach.TestDetach1N1"; 62 } 63 64 67 public void testCreateObjects() { 68 logger.log(BasicLevel.DEBUG, "***************testDetach1N1*****************"); 69 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 IntervenantHelper intervenant1 = new IntervenantHelper(); 80 intervenant1.setNomep(1); 81 82 ConventionHelper convention1 = new ConventionHelper(); 84 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 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 ZoneGeographiqueHelper zg1 = new ZoneGeographiqueHelper(); 111 zg1.setId(1); 112 zg1.setCommune(commune1); 114 commune1.addZoneGeographique(zg1); 115 ZoneGeographiqueHelper zg2 = new ZoneGeographiqueHelper(); 116 zg2.setId(2); 117 zg2.setCommune(commune2); 119 commune2.addZoneGeographique(zg2); 120 121 convention1.addZoneGeographique(zg1); 123 zg1.setConvention(convention1); 124 convention1.addZoneGeographique(zg2); 125 zg2.setConvention(convention2); 126 127 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 specialiteMercure1.setDomaine(domaine1); 138 domaine1.addSpecialiteMercure(specialiteMercure1); 139 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 specialiteMercure2.setDomaine(domaine1); 149 domaine1.addSpecialiteMercure(specialiteMercure2); 150 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 FetchPlan fp = pm.getFetchPlan(); 166 fp.clearGroups().addGroup("listeIntervenants"); 167 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 conv = intervenant1.getConventions(); 172 Collection copyConv = copyOfIntervenant1.getConventions(); 173 assertEquals("Size of conventions not the same for intervenant1 and its detached copy.", conv.size(), copyConv.size()); 174 Iterator itConv = conv.iterator(); 175 Iterator 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 query = pm.newQuery(ConventionHelper.class); 198 StringBuffer filter = new StringBuffer (); 199 filter.append("(intervenant.nomep == (\"" + 200 intervenant1.getNomep() + "\")) && "); 201 filter.append("(cdmem == (\"" + marque1.getCdmem() + "\"))"); 202 query.setFilter(filter.toString()); 203 fp = pm.getFetchPlan().clearGroups(); 205 fp.addGroup("listeConventions"); 206 Collection results = (Collection ) query.execute(); 208 Collection detachedConv = pm.detachCopyAll(results); 210 Iterator itDC = detachedConv.iterator(); 211 while (itDC.hasNext()) { 212 ConventionHelper dConv = (ConventionHelper) itDC.next(); 213 Collection 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 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 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 [] cs = new Class []{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 col = (Collection ) query.execute(); 250 Iterator it = col.iterator(); 251 while(it.hasNext()) { 252 Object 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 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 |