KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > detach > TestDetachAll


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

25
26 package org.objectweb.speedo.runtime.detach;
27
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.jdo.FetchPlan;
34 import javax.jdo.JDOException;
35 import javax.jdo.PersistenceManager;
36 import javax.jdo.Query;
37
38 import junit.framework.Assert;
39
40 import org.objectweb.speedo.SpeedoTestHelper;
41 import org.objectweb.speedo.api.ExceptionHelper;
42 import org.objectweb.speedo.pobjects.detach.Coach;
43 import org.objectweb.speedo.pobjects.detach.Player;
44 import org.objectweb.speedo.pobjects.detach.Team;
45 import org.objectweb.speedo.pobjects.detach.groupama.A;
46 import org.objectweb.speedo.pobjects.detach.groupama.B;
47 import org.objectweb.speedo.pobjects.detach.groupama.C;
48 import org.objectweb.util.monolog.api.BasicLevel;
49
50 /**
51  * Try to reproduce the bug of Unilog Groupama:
52  * when performing two detachAll on a query result, problem with the cache:
53  * PName already bound to another object
54  * @author Y.Bersihand
55  */

56 public class TestDetachAll extends SpeedoTestHelper {
57
58     public TestDetachAll(String JavaDoc s) {
59         super(s);
60     }
61
62     protected String JavaDoc getLoggerName() {
63         return LOG_NAME + ".rt.detach.TestDetachAll";
64     }
65     
66     /**
67      * Test the detachall method
68      */

69     public void testDetachAllOnce() {
70         logger.log(BasicLevel.DEBUG, "***************testDetachAllOnce*****************");
71         createABC();
72         //detach all once
73
try{
74             Collection JavaDoc as = detachAs();
75             Iterator JavaDoc it1 = as.iterator();
76             logger.log(BasicLevel.DEBUG, "1st detached:");
77             while(it1.hasNext()){
78                 A a = (A) it1.next();
79                 logger.log(BasicLevel.DEBUG, a.toString());
80             }
81         }
82         catch (Exception JavaDoc e) {
83             logger.log(BasicLevel.DEBUG, e.getMessage());
84         }
85     }
86     
87     public void testDetachAllTwice() {
88         logger.log(BasicLevel.DEBUG, "***************testDetachAllTwice*****************");
89         //detach all twice
90
try{
91             Collection JavaDoc as = detachAs();
92             Iterator JavaDoc it1 = as.iterator();
93             logger.log(BasicLevel.DEBUG, "2nd detached:");
94             while(it1.hasNext()){
95                 A a = (A) it1.next();
96                 logger.log(BasicLevel.DEBUG, a.toString());
97             }
98         }
99         catch (Exception JavaDoc e) {
100             logger.log(BasicLevel.DEBUG, e.getMessage());
101         }
102     }
103     
104     public void testRemovingOfPersistentObject() {
105         PersistenceManager pm = pmf.getPersistenceManager();
106         try {
107             Class JavaDoc[] cs = new Class JavaDoc[]{A.class, B.class, C.class,
108                     Coach.class, Player.class, Team.class};
109             pm.currentTransaction().begin();
110             for(int i=0; i<cs.length; i++) {
111                 Query query = pm.newQuery(cs[i]);
112                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
113                 Iterator JavaDoc it = col.iterator();
114                 while(it.hasNext()) {
115                     Object JavaDoc o = it.next();
116                     Assert.assertNotNull("null object in the query result"
117                         + cs[i].getName(), o);
118                     pm.deletePersistent(o);
119
120                 }
121                 query.close(col);
122             }
123             pm.currentTransaction().commit();
124         } catch (JDOException e) {
125             Exception JavaDoc ie = ExceptionHelper.getNested(e);
126             logger.log(BasicLevel.ERROR, "", ie);
127             fail(ie.getMessage());
128         } finally {
129             pm.close();
130         }
131     }
132     private void createPlayers(){
133         Team t = new Team("Bordeaux",null,null);
134         Collection JavaDoc players = new ArrayList JavaDoc();
135         Player p1 = new Player("p1", t, 25);
136         players.add(p1);
137         Player p2 = new Player("p2", t, 32);
138         players.add(p2);
139         t.setPlayers(players);
140         Coach c = new Coach("c1", 5, t);
141         t.setCoach(c);
142         
143         Team t2 = new Team("Nantes", null, null);
144         Collection JavaDoc players2 = new ArrayList JavaDoc();
145         Player p3 = new Player("p3", t2, 25);
146         players2.add(p3);
147         Player p4 = new Player("p4", t2, 32);
148         players2.add(p4);
149         t2.setPlayers(players2);
150         Coach c2 = new Coach("c2", 5, t2);
151         t2.setCoach(c2);
152         
153         PersistenceManager pm = pmf.getPersistenceManager();
154         pm.currentTransaction().begin();
155         logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
156         pm.makePersistent(t);
157         pm.makePersistent(t2);
158         pm.currentTransaction().commit();
159     }
160     
161     private Collection JavaDoc detachTeams() throws Exception JavaDoc{
162         PersistenceManager pm = pmf.getPersistenceManager();
163         try{
164             Query query = pm.newQuery(Team.class);
165             Collection JavaDoc results = (Collection JavaDoc)query.execute();
166             logger.log(BasicLevel.DEBUG, "Teams:");
167             Iterator JavaDoc it = results.iterator();
168             while(it.hasNext()){
169                 Team t = (Team) it.next();
170                 logger.log(BasicLevel.DEBUG, t.toString());
171             }
172             Collection JavaDoc detachedTeams = (Collection JavaDoc) pm.detachCopyAll(results);
173             query.closeAll();
174             return detachedTeams;
175         }
176         catch(Exception JavaDoc e){
177             logger.log(BasicLevel.DEBUG, e.toString());
178             return null;
179         }
180         finally{
181             try {
182                 logger.log(BasicLevel.DEBUG, "pm closed");
183                 pm.close();
184             }
185             catch (Exception JavaDoc e) {
186                 logger.log(BasicLevel.DEBUG, e.getMessage());
187             }
188         }
189     }
190     
191     private void createABC(){
192         A a1 = new A(1);
193         A a2 = new A(2);
194         A a3 = new A(3);
195         
196         C c1 = new C(1,1,1);
197         C c2 = new C(2,2,2);
198         C c3 = new C(3,3,3);
199         C c4 = new C(4,4,4);
200         C c5 = new C(5,5,5);
201         C c6 = new C(6,6,6);
202         
203         B b1 = new B(1, c1);
204         B b2 = new B(2, c2);
205         B b3 = new B(3, c3);
206         B b4 = new B(4, c4);
207         B b5 = new B(5, c5);
208         B b6 = new B(6, c6);
209         
210         a1.addB(b1);
211         a1.addB(b2);
212         a1.addB(b3);
213         
214         a2.addB(b4);
215         a2.addB(b5);
216         
217         a3.addB(b6);
218         
219         PersistenceManager pm = pmf.getPersistenceManager();
220         pm.currentTransaction().begin();
221         pm.makePersistent(a1);
222         pm.makePersistent(a2);
223         pm.makePersistent(a3);
224         pm.currentTransaction().commit();
225     }
226     
227     private Collection JavaDoc detachAs(){
228         PersistenceManager pm = pmf.getPersistenceManager();
229         FetchPlan fetchPlan = pm.getFetchPlan();
230         fetchPlan.addGroup("AList").removeGroup("default");
231         try{
232             Query query = pm.newQuery(A.class);
233             Collection JavaDoc results = (Collection JavaDoc)query.execute();
234             logger.log(BasicLevel.DEBUG, "A:");
235             Iterator JavaDoc it = results.iterator();
236             while(it.hasNext()){
237                 A a = (A) it.next();
238                 logger.log(BasicLevel.DEBUG, "" + a.toString());
239             }
240             Collection JavaDoc detachedAs = (Collection JavaDoc) pm.detachCopyAll(results);
241             query.closeAll();
242             return detachedAs;
243         }
244         catch(Exception JavaDoc e){
245             logger.log(BasicLevel.DEBUG, e.toString());
246             return null;
247         }
248         finally{
249             try {
250                 logger.log(BasicLevel.DEBUG, "pm closed");
251                 pm.close();
252             }
253             catch (Exception JavaDoc e) {
254                 logger.log(BasicLevel.DEBUG, e.getMessage());
255             }
256         }
257     }
258     
259 }
260
Popular Tags