KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 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 TestEjboo2 extends SpeedoTestHelper {
42
43     public TestEjboo2(String JavaDoc s) {
44         super(s);
45     }
46
47     protected String JavaDoc 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) { //pair
74
a = new ServicePersistantImpl(idArt);
75                 a.setNom("article_service_" + idArt);
76             } else { //impair
77
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 JavaDoc it = extent.iterator();
94         ArrayList JavaDoc errors = new ArrayList JavaDoc();
95         while(it.hasNext()) {
96             cat = (CataloguePersistantImpl) it.next();
97             logger.log(BasicLevel.DEBUG, "Catalogue " + cat.getId());
98             Collection JavaDoc arts = cat.getArticles();
99             Iterator JavaDoc articles = arts.iterator();
100             while(articles.hasNext()) {
101                 a = (ArticlePersistantImpl) articles.next();
102                 logger.log(BasicLevel.DEBUG, "\tArticle " + a.getId());
103                 Collection JavaDoc mars = a.getMarches();
104                 Iterator JavaDoc marches = mars.iterator();
105                 while (marches.hasNext()) {
106                     mar = (MarchePersistantImpl) marches.next();
107                     logger.log(BasicLevel.DEBUG, "\t\tMarche " + mar.getId());
108                     Collection JavaDoc m2as = mar.getArticles();
109                     if (!m2as.contains(a)) {
110                         errors.add(new Exception JavaDoc("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 JavaDoc iter = errors.iterator(); iter.hasNext();) {
121                 Exception JavaDoc e = (Exception JavaDoc) 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 JavaDoc l = (Long JavaDoc) query.executeWithArray(new Object JavaDoc[] { "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 JavaDoc) query.executeWithArray(new Object JavaDoc[] { "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 JavaDoc((Collection JavaDoc) 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