/**
* 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
*/
package org.objectweb.speedo.runtime.collection;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import javax.jdo.PersistenceManager;
import junit.framework.Assert;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.collection.AllCollection;
import org.objectweb.speedo.pobjects.collection.serialization.Course;
import org.objectweb.speedo.pobjects.collection.serialization.Professor;
import org.objectweb.speedo.pobjects.collection.serialization.Student;
import org.objectweb.util.monolog.api.BasicLevel;
/**
*
* @author S.Chassande-Barrioz
*/
public class TestAllCollection extends SpeedoTestHelper {
public TestAllCollection(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.collection.TestAllCollection";
}
public void testNullCollection() {
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
AllCollection ac = new AllCollection("testNullCollection");
pm.makePersistent(ac);
Object oid = pm.getObjectId(ac);
Assert.assertNotNull("The identifier is null", oid);
ac.setLongs(null);
ac.setRefs(null);
pm.currentTransaction().commit();
pm.close();
pm = null;
ac = null;
pm = pmf.getPersistenceManager();
ac = (AllCollection) pm.getObjectById(oid, true);
Assert.assertNotNull("No persistence found with the id " + oid, ac);
Assert.assertTrue("The collection of Long is not null", ac.getCol_Long().isEmpty());
Assert.assertTrue("The collection of reference is not null", ac.getCol_ref().isEmpty());
Assert.assertTrue("The Hashset of Long is not null", ac.getHset_Long().isEmpty());
Assert.assertTrue("The Hashset of reference is not null", ac.getHset_ref().isEmpty());
Assert.assertTrue("The list of Long is not null", ac.getList_Long().isEmpty());
Assert.assertTrue("The list of reference is not null", ac.getList_ref().isEmpty());
Assert.assertTrue("The set of Long is not null", ac.getSet_Long().isEmpty());
Assert.assertTrue("The set of reference is not null", ac.getSet_ref().isEmpty());
pm.currentTransaction().begin();
pm.deletePersistent(ac);
pm.currentTransaction().commit();
pm.close();
}
public void testEmptyCollection() {
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
AllCollection ac = new AllCollection("testEmptyCollection");
pm.makePersistent(ac);
Object oid = pm.getObjectId(ac);
assertNotNull("The identifier is null", oid);
ac.setLongs(new long[]{});
ac.setRefs(new Object[]{});
pm.currentTransaction().commit();
pm.close();
pm = null;
ac = null;
pm = pmf.getPersistenceManager();
ac = (AllCollection) pm.getObjectById(oid, true);
Assert.assertNotNull("No persistence found with the id " + oid, ac);
Assert.assertNotNull("The collection of Long is null", ac.getCol_Long());
Assert.assertEquals("The collection of Long is not empty", 0, ac.getCol_Long().size());
Assert.assertNotNull("The collection of reference is null", ac.getCol_ref());
Assert.assertEquals("The collection of reference is not empty", 0, ac.getCol_ref().size());
Assert.assertNotNull("The Hashset of Long is null", ac.getHset_Long());
Assert.assertEquals("The Hashset of Long is not empty", 0, ac.getHset_Long().size());
Assert.assertNotNull("The Hashset of reference is null", ac.getHset_ref());
Assert.assertEquals("The Hashset of reference is not empty", 0, ac.getHset_ref().size());
Assert.assertNotNull("The list of Long is null", ac.getList_Long());
Assert.assertEquals("The list of Long is not empty", 0, ac.getList_Long().size());
Assert.assertNotNull("The list of reference is null", ac.getList_ref());
Assert.assertEquals("The list of reference is not empty", 0, ac.getList_ref().size());
Assert.assertNotNull("The set of Long is null", ac.getSet_Long());
Assert.assertEquals("The set of Long is not empty", 0, ac.getSet_Long().size());
Assert.assertNotNull("The set of reference is null", ac.getSet_ref());
Assert.assertEquals("The set of reference is not empty", 0, ac.getSet_ref().size());
pm.currentTransaction().begin();
pm.deletePersistent(ac);
pm.currentTransaction().commit();
pm.close();
}
public void testOneElement() {
long[] ls = new long[]{217,218};
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
AllCollection ac = new AllCollection("testOneElement");
ac.setLongs(ls);
ac.setRefs(new Object[]{ac});
pm.makePersistent(ac);
Object oid = pm.getObjectId(ac);
assertNotNull("The identifier is null", oid);
pm.currentTransaction().commit();
pm.close();
pm = null;
ac = null;
pm = pmf.getPersistenceManager();
ac = (AllCollection) pm.getObjectById(oid, true);
assertNotNull("No persistence found with the id " + oid, ac);
List expectedLong = new ArrayList(ls.length);
for(int i=0; i<ls.length; i++) {
expectedLong.add(new Long(ls[i]));
}
List expectedRef = new ArrayList(1);
expectedRef.add(ac);
//Collection
Assert.assertNotNull("The collection of Long is null", ac.getCol_Long());
assertSameCollection("The collection of Long", expectedLong, ac.getCol_Long());
Assert.assertNotNull("The collection of reference is null", ac.getCol_ref());
assertSameCollection("The collection of ref", expectedRef, ac.getCol_ref());
//HashSet
Assert.assertNotNull("The Hashset of Long is null", ac.getHset_Long());
assertSameCollection("The Hashset of Long", expectedLong, ac.getHset_Long());
Assert.assertNotNull("The Hashset of reference is null", ac.getHset_ref());
assertSameCollection("The Hashset of ref", expectedRef, ac.getHset_ref());
//List
Assert.assertNotNull("The list of Long is null", ac.getList_Long());
assertSameList("The list of Long", expectedLong, ac.getList_Long());
Assert.assertNotNull("The list of reference is null", ac.getList_ref());
assertSameList("The list of reference", expectedRef, ac.getList_ref());
//Set
Assert.assertNotNull("The set of Long is null", ac.getSet_Long());
assertSameCollection("The set of Long", expectedLong, ac.getSet_Long());
Assert.assertNotNull("The set of reference is null", ac.getSet_ref());
assertSameCollection("The set of ref", expectedRef, ac.getSet_ref());
pm.currentTransaction().begin();
pm.deletePersistentAll(ac.getArraylist_ref());
ac.setRefs(null);
ac.setLongs(null);
pm.deletePersistent(ac);
pm.currentTransaction().commit();
pm.close();
}
public void testSeveralSameElement() {
long[] ls = new long[]{217,217,218,218,219,219,217,218,219};
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
AllCollection ac = new AllCollection("testSeveralSameElement");
ac.setLongs(ls);
pm.makePersistent(ac);
Object oid = pm.getObjectId(ac);
pm.currentTransaction().commit();
List expectedLong = new ArrayList(ls.length);
HashSet expectedLongset = new HashSet();
for(int i=0; i<ls.length; i++) {
expectedLong.add(new Long(ls[i]));
expectedLongset.add(new Long(ls[i]));
}
pm.currentTransaction().begin();
checkAC(ac, expectedLong, expectedLongset);
pm.currentTransaction().commit();
for(int i = 0; i<5; i++) {
pm.currentTransaction().begin();
ac.addAll(new long[]{217});
expectedLongset.add(new Long(217));
expectedLong.add(new Long(217));
checkAC(ac, expectedLong, expectedLongset);
pm.currentTransaction().commit();
pm.currentTransaction().begin();
checkAC(ac, expectedLong, expectedLongset);
pm.currentTransaction().commit();
}
for(int i = 0; i<5; i++) {
pm.currentTransaction().begin();
ac.addAll(new long[]{218,218});
expectedLongset.add(new Long(218));
expectedLongset.add(new Long(218));
expectedLong.add(new Long(218));
expectedLong.add(new Long(218));
checkAC(ac, expectedLong, expectedLongset);
pm.currentTransaction().commit();
pm.currentTransaction().begin();
checkAC(ac, expectedLong, expectedLongset);
pm.currentTransaction().commit();
}
ac = null;
pm.evictAll();
pm.currentTransaction().begin();
ac = (AllCollection) pm.getObjectById(oid, false);
checkAC(ac, expectedLong, expectedLongset);
pm.currentTransaction().commit();
pm.currentTransaction().begin();
ac.setLongs(null);
pm.deletePersistent(ac);
pm.currentTransaction().commit();
pm.close();
}
public void testSerializeWithCollection() {
PersistenceManager pm = pmf.getPersistenceManager();
Professor prof = new Professor("prof");
Student s1 = new Student(1);
Student s2 = new Student(2);
Student s3 = new Student(3);
Collection col = new ArrayList();
col.add(s1);
col.add(s2);
col.add(s3);
Course c = new Course("Course1");
c.setProf(prof);
c.setStudents(col);
pm.currentTransaction().begin();
pm.makePersistent(c);
Object oid = pm.getObjectId(c);
Course copyOfC = (Course) pm.detachCopy(c);
pm.currentTransaction().commit();
pm.close();
File f = new File(new File(new File("output"),"test"), "testSerializeWithCollection");
if (f.exists()) {
f.delete();
}
try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(f));
oos.writeObject(copyOfC);
oos.close();
c = null;
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(f));
c = (Course) ois.readObject();
ois.close();
assertEquals("Not the same professor", c.getProf().getName(), copyOfC.getProf().getName());
assertEquals("Not the same list of students", col.size(), copyOfC.getStudents().size());
int sid = 0;
int copySid = 0;
pm = pmf.getPersistenceManager();
Iterator itCol = col.iterator();
Iterator itCopy = copyOfC.getStudents().iterator();
while (itCol.hasNext()) {
Student s = (Student) itCol.next();
sid += s.getSid();
s = (Student) itCopy.next();
copySid += s.getSid();
}
assertEquals(sid, copySid);
pm.close();
} catch (Exception e) {
logger.log(BasicLevel.ERROR, "testSerializeWithCollection fails: ", e);
fail(e.getMessage());
} finally {
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.deletePersistent(pm.getObjectById(oid, false));
pm.currentTransaction().commit();
pm.close();
}
}
private void checkAC(AllCollection ac, List expectedLong, HashSet expectedLongset) {
//Collection
assertSameCollection("The collection of Long", expectedLongset, ac.getCol_Long());
//List
assertSameList("The list of Long", expectedLong, ac.getList_Long());
//ArrayList
assertSameList("The list of Long", expectedLong, ac.getArraylist_Long());
//Vector
assertSameList("The list of Long", expectedLong, ac.getVector_Long());
//HashSet
assertSameCollection("The Hashset of Long", expectedLongset, ac.getHset_Long());
//Set
assertSameCollection("The set of Long", expectedLongset, ac.getSet_Long());
}
}