KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > collection > TestAllCollection


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

18 package org.objectweb.speedo.runtime.collection;
19
20 import org.objectweb.speedo.SpeedoTestHelper;
21 import org.objectweb.speedo.pobjects.collection.AllCollection;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jdo.PersistenceManager;
28
29 import junit.framework.Assert;
30
31 /**
32  *
33  * @author S.Chassande-Barrioz
34  */

35 public class TestAllCollection extends SpeedoTestHelper {
36
37     public TestAllCollection(String JavaDoc s) {
38         super(s);
39     }
40
41     protected String JavaDoc getLoggerName() {
42         return LOG_NAME + ".rt.collection.TestAllCollection";
43     }
44
45     public void testNullCollection() {
46         PersistenceManager pm = pmf.getPersistenceManager();
47         pm.currentTransaction().begin();
48         AllCollection ac = new AllCollection("testNullCollection");
49         pm.makePersistent(ac);
50         Object JavaDoc oid = pm.getObjectId(ac);
51         Assert.assertNotNull("The identifier is null", oid);
52         ac.setLongs(null);
53         ac.setRefs(null);
54         pm.currentTransaction().commit();
55         pm.close();
56         pm = null;
57         ac = null;
58         pm = pmf.getPersistenceManager();
59         ac = (AllCollection) pm.getObjectById(oid, true);
60         Assert.assertNotNull("No persistence found with the id " + oid, ac);
61         Assert.assertTrue("The collection of Long is not null", ac.getCol_Long().isEmpty());
62         Assert.assertTrue("The collection of reference is not null", ac.getCol_ref().isEmpty());
63         Assert.assertTrue("The Hashset of Long is not null", ac.getHset_Long().isEmpty());
64         Assert.assertTrue("The Hashset of reference is not null", ac.getHset_ref().isEmpty());
65         Assert.assertTrue("The list of Long is not null", ac.getList_Long().isEmpty());
66         Assert.assertTrue("The list of reference is not null", ac.getList_ref().isEmpty());
67         Assert.assertTrue("The set of Long is not null", ac.getSet_Long().isEmpty());
68         Assert.assertTrue("The set of reference is not null", ac.getSet_ref().isEmpty());
69         pm.currentTransaction().begin();
70         pm.deletePersistent(ac);
71         pm.currentTransaction().commit();
72         pm.close();
73     }
74
75
76     public void testEmptyCollection() {
77         PersistenceManager pm = pmf.getPersistenceManager();
78         pm.currentTransaction().begin();
79         AllCollection ac = new AllCollection("testEmptyCollection");
80         pm.makePersistent(ac);
81         Object JavaDoc oid = pm.getObjectId(ac);
82         assertNotNull("The identifier is null", oid);
83         ac.setLongs(new long[]{});
84         ac.setRefs(new Object JavaDoc[]{});
85         pm.currentTransaction().commit();
86         pm.close();
87         pm = null;
88         ac = null;
89         pm = pmf.getPersistenceManager();
90         ac = (AllCollection) pm.getObjectById(oid, true);
91         Assert.assertNotNull("No persistence found with the id " + oid, ac);
92         Assert.assertNotNull("The collection of Long is null", ac.getCol_Long());
93         Assert.assertEquals("The collection of Long is not empty", 0, ac.getCol_Long().size());
94
95         Assert.assertNotNull("The collection of reference is null", ac.getCol_ref());
96         Assert.assertEquals("The collection of reference is not empty", 0, ac.getCol_ref().size());
97
98         Assert.assertNotNull("The Hashset of Long is null", ac.getHset_Long());
99         Assert.assertEquals("The Hashset of Long is not empty", 0, ac.getHset_Long().size());
100
101         Assert.assertNotNull("The Hashset of reference is null", ac.getHset_ref());
102         Assert.assertEquals("The Hashset of reference is not empty", 0, ac.getHset_ref().size());
103
104         Assert.assertNotNull("The list of Long is null", ac.getList_Long());
105         Assert.assertEquals("The list of Long is not empty", 0, ac.getList_Long().size());
106
107         Assert.assertNotNull("The list of reference is null", ac.getList_ref());
108         Assert.assertEquals("The list of reference is not empty", 0, ac.getList_ref().size());
109
110         Assert.assertNotNull("The set of Long is null", ac.getSet_Long());
111         Assert.assertEquals("The set of Long is not empty", 0, ac.getSet_Long().size());
112
113         Assert.assertNotNull("The set of reference is null", ac.getSet_ref());
114         Assert.assertEquals("The set of reference is not empty", 0, ac.getSet_ref().size());
115
116         pm.currentTransaction().begin();
117         pm.deletePersistent(ac);
118         pm.currentTransaction().commit();
119         pm.close();
120     }
121
122     public void testOneElement() {
123         long[] ls = new long[]{217,218};
124         PersistenceManager pm = pmf.getPersistenceManager();
125         pm.currentTransaction().begin();
126         AllCollection ac = new AllCollection("testOneElement");
127         ac.setLongs(ls);
128         ac.setRefs(new Object JavaDoc[]{ac});
129         pm.makePersistent(ac);
130         Object JavaDoc oid = pm.getObjectId(ac);
131         assertNotNull("The identifier is null", oid);
132         pm.currentTransaction().commit();
133         pm.close();
134         pm = null;
135         ac = null;
136         pm = pmf.getPersistenceManager();
137         ac = (AllCollection) pm.getObjectById(oid, true);
138         assertNotNull("No persistence found with the id " + oid, ac);
139
140         List JavaDoc expectedLong = new ArrayList JavaDoc(ls.length);
141         for(int i=0; i<ls.length; i++) {
142             expectedLong.add(new Long JavaDoc(ls[i]));
143         }
144
145         List JavaDoc expectedRef = new ArrayList JavaDoc(1);
146         expectedRef.add(ac);
147
148         //Collection
149
Assert.assertNotNull("The collection of Long is null", ac.getCol_Long());
150         assertSameCollection("The collection of Long", expectedLong, ac.getCol_Long());
151         Assert.assertNotNull("The collection of reference is null", ac.getCol_ref());
152         assertSameCollection("The collection of ref", expectedRef, ac.getCol_ref());
153
154         //HashSet
155
Assert.assertNotNull("The Hashset of Long is null", ac.getHset_Long());
156         assertSameCollection("The Hashset of Long", expectedLong, ac.getHset_Long());
157         Assert.assertNotNull("The Hashset of reference is null", ac.getHset_ref());
158         assertSameCollection("The Hashset of ref", expectedRef, ac.getHset_ref());
159
160         //List
161
Assert.assertNotNull("The list of Long is null", ac.getList_Long());
162         assertSameList("The list of Long", expectedLong, ac.getList_Long());
163         Assert.assertNotNull("The list of reference is null", ac.getList_ref());
164         assertSameList("The list of reference", expectedRef, ac.getList_ref());
165
166         //Set
167
Assert.assertNotNull("The set of Long is null", ac.getSet_Long());
168         assertSameCollection("The set of Long", expectedLong, ac.getSet_Long());
169         Assert.assertNotNull("The set of reference is null", ac.getSet_ref());
170         assertSameCollection("The set of ref", expectedRef, ac.getSet_ref());
171
172         pm.currentTransaction().begin();
173         pm.deletePersistentAll(ac.getArraylist_ref());
174         ac.setRefs(null);
175         ac.setLongs(null);
176         pm.deletePersistent(ac);
177         pm.currentTransaction().commit();
178         pm.close();
179     }
180
181     public void testSeveralSameElement() {
182         long[] ls = new long[]{217,217,218,218,219,219,217,218,219};
183         PersistenceManager pm = pmf.getPersistenceManager();
184         pm.currentTransaction().begin();
185         AllCollection ac = new AllCollection("testSeveralSameElement");
186         ac.setLongs(ls);
187         pm.makePersistent(ac);
188         Object JavaDoc oid = pm.getObjectId(ac);
189         pm.currentTransaction().commit();
190
191         List JavaDoc expectedLong = new ArrayList JavaDoc(ls.length);
192         HashSet JavaDoc expectedLongset = new HashSet JavaDoc();
193         for(int i=0; i<ls.length; i++) {
194             expectedLong.add(new Long JavaDoc(ls[i]));
195             expectedLongset.add(new Long JavaDoc(ls[i]));
196         }
197
198         pm.currentTransaction().begin();
199         checkAC(ac, expectedLong, expectedLongset);
200         pm.currentTransaction().commit();
201
202         for(int i = 0; i<5; i++) {
203             pm.currentTransaction().begin();
204             ac.addAll(new long[]{217});
205             expectedLongset.add(new Long JavaDoc(217));
206             expectedLong.add(new Long JavaDoc(217));
207             checkAC(ac, expectedLong, expectedLongset);
208             pm.currentTransaction().commit();
209
210             pm.currentTransaction().begin();
211             checkAC(ac, expectedLong, expectedLongset);
212             pm.currentTransaction().commit();
213         }
214
215         for(int i = 0; i<5; i++) {
216             pm.currentTransaction().begin();
217             ac.addAll(new long[]{218,218});
218             expectedLongset.add(new Long JavaDoc(218));
219             expectedLongset.add(new Long JavaDoc(218));
220             expectedLong.add(new Long JavaDoc(218));
221             expectedLong.add(new Long JavaDoc(218));
222             checkAC(ac, expectedLong, expectedLongset);
223             pm.currentTransaction().commit();
224
225             pm.currentTransaction().begin();
226             checkAC(ac, expectedLong, expectedLongset);
227             pm.currentTransaction().commit();
228         }
229
230         ac = null;
231         pm.evictAll();
232         
233         pm.currentTransaction().begin();
234         ac = (AllCollection) pm.getObjectById(oid, false);
235         checkAC(ac, expectedLong, expectedLongset);
236         pm.currentTransaction().commit();
237         
238         pm.currentTransaction().begin();
239         ac.setLongs(null);
240         pm.deletePersistent(ac);
241         pm.currentTransaction().commit();
242         pm.close();
243     }
244     
245     private void checkAC(AllCollection ac, List JavaDoc expectedLong, HashSet JavaDoc expectedLongset) {
246         //Collection
247
assertSameCollection("The collection of Long", expectedLongset, ac.getCol_Long());
248         //List
249
assertSameList("The list of Long", expectedLong, ac.getList_Long());
250         //ArrayList
251
assertSameList("The list of Long", expectedLong, ac.getArraylist_Long());
252         //Vector
253
assertSameList("The list of Long", expectedLong, ac.getVector_Long());
254         //HashSet
255
assertSameCollection("The Hashset of Long", expectedLongset, ac.getHset_Long());
256         //Set
257
assertSameCollection("The set of Long", expectedLongset, ac.getSet_Long());
258     }
259     
260     
261 }
262
Popular Tags