/**
* 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 org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.collection.Employee;
import org.objectweb.speedo.pobjects.collection.AMMB;
import org.objectweb.speedo.pobjects.collection.BMMB;
import org.objectweb.util.monolog.api.BasicLevel;
import javax.jdo.PersistenceManager;
import junit.framework.Assert;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
*
* @author S.Chassande-Barrioz
*/
public class POBuilder extends SpeedoTestHelper {
public final static String EMPLOYEE_NAME = "POBuilder_testCreateEmployee";
public final static String EMPLOYEE_NAME2 = "POBuilder_testSetCollection";
public final static int NB_ELEMENTS = 20;
public final static int NB_ELEMENTS2 = 5;
public static String getName(int i) {
return "testIterator_e" + i;
}
public static String getName2(int i) {
return "testSetCollection_e" + i;
}
public static List getElementNames() {
ArrayList res = new ArrayList(NB_ELEMENTS);
for(int i=0; i<NB_ELEMENTS; i++) {
res.add(getName(i));
}
return res;
}
public static List getElementNames2() {
ArrayList res = new ArrayList(NB_ELEMENTS2);
for(int i=0; i<NB_ELEMENTS2; i++) {
res.add(getName2(i));
}
return res;
}
public POBuilder(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.collection.POBuilder";
}
public void testCreateEmployee() {
logger.log(BasicLevel.DEBUG, "Start testCreateEmployee");
Employee e = new Employee(EMPLOYEE_NAME, null);
PersistenceManager pm = pmf.getPersistenceManager();
pm.makePersistent(e);
Employee[] es = new Employee[NB_ELEMENTS];
for(int i=0; i<NB_ELEMENTS; i++) {
es[i]= new Employee(getName(i));
e.addFriend(es[i]);
e.addInt(i);
}
Assert.assertEquals("Bad sise:" , NB_ELEMENTS, e.friends.size());
Collection elems = e.friends;
Assert.assertNotNull("Null collection field", elems);
Iterator elemIt = elems.iterator();
Assert.assertNotNull("Null iterator over elements", elemIt);
ArrayList elemNames = new ArrayList();
while(elemIt.hasNext()) {
Object elem = elemIt.next();
Assert.assertNotNull("Null element", elem);
Assert.assertEquals("bad elem type", Employee.class, elem.getClass());
elemNames.add(((Employee) elem).name);
}
assertSameCollection("Bad element names: ", POBuilder.getElementNames(), elemNames);
pm.close();
}
public void testSetCollection() {
logger.log(BasicLevel.DEBUG, "Start testSetCollection");
Employee e = new Employee(EMPLOYEE_NAME2, null);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(e);
pm.currentTransaction().commit();
ArrayList es = new ArrayList();
pm.currentTransaction().begin();
for(int i=0; i<NB_ELEMENTS2; i++) {
es.add(new Employee(getName2(i)));
}
e.setFriends(es);
Collection elems = e.friends;
Assert.assertNotNull("Null collection field", elems);
Iterator elemIt = elems.iterator();
Assert.assertNotNull("Null iterator over elements", elemIt);
ArrayList elemNames = new ArrayList();
while(elemIt.hasNext()) {
Object elem = elemIt.next();
Assert.assertNotNull("Null element", elem);
Assert.assertEquals("bad elem type", Employee.class, elem.getClass());
elemNames.add(((Employee) elem).name);
}
assertSameCollection("Bad element names: ", POBuilder.getElementNames2(), elemNames);
pm.currentTransaction().commit();
pm.close();
}
public void testCreateXMMB() {
logger.log(BasicLevel.DEBUG, "Start testCreateXMMB");
long ida = 1230;
AMMB a = new AMMB(ida);
ArrayList bs = new ArrayList();
ArrayList as = new ArrayList(1);
as.add(a);
for(int i=0; i<5; i++) {
BMMB b = new BMMB(ida + i);
b.setAs(as);
bs.add(b);
}
a.setBs(bs);
PersistenceManager pm = pmf.getPersistenceManager();
pm.makePersistent(a);
pm.close();
}
}