001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.runtime.detach;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.jdo.FetchPlan;
031: import javax.jdo.JDOException;
032: import javax.jdo.PersistenceManager;
033: import javax.jdo.Query;
034:
035: import junit.framework.Assert;
036:
037: import org.objectweb.speedo.SpeedoTestHelper;
038: import org.objectweb.speedo.api.ExceptionHelper;
039: import org.objectweb.speedo.pobjects.detach.Coach;
040: import org.objectweb.speedo.pobjects.detach.Player;
041: import org.objectweb.speedo.pobjects.detach.Team;
042: import org.objectweb.speedo.pobjects.detach.groupama.A;
043: import org.objectweb.speedo.pobjects.detach.groupama.B;
044: import org.objectweb.speedo.pobjects.detach.groupama.C;
045: import org.objectweb.util.monolog.api.BasicLevel;
046:
047: /**
048: * Try to reproduce the bug of Unilog Groupama:
049: * when performing two detachAll on a query result, problem with the cache:
050: * PName already bound to another object
051: * @author Y.Bersihand
052: */
053: public class TestDetachAll extends SpeedoTestHelper {
054:
055: public TestDetachAll(String s) {
056: super (s);
057: }
058:
059: protected String getLoggerName() {
060: return LOG_NAME + ".rt.detach.TestDetachAll";
061: }
062:
063: /**
064: * Test the detachall method
065: */
066: public void testDetachAllOnce() {
067: logger.log(BasicLevel.DEBUG,
068: "***************testDetachAllOnce*****************");
069: createABC();
070: //detach all once
071: try {
072: Collection as = detachAs();
073: Iterator it1 = as.iterator();
074: logger.log(BasicLevel.DEBUG, "1st detached:");
075: while (it1.hasNext()) {
076: A a = (A) it1.next();
077: logger.log(BasicLevel.DEBUG, a.toString());
078: }
079: } catch (Exception e) {
080: logger.log(BasicLevel.DEBUG, e.getMessage());
081: }
082: }
083:
084: public void testDetachAllTwice() {
085: logger.log(BasicLevel.DEBUG,
086: "***************testDetachAllTwice*****************");
087: //detach all twice
088: try {
089: Collection as = detachAs();
090: Iterator it1 = as.iterator();
091: logger.log(BasicLevel.DEBUG, "2nd detached:");
092: while (it1.hasNext()) {
093: A a = (A) it1.next();
094: logger.log(BasicLevel.DEBUG, a.toString());
095: }
096: } catch (Exception e) {
097: logger.log(BasicLevel.DEBUG, e.getMessage());
098: }
099: }
100:
101: public void testRemovingOfPersistentObject() {
102: PersistenceManager pm = pmf.getPersistenceManager();
103: try {
104: Class[] cs = new Class[] { A.class, B.class, C.class,
105: Coach.class, Player.class, Team.class };
106: pm.currentTransaction().begin();
107: for (int i = 0; i < cs.length; i++) {
108: Query query = pm.newQuery(cs[i]);
109: Collection col = (Collection) query.execute();
110: Iterator it = col.iterator();
111: while (it.hasNext()) {
112: Object o = it.next();
113: Assert.assertNotNull(
114: "null object in the query result"
115: + cs[i].getName(), o);
116: pm.deletePersistent(o);
117:
118: }
119: query.close(col);
120: }
121: pm.currentTransaction().commit();
122: } catch (JDOException e) {
123: Exception ie = ExceptionHelper.getNested(e);
124: logger.log(BasicLevel.ERROR, "", ie);
125: fail(ie.getMessage());
126: } finally {
127: pm.close();
128: }
129: }
130:
131: private void createPlayers() {
132: Team t = new Team("Bordeaux", null, null);
133: Collection players = new ArrayList();
134: Player p1 = new Player("p1", t, 25);
135: players.add(p1);
136: Player p2 = new Player("p2", t, 32);
137: players.add(p2);
138: t.setPlayers(players);
139: Coach c = new Coach("c1", 5, t);
140: t.setCoach(c);
141:
142: Team t2 = new Team("Nantes", null, null);
143: Collection players2 = new ArrayList();
144: Player p3 = new Player("p3", t2, 25);
145: players2.add(p3);
146: Player p4 = new Player("p4", t2, 32);
147: players2.add(p4);
148: t2.setPlayers(players2);
149: Coach c2 = new Coach("c2", 5, t2);
150: t2.setCoach(c2);
151:
152: PersistenceManager pm = pmf.getPersistenceManager();
153: pm.currentTransaction().begin();
154: logger.log(BasicLevel.DEBUG, "make persistent the team "
155: + t.toString());
156: pm.makePersistent(t);
157: pm.makePersistent(t2);
158: pm.currentTransaction().commit();
159: }
160:
161: private Collection detachTeams() throws Exception {
162: PersistenceManager pm = pmf.getPersistenceManager();
163: try {
164: Query query = pm.newQuery(Team.class);
165: Collection results = (Collection) query.execute();
166: logger.log(BasicLevel.DEBUG, "Teams:");
167: Iterator it = results.iterator();
168: while (it.hasNext()) {
169: Team t = (Team) it.next();
170: logger.log(BasicLevel.DEBUG, t.toString());
171: }
172: Collection detachedTeams = (Collection) pm
173: .detachCopyAll(results);
174: query.closeAll();
175: return detachedTeams;
176: } catch (Exception e) {
177: logger.log(BasicLevel.DEBUG, e.toString());
178: return null;
179: } finally {
180: try {
181: logger.log(BasicLevel.DEBUG, "pm closed");
182: pm.close();
183: } catch (Exception e) {
184: logger.log(BasicLevel.DEBUG, e.getMessage());
185: }
186: }
187: }
188:
189: private void createABC() {
190: A a1 = new A(1);
191: A a2 = new A(2);
192: A a3 = new A(3);
193:
194: C c1 = new C(1, 1, 1);
195: C c2 = new C(2, 2, 2);
196: C c3 = new C(3, 3, 3);
197: C c4 = new C(4, 4, 4);
198: C c5 = new C(5, 5, 5);
199: C c6 = new C(6, 6, 6);
200:
201: B b1 = new B(1, c1);
202: B b2 = new B(2, c2);
203: B b3 = new B(3, c3);
204: B b4 = new B(4, c4);
205: B b5 = new B(5, c5);
206: B b6 = new B(6, c6);
207:
208: a1.addB(b1);
209: a1.addB(b2);
210: a1.addB(b3);
211:
212: a2.addB(b4);
213: a2.addB(b5);
214:
215: a3.addB(b6);
216:
217: PersistenceManager pm = pmf.getPersistenceManager();
218: pm.currentTransaction().begin();
219: pm.makePersistent(a1);
220: pm.makePersistent(a2);
221: pm.makePersistent(a3);
222: pm.currentTransaction().commit();
223: }
224:
225: private Collection detachAs() {
226: PersistenceManager pm = pmf.getPersistenceManager();
227: FetchPlan fetchPlan = pm.getFetchPlan();
228: fetchPlan.addGroup("AList").removeGroup("default");
229: try {
230: Query query = pm.newQuery(A.class);
231: Collection results = (Collection) query.execute();
232: logger.log(BasicLevel.DEBUG, "A:");
233: Iterator it = results.iterator();
234: while (it.hasNext()) {
235: A a = (A) it.next();
236: logger.log(BasicLevel.DEBUG, "" + a.toString());
237: }
238: Collection detachedAs = (Collection) pm
239: .detachCopyAll(results);
240: query.closeAll();
241: return detachedAs;
242: } catch (Exception e) {
243: logger.log(BasicLevel.DEBUG, e.toString());
244: return null;
245: } finally {
246: try {
247: logger.log(BasicLevel.DEBUG, "pm closed");
248: pm.close();
249: } catch (Exception e) {
250: logger.log(BasicLevel.DEBUG, e.getMessage());
251: }
252: }
253: }
254:
255: }
|