/**
* 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.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.inheritance.filtered.Zoo;
import org.objectweb.speedo.pobjects.inheritance.filtered.Kangaroo;
import org.objectweb.speedo.pobjects.inheritance.filtered.Koala;
import org.objectweb.speedo.pobjects.inheritance.filtered.Animal;
import org.objectweb.speedo.pobjects.inheritance.filtered.AnimalId;
import org.objectweb.util.monolog.api.BasicLevel;
import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.Collection;
import java.util.Iterator;
/**
*
* @author S.Chassande-Barrioz
*/
public class TestFiltered extends SpeedoTestHelper {
public TestFiltered(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + "rt.inheritance.TestFiltered";
}
public void testA() {
int NB_ANIMAL = 20;
String zooName = "ASR";
Zoo zoo = new Zoo(zooName);
int sum = NB_ANIMAL;
zoo.setaKangaroo(new Kangaroo("Franck", NB_ANIMAL, true, (long) 1, (float) 2.0));
for(int i=0; i<NB_ANIMAL; i++) {
Kangaroo ka = new Kangaroo("kang" + i, i, true, (long) 1, (float) 2.0);
Koala ko = new Koala("koala" + i, i, true, (long) 1, 2);
sum += 2*i;
zoo.getKangaroos().add(ka);
zoo.getAnimals().add(ka);
zoo.getAnimals().add(ko);
}
Koala olivier = new Koala("Olivier", NB_ANIMAL + 1, true, (long) 1, 20);
sum += NB_ANIMAL + 1;
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(zoo);
pm.makePersistent(olivier);
zoo = null;
olivier = null;
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
pm = pmf.getPersistenceManager();
pm.evictAll();
try {
zoo = (Zoo) pm.getObjectById(
pm.newObjectIdInstance(Zoo.class, zooName), false);
} catch (Exception e) {
fail("Zoo not found");
}
assertNotNull("Null zoo", zoo);
Collection animals = zoo.getAnimals();
for(Iterator it = animals.iterator(); it.hasNext(); ) {
Animal a = (Animal) it.next();
logger.log(BasicLevel.INFO, "Animal: " + a.getName());
}
Object a = zoo.getaKangaroo();
assertNotNull("Null AnAnimal", a);
assertTrue("Bad Animal type", a instanceof Kangaroo);
Kangaroo ko = (Kangaroo) a;
assertEquals("Bad AnAnimal", "Franck", ko.getName());
assertNull("Null AnAnimal", zoo.getAnAnimal());
try {
ko = (Kangaroo) pm.getObjectById(
new AnimalId("Franck", Kangaroo.KANGAROO_SPECIES),
false);
logger.log(BasicLevel.INFO, "ko: " + ko.getName());
} catch (Exception e) {
logger.log(BasicLevel.ERROR, "Error the kangaroo fetching:",
ExceptionHelper.getNested(e));
fail("Kangaroo not found");
}
try {
olivier = (Koala) pm.getObjectById(
new AnimalId("Olivier", "koala"),
false);
logger.log(BasicLevel.INFO, "koala: " + olivier.getName());
} catch (Exception e) {
logger.log(BasicLevel.ERROR, "Error the koala fetching:",
ExceptionHelper.getNested(e));
fail("koala not found");
}
Extent extent = pm.getExtent(Animal.class, true);
Iterator it = extent.iterator();
int i = 0;
while(it.hasNext()) {
i += ((Animal) it.next()).retrieveSize();
}
assertEquals("Bad Sum control", sum, i);
extent.closeAll();
Query q = pm.newQuery(Animal.class);
q.declareParameters("int min, int max");
q.setFilter("((size > min) && (size < max))");
Collection col = (Collection) q.execute(
new Integer(NB_ANIMAL / 2), new Integer(NB_ANIMAL));
for(Iterator it2 = col.iterator(); it2.hasNext();) {
a = it2.next();
assertNotNull("Null AnAnimal", a);
assertTrue("Bad type", a instanceof Animal);
int s = ((Animal) a).retrieveSize();
assertTrue("Animal size (" + s +") is not greater than " + (NB_ANIMAL /2), s > (NB_ANIMAL /2));
assertTrue("Animal size (" + s +") is not lesser than " + NB_ANIMAL, s < NB_ANIMAL);
}
assertEquals("Bad query size", NB_ANIMAL -2, col.size());
q.closeAll();
q = pm.newQuery(Kangaroo.class);
q.declareParameters("int min, int max");
q.setFilter("((size > min) && (size < max))");
col = (Collection) q.execute(
new Integer(NB_ANIMAL / 2), new Integer(NB_ANIMAL));
for(Iterator it2 = col.iterator(); it2.hasNext();) {
a = it2.next();
assertNotNull("Null AnAnimal", a);
assertTrue("Bad type", a instanceof Kangaroo);
int s = ((Kangaroo) a).retrieveSize();
assertTrue("Not greater than " + (NB_ANIMAL /2), s > (NB_ANIMAL /2));
assertTrue("Not lesser than " + NB_ANIMAL, s < NB_ANIMAL);
}
assertEquals("Bad query size", (NB_ANIMAL / 2) - 1 , col.size());
q.closeAll();
pm.currentTransaction().begin();
extent = pm.getExtent(Animal.class, true);
it = extent.iterator();
while (it.hasNext()) {
pm.deletePersistent(it.next());
}
pm.deletePersistent(zoo);
pm.currentTransaction().commit();
extent.closeAll();
pm.close();
}
}