/**
* 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.G;
import org.objectweb.speedo.pobjects.collection.H;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
*
* @author S.Chassande-Barrioz
*/
public class TestGH extends SpeedoTestHelper {
public TestGH(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.collection.TestGH";
}
public void testA() {
int NBOBJ = 10;
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
G g = new G("g1_testA");
Collection hs = g.getHs();
for(int i=0; i<NBOBJ; i++) {
H h = new H("hid_" + i);
h.setH1("h1_testA");
hs.add(h);
}
pm.makePersistent(g);
g = null;
hs = null;
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
pm = pmf.getPersistenceManager();
pm.evictAll();
pm.currentTransaction().begin();
H h0 = (H) pm.getObjectById(pm.newObjectIdInstance(H.class, "hid_0"), false);
assertNotNull("Null h0", h0);
g = h0.getG();
assertNotNull("Null g", g);
pm.deletePersistentAll(g.getHs());
pm.deletePersistent(g);
pm.currentTransaction().commit();
pm.close();
}
public void testCollectionElementLoadingWithPrefetch() {
int NBOBJ = 2;
PersistenceManager pm = pmf.getPersistenceManager();
//create persistent instances
pm.currentTransaction().begin();
G g = new G("g1_testCollectionElementLoadingWithPrefetch");
Collection hs = g.getHs();
for(int i=0; i<NBOBJ; i++) {
H h = new H("hid_" + i);
h.setH1("h1_testCollectionElementLoadingWithPrefetch");
hs.add(h);
}
hs = null;
pm.makePersistent(g);
Object g_oid = pm.getObjectId(g);
g = null;
pm.currentTransaction().commit();
//evict all instances
pm.evictAll();
pm.currentTransaction().begin();
Query q = pm.newQuery(H.class);
q.declareParameters("String p1");
q.setFilter("hid == p1");
Object o = new ArrayList(((Collection) q.execute("hid_" + (NBOBJ-1))));
q.closeAll();
g = (G) pm.getObjectById(g_oid, false);
for (Iterator iter = g.getHs().iterator(); iter.hasNext();) {
iter.next();
}
pm.currentTransaction().commit();
//Clean up
pm.currentTransaction().begin();
pm.deletePersistentAll(g.getHs());
pm.deletePersistent(g);
pm.currentTransaction().commit();
pm.close();
}
}