/**
* 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
*
*/
package org.objectweb.speedo.runtime.fetchgroup;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.jdo.FetchPlan;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import junit.framework.Assert;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.fetchgroup.Address;
import org.objectweb.speedo.pobjects.fetchgroup.Country;
import org.objectweb.speedo.pobjects.fetchgroup.EdgeWeight;
import org.objectweb.speedo.pobjects.fetchgroup.Node;
import org.objectweb.speedo.pobjects.fetchgroup.Person;
import org.objectweb.util.monolog.api.BasicLevel;
/**
*
* @author Y.Bersihand
*/
public class TestRefreshFetchGroup extends SpeedoTestHelper {
public TestRefreshFetchGroup(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.fetchgroup.TestRefreshFetchGroup";
}
/**
* Test the refresh with the detail fetch group :
* test the definition of a fetch-group in the jdo file with a a.b.c field
* <field name="a.b.c">
*/
public void testRefreshReference() {
logger.log(BasicLevel.DEBUG, "***************testRefreshReference*****************");
Country country = new Country("it","Italie");
Address address = new Address("Rue Spiaggi", "Milan", country);
Person parent = new Person();
parent.setName("Del Piero Joel");
parent.setAge(32);
parent.setAddress(address);
Person child1 = new Person("Del Piero Sophie", address, null, 14);
Person child2 = new Person("Del Piero Mikael", address, null, 11);
Set children = new HashSet();
children.add(child1);
children.add(child2);
parent.setChildren(children);
PersistenceManager pm = pmf.getPersistenceManager();
try {
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups();
fp.addGroup("detail").removeGroup("default");
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
pm.makePersistent(parent);
Object id = pm.getObjectId(parent);
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
pm.currentTransaction().begin();
//update the age and the address
parent.setAge(99);
Address newAddress = new Address("rue Ampere", "Rennes", new Country("bz", "Bretagne"));
parent.setAddress(newAddress);
pm.refresh(parent);
pm.currentTransaction().commit();
assertEquals(32, parent.getAge());
assertEquals(address.getCity(), parent.getAddress().getCity());
assertEquals(address.getStreet(), parent.getAddress().getStreet());
} catch(Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
/**
* Test the refresh with the detail+children-names fetch group:
* test the definition of a fetch-group in the jdo file with a a#element.b field
* <field name="a#element.b">
*/
public void testRefreshArrayElement() {
logger.log(BasicLevel.DEBUG, "************testRefreshArrayElement**************");
Country country = new Country("be","Belgique");
Address address = new Address("Rue Anvers", "Bruges", country);
Person parent = new Person();
parent.setName("Dermuck Joel");
parent.setAge(32);
parent.setAddress(address);
int ageChild1 = 14;
int ageChil2 = 11;
Person child1 = new Person("Dermuck Sophie", address, null, ageChild1);
Person child2 = new Person("Dermuck Mikael", address, null, ageChil2);
int totalAgeChildren = 14 + 11;
Set children = new HashSet();
children.add(child1);
children.add(child2);
parent.setChildren(children);
PersistenceManager pm = pmf.getPersistenceManager();
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups();
fp.addGroup("detail+children-names").removeGroup("default");
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
pm.makePersistent(parent);
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
try{
pm.currentTransaction().begin();
//update the first child age and address
Person p = (Person) parent.getChildren().iterator().next();
p.setAge(2000);
Address newAddress = new Address("Rue Refresh", "Moon", new Country("m", "mars"));
p.setAddress(newAddress);
pm.refresh(parent);
int totalAgeRefreshed = 0;
Iterator it = parent.getChildren().iterator();
Person ch = null;
while (it.hasNext()) {
ch = (Person) it.next();
totalAgeRefreshed += ch.getAge();
assertEquals(address.getCity(), ch.getAddress().getCity());
assertEquals(address.getStreet(), ch.getAddress().getStreet());
}
pm.currentTransaction().commit();
assertEquals(totalAgeChildren, totalAgeRefreshed);
} catch (Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
/**
* Test the refresh with the detail+children-list fetch group:
* test the definition of a fetch-group in the jdo file with a fetch-group attribute in a field
* <field name="a" fetch-group="fg"/>
*/
public void testRefreshFetchGroupField() {
logger.log(BasicLevel.DEBUG, "************testRefreshFetchGroupField**************");
Country country = new Country("us","Etats-Unis");
Address address = new Address("Rue Enclif", "San Diego", country);
Person parent = new Person();
parent.setName("Smith Joel");
parent.setAge(32);
parent.setAddress(address);
Person child1 = new Person("Smith Sofia", address, null, 14);
Person child2 = new Person("Smith Michael", address, null, 11);
Set children = new HashSet();
children.add(child1);
children.add(child2);
parent.setChildren(children);
PersistenceManager pm = pmf.getPersistenceManager();
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups();
fp.addGroup("detail+children-list").removeGroup("default");
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
pm.makePersistent(parent);
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
try{
pm.currentTransaction().begin();
// update the first child age and address
Person p = (Person) parent.getChildren().iterator().next();
p.setAge(2000);
Address newAddress = new Address("Rue Far", "Moon", new Country("n", "neptune"));
p.setAddress(newAddress);
pm.refresh(parent);
pm.currentTransaction().commit();
Person ch = (Person) parent.getChildren().iterator().next();
int expectedAge = 0;
if (ch.getName().equals(child1.getName())) {
expectedAge = child1.getAge();
} else {
expectedAge = child2.getAge();
}
assertEquals(expectedAge, ch.getAge());
assertEquals(address.getCity(), ch.getAddress().getCity());
assertEquals(address.getStreet(), ch.getAddress().getStreet());
} catch (Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
/**
* Test the refresh with the detailChildren fetch group:
* test the definition of a fetch-group in the jdo file with a depth attribute in a field
* for recursive reference
* <field name="a" depth="X"/>
*/
public void testRefreshRecursiveDepth() {
logger.log(BasicLevel.DEBUG, "************testRefreshRecursiveDepth**************");
Country country = new Country("sp","Espagne");
Address address = new Address("Rue Rio", "Santander", country);
Person parent = new Person();
parent.setName("Casillas Joel");
parent.setAge(63);
parent.setAddress(address);
Person child1 = new Person("Casillas Sofia", address, null, 30);
Person child2 = new Person("Casillas Michael", address, null, 40);
Set children = new HashSet();
children.add(child1);
children.add(child2);
parent.setChildren(children);
Person child11 = new Person("Casillas Maria", address, null, 14);
Person child21 = new Person("Casillas Juan", address, null, 11);
Set children1 = new HashSet();
children1.add(child11);
Set children2 = new HashSet();
children2.add(child21);
child1.setChildren(children1);
child2.setChildren(children2);
PersistenceManager pm = pmf.getPersistenceManager();
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups();
fp.addGroup("detailChildren").removeGroup("default");
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
pm.makePersistent(parent);
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
try{
Address a = new Address("Rue So", "Moon", new Country("k", "Krypton"));
pm.currentTransaction().begin();
Person p = (Person) parent.getChildren().iterator().next();
p.setAge(2000);
p.setAddress(a);
Person pc = (Person) p.getChildren().iterator().next();
pc.setAge(3000);
pc.setAddress(a);
pm.refresh(parent);
logger.log(BasicLevel.DEBUG, "Warning: the child of child will not be refreshed.");
pm.currentTransaction().commit();
Person ch = (Person) parent.getChildren().iterator().next();
int expectedAge = 0;
if (ch.getName().equals(child1.getName())) {
expectedAge = child1.getAge();
} else {
expectedAge = child2.getAge();
}
assertEquals(expectedAge, ch.getAge());
assertEquals(address.getCity(), ch.getAddress().getCity());
ch = (Person) ch.getChildren().iterator().next();
assertEquals(3000, ch.getAge());
assertEquals("Moon", ch.getAddress().getCity());
} catch (Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
/**
* Test the refresh with the detailChildren fetch group:
* test the definition of a fetch-group in the jdo file with a depth attribute defined twice for a field
* <field name="a" depth="X"/>
*/
public void testRefreshDoubleDepth() {
logger.log(BasicLevel.DEBUG, "************testRefreshDoubleDepth**************");
Country country = new Country("bl","Belarus");
Address address = new Address("Rue Kaloc", "Minsk", country);
Person parent = new Person();
parent.setName("Castuk Joel");
parent.setAge(63);
parent.setAddress(address);
Person child1 = new Person("Castuk Sofia", address, null, 40);
Person child2 = new Person("Castuk Michael", address, null, 40);
Set children = new HashSet();
children.add(child1);
children.add(child2);
parent.setChildren(children);
Person child11 = new Person("Castuk Maria", address, null, 11);
Person child21 = new Person("Castuk Juan", address, null, 11);
Set children1 = new HashSet();
children1.add(child11);
Set children2 = new HashSet();
children2.add(child21);
child1.setChildren(children1);
child2.setChildren(children2);
PersistenceManager pm = pmf.getPersistenceManager();
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups();
fp.addGroup("detailChildren2").removeGroup("default");
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
pm.makePersistent(parent);
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
try{
pm.currentTransaction().begin();
Address a = new Address("Rue Away", "Moon", new Country("pl", "Pluton"));
Person p = (Person) parent.getChildren().iterator().next();
p.setAge(2000);
p.setAddress(a);
Person pc = (Person) p.getChildren().iterator().next();
pc.setAge(3000);
pc.setAddress(a);
pm.refresh(parent);
pm.currentTransaction().commit();
Person ch = (Person) parent.getChildren().iterator().next();
assertEquals(40, ch.getAge());
assertEquals(address.getCity(), ch.getAddress().getCity());
ch = (Person) ch.getChildren().iterator().next();
assertEquals(11, ch.getAge());
assertEquals(address.getCity(), ch.getAddress().getCity());
} catch(Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
/**
* Test the refresh with the detailChildren fetch group:
* test the definition of a fetch-group in the jdo file with a depth attribute defined twice for a field
* one of them is unlimited
* <field name="a" depth="X"/>
*/
public void testRefreshDoubleDepthUnlimited() {
logger.log(BasicLevel.DEBUG, "************testRefreshDoubleDepthUnlimited**************");
Country country = new Country("p","Portugal");
Address address = new Address("Rue Christiano", "Lisbonne", country);
Person grandParent = new Person();
grandParent.setName("Simoes Joel");
grandParent.setAge(90);
grandParent.setAddress(address);
Person parent1 = new Person("Simoes Sofia", address, null, 70);
Person parent2 = new Person("Simoes Michael", address, null, 70);
Set parent = new HashSet();
parent.add(parent1);
parent.add(parent2);
grandParent.setChildren(parent);
Person child1 = new Person("Simoes Maria", address, null, 40);
Person child2 = new Person("Simoes Juan", address, null, 40);
Set children1 = new HashSet();
children1.add(child1);
Set children2 = new HashSet();
children2.add(child2);
parent1.setChildren(children1);
parent2.setChildren(children2);
Person grandChild1 = new Person("Simoes Leia", address, null, 10);
Person grandChild2 = new Person("Simoes Carlos", address, null, 10);
Set grandChildren1 = new HashSet();
grandChildren1.add(grandChild1);
Set grandChildren2 = new HashSet();
grandChildren2.add(grandChild2);
child1.setChildren(grandChildren1);
child2.setChildren(grandChildren2);
PersistenceManager pm = pmf.getPersistenceManager();
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups();
fp.addGroup("detailChildren3").removeGroup("default");
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the person " + grandParent.toString());
pm.makePersistent(grandParent);
pm.currentTransaction().commit();
logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
try {
pm.currentTransaction().begin();
Address a = new Address("Rue From", "Moon", new Country("sl", "Solarus"));
Person pParent = (Person) grandParent.getChildren().iterator().next();
pParent.setAge(2000);
pParent.setAddress(a);
Person pChild = (Person) pParent.getChildren().iterator().next();
pChild.setAge(3000);
pChild.setAddress(a);
Person pGrandChild = (Person) pChild.getChildren().iterator().next();
pGrandChild.setAge(4000);
pGrandChild.setAddress(a);
pm.refresh(grandParent);
pm.currentTransaction().commit();
Person cParent = (Person) grandParent.getChildren().iterator().next();
assertEquals(70, cParent.getAge());
assertEquals(address.getCity(), cParent.getAddress().getCity());
Person cChild = (Person) cParent.getChildren().iterator().next();
assertEquals(40, cChild.getAge());
assertEquals(address.getCity(), cChild.getAddress().getCity());
Person cGrandChild = (Person) cChild.getChildren().iterator().next();
assertEquals(10, cGrandChild.getAge());
assertEquals(address.getCity(), cGrandChild.getAddress().getCity());
} catch(Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
/**
* Test the refresh with the keyValue fetchgroup:
* test the definition of a fetch-group with a map, refresh both keys and values
* <field name="map#key">
* <field name="map#value">
*/
public void testRefreshMapKeyValue() {
logger.log(BasicLevel.DEBUG, "************testRefreshMapKeyValue**************");
Node n1 = new Node("n1");
Node n2 = new Node("n2");
Node n3 = new Node("n3");
Node n4 = new Node("n4");
Node n5 = new Node("n5");
n1.addEdge(n2.getName(), new EdgeWeight(1));
n1.addEdge(n3.getName(), new EdgeWeight(2));
n2.addEdge(n4.getName(), new EdgeWeight(7));
n2.addEdge(n5.getName(), new EdgeWeight(4));
PersistenceManager pm = pmf.getPersistenceManager();
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups();
fp.addGroup("keyValue").removeGroup("default");
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the nodes " + n1.toString() + ", " + n2.toString()
+ ", " + n3.toString() + ", " + n4.toString() + ", " + n5.toString());
pm.makePersistent(n1);
pm.makePersistent(n2);
pm.makePersistent(n3);
pm.makePersistent(n4);
pm.makePersistent(n5);
pm.currentTransaction().commit();
FetchPlan f = pm.getFetchPlan();
logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
try{
pm.currentTransaction().begin();
String nodeName = (String) n1.getEdges().keySet().iterator().next();
EdgeWeight ew = (EdgeWeight) n1.getEdges().get(nodeName);
ew.setWeight(3000);
pm.refresh(n1);
pm.currentTransaction().commit();
assertEquals(2, ew.getWeight());
} catch(Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
/**
* Test the refresh with the keyOnly fetchgroup:
* test the definition of a fetch-group with a map, refresh only keys
* <field name="map#key">
*/
public void testRefreshMapKeyOnly() {
logger.log(BasicLevel.DEBUG, "************testRefreshMapKeyOnly**************");
Node n1 = new Node("n11");
Node n2 = new Node("n21");
Node n3 = new Node("n31");
Node n4 = new Node("n41");
Node n5 = new Node("n51");
n1.addEdge(n2.getName(), new EdgeWeight(1));
n1.addEdge(n3.getName(), new EdgeWeight(2));
n2.addEdge(n4.getName(), new EdgeWeight(7));
n2.addEdge(n5.getName(), new EdgeWeight(4));
PersistenceManager pm = pmf.getPersistenceManager();
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups();
fp.addGroup("keyOnly").removeGroup("default");
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the nodes " + n1.toString() + ", " + n2.toString()
+ ", " + n3.toString() + ", " + n4.toString() + ", " + n5.toString());
pm.makePersistent(n1);
pm.makePersistent(n2);
pm.makePersistent(n3);
pm.makePersistent(n4);
pm.makePersistent(n5);
pm.currentTransaction().commit();
FetchPlan f = pm.getFetchPlan();
logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
try {
pm.currentTransaction().begin();
String nodeName = (String) n1.getEdges().keySet().iterator().next();
EdgeWeight ew = (EdgeWeight) n1.getEdges().get(nodeName);
ew.setWeight(3000);
pm.refresh(n1);
pm.currentTransaction().commit();
assertEquals(3000, ew.getWeight());
} catch(Exception e) {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
fail(e.getMessage());
} finally {
pm.close();
}
}
public void testRemovingOfPersistentObject() {
PersistenceManager pm = pmf.getPersistenceManager();
try {
Class[] cs = new Class[]{Address.class, Country.class, Person.class, Node.class, EdgeWeight.class};
pm.currentTransaction().begin();
for(int i=0; i<cs.length; i++) {
Query query = pm.newQuery(cs[i]);
Collection col = (Collection) query.execute();
Iterator it = col.iterator();
while(it.hasNext()) {
Object o = it.next();
Assert.assertNotNull("null object in the query result"
+ cs[i].getName(), o);
pm.deletePersistent(o);
}
query.close(col);
}
pm.currentTransaction().commit();
} catch (JDOException e) {
Exception ie = ExceptionHelper.getNested(e);
logger.log(BasicLevel.ERROR, "", ie);
fail(ie.getMessage());
} finally {
pm.close();
}
}
}