/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* 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
*
*
*
* Contact: speedo@objectweb.org
*
* Authors: S.Chassande-Barrioz.
*
*/
package org.objectweb.speedo.runtime.map;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.map.Registry;
import org.objectweb.speedo.pobjects.map.A;
import org.objectweb.speedo.pobjects.map.B;
import javax.jdo.PersistenceManager;
import javax.jdo.JDOException;
import junit.framework.Assert;
import java.util.Collection;
public class TestMap extends SpeedoTestHelper {
public TestMap(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.map.TestMap";
}
public void testCreation1() {
Registry r1 = new Registry("r1");
Registry r2 = new Registry("r2");
Registry r3 = new Registry("r3");
r1.bind("a", r2);
r1.bind("b", r3);
PersistenceManager pm = pmf.getPersistenceManager();
pm.makePersistent(r1);
Object oid1 = pm.getObjectId(r1);
Assert.assertNotNull("null object identifier of r1", r1);
pm.close();
r1 = null;
r2 = null;
r3 = null;
pm = pmf.getPersistenceManager();
pm.evictAll();
r1 = (Registry) pm.getObjectById(oid1, true);
Assert.assertNotNull("null instance returned by getObjectById(oid1)", r1);
r2 = (Registry) r1.lookup("a");
Assert.assertNotNull("null instance returned by r1.lookup(a)", r2);
r3 = (Registry) r1.lookup("b");
Assert.assertNotNull("null instance returned by r1.lookup(b)", r3);
pm.currentTransaction().begin();
pm.deletePersistent(r1);
pm.deletePersistent(r2);
pm.deletePersistent(r3);
pm.currentTransaction().commit();
pm.close();
}
public void testA() {
//Make persitent the a instance
PersistenceManager pm = pmf.getPersistenceManager();
A a = new A(0);
pm.makePersistent(a);
pm.close();
//Add B instances into the map
final int NB_B = 10;
final String IDX_PREFIX = "index2B";
final String F1_PREFIX = "toto";
pm = pmf.getPersistenceManager();
for(int i=0; i<NB_B; i++) {
a.add(IDX_PREFIX + i, new B(i, F1_PREFIX + i));
}
pm.close();
//Clear the cache
a = null; //permit the GC to garbage instances
pm = pmf.getPersistenceManager();
pm.evictAll();
pm.close();
//Check the instance existing
pm = pmf.getPersistenceManager();
try {
a = (A) pm.getObjectById(pm.newObjectIdInstance(A.class, "" + 0), false);
} catch (JDOException e) {
fail("The A instance is not found on the data support");
}
Collection names = a.names();
assertNotNull("name collection is null", names);
assertEquals("Bad name collection size", NB_B, names.size());
B[] bs = new B[NB_B];
for(int i=0; i<NB_B; i++) {
String idx = IDX_PREFIX + i;
assertTrue("The name collection does not contain the index: " + idx,
names.contains(idx));
bs[i] = a.getB(idx);
assertNotNull("Null element assocaited to the index " + idx, bs[i]);
assertEquals("Bad F1 value for the element assocaited to the index "
+ idx, F1_PREFIX + i, bs[i].getF1());
}
a.clear();
pm.currentTransaction().begin();
pm.deletePersistentAll(bs);
pm.deletePersistent(a);
pm.currentTransaction().commit();
pm.close();
}
}