001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.runtime.detach;
025:
026: import java.math.BigDecimal;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.jdo.JDOException;
031: import javax.jdo.PersistenceManager;
032: import javax.jdo.Query;
033:
034: import junit.framework.Assert;
035:
036: import org.objectweb.speedo.SpeedoTestHelper;
037: import org.objectweb.speedo.api.ExceptionHelper;
038: import org.objectweb.speedo.pobjects.detach.Share;
039: import org.objectweb.speedo.pobjects.detach.SharePrice;
040: import org.objectweb.util.monolog.api.BasicLevel;
041:
042: /**
043: * Initially, java.math.* was considered as a reference.
044: * See visitFieldInsn() method of ClassAccessorModifier class.
045: * The test:
046: * detaches a Share
047: * modify a BigDecimal value of a SharePrice of the prices
048: * attaches the Share
049: * @author Y.Bersihand
050: */
051: public class TestAttachBigDecimal extends SpeedoTestHelper {
052:
053: public TestAttachBigDecimal(String s) {
054: super (s);
055: }
056:
057: protected String getLoggerName() {
058: return LOG_NAME + ".rt.detach.TestAttachBigDecimal";
059: }
060:
061: /**
062: * Test the detach method on a Big Decimal : bug fixed to update a big decimal object when detaching it.
063: */
064: public void testDetachModifyAttachCollection() {
065: logger
066: .log(BasicLevel.DEBUG,
067: "*****************testDetachModifyAttachCollection************");
068: //1st create 2 shares and prices
069: Share share1 = new Share(1);
070: Share share2 = new Share(2);
071:
072: SharePrice sp11 = new SharePrice(11, 2004, 11, new BigDecimal(
073: 11000));
074: SharePrice sp12 = new SharePrice(12, 2004, 11, new BigDecimal(
075: 12000));
076: SharePrice sp13 = new SharePrice(13, 2004, 11, new BigDecimal(
077: 13000));
078:
079: share1.addPrice(sp11);
080: share1.addPrice(sp12);
081: share1.addPrice(sp13);
082:
083: SharePrice sp21 = new SharePrice(21, 2005, 11, new BigDecimal(
084: 21000));
085: SharePrice sp22 = new SharePrice(22, 2005, 11, new BigDecimal(
086: 22000));
087: SharePrice sp23 = new SharePrice(23, 2005, 11, new BigDecimal(
088: 23000));
089:
090: share1.addPrice(sp21);
091: share1.addPrice(sp22);
092: share1.addPrice(sp23);
093:
094: PersistenceManager pm = pmf.getPersistenceManager();
095: pm.currentTransaction().begin();
096: pm.makePersistent(share1);
097: pm.makePersistent(share2);
098: pm.currentTransaction().commit();
099:
100: Share copyOfShare = (Share) pm.detachCopy((Object) share1);
101: logger.log(BasicLevel.DEBUG, "share detached");
102: SharePrice price = copyOfShare.getPrice(12, 2004, 11);
103: if (price != null) {
104: price.setPrice(new BigDecimal(99000));
105: }
106: SharePrice newSp = new SharePrice(31, 2006, 11, new BigDecimal(
107: 31000));
108: copyOfShare.addPrice(newSp);
109: try {
110: logger.log(BasicLevel.DEBUG, "share price updated");
111: pm.currentTransaction().begin();
112: Share attachedShare = (Share) pm
113: .makePersistent((Object) copyOfShare);
114: pm.currentTransaction().commit();
115: assertEquals(copyOfShare.getId(), attachedShare.getId());
116: assertEquals(
117: "Collection of prices for the detached share and its attached copy is not the same.",
118: copyOfShare.getPrices().size(), attachedShare
119: .getPrices().size());
120: logger.log(BasicLevel.DEBUG, "share attached");
121: } catch (Exception e) {
122: fail(e.getMessage());
123: } finally {
124: pm.close();
125: }
126: }
127:
128: public void testRemovingOfPersistentObject() {
129: PersistenceManager pm = pmf.getPersistenceManager();
130: try {
131: Class[] cs = new Class[] { SharePrice.class, Share.class };
132: pm.currentTransaction().begin();
133: for (int i = 0; i < cs.length; i++) {
134: Query query = pm.newQuery(cs[i]);
135: Collection col = (Collection) query.execute();
136: Iterator it = col.iterator();
137: while (it.hasNext()) {
138: Object o = it.next();
139: Assert.assertNotNull(
140: "null object in the query result"
141: + cs[i].getName(), o);
142: pm.deletePersistent(o);
143:
144: }
145: query.close(col);
146: }
147: pm.currentTransaction().commit();
148: } catch (JDOException e) {
149: Exception ie = ExceptionHelper.getNested(e);
150: logger.log(BasicLevel.ERROR, "", ie);
151: fail(ie.getMessage());
152: } finally {
153: pm.close();
154: }
155: }
156:
157: }
|