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.ejboo.Article;
020: import org.objectweb.speedo.pobjects.inheritance.ejboo.Catalogue;
021: import org.objectweb.speedo.pobjects.inheritance.ejboo.Marche;
022: import org.objectweb.speedo.pobjects.inheritance.ejboo.Service;
023: import org.objectweb.speedo.pobjects.inheritance.ejboo.Materiel;
024: import org.objectweb.speedo.SpeedoTestHelper;
025: import org.objectweb.util.monolog.api.BasicLevel;
026:
027: import javax.jdo.PersistenceManager;
028: import javax.jdo.Extent;
029: import javax.jdo.Query;
030:
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.Iterator;
034: import java.util.Collection;
035:
036: /**
037: *
038: * @author S.Chassande-Barrioz
039: */
040: public class TestEjboo extends SpeedoTestHelper {
041:
042: public TestEjboo(String s) {
043: super (s);
044: }
045:
046: protected String getLoggerName() {
047: return LOG_NAME + "rt.inheritance.TesEjboo";
048: }
049:
050: public void testA() {
051: final int NB_ARTICLE = 10;
052: final int CATLAOGUE_SIZE = 2;
053: final int MARCHE_SIZE = 2;
054: PersistenceManager pm = pmf.getPersistenceManager();
055: pm.currentTransaction().begin();
056: Catalogue cat = null;
057: int nbCat = 0;
058: Marche mar = null;
059: int nbMar = 0;
060: Article a;
061: for (int idArt = 0; idArt < NB_ARTICLE; idArt++) {
062: if ((idArt / CATLAOGUE_SIZE) == nbCat) {
063: cat = new Catalogue();
064: pm.makePersistent(cat);
065: nbCat++;
066: }
067: if ((idArt / MARCHE_SIZE) == nbMar) {
068: mar = new Marche();
069: pm.makePersistent(mar);
070: nbMar++;
071: }
072: if ((idArt % 2) == 0) { //pair
073: a = new Service(idArt);
074: } else { //impair
075: a = new Materiel(idArt);
076: }
077: pm.makePersistent(a);
078: a.setCatalogue(cat);
079: mar.getArticles().add(a);
080: }
081: pm.currentTransaction().commit();
082:
083: a = null;
084: cat = null;
085: mar = null;
086: pm.evictAll();
087:
088: Article a2 = null;
089:
090: Extent extent = pm.getExtent(Catalogue.class, true);
091: Iterator it = extent.iterator();
092: while (it.hasNext()) {
093: cat = (Catalogue) it.next();
094: logger.log(BasicLevel.DEBUG, "Catalogue " + cat.getId());
095: Collection arts = cat.getArticles();
096: Iterator articles = arts.iterator();
097: while (articles.hasNext()) {
098: a2 = a;
099: a = (Article) articles.next();
100: logger.log(BasicLevel.DEBUG, "\tArticle " + a.getId());
101: Collection mars = a.getMarches();
102: Iterator marches = mars.iterator();
103: while (marches.hasNext()) {
104: mar = (Marche) marches.next();
105: logger.log(BasicLevel.DEBUG, "\t\tMarche "
106: + mar.getId());
107: Collection m2as = mar.getArticles();
108: assertTrue("The article '" + a.getId()
109: + "' is not in the collection marche("
110: + mar.getId() + ").articles", m2as
111: .contains(a));
112: }
113: }
114: }
115: extent.closeAll();
116:
117: pm.currentTransaction().begin();
118: Query q = pm.newQuery(Catalogue.class);
119: q.setResult("distinct this");
120: q
121: .setFilter("articles.contains(a) && a.marches.contains(m) && m.id==MID");
122: q.declareParameters("long MID");
123: q.declareVariables("Marche m;Article a");
124: Collection c = (Collection) q.execute(new Long(mar.getId()));
125: Collection expectedResults = Collections.singletonList(cat);
126: assertSameCollection(
127: "Collection of results is not the one expected",
128: expectedResults, c);
129: q.closeAll();
130: pm.currentTransaction().commit();
131:
132: a = null;
133: cat = null;
134: mar = null;
135: pm.currentTransaction().begin();
136: extent = pm.getExtent(Article.class, true);
137: it = extent.iterator();
138: while (it.hasNext()) {
139: a = (Article) it.next();
140: cat = a.getCatalogue();
141: if (cat != null) {
142: pm.deletePersistent(cat);
143: }
144: pm.deletePersistentAll(a.getMarches());
145: pm.deletePersistent(a);
146: }
147: pm.currentTransaction().commit();
148: pm.close();
149: }
150: }
|