1 25 26 package org.objectweb.speedo.runtime.fetchgroup; 27 28 29 import java.util.Collection ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Set ; 33 34 import javax.jdo.FetchPlan; 35 import javax.jdo.JDODetachedFieldAccessException; 36 import javax.jdo.JDOException; 37 import javax.jdo.PersistenceManager; 38 import javax.jdo.Query; 39 40 import org.objectweb.speedo.SpeedoTestHelper; 41 import org.objectweb.speedo.api.ExceptionHelper; 42 import org.objectweb.speedo.pm.api.ProxyManager; 43 import org.objectweb.speedo.pobjects.fetchgroup.Address; 44 import org.objectweb.speedo.pobjects.fetchgroup.Country; 45 import org.objectweb.speedo.pobjects.fetchgroup.Person; 46 import org.objectweb.util.monolog.api.BasicLevel; 47 48 58 public class TestPredefinedFetchGroup extends SpeedoTestHelper { 59 60 public TestPredefinedFetchGroup(String s) { 61 super(s); 62 } 63 64 protected String getLoggerName() { 65 return LOG_NAME + ".rt.fetchgroup.TestPredefinedFetchGroup"; 66 } 67 68 71 public void testLoadingAll() { 72 logger.log(BasicLevel.DEBUG, "***************testLoadingAll*****************"); 73 Country country = new Country("it","Italie"); 74 Address address = new Address("Rue Spiaggi", "Milan", country); 75 Person parent = new Person(); 76 parent.setName("Del Piero Joel"); 77 parent.setAge(32); 78 parent.setAddress(address); 79 Person child1 = new Person("Del Piero Sophie", address, null, 14); 80 Person child2 = new Person("Del Piero Mikael", address, null, 11); 81 Set children = new HashSet (); 82 children.add(child1); 83 children.add(child2); 84 parent.setChildren(children); 85 86 PersistenceManager pm = pmf.getPersistenceManager(); 87 FetchPlan fp = pm.getFetchPlan(); 88 fp.addGroup("all").removeGroup("default"); 89 pm.currentTransaction().begin(); 90 logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString()); 91 pm.makePersistent(parent); 92 pm.currentTransaction().commit(); 93 94 FetchPlan f = pm.getFetchPlan(); 95 logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups()); 96 97 try{ 98 pm.currentTransaction().begin(); 99 Person detachedParent = (Person) pm.detachCopy(parent); 100 logger.log(BasicLevel.DEBUG, "All fields can be accessed: " + detachedParent.toString()); 101 assertEquals(parent.getName(), detachedParent.getName()); 102 assertEquals(parent.getAge(), detachedParent.getAge()); 103 assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity()); 104 assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode()); 105 assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName()); 106 assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet()); 107 assertEquals(parent.getChildren().size(), detachedParent.getChildren().size()); 108 } 109 catch(Exception e){ 110 logger.log(BasicLevel.DEBUG, "Error: " + e); 111 } finally { 112 if (pm.currentTransaction().isActive()) 113 pm.currentTransaction().rollback(); 114 pm.close(); 115 } 116 } 117 118 121 public void testLoadingDefault() { 122 logger.log(BasicLevel.DEBUG, "***************testLoadingDefault*****************"); 123 Country country = new Country("fr","France"); 124 Address address = new Address("rue Anatole France", "Tours", country); 125 Person parent = new Person(); 126 parent.setName("Bordes Joel"); 127 parent.setAge(32); 128 parent.setAddress(address); 129 Person child1 = new Person("Bordes Sophie", address, null, 14); 130 Person child2 = new Person("Bordes Mikael", address, null, 11); 131 Set children = new HashSet (); 132 children.add(child1); 133 children.add(child2); 134 parent.setChildren(children); 135 136 PersistenceManager pm = pmf.getPersistenceManager(); 137 FetchPlan fp = pm.getFetchPlan(); 138 fp.removeGroup("all").addGroup("default"); 139 pm.currentTransaction().begin(); 140 logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString()); 141 pm.makePersistent(parent); 142 pm.currentTransaction().commit(); 143 144 FetchPlan f = pm.getFetchPlan(); 145 logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups()); 146 147 try{ 148 pm.currentTransaction().begin(); 149 Person detachedParent = (Person) ((ProxyManager)pm).detachCopy(parent); 150 logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName()); 151 logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge()); 152 logger.log(BasicLevel.DEBUG, "Address can be accessed: " + detachedParent.getAddress().toString()); 153 assertEquals(parent.getName(), detachedParent.getName()); 154 assertEquals(parent.getAge(), detachedParent.getAge()); 155 assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity()); 156 assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode()); 157 assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName()); 158 assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet()); 159 logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren()); 160 } 161 catch(Exception e){ 162 assertEquals(e.getClass(), JDODetachedFieldAccessException.class); 163 pm.close(); 164 if (e instanceof JDODetachedFieldAccessException) 165 logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e); 166 else 167 logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e); 168 } finally { 169 if (pm.currentTransaction().isActive()) 170 pm.currentTransaction().rollback(); 171 pm.close(); 172 } 173 } 174 175 176 179 public void testLoadingNone() { 180 logger.log(BasicLevel.DEBUG, "***************testLoadingNone*****************"); 181 Country country = new Country("uk","Royaume-Uni"); 182 Address address = new Address("Sharrow Street", "Luton", country); 183 Person parent = new Person(); 184 parent.setName("None Joel"); 185 parent.setAge(32); 186 parent.setAddress(address); 187 Person child1 = new Person("None Sophie", address, null, 14); 188 Person child2 = new Person("None Mikael", address, null, 11); 189 Set children = new HashSet (); 190 children.add(child1); 191 children.add(child2); 192 parent.setChildren(children); 193 194 PersistenceManager pm = pmf.getPersistenceManager(); 195 FetchPlan fp = pm.getFetchPlan(); 196 fp.removeGroup("default").addGroup("none"); 197 pm.currentTransaction().begin(); 198 logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString()); 199 pm.makePersistent(parent); 200 pm.currentTransaction().commit(); 201 202 FetchPlan f = pm.getFetchPlan(); 203 logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups()); 204 205 try{ 206 pm.currentTransaction().begin(); 207 Person detachedParent = (Person) ((ProxyManager)pm).detachCopy(parent); 208 logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName()); 209 logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge()); 210 assertEquals(parent.getName(), detachedParent.getName()); 211 assertEquals(parent.getAge(), detachedParent.getAge()); 212 logger.log(BasicLevel.DEBUG, "Address should not be accessed: " + detachedParent.getAddress()); 213 logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren()); 214 } 215 catch(Exception e){ 216 assertEquals(e.getClass(), JDODetachedFieldAccessException.class); 217 if(e instanceof JDODetachedFieldAccessException) 218 logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e); 219 else 220 logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e); 221 } finally { 222 if (pm.currentTransaction().isActive()) 223 pm.currentTransaction().rollback(); 224 pm.close(); 225 } 226 } 227 228 231 public void testLoadingValues() { 232 logger.log(BasicLevel.DEBUG, "***************testLoadingValues*****************"); 233 Country country = new Country("gr","Grece"); 234 Address address = new Address("Feta", "Athenes", country); 235 Person parent = new Person(); 236 parent.setName("Kapic Joel"); 237 parent.setAge(32); 238 parent.setAddress(address); 239 Person child1 = new Person("Kapic Sophie", address, null, 14); 240 Person child2 = new Person("Kapic Mikael", address, null, 11); 241 Set children = new HashSet (); 242 children.add(child1); 243 children.add(child2); 244 parent.setChildren(children); 245 246 PersistenceManager pm = pmf.getPersistenceManager(); 247 FetchPlan fp = pm.getFetchPlan(); 248 fp.removeGroup("none").addGroup("values"); 249 pm.currentTransaction().begin(); 250 logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString()); 251 pm.makePersistent(parent); 252 pm.currentTransaction().commit(); 253 254 FetchPlan f = pm.getFetchPlan(); 255 logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups()); 256 257 try{ 258 pm.currentTransaction().begin(); 259 Person detachedParent = (Person) pm.detachCopy(parent); 260 logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName()); 261 logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge()); 262 assertEquals(parent.getName(), detachedParent.getName()); 263 assertEquals(parent.getAge(), detachedParent.getAge()); 264 logger.log(BasicLevel.DEBUG, "Address should not be accessed: " + detachedParent.getAddress()); 265 logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren()); 266 } 267 catch(Exception e){ 268 assertEquals(e.getClass(), JDODetachedFieldAccessException.class); 269 if(e instanceof JDODetachedFieldAccessException) 270 logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e); 271 else 272 logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e); 273 } finally { 274 if (pm.currentTransaction().isActive()) 275 pm.currentTransaction().rollback(); 276 pm.close(); 277 } 278 } 279 280 281 public void testRemovingOfPersistentObject() { 282 PersistenceManager pm = pmf.getPersistenceManager(); 283 try { 284 Class [] cs = new Class []{Person.class, Address.class, Country.class}; 285 pm.currentTransaction().begin(); 286 for(int i=0; i<cs.length; i++) { 287 Query query = pm.newQuery(cs[i]); 288 Collection col = (Collection ) query.execute(); 289 Iterator it = col.iterator(); 290 while(it.hasNext()) { 291 Object o = it.next(); 292 assertNotNull("null object in the query result" 293 + cs[i].getName(), o); 294 pm.deletePersistent(o); 295 296 } 297 query.close(col); 298 } 299 pm.currentTransaction().commit(); 300 } catch (JDOException e) { 301 Exception ie = ExceptionHelper.getNested(e); 302 logger.log(BasicLevel.ERROR, "", ie); 303 fail(ie.getMessage()); 304 } finally { 305 if (pm.currentTransaction().isActive()) 306 pm.currentTransaction().rollback(); 307 pm.close(); 308 } 309 } 310 } 311 | Popular Tags |