001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.inheritance;
018:
019: import org.objectweb.speedo.pobjects.inheritance.ejboo2.ArticlePersistantImpl;
020: import org.objectweb.speedo.pobjects.inheritance.ejboo2.CataloguePersistant;
021: import org.objectweb.speedo.pobjects.inheritance.ejboo2.CataloguePersistantImpl;
022: import org.objectweb.speedo.pobjects.inheritance.ejboo2.MarchePersistantImpl;
023: import org.objectweb.speedo.pobjects.inheritance.ejboo2.ServicePersistantImpl;
024: import org.objectweb.speedo.pobjects.inheritance.ejboo2.MaterielPersistantImpl;
025: import org.objectweb.speedo.SpeedoTestHelper;
026: import org.objectweb.util.monolog.api.BasicLevel;
027:
028: import javax.jdo.PersistenceManager;
029: import javax.jdo.Extent;
030: import javax.jdo.Query;
031:
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.Collection;
035:
036: /**
037: *
038: * @author S.Chassande-Barrioz
039: */
040: public class TestEjboo2 extends SpeedoTestHelper {
041:
042: public TestEjboo2(String s) {
043: super (s);
044: }
045:
046: protected String getLoggerName() {
047: return LOG_NAME + "rt.inheritance.TesEjboo2";
048: }
049:
050: public void testA() {
051: final int NB_ARTICLE = 6;
052: final int CATLAOGUE_SIZE = 3;
053: final int MARCHE_SIZE = 2;
054: PersistenceManager pm = pmf.getPersistenceManager();
055: pm.currentTransaction().begin();
056: CataloguePersistant cat = null;
057: int nbCat = 0;
058: MarchePersistantImpl mar = null;
059: int nbMar = 0;
060: ArticlePersistantImpl a;
061: for (int idArt = 0; idArt < NB_ARTICLE; idArt++) {
062: if ((idArt / CATLAOGUE_SIZE) == nbCat) {
063: cat = new CataloguePersistantImpl(nbCat);
064: pm.makePersistent(cat);
065: nbCat++;
066: }
067: if ((idArt / MARCHE_SIZE) == nbMar) {
068: mar = new MarchePersistantImpl(nbMar);
069: pm.makePersistent(mar);
070: nbMar++;
071: }
072: if ((idArt % 2) == 0) { //pair
073: a = new ServicePersistantImpl(idArt);
074: a.setNom("article_service_" + idArt);
075: } else { //impair
076: a = new MaterielPersistantImpl(idArt);
077: a.setNom("article_materiel_" + idArt);
078: }
079: pm.makePersistent(a);
080: a.setCatalogue(cat);
081: mar.getArticles().add(a);
082: }
083: pm.currentTransaction().commit();
084:
085: a = null;
086: cat = null;
087: mar = null;
088: pm.evictAll();
089:
090: pm.currentTransaction().begin();
091: Extent extent = pm.getExtent(CataloguePersistantImpl.class,
092: true);
093: Iterator it = extent.iterator();
094: ArrayList errors = new ArrayList();
095: while (it.hasNext()) {
096: cat = (CataloguePersistantImpl) it.next();
097: logger.log(BasicLevel.DEBUG, "Catalogue " + cat.getId());
098: Collection arts = cat.getArticles();
099: 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 "
108: + mar.getId());
109: Collection m2as = mar.getArticles();
110: if (!m2as.contains(a)) {
111: errors.add(new Exception("The article '"
112: + a.getId()
113: + "' is not in the collection marche("
114: + mar.getId() + ").articles"));
115: }
116: }
117: }
118: }
119: extent.closeAll();
120: pm.currentTransaction().commit();
121: if (!errors.isEmpty()) {
122: for (Iterator iter = errors.iterator(); iter.hasNext();) {
123: Exception e = (Exception) iter.next();
124: logger.log(BasicLevel.ERROR, e.getMessage());
125: }
126: fail(errors.size() + " error(s)");
127: }
128: a = null;
129: mar = null;
130:
131: pm.currentTransaction().begin();
132: Query query = pm.newQuery(ArticlePersistantImpl.class);
133: query.declareParameters("String p1,CataloguePersistantImpl p2");
134: query.setFilter("(nom.startsWith(p1) && (catalogue == p2))");
135: query.setResult("count(*)");
136: query.setUnique(true);
137: query.setRange(0, 2);
138: Long l = (Long) query.executeWithArray(new Object[] {
139: "article", cat });
140: assertTrue("", l.longValue() > 0);
141: query.closeAll();
142: pm.currentTransaction().commit();
143:
144: pm.currentTransaction().begin();
145: query = pm.newQuery(ArticlePersistantImpl.class);
146: query.declareParameters("String p1,CataloguePersistantImpl p2");
147: query.setFilter("(nom.startsWith(p1) && (catalogue == p2))");
148: query.setResult("count(*)");
149: query.setUnique(true);
150: query.setRange(0, 5);
151: l = (Long) query
152: .executeWithArray(new Object[] { "article", cat });
153: query.closeAll();
154: assertTrue("", l.longValue() > 0);
155: pm.currentTransaction().commit();
156:
157: pm.currentTransaction().begin();
158: query = pm.newQuery(ArticlePersistantImpl.class);
159: query.setRange(0, 2);
160: new ArrayList((Collection) query.execute());
161: query.closeAll();
162: pm.currentTransaction().commit();
163:
164: cat = null;
165:
166: pm.currentTransaction().begin();
167: extent = pm.getExtent(ArticlePersistantImpl.class, true);
168: it = extent.iterator();
169: while (it.hasNext()) {
170: a = (ArticlePersistantImpl) it.next();
171: cat = a.getCatalogue();
172: if (cat != null) {
173: pm.deletePersistent(cat);
174: }
175: pm.deletePersistentAll(a.getMarches());
176: pm.deletePersistent(a);
177: }
178: pm.currentTransaction().commit();
179: pm.close();
180: }
181: }
|