1 25 26 package org.objectweb.speedo.runtime.detach; 27 28 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import javax.jdo.JDOException; 33 import javax.jdo.PersistenceManager; 34 import javax.jdo.Query; 35 36 import junit.framework.Assert; 37 38 import org.objectweb.speedo.SpeedoTestHelper; 39 import org.objectweb.speedo.api.ExceptionHelper; 40 import org.objectweb.speedo.pobjects.detach.Address; 41 import org.objectweb.speedo.pobjects.detach.Person; 42 import org.objectweb.util.monolog.api.BasicLevel; 43 44 47 public class TestSwiss extends SpeedoTestHelper { 48 49 public TestSwiss(String s) { 50 super(s); 51 } 52 53 protected String getLoggerName() { 54 return LOG_NAME + ".rt.detach.TestSwiss"; 55 } 56 57 60 public void testDetachManyTimes() { 61 try { 62 long personId = createPersonLong(); 63 Person detached1 = getPersonById(personId); 64 logger.log(BasicLevel.DEBUG, "detached1 address: " + detached1.getAddress().getType()); 65 Person detached2 = getPersonById(personId); 66 logger.log(BasicLevel.DEBUG, "detached2 address: " + detached2.getAddress().getType()); 67 assertEquals(detached1.getId(), detached2.getId()); 68 assertEquals(detached1.getAddress().getType(), detached2.getAddress().getType()); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 } 73 74 public void testRemovingOfPersistentObject() { 75 PersistenceManager pm = pmf.getPersistenceManager(); 76 try { 77 Class [] cs = new Class []{Address.class, Person.class}; 78 pm.currentTransaction().begin(); 79 for(int i=0; i<cs.length; i++) { 80 Query query = pm.newQuery(cs[i]); 81 Collection col = (Collection ) query.execute(); 82 Iterator it = col.iterator(); 83 while(it.hasNext()) { 84 Object o = it.next(); 85 Assert.assertNotNull("null object in the query result" 86 + cs[i].getName(), o); 87 pm.deletePersistent(o); 88 89 } 90 query.close(col); 91 } 92 pm.currentTransaction().commit(); 93 } catch (JDOException e) { 94 Exception ie = ExceptionHelper.getNested(e); 95 logger.log(BasicLevel.ERROR, "", ie); 96 fail(ie.getMessage()); 97 } finally { 98 pm.close(); 99 } 100 } 101 102 private Object createPerson(){ 103 logger.log(BasicLevel.DEBUG, "***************createPerson Object*****************"); 104 Address address = new Address("mail"); 105 Person person = new Person(address); 106 PersistenceManager pm = pmf.getPersistenceManager(); 107 try { 108 pm.currentTransaction().begin(); 109 pm.makePersistent(person); 110 pm.currentTransaction().commit(); 111 return pm.getObjectId(person); 112 } catch (Exception e) { 113 fail(e.getMessage()); 114 return null; 115 } finally { 116 pm.close(); 117 } 118 } 119 120 private long createPersonLong(){ 121 logger.log(BasicLevel.DEBUG, "***************createPersonLong*****************"); 122 Address address = new Address("mail"); 123 Person person = new Person(address); 124 PersistenceManager pm = pmf.getPersistenceManager(); 125 try { 126 pm.currentTransaction().begin(); 127 pm.makePersistent(person); 128 pm.currentTransaction().commit(); 129 return person.getId(); 130 } catch (Exception e) { 131 fail(e.getMessage()); 132 return -1; 133 } finally { 134 pm.close(); 135 } 136 } 137 138 private Person getPersonById(Object personId) { 139 logger.log(BasicLevel.DEBUG, "***************getPersonById*****************"); 140 Person result = null; 141 PersistenceManager pm = pmf.getPersistenceManager(); 142 try { 143 Person p = (Person) pm.getObjectById(personId, false); 145 if (p != null) { 146 p.getAddress(); 147 result = (Person) pm.detachCopy(p); 148 } 149 } catch (Exception e) { 150 logger.log(BasicLevel.DEBUG, "Person with Key=" + personId + " not found."); 151 } finally { 152 pm.close(); 153 } 154 return result; 155 } 156 157 private Person getPersonById(long personId) { 158 logger.log(BasicLevel.DEBUG, "***************getPersonById*****************"); 159 Person result = null; 160 PersistenceManager pm = pmf.getPersistenceManager(); 161 try { 162 Object oid = pm.newObjectIdInstance(Person.class, "" + personId); 163 Person p = (Person) pm.getObjectById(oid, false); 164 if (p != null) { 165 p.getAddress(); 166 result = (Person) pm.detachCopy(p); 167 } 168 } catch (Exception e) { 169 logger.log(BasicLevel.DEBUG, "Person with Key=" + personId + " not found."); 170 } finally { 171 pm.close(); 172 } 173 return result; 174 } 175 } 176 | Popular Tags |