001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.inheritance;
018:
019: import org.objectweb.speedo.SpeedoTestHelper;
020: import org.objectweb.speedo.api.ExceptionHelper;
021: import org.objectweb.speedo.pobjects.inheritance.filtered.Zoo;
022: import org.objectweb.speedo.pobjects.inheritance.filtered.Kangaroo;
023: import org.objectweb.speedo.pobjects.inheritance.filtered.Koala;
024: import org.objectweb.speedo.pobjects.inheritance.filtered.Animal;
025: import org.objectweb.speedo.pobjects.inheritance.filtered.AnimalId;
026: import org.objectweb.util.monolog.api.BasicLevel;
027:
028: import javax.jdo.Extent;
029: import javax.jdo.PersistenceManager;
030: import javax.jdo.Query;
031: import java.util.Collection;
032: import java.util.Iterator;
033:
034: /**
035: *
036: * @author S.Chassande-Barrioz
037: */
038: public class TestFiltered extends SpeedoTestHelper {
039:
040: public TestFiltered(String s) {
041: super (s);
042: }
043:
044: protected String getLoggerName() {
045: return LOG_NAME + "rt.inheritance.TestFiltered";
046: }
047:
048: public void testA() {
049: int NB_ANIMAL = 20;
050: String zooName = "ASR";
051: Zoo zoo = new Zoo(zooName);
052: int sum = NB_ANIMAL;
053: zoo.setaKangaroo(new Kangaroo("Franck", NB_ANIMAL, true,
054: (long) 1, (float) 2.0));
055: for (int i = 0; i < NB_ANIMAL; i++) {
056: Kangaroo ka = new Kangaroo("kang" + i, i, true, (long) 1,
057: (float) 2.0);
058: Koala ko = new Koala("koala" + i, i, true, (long) 1, 2);
059: sum += 2 * i;
060: zoo.getKangaroos().add(ka);
061: zoo.getAnimals().add(ka);
062: zoo.getAnimals().add(ko);
063: }
064: Koala olivier = new Koala("Olivier", NB_ANIMAL + 1, true,
065: (long) 1, 20);
066: sum += NB_ANIMAL + 1;
067: PersistenceManager pm = pmf.getPersistenceManager();
068: pm.currentTransaction().begin();
069: pm.makePersistent(zoo);
070: pm.makePersistent(olivier);
071: zoo = null;
072: olivier = null;
073: pm.currentTransaction().commit();
074:
075: pm.evictAll();
076: pm.close();
077: pm = pmf.getPersistenceManager();
078: pm.evictAll();
079: try {
080: zoo = (Zoo) pm.getObjectById(pm.newObjectIdInstance(
081: Zoo.class, zooName), false);
082: } catch (Exception e) {
083: fail("Zoo not found");
084: }
085: assertNotNull("Null zoo", zoo);
086: Collection animals = zoo.getAnimals();
087: for (Iterator it = animals.iterator(); it.hasNext();) {
088: Animal a = (Animal) it.next();
089: logger.log(BasicLevel.INFO, "Animal: " + a.getName());
090: }
091: Object a = zoo.getaKangaroo();
092: assertNotNull("Null AnAnimal", a);
093: assertTrue("Bad Animal type", a instanceof Kangaroo);
094: Kangaroo ko = (Kangaroo) a;
095: assertEquals("Bad AnAnimal", "Franck", ko.getName());
096: assertNull("Null AnAnimal", zoo.getAnAnimal());
097: try {
098: ko = (Kangaroo) pm.getObjectById(new AnimalId("Franck",
099: Kangaroo.KANGAROO_SPECIES), false);
100: logger.log(BasicLevel.INFO, "ko: " + ko.getName());
101: } catch (Exception e) {
102: logger.log(BasicLevel.ERROR,
103: "Error the kangaroo fetching:", ExceptionHelper
104: .getNested(e));
105: fail("Kangaroo not found");
106: }
107:
108: try {
109: olivier = (Koala) pm.getObjectById(new AnimalId("Olivier",
110: "koala"), false);
111: logger.log(BasicLevel.INFO, "koala: " + olivier.getName());
112: } catch (Exception e) {
113: logger.log(BasicLevel.ERROR, "Error the koala fetching:",
114: ExceptionHelper.getNested(e));
115: fail("koala not found");
116: }
117:
118: Extent extent = pm.getExtent(Animal.class, true);
119: Iterator it = extent.iterator();
120: int i = 0;
121: while (it.hasNext()) {
122: i += ((Animal) it.next()).retrieveSize();
123: }
124: assertEquals("Bad Sum control", sum, i);
125: extent.closeAll();
126:
127: Query q = pm.newQuery(Animal.class);
128: q.declareParameters("int min, int max");
129: q.setFilter("((size > min) && (size < max))");
130: Collection col = (Collection) q.execute(new Integer(
131: NB_ANIMAL / 2), new Integer(NB_ANIMAL));
132: for (Iterator it2 = col.iterator(); it2.hasNext();) {
133: a = it2.next();
134: assertNotNull("Null AnAnimal", a);
135: assertTrue("Bad type", a instanceof Animal);
136: int s = ((Animal) a).retrieveSize();
137: assertTrue("Animal size (" + s + ") is not greater than "
138: + (NB_ANIMAL / 2), s > (NB_ANIMAL / 2));
139: assertTrue("Animal size (" + s + ") is not lesser than "
140: + NB_ANIMAL, s < NB_ANIMAL);
141: }
142: assertEquals("Bad query size", NB_ANIMAL - 2, col.size());
143: q.closeAll();
144:
145: q = pm.newQuery(Kangaroo.class);
146: q.declareParameters("int min, int max");
147: q.setFilter("((size > min) && (size < max))");
148: col = (Collection) q.execute(new Integer(NB_ANIMAL / 2),
149: new Integer(NB_ANIMAL));
150: for (Iterator it2 = col.iterator(); it2.hasNext();) {
151: a = it2.next();
152: assertNotNull("Null AnAnimal", a);
153: assertTrue("Bad type", a instanceof Kangaroo);
154: int s = ((Kangaroo) a).retrieveSize();
155: assertTrue("Not greater than " + (NB_ANIMAL / 2),
156: s > (NB_ANIMAL / 2));
157: assertTrue("Not lesser than " + NB_ANIMAL, s < NB_ANIMAL);
158: }
159: assertEquals("Bad query size", (NB_ANIMAL / 2) - 1, col.size());
160: q.closeAll();
161:
162: pm.currentTransaction().begin();
163: extent = pm.getExtent(Animal.class, true);
164: it = extent.iterator();
165: while (it.hasNext()) {
166: pm.deletePersistent(it.next());
167: }
168: pm.deletePersistent(zoo);
169: pm.currentTransaction().commit();
170: extent.closeAll();
171: pm.close();
172: }
173: }
|