001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.collection;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import javax.jdo.PersistenceManager;
031:
032: import junit.framework.Assert;
033:
034: import org.objectweb.speedo.SpeedoTestHelper;
035: import org.objectweb.speedo.pobjects.collection.AllCollection;
036: import org.objectweb.speedo.pobjects.collection.serialization.Course;
037: import org.objectweb.speedo.pobjects.collection.serialization.Professor;
038: import org.objectweb.speedo.pobjects.collection.serialization.Student;
039: import org.objectweb.util.monolog.api.BasicLevel;
040:
041: /**
042: *
043: * @author S.Chassande-Barrioz
044: */
045: public class TestAllCollection extends SpeedoTestHelper {
046:
047: public TestAllCollection(String s) {
048: super (s);
049: }
050:
051: protected String getLoggerName() {
052: return LOG_NAME + ".rt.collection.TestAllCollection";
053: }
054:
055: public void testNullCollection() {
056: PersistenceManager pm = pmf.getPersistenceManager();
057: pm.currentTransaction().begin();
058: AllCollection ac = new AllCollection("testNullCollection");
059: pm.makePersistent(ac);
060: Object oid = pm.getObjectId(ac);
061: Assert.assertNotNull("The identifier is null", oid);
062: ac.setLongs(null);
063: ac.setRefs(null);
064: pm.currentTransaction().commit();
065: pm.close();
066: pm = null;
067: ac = null;
068: pm = pmf.getPersistenceManager();
069: ac = (AllCollection) pm.getObjectById(oid, true);
070: Assert.assertNotNull("No persistence found with the id " + oid,
071: ac);
072: Assert.assertTrue("The collection of Long is not null", ac
073: .getCol_Long().isEmpty());
074: Assert.assertTrue("The collection of reference is not null", ac
075: .getCol_ref().isEmpty());
076: Assert.assertTrue("The Hashset of Long is not null", ac
077: .getHset_Long().isEmpty());
078: Assert.assertTrue("The Hashset of reference is not null", ac
079: .getHset_ref().isEmpty());
080: Assert.assertTrue("The list of Long is not null", ac
081: .getList_Long().isEmpty());
082: Assert.assertTrue("The list of reference is not null", ac
083: .getList_ref().isEmpty());
084: Assert.assertTrue("The set of Long is not null", ac
085: .getSet_Long().isEmpty());
086: Assert.assertTrue("The set of reference is not null", ac
087: .getSet_ref().isEmpty());
088: pm.currentTransaction().begin();
089: pm.deletePersistent(ac);
090: pm.currentTransaction().commit();
091: pm.close();
092: }
093:
094: public void testEmptyCollection() {
095: PersistenceManager pm = pmf.getPersistenceManager();
096: pm.currentTransaction().begin();
097: AllCollection ac = new AllCollection("testEmptyCollection");
098: pm.makePersistent(ac);
099: Object oid = pm.getObjectId(ac);
100: assertNotNull("The identifier is null", oid);
101: ac.setLongs(new long[] {});
102: ac.setRefs(new Object[] {});
103: pm.currentTransaction().commit();
104: pm.close();
105: pm = null;
106: ac = null;
107: pm = pmf.getPersistenceManager();
108: ac = (AllCollection) pm.getObjectById(oid, true);
109: Assert.assertNotNull("No persistence found with the id " + oid,
110: ac);
111: Assert.assertNotNull("The collection of Long is null", ac
112: .getCol_Long());
113: Assert.assertEquals("The collection of Long is not empty", 0,
114: ac.getCol_Long().size());
115:
116: Assert.assertNotNull("The collection of reference is null", ac
117: .getCol_ref());
118: Assert.assertEquals("The collection of reference is not empty",
119: 0, ac.getCol_ref().size());
120:
121: Assert.assertNotNull("The Hashset of Long is null", ac
122: .getHset_Long());
123: Assert.assertEquals("The Hashset of Long is not empty", 0, ac
124: .getHset_Long().size());
125:
126: Assert.assertNotNull("The Hashset of reference is null", ac
127: .getHset_ref());
128: Assert.assertEquals("The Hashset of reference is not empty", 0,
129: ac.getHset_ref().size());
130:
131: Assert.assertNotNull("The list of Long is null", ac
132: .getList_Long());
133: Assert.assertEquals("The list of Long is not empty", 0, ac
134: .getList_Long().size());
135:
136: Assert.assertNotNull("The list of reference is null", ac
137: .getList_ref());
138: Assert.assertEquals("The list of reference is not empty", 0, ac
139: .getList_ref().size());
140:
141: Assert.assertNotNull("The set of Long is null", ac
142: .getSet_Long());
143: Assert.assertEquals("The set of Long is not empty", 0, ac
144: .getSet_Long().size());
145:
146: Assert.assertNotNull("The set of reference is null", ac
147: .getSet_ref());
148: Assert.assertEquals("The set of reference is not empty", 0, ac
149: .getSet_ref().size());
150:
151: pm.currentTransaction().begin();
152: pm.deletePersistent(ac);
153: pm.currentTransaction().commit();
154: pm.close();
155: }
156:
157: public void testOneElement() {
158: long[] ls = new long[] { 217, 218 };
159: PersistenceManager pm = pmf.getPersistenceManager();
160: pm.currentTransaction().begin();
161: AllCollection ac = new AllCollection("testOneElement");
162: ac.setLongs(ls);
163: ac.setRefs(new Object[] { ac });
164: pm.makePersistent(ac);
165: Object oid = pm.getObjectId(ac);
166: assertNotNull("The identifier is null", oid);
167: pm.currentTransaction().commit();
168: pm.close();
169: pm = null;
170: ac = null;
171: pm = pmf.getPersistenceManager();
172: ac = (AllCollection) pm.getObjectById(oid, true);
173: assertNotNull("No persistence found with the id " + oid, ac);
174:
175: List expectedLong = new ArrayList(ls.length);
176: for (int i = 0; i < ls.length; i++) {
177: expectedLong.add(new Long(ls[i]));
178: }
179:
180: List expectedRef = new ArrayList(1);
181: expectedRef.add(ac);
182:
183: //Collection
184: Assert.assertNotNull("The collection of Long is null", ac
185: .getCol_Long());
186: assertSameCollection("The collection of Long", expectedLong, ac
187: .getCol_Long());
188: Assert.assertNotNull("The collection of reference is null", ac
189: .getCol_ref());
190: assertSameCollection("The collection of ref", expectedRef, ac
191: .getCol_ref());
192:
193: //HashSet
194: Assert.assertNotNull("The Hashset of Long is null", ac
195: .getHset_Long());
196: assertSameCollection("The Hashset of Long", expectedLong, ac
197: .getHset_Long());
198: Assert.assertNotNull("The Hashset of reference is null", ac
199: .getHset_ref());
200: assertSameCollection("The Hashset of ref", expectedRef, ac
201: .getHset_ref());
202:
203: //List
204: Assert.assertNotNull("The list of Long is null", ac
205: .getList_Long());
206: assertSameList("The list of Long", expectedLong, ac
207: .getList_Long());
208: Assert.assertNotNull("The list of reference is null", ac
209: .getList_ref());
210: assertSameList("The list of reference", expectedRef, ac
211: .getList_ref());
212:
213: //Set
214: Assert.assertNotNull("The set of Long is null", ac
215: .getSet_Long());
216: assertSameCollection("The set of Long", expectedLong, ac
217: .getSet_Long());
218: Assert.assertNotNull("The set of reference is null", ac
219: .getSet_ref());
220: assertSameCollection("The set of ref", expectedRef, ac
221: .getSet_ref());
222:
223: pm.currentTransaction().begin();
224: pm.deletePersistentAll(ac.getArraylist_ref());
225: ac.setRefs(null);
226: ac.setLongs(null);
227: pm.deletePersistent(ac);
228: pm.currentTransaction().commit();
229: pm.close();
230: }
231:
232: public void testSeveralSameElement() {
233: long[] ls = new long[] { 217, 217, 218, 218, 219, 219, 217,
234: 218, 219 };
235: PersistenceManager pm = pmf.getPersistenceManager();
236: pm.currentTransaction().begin();
237: AllCollection ac = new AllCollection("testSeveralSameElement");
238: ac.setLongs(ls);
239: pm.makePersistent(ac);
240: Object oid = pm.getObjectId(ac);
241: pm.currentTransaction().commit();
242:
243: List expectedLong = new ArrayList(ls.length);
244: HashSet expectedLongset = new HashSet();
245: for (int i = 0; i < ls.length; i++) {
246: expectedLong.add(new Long(ls[i]));
247: expectedLongset.add(new Long(ls[i]));
248: }
249:
250: pm.currentTransaction().begin();
251: checkAC(ac, expectedLong, expectedLongset);
252: pm.currentTransaction().commit();
253:
254: for (int i = 0; i < 5; i++) {
255: pm.currentTransaction().begin();
256: ac.addAll(new long[] { 217 });
257: expectedLongset.add(new Long(217));
258: expectedLong.add(new Long(217));
259: checkAC(ac, expectedLong, expectedLongset);
260: pm.currentTransaction().commit();
261:
262: pm.currentTransaction().begin();
263: checkAC(ac, expectedLong, expectedLongset);
264: pm.currentTransaction().commit();
265: }
266:
267: for (int i = 0; i < 5; i++) {
268: pm.currentTransaction().begin();
269: ac.addAll(new long[] { 218, 218 });
270: expectedLongset.add(new Long(218));
271: expectedLongset.add(new Long(218));
272: expectedLong.add(new Long(218));
273: expectedLong.add(new Long(218));
274: checkAC(ac, expectedLong, expectedLongset);
275: pm.currentTransaction().commit();
276:
277: pm.currentTransaction().begin();
278: checkAC(ac, expectedLong, expectedLongset);
279: pm.currentTransaction().commit();
280: }
281:
282: ac = null;
283: pm.evictAll();
284:
285: pm.currentTransaction().begin();
286: ac = (AllCollection) pm.getObjectById(oid, false);
287: checkAC(ac, expectedLong, expectedLongset);
288: pm.currentTransaction().commit();
289:
290: pm.currentTransaction().begin();
291: ac.setLongs(null);
292: pm.deletePersistent(ac);
293: pm.currentTransaction().commit();
294: pm.close();
295: }
296:
297: public void testSerializeWithCollection() {
298: PersistenceManager pm = pmf.getPersistenceManager();
299: Professor prof = new Professor("prof");
300: Student s1 = new Student(1);
301: Student s2 = new Student(2);
302: Student s3 = new Student(3);
303: Collection col = new ArrayList();
304: col.add(s1);
305: col.add(s2);
306: col.add(s3);
307: Course c = new Course("Course1");
308: c.setProf(prof);
309: c.setStudents(col);
310: pm.currentTransaction().begin();
311: pm.makePersistent(c);
312: Object oid = pm.getObjectId(c);
313: Course copyOfC = (Course) pm.detachCopy(c);
314: pm.currentTransaction().commit();
315: pm.close();
316: File f = new File(new File(new File("output"), "test"),
317: "testSerializeWithCollection");
318: if (f.exists()) {
319: f.delete();
320: }
321: try {
322: ObjectOutputStream oos = new ObjectOutputStream(
323: new FileOutputStream(f));
324: oos.writeObject(copyOfC);
325: oos.close();
326:
327: c = null;
328: ObjectInputStream ois = new ObjectInputStream(
329: new FileInputStream(f));
330: c = (Course) ois.readObject();
331: ois.close();
332: assertEquals("Not the same professor", c.getProf()
333: .getName(), copyOfC.getProf().getName());
334: assertEquals("Not the same list of students", col.size(),
335: copyOfC.getStudents().size());
336: int sid = 0;
337: int copySid = 0;
338: pm = pmf.getPersistenceManager();
339: Iterator itCol = col.iterator();
340: Iterator itCopy = copyOfC.getStudents().iterator();
341: while (itCol.hasNext()) {
342: Student s = (Student) itCol.next();
343: sid += s.getSid();
344: s = (Student) itCopy.next();
345: copySid += s.getSid();
346: }
347: assertEquals(sid, copySid);
348: pm.close();
349: } catch (Exception e) {
350: logger.log(BasicLevel.ERROR,
351: "testSerializeWithCollection fails: ", e);
352: fail(e.getMessage());
353: } finally {
354: pm = pmf.getPersistenceManager();
355: pm.currentTransaction().begin();
356: pm.deletePersistent(pm.getObjectById(oid, false));
357: pm.currentTransaction().commit();
358: pm.close();
359: }
360: }
361:
362: private void checkAC(AllCollection ac, List expectedLong,
363: HashSet expectedLongset) {
364: //Collection
365: assertSameCollection("The collection of Long", expectedLongset,
366: ac.getCol_Long());
367: //List
368: assertSameList("The list of Long", expectedLong, ac
369: .getList_Long());
370: //ArrayList
371: assertSameList("The list of Long", expectedLong, ac
372: .getArraylist_Long());
373: //Vector
374: assertSameList("The list of Long", expectedLong, ac
375: .getVector_Long());
376: //HashSet
377: assertSameCollection("The Hashset of Long", expectedLongset, ac
378: .getHset_Long());
379: //Set
380: assertSameCollection("The set of Long", expectedLongset, ac
381: .getSet_Long());
382: }
383:
384: }
|