1 25 26 package org.objectweb.speedo.runtime.detach; 27 28 29 import java.math.BigDecimal ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 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.detach.Share; 42 import org.objectweb.speedo.pobjects.detach.SharePrice; 43 import org.objectweb.util.monolog.api.BasicLevel; 44 45 54 public class TestAttachBigDecimal extends SpeedoTestHelper { 55 56 public TestAttachBigDecimal(String s) { 57 super(s); 58 } 59 60 protected String getLoggerName() { 61 return LOG_NAME + ".rt.detach.TestAttachBigDecimal"; 62 } 63 64 67 public void testDetachModifyAttachCollection() { 68 logger.log(BasicLevel.DEBUG, "*****************testDetachModifyAttachCollection************"); 69 Share share1 = new Share(1); 71 Share share2 = new Share(2); 72 73 SharePrice sp11 = new SharePrice(11, 2004, 11, new BigDecimal (11000)); 74 SharePrice sp12 = new SharePrice(12, 2004, 11, new BigDecimal (12000)); 75 SharePrice sp13 = new SharePrice(13, 2004, 11, new BigDecimal (13000)); 76 77 share1.addPrice(sp11); 78 share1.addPrice(sp12); 79 share1.addPrice(sp13); 80 81 SharePrice sp21 = new SharePrice(21, 2005, 11, new BigDecimal (21000)); 82 SharePrice sp22 = new SharePrice(22, 2005, 11, new BigDecimal (22000)); 83 SharePrice sp23 = new SharePrice(23, 2005, 11, new BigDecimal (23000)); 84 85 share1.addPrice(sp21); 86 share1.addPrice(sp22); 87 share1.addPrice(sp23); 88 89 PersistenceManager pm = pmf.getPersistenceManager(); 90 pm.currentTransaction().begin(); 91 pm.makePersistent(share1); 92 pm.makePersistent(share2); 93 pm.currentTransaction().commit(); 94 95 Share copyOfShare = (Share) pm.detachCopy((Object )share1); 96 logger.log(BasicLevel.DEBUG, "share detached"); 97 SharePrice price = copyOfShare.getPrice(12,2004,11); 98 if (price != null) { 99 price.setPrice(new BigDecimal (99000)); 100 } 101 SharePrice newSp = new SharePrice(31, 2006, 11, new BigDecimal (31000)); 102 copyOfShare.addPrice(newSp); 103 try { 104 logger.log(BasicLevel.DEBUG, "share price updated"); 105 pm.currentTransaction().begin(); 106 Share attachedShare = (Share) pm.attachCopy((Object )copyOfShare,false); 107 pm.currentTransaction().commit(); 108 assertEquals(copyOfShare.getId(), attachedShare.getId()); 109 assertEquals("Collection of prices for the detached share and its attached copy is not the same.", copyOfShare.getPrices().size(), attachedShare.getPrices().size()); 110 logger.log(BasicLevel.DEBUG, "share attached"); 111 } catch (Exception e) { 112 fail(e.getMessage()); 113 } finally { 114 pm.close(); 115 } 116 } 117 118 public void testRemovingOfPersistentObject() { 119 PersistenceManager pm = pmf.getPersistenceManager(); 120 try { 121 Class [] cs = new Class []{SharePrice.class, Share.class}; 122 pm.currentTransaction().begin(); 123 for(int i=0; i<cs.length; i++) { 124 Query query = pm.newQuery(cs[i]); 125 Collection col = (Collection ) query.execute(); 126 Iterator it = col.iterator(); 127 while(it.hasNext()) { 128 Object o = it.next(); 129 Assert.assertNotNull("null object in the query result" 130 + cs[i].getName(), o); 131 pm.deletePersistent(o); 132 133 } 134 query.close(col); 135 } 136 pm.currentTransaction().commit(); 137 } catch (JDOException e) { 138 Exception ie = ExceptionHelper.getNested(e); 139 logger.log(BasicLevel.ERROR, "", ie); 140 fail(ie.getMessage()); 141 } finally { 142 pm.close(); 143 } 144 } 145 146 } 147 | Popular Tags |