KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > inheritance > TestEjboo


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.runtime.inheritance;
19
20 import org.objectweb.speedo.pobjects.inheritance.ejboo.Article;
21 import org.objectweb.speedo.pobjects.inheritance.ejboo.Catalogue;
22 import org.objectweb.speedo.pobjects.inheritance.ejboo.Marche;
23 import org.objectweb.speedo.pobjects.inheritance.ejboo.Service;
24 import org.objectweb.speedo.pobjects.inheritance.ejboo.Materiel;
25 import org.objectweb.speedo.SpeedoTestHelper;
26 import org.objectweb.util.monolog.api.BasicLevel;
27
28 import javax.jdo.PersistenceManager;
29 import javax.jdo.Extent;
30 import javax.jdo.Query;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Collection JavaDoc;
36
37 /**
38  *
39  * @author S.Chassande-Barrioz
40  */

41 public class TestEjboo extends SpeedoTestHelper {
42
43     public TestEjboo(String JavaDoc s) {
44         super(s);
45     }
46
47     protected String JavaDoc getLoggerName() {
48         return LOG_NAME + "rt.inheritance.TesEjboo";
49     }
50
51     public void testA() {
52         final int NB_ARTICLE = 10;
53         final int CATLAOGUE_SIZE = 2;
54         final int MARCHE_SIZE = 2;
55         PersistenceManager pm = pmf.getPersistenceManager();
56         pm.currentTransaction().begin();
57         Catalogue cat = null;
58         int nbCat = 0;
59         Marche mar= null;
60         int nbMar = 0;
61         Article a;
62         for(int idArt=0; idArt<NB_ARTICLE; idArt++) {
63             if ((idArt / CATLAOGUE_SIZE) == nbCat) {
64                 cat = new Catalogue();
65                 pm.makePersistent(cat);
66                 nbCat ++;
67             }
68             if ((idArt / MARCHE_SIZE) == nbMar) {
69                 mar = new Marche();
70                 pm.makePersistent(mar);
71                 nbMar ++;
72             }
73             if ((idArt % 2) == 0) { //pair
74
a = new Service(idArt);
75             } else { //impair
76
a = new Materiel(idArt);
77             }
78             pm.makePersistent(a);
79             a.setCatalogue(cat);
80             mar.getArticles().add(a);
81         }
82         pm.currentTransaction().commit();
83
84         a = null;
85         cat = null;
86         mar = null;
87         pm.evictAll();
88
89         Article a2 = null;
90         
91         Extent extent = pm.getExtent(Catalogue.class, true);
92         Iterator JavaDoc it = extent.iterator();
93         while(it.hasNext()) {
94             cat = (Catalogue) it.next();
95             logger.log(BasicLevel.DEBUG, "Catalogue " + cat.getId());
96             Collection JavaDoc arts = cat.getArticles();
97             Iterator JavaDoc articles = arts.iterator();
98             while(articles.hasNext()) {
99                 a2 = a;
100                 a = (Article) articles.next();
101                 logger.log(BasicLevel.DEBUG, "\tArticle " + a.getId());
102                 Collection JavaDoc mars = a.getMarches();
103                 Iterator JavaDoc marches = mars.iterator();
104                 while (marches.hasNext()) {
105                     mar = (Marche) marches.next();
106                     logger.log(BasicLevel.DEBUG, "\t\tMarche " + mar.getId());
107                     Collection JavaDoc m2as = mar.getArticles();
108                     assertTrue("The article '" + a.getId()
109                         + "' is not in the collection marche(" + mar.getId()
110                         + ").articles", m2as.contains(a));
111                 }
112             }
113         }
114         extent.closeAll();
115
116         pm.currentTransaction().begin();
117         Query q = pm.newQuery(Catalogue.class);
118         q.setResult("distinct this");
119         q.setFilter("articles.contains(a) && a.marches.contains(m) && m.id==MID");
120         q.declareParameters("long MID");
121         q.declareVariables("Marche m;Article a");
122         Collection JavaDoc c = (Collection JavaDoc) q.execute(new Long JavaDoc(mar.getId()));
123         Collection JavaDoc expectedResults = Collections.singletonList(cat);
124         assertSameCollection("Collection of results is not the one expected", expectedResults, c);
125         q.closeAll();
126         pm.currentTransaction().commit();
127         
128         a = null;
129         cat = null;
130         mar = null;
131         pm.currentTransaction().begin();
132         extent = pm.getExtent(Article.class, true);
133         it = extent.iterator();
134         while (it.hasNext()) {
135             a = (Article) it.next();
136             cat = a.getCatalogue();
137             if (cat != null) {
138                 pm.deletePersistent(cat);
139             }
140             pm.deletePersistentAll(a.getMarches());
141             pm.deletePersistent(a);
142         }
143         pm.currentTransaction().commit();
144         pm.close();
145     }
146 }
147
Popular Tags