/**
* 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.inheritance;
import org.objectweb.speedo.pobjects.inheritance.ejboo2.ArticlePersistantImpl;
import org.objectweb.speedo.pobjects.inheritance.ejboo2.CataloguePersistant;
import org.objectweb.speedo.pobjects.inheritance.ejboo2.CataloguePersistantImpl;
import org.objectweb.speedo.pobjects.inheritance.ejboo2.MarchePersistantImpl;
import org.objectweb.speedo.pobjects.inheritance.ejboo2.ServicePersistantImpl;
import org.objectweb.speedo.pobjects.inheritance.ejboo2.MaterielPersistantImpl;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.util.monolog.api.BasicLevel;
import javax.jdo.PersistenceManager;
import javax.jdo.Extent;
import javax.jdo.Query;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collection;
/**
*
* @author S.Chassande-Barrioz
*/
public class TestEjboo2 extends SpeedoTestHelper {
public TestEjboo2(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + "rt.inheritance.TesEjboo2";
}
public void testA() {
final int NB_ARTICLE = 6;
final int CATLAOGUE_SIZE = 3;
final int MARCHE_SIZE = 2;
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
CataloguePersistant cat = null;
int nbCat = 0;
MarchePersistantImpl mar= null;
int nbMar = 0;
ArticlePersistantImpl a;
for(int idArt=0; idArt<NB_ARTICLE; idArt++) {
if ((idArt / CATLAOGUE_SIZE) == nbCat) {
cat = new CataloguePersistantImpl(nbCat);
pm.makePersistent(cat);
nbCat ++;
}
if ((idArt / MARCHE_SIZE) == nbMar) {
mar = new MarchePersistantImpl(nbMar);
pm.makePersistent(mar);
nbMar ++;
}
if ((idArt % 2) == 0) { //pair
a = new ServicePersistantImpl(idArt);
a.setNom("article_service_" + idArt);
} else { //impair
a = new MaterielPersistantImpl(idArt);
a.setNom("article_materiel_" + idArt);
}
pm.makePersistent(a);
a.setCatalogue(cat);
mar.getArticles().add(a);
}
pm.currentTransaction().commit();
a = null;
cat = null;
mar = null;
pm.evictAll();
pm.currentTransaction().begin();
Extent extent = pm.getExtent(CataloguePersistantImpl.class, true);
Iterator it = extent.iterator();
ArrayList errors = new ArrayList();
while(it.hasNext()) {
cat = (CataloguePersistantImpl) it.next();
logger.log(BasicLevel.DEBUG, "Catalogue " + cat.getId());
Collection arts = cat.getArticles();
Iterator articles = arts.iterator();
while(articles.hasNext()) {
a = (ArticlePersistantImpl) articles.next();
logger.log(BasicLevel.DEBUG, "\tArticle " + a.getId());
Collection mars = a.getMarches();
Iterator marches = mars.iterator();
while (marches.hasNext()) {
mar = (MarchePersistantImpl) marches.next();
logger.log(BasicLevel.DEBUG, "\t\tMarche " + mar.getId());
Collection m2as = mar.getArticles();
if (!m2as.contains(a)) {
errors.add(new Exception("The article '" + a.getId()
+ "' is not in the collection marche(" + mar.getId()
+ ").articles"));
}
}
}
}
extent.closeAll();
pm.currentTransaction().commit();
if (!errors.isEmpty()) {
for (Iterator iter = errors.iterator(); iter.hasNext();) {
Exception e = (Exception) iter.next();
logger.log(BasicLevel.ERROR, e.getMessage());
}
fail(errors.size() + " error(s)");
}
a = null;
mar = null;
pm.currentTransaction().begin();
Query query = pm.newQuery(ArticlePersistantImpl.class);
query.declareParameters("String p1,CataloguePersistantImpl p2");
query.setFilter("(nom.startsWith(p1) && (catalogue == p2))");
query.setResult("count(*)");
query.setUnique(true);
query.setRange(0,2);
Long l = (Long) query.executeWithArray(new Object[] { "article", cat });
assertTrue("", l.longValue() > 0);
query.closeAll();
pm.currentTransaction().commit();
pm.currentTransaction().begin();
query = pm.newQuery(ArticlePersistantImpl.class);
query.declareParameters("String p1,CataloguePersistantImpl p2");
query.setFilter("(nom.startsWith(p1) && (catalogue == p2))");
query.setResult("count(*)");
query.setUnique(true);
query.setRange(0,5);
l = (Long) query.executeWithArray(new Object[] { "article", cat });
query.closeAll();
assertTrue("", l.longValue() > 0);
pm.currentTransaction().commit();
pm.currentTransaction().begin();
query = pm.newQuery(ArticlePersistantImpl.class);
query.setRange(0,2);
new ArrayList((Collection) query.execute());
query.closeAll();
pm.currentTransaction().commit();
cat = null;
pm.currentTransaction().begin();
extent = pm.getExtent(ArticlePersistantImpl.class, true);
it = extent.iterator();
while (it.hasNext()) {
a = (ArticlePersistantImpl) it.next();
cat = a.getCatalogue();
if (cat != null) {
pm.deletePersistent(cat);
}
pm.deletePersistentAll(a.getMarches());
pm.deletePersistent(a);
}
pm.currentTransaction().commit();
pm.close();
}
}