/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
*/
package org.objectweb.speedo.runtime.detach;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Iterator;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import junit.framework.Assert;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.detach.Share;
import org.objectweb.speedo.pobjects.detach.SharePrice;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* Initially, java.math.* was considered as a reference.
* See visitFieldInsn() method of ClassAccessorModifier class.
* The test:
* detaches a Share
* modify a BigDecimal value of a SharePrice of the prices
* attaches the Share
* @author Y.Bersihand
*/
public class TestAttachBigDecimal extends SpeedoTestHelper {
public TestAttachBigDecimal(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.detach.TestAttachBigDecimal";
}
/**
* Test the detach method on a Big Decimal : bug fixed to update a big decimal object when detaching it.
*/
public void testDetachModifyAttachCollection() {
logger.log(BasicLevel.DEBUG, "*****************testDetachModifyAttachCollection************");
//1st create 2 shares and prices
Share share1 = new Share(1);
Share share2 = new Share(2);
SharePrice sp11 = new SharePrice(11, 2004, 11, new BigDecimal(11000));
SharePrice sp12 = new SharePrice(12, 2004, 11, new BigDecimal(12000));
SharePrice sp13 = new SharePrice(13, 2004, 11, new BigDecimal(13000));
share1.addPrice(sp11);
share1.addPrice(sp12);
share1.addPrice(sp13);
SharePrice sp21 = new SharePrice(21, 2005, 11, new BigDecimal(21000));
SharePrice sp22 = new SharePrice(22, 2005, 11, new BigDecimal(22000));
SharePrice sp23 = new SharePrice(23, 2005, 11, new BigDecimal(23000));
share1.addPrice(sp21);
share1.addPrice(sp22);
share1.addPrice(sp23);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(share1);
pm.makePersistent(share2);
pm.currentTransaction().commit();
Share copyOfShare = (Share) pm.detachCopy((Object)share1);
logger.log(BasicLevel.DEBUG, "share detached");
SharePrice price = copyOfShare.getPrice(12,2004,11);
if (price != null) {
price.setPrice(new BigDecimal(99000));
}
SharePrice newSp = new SharePrice(31, 2006, 11, new BigDecimal(31000));
copyOfShare.addPrice(newSp);
try {
logger.log(BasicLevel.DEBUG, "share price updated");
pm.currentTransaction().begin();
Share attachedShare = (Share) pm.makePersistent((Object)copyOfShare);
pm.currentTransaction().commit();
assertEquals(copyOfShare.getId(), attachedShare.getId());
assertEquals("Collection of prices for the detached share and its attached copy is not the same.", copyOfShare.getPrices().size(), attachedShare.getPrices().size());
logger.log(BasicLevel.DEBUG, "share attached");
} catch (Exception e) {
fail(e.getMessage());
} finally {
pm.close();
}
}
public void testRemovingOfPersistentObject() {
PersistenceManager pm = pmf.getPersistenceManager();
try {
Class[] cs = new Class[]{SharePrice.class, Share.class};
pm.currentTransaction().begin();
for(int i=0; i<cs.length; i++) {
Query query = pm.newQuery(cs[i]);
Collection col = (Collection) query.execute();
Iterator it = col.iterator();
while(it.hasNext()) {
Object o = it.next();
Assert.assertNotNull("null object in the query result"
+ cs[i].getName(), o);
pm.deletePersistent(o);
}
query.close(col);
}
pm.currentTransaction().commit();
} catch (JDOException e) {
Exception ie = ExceptionHelper.getNested(e);
logger.log(BasicLevel.ERROR, "", ie);
fail(ie.getMessage());
} finally {
pm.close();
}
}
}