1 18 package org.objectweb.speedo.runtime.inheritance; 19 20 import org.objectweb.speedo.SpeedoTestHelper; 21 import org.objectweb.speedo.api.ExceptionHelper; 22 import org.objectweb.speedo.pobjects.inheritance.filtered.Zoo; 23 import org.objectweb.speedo.pobjects.inheritance.filtered.Kangaroo; 24 import org.objectweb.speedo.pobjects.inheritance.filtered.Koala; 25 import org.objectweb.speedo.pobjects.inheritance.filtered.Animal; 26 import org.objectweb.speedo.pobjects.inheritance.filtered.AnimalId; 27 import org.objectweb.util.monolog.api.BasicLevel; 28 29 import javax.jdo.Extent; 30 import javax.jdo.PersistenceManager; 31 import javax.jdo.Query; 32 import java.util.Collection ; 33 import java.util.Iterator ; 34 35 39 public class TestFiltered extends SpeedoTestHelper { 40 41 public TestFiltered(String s) { 42 super(s); 43 } 44 45 protected String getLoggerName() { 46 return LOG_NAME + "rt.inheritance.TestFiltered"; 47 } 48 49 public void testA() { 50 int NB_ANIMAL = 20; 51 String zooName = "ASR"; 52 Zoo zoo = new Zoo(zooName); 53 int sum = NB_ANIMAL; 54 zoo.setaKangaroo(new Kangaroo("Franck", NB_ANIMAL, true, (long) 1, (float) 2.0)); 55 for(int i=0; i<NB_ANIMAL; i++) { 56 Kangaroo ka = new Kangaroo("kang" + i, i, true, (long) 1, (float) 2.0); 57 Koala ko = new Koala("koala" + i, i, true, (long) 1, 2); 58 sum += 2*i; 59 zoo.getKangaroos().add(ka); 60 zoo.getAnimals().add(ka); 61 zoo.getAnimals().add(ko); 62 } 63 Koala olivier = new Koala("Olivier", NB_ANIMAL + 1, true, (long) 1, 20); 64 sum += NB_ANIMAL + 1; 65 PersistenceManager pm = pmf.getPersistenceManager(); 66 pm.currentTransaction().begin(); 67 pm.makePersistent(zoo); 68 pm.makePersistent(olivier); 69 zoo = null; 70 olivier = null; 71 pm.currentTransaction().commit(); 72 73 pm.evictAll(); 74 pm.close(); 75 pm = pmf.getPersistenceManager(); 76 pm.evictAll(); 77 try { 78 zoo = (Zoo) pm.getObjectById( 79 pm.newObjectIdInstance(Zoo.class, zooName), false); 80 } catch (Exception e) { 81 fail("Zoo not found"); 82 } 83 assertNotNull("Null zoo", zoo); 84 Collection animals = zoo.getAnimals(); 85 for(Iterator it = animals.iterator(); it.hasNext(); ) { 86 Animal a = (Animal) it.next(); 87 logger.log(BasicLevel.INFO, "Animal: " + a.getName()); 88 } 89 Object a = zoo.getaKangaroo(); 90 assertNotNull("Null AnAnimal", a); 91 assertTrue("Bad Animal type", a instanceof Kangaroo); 92 Kangaroo ko = (Kangaroo) a; 93 assertEquals("Bad AnAnimal", "Franck", ko.getName()); 94 assertNull("Null AnAnimal", zoo.getAnAnimal()); 95 try { 96 ko = (Kangaroo) pm.getObjectById( 97 new AnimalId("Franck", Kangaroo.KANGAROO_SPECIES), 98 false); 99 logger.log(BasicLevel.INFO, "ko: " + ko.getName()); 100 } catch (Exception e) { 101 logger.log(BasicLevel.ERROR, "Error the kangaroo fetching:", 102 ExceptionHelper.getNested(e)); 103 fail("Kangaroo not found"); 104 } 105 106 try { 107 olivier = (Koala) pm.getObjectById( 108 new AnimalId("Olivier", "koala"), 109 false); 110 logger.log(BasicLevel.INFO, "koala: " + olivier.getName()); 111 } catch (Exception e) { 112 logger.log(BasicLevel.ERROR, "Error the koala fetching:", 113 ExceptionHelper.getNested(e)); 114 fail("koala not found"); 115 } 116 117 Extent extent = pm.getExtent(Animal.class, true); 118 Iterator it = extent.iterator(); 119 int i = 0; 120 while(it.hasNext()) { 121 i += ((Animal) it.next()).retrieveSize(); 122 } 123 assertEquals("Bad Sum control", sum, i); 124 extent.closeAll(); 125 126 Query q = pm.newQuery(Animal.class); 127 q.declareParameters("int min, int max"); 128 q.setFilter("((size > min) && (size < max))"); 129 Collection col = (Collection ) q.execute( 130 new Integer (NB_ANIMAL / 2), new Integer (NB_ANIMAL)); 131 for(Iterator it2 = col.iterator(); it2.hasNext();) { 132 a = it2.next(); 133 assertNotNull("Null AnAnimal", a); 134 assertTrue("Bad type", a instanceof Animal); 135 int s = ((Animal) a).retrieveSize(); 136 assertTrue("Animal size (" + s +") is not greater than " + (NB_ANIMAL /2), s > (NB_ANIMAL /2)); 137 assertTrue("Animal size (" + s +") is not lesser than " + NB_ANIMAL, s < NB_ANIMAL); 138 } 139 assertEquals("Bad query size", NB_ANIMAL -2, col.size()); 140 q.closeAll(); 141 142 q = pm.newQuery(Kangaroo.class); 143 q.declareParameters("int min, int max"); 144 q.setFilter("((size > min) && (size < max))"); 145 col = (Collection ) q.execute( 146 new Integer (NB_ANIMAL / 2), new Integer (NB_ANIMAL)); 147 for(Iterator it2 = col.iterator(); it2.hasNext();) { 148 a = it2.next(); 149 assertNotNull("Null AnAnimal", a); 150 assertTrue("Bad type", a instanceof Kangaroo); 151 int s = ((Kangaroo) a).retrieveSize(); 152 assertTrue("Not greater than " + (NB_ANIMAL /2), s > (NB_ANIMAL /2)); 153 assertTrue("Not lesser than " + NB_ANIMAL, s < NB_ANIMAL); 154 } 155 assertEquals("Bad query size", (NB_ANIMAL / 2) - 1 , col.size()); 156 q.closeAll(); 157 158 159 pm.currentTransaction().begin(); 160 extent = pm.getExtent(Animal.class, true); 161 it = extent.iterator(); 162 while (it.hasNext()) { 163 pm.deletePersistent(it.next()); 164 } 165 pm.deletePersistent(zoo); 166 pm.currentTransaction().commit(); 167 extent.closeAll(); 168 pm.close(); 169 } 170 } 171 | Popular Tags |