1 25 26 package org.objectweb.speedo.runtime.inheritance; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import javax.jdo.Extent; 33 import javax.jdo.JDOException; 34 import javax.jdo.PersistenceManager; 35 import javax.jdo.Query; 36 37 import junit.framework.Assert; 38 39 import org.objectweb.speedo.SpeedoTestHelper; 40 import org.objectweb.speedo.api.ExceptionHelper; 41 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A1; 42 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A21; 43 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A22; 44 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A3; 45 import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.B; 46 import org.objectweb.util.monolog.api.BasicLevel; 47 48 52 public class TestCompositeFilterOutOfPK extends SpeedoTestHelper { 53 54 public TestCompositeFilterOutOfPK(String s) { 55 super(s); 56 } 57 58 protected String getLoggerName() { 59 return LOG_NAME + ".rt.inheritance.TestCompositeFilterOutOfPK"; 60 } 61 62 int nbOfA1 = 3; 63 int nbOfA22 = 2; 64 int nbOfA3 = 2; 65 int nbToRetrieve = 0; 66 67 public void testCompositeFilterOutOfPK() { 68 logger.log(BasicLevel.DEBUG, "***************testCompositeFilterOutOfPK*****************"); 69 createInheritedObjects(); 71 iterateExtent(A1.class); 73 iterateExtent(A21.class); 75 iterateExtent(A22.class); 77 iterateExtent(A3.class); 79 retrieveAllXXA(1000); 81 } 82 83 86 public void createInheritedObjects(){ 87 PersistenceManager pm = pmf.getPersistenceManager(); 88 try { 89 Collection col = new ArrayList (); 90 91 B b1 = new B(1000, "1000"); 92 col.add(b1); 93 B b2 = new B(2000, "2000"); 94 col.add(b2); 95 A1[] a1 = new A1[nbOfA1]; 97 for (int i = 0; i < a1.length; i++) { 98 a1[i] = new A1(i+1, ""+(i+1), 1, b1); 99 col.add(a1[i]); 100 nbToRetrieve++; 101 } 102 A22[] a22 = new A22[nbOfA22]; 104 for (int i = 0; i < a22.length; i++) { 105 a22[i] = new A22(i+1+nbOfA1, ""+(i+1+nbOfA1), 22, b1, "this is an A22"); 106 col.add(a22[i]); 107 nbToRetrieve++; 108 } 109 A3[] a3 = new A3[nbOfA3]; 111 for (int i = 0; i < a3.length; i++) { 112 a3[i] = new A3(i+1+nbOfA1+nbOfA22, ""+(i+1+nbOfA1+nbOfA22), 3, b2, true, 9); 113 col.add(a3[i]); 114 } 115 116 117 pm.currentTransaction().begin(); 119 pm.makePersistentAll(col); 120 pm.currentTransaction().commit(); 121 } catch (Exception e) { 122 fail(e.getMessage()); 123 } finally { 124 if (pm.currentTransaction().isActive()) 125 pm.currentTransaction().rollback(); 126 pm.evictAll(); 128 pm.close(); 129 } 130 } 131 132 public void iterateExtent(Class cl){ 134 PersistenceManager pm = pmf.getPersistenceManager(); 135 try { 136 int nb = 0; 137 int nbExpected = 0; 138 if (cl == A1.class) { 139 nbExpected = nbOfA1 + nbOfA22+ nbOfA3; 140 } else if (cl == A21.class) { 141 nbExpected = nbOfA22; 142 } else if (cl == A22.class) { 143 nbExpected = nbOfA22; 144 } else if (cl == A3.class) { 145 nbExpected = nbOfA3; 146 } 147 148 pm.currentTransaction().begin(); 149 Extent extent = pm.getExtent(cl, true); 150 Iterator it = extent.iterator(); 151 String className = cl.getName().substring(cl.getName().lastIndexOf(".")); 152 logger.log(BasicLevel.DEBUG, "All " + cl.getName() + " instances:"); 153 while(it.hasNext()){ 154 A1 a = (A1) it.next(); 155 assertNotNull("The A should not be null", a); 156 nb++; 157 } 158 extent.close(it); 159 pm.currentTransaction().commit(); 160 assertEquals("The size of elements retrieved is not right.", nbExpected, nb); 161 } catch (Exception e) { 162 fail(e.getMessage()); 163 } finally { 164 if (pm.currentTransaction().isActive()) 165 pm.currentTransaction().rollback(); 166 pm.evictAll(); 167 pm.close(); 168 } 169 } 170 171 public void retrieveAllXXA(int bid){ 172 PersistenceManager pm = pmf.getPersistenceManager(); 173 try { 174 Query query = pm.newQuery(A1.class,"b.b1==" + bid); 175 Collection results = (Collection )query.execute(); 176 assertEquals(nbToRetrieve, results.size()); 177 logger.log(BasicLevel.DEBUG, "All A1 linked to the " + bid + " B:"); 178 Iterator it = results.iterator(); 179 while(it.hasNext()){ 180 A1 a = (A1) it.next(); 181 assertEquals(bid, a.getB().getB1()); 182 } 183 query.closeAll(); 184 } catch (Exception e) { 185 fail(e.getMessage()); 186 } finally { 187 pm.evictAll(); 188 pm.close(); 189 } 190 } 191 public void testRemovingOfPersistentObject() { 192 PersistenceManager pm = pmf.getPersistenceManager(); 193 try { 194 Class [] cs = new Class []{A1.class, B.class}; 195 pm.currentTransaction().begin(); 196 for(int i=0; i<cs.length; i++) { 197 Query query = pm.newQuery(cs[i]); 198 Collection col = (Collection ) query.execute(); 199 Iterator it = col.iterator(); 200 while(it.hasNext()) { 201 Object o = it.next(); 202 Assert.assertNotNull("null object in the query result" 203 + cs[i].getName(), o); 204 pm.deletePersistent(o); 205 206 } 207 query.close(col); 208 } 209 pm.currentTransaction().commit(); 210 } catch (JDOException e) { 211 Exception ie = ExceptionHelper.getNested(e); 212 logger.log(BasicLevel.ERROR, "", ie); 213 fail(ie.getMessage()); 214 } finally { 215 pm.close(); 216 } 217 } 218 } 219 | Popular Tags |