KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > detach > TestAttachBigDecimal


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.runtime.detach;
27
28
29 import java.math.BigDecimal JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
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 /**
46  * Initially, java.math.* was considered as a reference.
47  * See visitFieldInsn() method of ClassAccessorModifier class.
48  * The test:
49  * detaches a Share
50  * modify a BigDecimal value of a SharePrice of the prices
51  * attaches the Share
52  * @author Y.Bersihand
53  */

54 public class TestAttachBigDecimal extends SpeedoTestHelper {
55
56     public TestAttachBigDecimal(String JavaDoc s) {
57         super(s);
58     }
59
60     protected String JavaDoc getLoggerName() {
61         return LOG_NAME + ".rt.detach.TestAttachBigDecimal";
62     }
63     
64     /**
65      * Test the detach method on a Big Decimal : bug fixed to update a big decimal object when detaching it.
66      */

67     public void testDetachModifyAttachCollection() {
68         logger.log(BasicLevel.DEBUG, "*****************testDetachModifyAttachCollection************");
69         //1st create 2 shares and prices
70
Share share1 = new Share(1);
71         Share share2 = new Share(2);
72         
73         SharePrice sp11 = new SharePrice(11, 2004, 11, new BigDecimal JavaDoc(11000));
74         SharePrice sp12 = new SharePrice(12, 2004, 11, new BigDecimal JavaDoc(12000));
75         SharePrice sp13 = new SharePrice(13, 2004, 11, new BigDecimal JavaDoc(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 JavaDoc(21000));
82         SharePrice sp22 = new SharePrice(22, 2005, 11, new BigDecimal JavaDoc(22000));
83         SharePrice sp23 = new SharePrice(23, 2005, 11, new BigDecimal JavaDoc(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 JavaDoc)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 JavaDoc(99000));
100         }
101         SharePrice newSp = new SharePrice(31, 2006, 11, new BigDecimal JavaDoc(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 JavaDoc)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 JavaDoc 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 JavaDoc[] cs = new Class JavaDoc[]{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 JavaDoc col = (Collection JavaDoc) query.execute();
126                 Iterator JavaDoc it = col.iterator();
127                 while(it.hasNext()) {
128                     Object JavaDoc 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 JavaDoc 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