1 25 26 package org.objectweb.speedo.runtime.inheritance; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import javax.jdo.Extent; 33 import javax.jdo.JDOException; 34 import javax.jdo.PersistenceManager; 35 import javax.jdo.Query; 36 37 import junit.framework.Assert; 38 39 import org.objectweb.speedo.SpeedoTestHelper; 40 import org.objectweb.speedo.api.ExceptionHelper; 41 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.BookForm; 42 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.ContactDetails; 43 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.CreditCard; 44 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.Form; 45 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.single.PurchaseForm; 46 import org.objectweb.util.monolog.api.BasicLevel; 47 48 52 public class TestFilterOutOfPK extends SpeedoTestHelper { 53 54 public TestFilterOutOfPK(String s) { 55 super(s); 56 } 57 58 protected String getLoggerName() { 59 return LOG_NAME + ".rt.inheritance.TestFilterOutOfPK"; 60 } 61 62 protected int nbToRetrieve = 0; 63 protected String phone = "123456789"; 64 65 69 public void testFilterOutOfPK() { 70 logger.log(BasicLevel.DEBUG, "***************testFilterOutOfPK*****************"); 71 createInheritedObjects(); 73 iterateExtent(Form.class); 75 iterateExtent(BookForm.class); 77 iterateExtent(PurchaseForm.class); 79 retrieveAllXXForms(phone); 80 queryTruble("1"); 81 } 82 83 86 public void createInheritedObjects(){ 87 PersistenceManager pm = pmf.getPersistenceManager(); 88 try { 89 ContactDetails contactDetails1 = new ContactDetails("Jerry", "Rice", phone); 90 ContactDetails contactDetails2 = new ContactDetails("Ray", "Allen", "987654321"); 91 92 CreditCard card1 = new CreditCard(1, "visa"); 93 CreditCard card2 = new CreditCard(2, "american express"); 94 95 BookForm bookForm1 = new BookForm(1, contactDetails1, 1); 96 BookForm bookForm2 = new BookForm(2, contactDetails1, 2); 97 BookForm bookForm3 = new BookForm(3, contactDetails1, 3); 98 PurchaseForm purchaseForm1 = new PurchaseForm(4, contactDetails1, 400, card1); 99 PurchaseForm purchaseForm2 = new PurchaseForm(5, contactDetails1, 500, card2); 100 101 BookForm bookForm4 = new BookForm(6, contactDetails2, 6); 102 BookForm bookForm5 = new BookForm(7, contactDetails2, 7); 103 104 Collection forms = new ArrayList (); 105 forms.add(bookForm1); 106 forms.add(bookForm2); 107 forms.add(bookForm3); 108 forms.add(bookForm4); 109 forms.add(bookForm5); 110 forms.add(purchaseForm1); 111 forms.add(purchaseForm2); 112 113 Iterator it = forms.iterator(); 114 nbToRetrieve = 0; 115 while (it.hasNext()) { 116 Form f = (Form) it.next(); 117 if (f.getContactDetails().getPhoneNumber().equals(phone)) 118 nbToRetrieve++; 119 } 120 pm.currentTransaction().begin(); 122 pm.makePersistentAll(forms); 123 pm.currentTransaction().commit(); 124 } catch (Exception e) { 125 fail(e.getMessage()); 126 } finally { 127 if (pm.currentTransaction().isActive()) 128 pm.currentTransaction().rollback(); 129 pm.evictAll(); 131 pm.close(); 132 } 133 } 134 135 public void iterateExtent(Class cl){ 137 PersistenceManager pm = pmf.getPersistenceManager(); 138 try { 139 pm.currentTransaction().begin(); 140 Extent extent = pm.getExtent(cl, true); 141 Iterator it = extent.iterator(); 142 String className = cl.getName().substring(cl.getName().lastIndexOf(".")); 143 logger.log(BasicLevel.DEBUG, "All " + cl.getName() + " instances:"); 144 while(it.hasNext()){ 145 Form f = (Form) it.next(); 146 assertNotNull("The form should not be null", f); 147 } 149 extent.close(it); 150 pm.currentTransaction().commit(); 151 } catch (Exception e) { 152 fail(e.getMessage()); 153 } finally { 154 if (pm.currentTransaction().isActive()) 155 pm.currentTransaction().rollback(); 156 pm.evictAll(); 157 pm.close(); 158 } 159 } 160 161 public void retrieveAllXXForms(String phoneNumber){ 162 PersistenceManager pm = pmf.getPersistenceManager(); 163 try { 164 Query query = pm.newQuery(Form.class,"contactDetails.phoneNumber==" + phoneNumber); 165 Collection results = (Collection )query.execute(); 166 assertEquals(nbToRetrieve, results.size()); 167 logger.log(BasicLevel.DEBUG, "All " + phoneNumber + " forms:"); 168 Iterator it = results.iterator(); 169 while(it.hasNext()){ 170 Form f = (Form) it.next(); 171 assertEquals(phoneNumber, f.getContactDetails().getPhoneNumber()); 172 } 173 query.closeAll(); 174 } catch (Exception e) { 175 fail(e.getMessage()); 176 } finally { 177 pm.evictAll(); 178 pm.close(); 179 } 180 } 181 182 public void queryTruble(String myId) { 184 PersistenceManager pm = pmf.getPersistenceManager(); 185 try { 186 pm.currentTransaction().begin(); 187 188 BookForm b = null; 189 Query query = pm.newQuery(BookForm.class, "(id == argId)"); 190 query.declareParameters("int argId"); 191 Integer id = Integer.valueOf(myId); 192 Collection results = (Collection )query.execute(id); 193 Iterator it = results.iterator(); 194 if(it.hasNext()){ 195 b = (BookForm) it.next(); 196 logger.log(BasicLevel.DEBUG, b.getTitle()); 197 } 198 query.closeAll(); 199 pm.currentTransaction().commit(); 200 } catch (Exception e) { 201 if (pm.currentTransaction().isActive()) 202 pm.currentTransaction().rollback(); 203 fail(e.getMessage()); 204 } finally { 205 pm.close(); 206 } 207 } 208 209 public void testRemovingOfPersistentObject() { 210 PersistenceManager pm = pmf.getPersistenceManager(); 211 try { 212 Class [] cs = new Class []{Form.class, CreditCard.class, ContactDetails.class}; 213 pm.currentTransaction().begin(); 214 for(int i=0; i<cs.length; i++) { 215 Query query = pm.newQuery(cs[i]); 216 Collection col = (Collection ) query.execute(); 217 Iterator it = col.iterator(); 218 while(it.hasNext()) { 219 Object o = it.next(); 220 Assert.assertNotNull("null object in the query result" 221 + cs[i].getName(), o); 222 pm.deletePersistent(o); 223 224 } 225 query.close(col); 226 } 227 pm.currentTransaction().commit(); 228 } catch (JDOException e) { 229 Exception ie = ExceptionHelper.getNested(e); 230 logger.log(BasicLevel.ERROR, "", ie); 231 fail(ie.getMessage()); 232 } finally { 233 pm.close(); 234 } 235 } 236 } 237 | Popular Tags |