1 18 package org.objectweb.speedo.runtime.inheritance; 19 20 import org.objectweb.speedo.pobjects.inheritance.ejboo2.ArticlePersistantImpl; 21 import org.objectweb.speedo.pobjects.inheritance.ejboo2.CataloguePersistant; 22 import org.objectweb.speedo.pobjects.inheritance.ejboo2.CataloguePersistantImpl; 23 import org.objectweb.speedo.pobjects.inheritance.ejboo2.MarchePersistantImpl; 24 import org.objectweb.speedo.pobjects.inheritance.ejboo2.ServicePersistantImpl; 25 import org.objectweb.speedo.pobjects.inheritance.ejboo2.MaterielPersistantImpl; 26 import org.objectweb.speedo.SpeedoTestHelper; 27 import org.objectweb.util.monolog.api.BasicLevel; 28 29 import javax.jdo.PersistenceManager; 30 import javax.jdo.Extent; 31 import javax.jdo.Query; 32 33 import java.util.ArrayList ; 34 import java.util.Iterator ; 35 import java.util.Collection ; 36 37 41 public class TestEjboo2 extends SpeedoTestHelper { 42 43 public TestEjboo2(String s) { 44 super(s); 45 } 46 47 protected String getLoggerName() { 48 return LOG_NAME + "rt.inheritance.TesEjboo2"; 49 } 50 51 public void testA() { 52 final int NB_ARTICLE = 6; 53 final int CATLAOGUE_SIZE = 3; 54 final int MARCHE_SIZE = 2; 55 PersistenceManager pm = pmf.getPersistenceManager(); 56 pm.currentTransaction().begin(); 57 CataloguePersistant cat = null; 58 int nbCat = 0; 59 MarchePersistantImpl mar= null; 60 int nbMar = 0; 61 ArticlePersistantImpl a; 62 for(int idArt=0; idArt<NB_ARTICLE; idArt++) { 63 if ((idArt / CATLAOGUE_SIZE) == nbCat) { 64 cat = new CataloguePersistantImpl(nbCat); 65 pm.makePersistent(cat); 66 nbCat ++; 67 } 68 if ((idArt / MARCHE_SIZE) == nbMar) { 69 mar = new MarchePersistantImpl(nbMar); 70 pm.makePersistent(mar); 71 nbMar ++; 72 } 73 if ((idArt % 2) == 0) { a = new ServicePersistantImpl(idArt); 75 a.setNom("article_service_" + idArt); 76 } else { a = new MaterielPersistantImpl(idArt); 78 a.setNom("article_materiel_" + idArt); 79 } 80 pm.makePersistent(a); 81 a.setCatalogue(cat); 82 mar.getArticles().add(a); 83 } 84 pm.currentTransaction().commit(); 85 86 a = null; 87 cat = null; 88 mar = null; 89 pm.evictAll(); 90 91 pm.currentTransaction().begin(); 92 Extent extent = pm.getExtent(CataloguePersistantImpl.class, true); 93 Iterator it = extent.iterator(); 94 ArrayList errors = new ArrayList (); 95 while(it.hasNext()) { 96 cat = (CataloguePersistantImpl) it.next(); 97 logger.log(BasicLevel.DEBUG, "Catalogue " + cat.getId()); 98 Collection arts = cat.getArticles(); 99 Iterator articles = arts.iterator(); 100 while(articles.hasNext()) { 101 a = (ArticlePersistantImpl) articles.next(); 102 logger.log(BasicLevel.DEBUG, "\tArticle " + a.getId()); 103 Collection mars = a.getMarches(); 104 Iterator marches = mars.iterator(); 105 while (marches.hasNext()) { 106 mar = (MarchePersistantImpl) marches.next(); 107 logger.log(BasicLevel.DEBUG, "\t\tMarche " + mar.getId()); 108 Collection m2as = mar.getArticles(); 109 if (!m2as.contains(a)) { 110 errors.add(new Exception ("The article '" + a.getId() 111 + "' is not in the collection marche(" + mar.getId() 112 + ").articles")); 113 } 114 } 115 } 116 } 117 extent.closeAll(); 118 pm.currentTransaction().commit(); 119 if (!errors.isEmpty()) { 120 for (Iterator iter = errors.iterator(); iter.hasNext();) { 121 Exception e = (Exception ) iter.next(); 122 logger.log(BasicLevel.ERROR, e.getMessage()); 123 } 124 fail(errors.size() + " error(s)"); 125 } 126 a = null; 127 mar = null; 128 129 pm.currentTransaction().begin(); 130 Query query = pm.newQuery(ArticlePersistantImpl.class); 131 query.declareParameters("String p1,CataloguePersistantImpl p2"); 132 query.setFilter("(nom.startsWith(p1) && (catalogue == p2))"); 133 query.setResult("count(*)"); 134 query.setUnique(true); 135 query.setRange(0,2); 136 Long l = (Long ) query.executeWithArray(new Object [] { "article", cat }); 137 assertTrue("", l.longValue() > 0); 138 query.closeAll(); 139 pm.currentTransaction().commit(); 140 141 pm.currentTransaction().begin(); 142 query = pm.newQuery(ArticlePersistantImpl.class); 143 query.declareParameters("String p1,CataloguePersistantImpl p2"); 144 query.setFilter("(nom.startsWith(p1) && (catalogue == p2))"); 145 query.setResult("count(*)"); 146 query.setUnique(true); 147 query.setRange(0,5); 148 l = (Long ) query.executeWithArray(new Object [] { "article", cat }); 149 query.closeAll(); 150 assertTrue("", l.longValue() > 0); 151 pm.currentTransaction().commit(); 152 153 pm.currentTransaction().begin(); 154 query = pm.newQuery(ArticlePersistantImpl.class); 155 query.setRange(0,2); 156 new ArrayList ((Collection ) query.execute()); 157 query.closeAll(); 158 pm.currentTransaction().commit(); 159 160 cat = null; 161 162 pm.currentTransaction().begin(); 163 extent = pm.getExtent(ArticlePersistantImpl.class, true); 164 it = extent.iterator(); 165 while (it.hasNext()) { 166 a = (ArticlePersistantImpl) it.next(); 167 cat = a.getCatalogue(); 168 if (cat != null) { 169 pm.deletePersistent(cat); 170 } 171 pm.deletePersistentAll(a.getMarches()); 172 pm.deletePersistent(a); 173 } 174 pm.currentTransaction().commit(); 175 pm.close(); 176 } 177 } 178 | Popular Tags |