1 25 26 package org.objectweb.speedo.runtime.detach; 27 28 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 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 56 public class TestDetachAll extends SpeedoTestHelper { 57 58 public TestDetachAll(String s) { 59 super(s); 60 } 61 62 protected String getLoggerName() { 63 return LOG_NAME + ".rt.detach.TestDetachAll"; 64 } 65 66 69 public void testDetachAllOnce() { 70 logger.log(BasicLevel.DEBUG, "***************testDetachAllOnce*****************"); 71 createABC(); 72 try{ 74 Collection as = detachAs(); 75 Iterator 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 e) { 83 logger.log(BasicLevel.DEBUG, e.getMessage()); 84 } 85 } 86 87 public void testDetachAllTwice() { 88 logger.log(BasicLevel.DEBUG, "***************testDetachAllTwice*****************"); 89 try{ 91 Collection as = detachAs(); 92 Iterator 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 e) { 100 logger.log(BasicLevel.DEBUG, e.getMessage()); 101 } 102 } 103 104 public void testRemovingOfPersistentObject() { 105 PersistenceManager pm = pmf.getPersistenceManager(); 106 try { 107 Class [] cs = new Class []{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 col = (Collection ) query.execute(); 113 Iterator it = col.iterator(); 114 while(it.hasNext()) { 115 Object 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 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 players = new ArrayList (); 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 players2 = new ArrayList (); 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 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.detachCopyAll(results); 173 query.closeAll(); 174 return detachedTeams; 175 } 176 catch(Exception 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 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 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 results = (Collection )query.execute(); 234 logger.log(BasicLevel.DEBUG, "A:"); 235 Iterator it = results.iterator(); 236 while(it.hasNext()){ 237 A a = (A) it.next(); 238 logger.log(BasicLevel.DEBUG, "" + a.toString()); 239 } 240 Collection detachedAs = (Collection ) pm.detachCopyAll(results); 241 query.closeAll(); 242 return detachedAs; 243 } 244 catch(Exception 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 e) { 254 logger.log(BasicLevel.DEBUG, e.getMessage()); 255 } 256 } 257 } 258 259 } 260 | Popular Tags |