/**
* 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.map;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.map.C;
import org.objectweb.speedo.pobjects.map.D;
import java.util.ArrayList;
import java.util.Map;
import javax.jdo.PersistenceManager;
/**
*
* @author S.Chassande-Barrioz
*/
public class TestKeyFieldMap extends SpeedoTestHelper {
public TestKeyFieldMap(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.map.TestKeyFieldMap";
}
public void test1() {
int MAP_SIZE = 10;
String F1_PREFIX = "f1_";
PersistenceManager pm = pmf.getPersistenceManager();
//Create instances
pm.currentTransaction().begin();
C c = new C(1);
ArrayList ds = new ArrayList(MAP_SIZE * 2);
for(int i=0; i<MAP_SIZE; i++) {
String f1 = F1_PREFIX + i;
ds.add(new D(i, f1));
}
pm.makePersistent(c);
pm.makePersistentAll(ds);
pm.currentTransaction().commit();
//Add into the map
pm.currentTransaction().begin();
Map m = c.getDkey2d();
for(int i=0; i<MAP_SIZE; i++) {
D d = (D) ds.get(i);
m.put(d.getF1(), d);
}
c = null;
ds = null;
pm.currentTransaction().commit();
finishTest(pm, MAP_SIZE, F1_PREFIX);
}
public void test2() {
int MAP_SIZE = 10;
String F1_PREFIX = "f1_";
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
C c = new C(1);
Map m = c.getDkey2d();
for(int i=0; i<MAP_SIZE; i++) {
String f1 = "f1_" + i;
D d = new D(i, f1);
m.put(f1, d);
}
pm.makePersistent(c);
pm.currentTransaction().commit();
finishTest(pm, MAP_SIZE, F1_PREFIX);
}
public void test3() {
int MAP_SIZE = 10;
String F1_PREFIX = "f1_";
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
C c = new C(1);
for(int i=0; i<MAP_SIZE; i++) {
String f1 = "f1_" + i;
D d = new D(i, f1);
d.setMyc(c);
pm.makePersistent(d);
}
pm.currentTransaction().commit();
finishTest(pm, MAP_SIZE, F1_PREFIX);
}
private void finishTest(PersistenceManager pm,
final int MAP_SIZE,
final String F1_PREFIX) {
pm.currentTransaction().begin();
long id = MAP_SIZE/2;
String f1 = F1_PREFIX + id;
C c = (C) pm.getObjectById(pm.newObjectIdInstance(C.class, "1"), false);
Object d = c.getDkey2d().get(f1);
assertNotNull("No Object associated to the key '" + f1 + "'", d);
assertTrue("Object associated to the key '" + f1 +"'", d instanceof D);
assertEquals("Bad D identifier ", id, ((D) d).getMyid());
assertNotNull("Bad D identifier ", ((D) d).getMyc());
assertEquals("Bad C identifier ", 1, ((D) d).getMyc().getMyid());
c = ((D) d).getMyc();
((D) d).setMyc(null);
assertNull("The map contains again the D instance", c.getDkey2d().get(f1));
pm.currentTransaction().commit();
//Delete
pm.currentTransaction().begin();
pm.deletePersistent(d);
pm.deletePersistentAll(c.getDkey2d().values());
pm.deletePersistent(c);
pm.currentTransaction().commit();
pm.close();
}
}