1 5 package org.apache.ojb.broker; 6 7 import org.apache.ojb.broker.query.Criteria; 8 import org.apache.ojb.broker.query.Query; 9 import org.apache.ojb.broker.query.QueryFactory; 10 import org.apache.ojb.junit.PBTestCase; 11 12 import java.sql.Timestamp ; 13 import java.util.Iterator ; 14 15 18 public class ComplexMultiMappedTableWithCollectionByQueryTest extends PBTestCase 19 { 20 private int COUNT = 10; 21 private static Class CLASS = ComplexMultiMappedTableWithCollectionByQueryTest.class; 22 23 public static void main(String [] args) 24 { 25 String [] arr = {CLASS.getName()}; 26 junit.textui.TestRunner.main(arr); 27 } 28 29 33 public ComplexMultiMappedTableWithCollectionByQueryTest(String name) 34 { 35 super(name); 36 } 37 38 42 public void setUp() throws Exception 43 { 44 super.setUp(); 45 createTestData(); 46 } 47 48 private int deleteAllData() 49 { 50 int number_deleted = 0; 51 Criteria crit = new Criteria(); 52 Query q = QueryFactory.newQuery(ComplexMultiMapped.PersistentA.class, crit); 53 broker.beginTransaction(); 54 Iterator iter = broker.getCollectionByQuery(q).iterator(); 55 while (iter.hasNext()) 56 { 57 broker.delete(iter.next()); 58 number_deleted++; 59 } 60 63 crit = new Criteria(); 64 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentB.class, crit); 65 iter = broker.getCollectionByQuery(q).iterator(); 66 67 while (iter.hasNext()) 68 { 69 broker.delete(iter.next()); 70 number_deleted++; 71 } 72 75 crit = new Criteria(); 76 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentC.class, crit); 77 iter = broker.getCollectionByQuery(q).iterator(); 78 while (iter.hasNext()) 79 { 80 broker.delete(iter.next()); 81 number_deleted++; 82 } 83 broker.commitTransaction(); 84 return number_deleted; 85 } 86 87 private void createTestData() 88 { 89 92 broker.beginTransaction(); 93 for (int i = 0; i < COUNT; i++) 94 { 95 ComplexMultiMapped.PersistentA a = new ComplexMultiMapped.PersistentA(); 96 a.setValue1("a"); 97 a.setValue2(i); 98 a.setValue3(new Timestamp (System.currentTimeMillis())); 99 broker.store(a); 100 101 ComplexMultiMapped.PersistentB b = new ComplexMultiMapped.PersistentB(); 102 b.setValue4("b"); 103 b.setValue5(i); 104 b.setValue6(new Timestamp (System.currentTimeMillis())); 105 broker.store(b); 106 107 ComplexMultiMapped.PersistentC c = new ComplexMultiMapped.PersistentC(); 108 c.setValue1("c"); 109 c.setValue2(i); 110 c.setValue3(new Timestamp (System.currentTimeMillis())); 111 c.setValue4("c"); 112 c.setValue5(i); 113 c.setValue6(new Timestamp (System.currentTimeMillis())); 114 broker.store(c); 115 116 ComplexMultiMapped.PersistentD d = new ComplexMultiMapped.PersistentD(); 117 d.setValue1("d"); 118 d.setValue2(i); 119 d.setValue3(new Timestamp (System.currentTimeMillis())); 120 d.setValue4("d"); 121 d.setValue5(i); 122 d.setValue6(new Timestamp (System.currentTimeMillis())); 123 broker.store(d); 124 125 ComplexMultiMapped.PersistentE e = new ComplexMultiMapped.PersistentE(); 126 e.setValue1("e"); 127 e.setValue2(i); 128 e.setValue3(new Timestamp (System.currentTimeMillis())); 129 e.setValue4("e"); 130 e.setValue5(i); 131 e.setValue6(new Timestamp (System.currentTimeMillis())); 132 broker.store(e); 133 134 ComplexMultiMapped.PersistentF f = new ComplexMultiMapped.PersistentF(); 135 f.setValue1("f"); 136 f.setValue2(i); 137 f.setValue3(new Timestamp (System.currentTimeMillis())); 138 f.setValue4("f"); 139 f.setValue5(i); 140 f.setValue6(new Timestamp (System.currentTimeMillis())); 141 broker.store(f); 142 } 143 broker.commitTransaction(); 144 } 145 146 public void testCreate() 147 { 148 createTestData(); 149 } 150 151 public void testGet() 152 { 153 156 deleteAllData(); 157 160 createTestData(); 161 162 Criteria crit = new Criteria(); 163 Query q; 164 Iterator iter; 165 int count; 166 169 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentA.class, crit); 170 iter = broker.getCollectionByQuery(q).iterator(); 171 ComplexMultiMapped.PersistentA a = null; 172 count = 0; 173 while (iter.hasNext()) 174 { 175 a = (ComplexMultiMapped.PersistentA) iter.next(); 176 if (!a.getValue1().equals("a")) 177 { 178 fail("getValue1 should have returned 'a', it in fact returned '" + a.getValue1() + "'"); 179 } 180 count++; 181 } 182 if (count != COUNT) 183 fail("should have found " + COUNT + " ComplexMultiMapped.PersistentA's, in fact found " + count); 184 185 assertEquals("counted size", broker.getCount(q), count); 186 187 190 crit = new Criteria(); 191 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentB.class, crit); 192 iter = broker.getCollectionByQuery(q).iterator(); 193 ComplexMultiMapped.PersistentB b = null; 194 count = 0; 195 while (iter.hasNext()) 196 { 197 b = (ComplexMultiMapped.PersistentB) iter.next(); 198 if (!b.getValue4().equals("b") && !b.getValue4().equals("d") && !b.getValue4().equals("e") && !b.getValue4().equals("f")) 199 { 200 fail("getValue4 should have returned 'b' or 'd' or 'e' or 'f' (from extent), it in fact returned '" + b.getValue4() + "'"); 201 } 202 count++; 203 } 204 207 if (count != COUNT*4) 208 fail("should have found " + (COUNT *4) + " ComplexMultiMapped.PersistentB's, in fact found " + count); 209 210 assertEquals("counted size", broker.getCount(q), count); 211 212 215 crit = new Criteria(); 216 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentC.class, crit); 217 iter = broker.getCollectionByQuery(q).iterator(); 218 ComplexMultiMapped.PersistentC c = null; 219 count = 0; 220 while (iter.hasNext()) 221 { 222 c = (ComplexMultiMapped.PersistentC) iter.next(); 223 if (!c.getValue1().equals("c")) 224 { 225 fail("getValue1 should have returned 'c', it in fact returned '" + c.getValue1() + "'"); 226 } 227 count++; 228 } 229 if (count != COUNT) 230 fail("should have found " + COUNT + " ComplexMultiMapped.PersistentC's, in fact found " + count); 231 232 assertEquals("counted size", broker.getCount(q), count); 233 234 237 crit = new Criteria(); 238 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentD.class, crit); 239 iter = broker.getCollectionByQuery(q).iterator(); 240 ComplexMultiMapped.PersistentD d = null; 241 count = 0; 242 while (iter.hasNext()) 243 { 244 d = (ComplexMultiMapped.PersistentD) iter.next(); 245 if (!d.getValue1().equals("d")) 246 { 247 fail("getValue1 should have returned 'd', it in fact returned '" + d.getValue1() + "'"); 248 } 249 count++; 250 } 251 if (count != COUNT) 252 fail("should have found " + COUNT + " ComplexMultiMapped.PersistentD's, in fact found " + count); 253 254 assertEquals("counted size", broker.getCount(q), count); 255 256 259 crit = new Criteria(); 260 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentE.class, crit); 261 iter = broker.getCollectionByQuery(q).iterator(); 262 ComplexMultiMapped.PersistentE e = null; 263 count = 0; 264 while (iter.hasNext()) 265 { 266 e = (ComplexMultiMapped.PersistentE) iter.next(); 267 if (!e.getValue1().equals("e") && !e.getValue1().equals("f")) 268 { 269 fail("getValue1 should have returned 'e' or 'f' (extent), it in fact returned '" + e.getValue1() + "'"); 270 } 271 count++; 272 } 273 if (count != COUNT *2) 274 fail("should have found " + (COUNT*2) + " ComplexMultiMapped.PersistentE's, in fact found " + count); 275 276 assertEquals("counted size", broker.getCount(q), count); 277 278 297 298 } 299 300 public void testDeleteWithData() 301 { 302 305 createTestData(); 306 309 int number_deleted = deleteAllData(); 310 313 if (number_deleted < (5*COUNT)) 314 { 315 fail("Should have deleted at least " + (4*COUNT) + " actually deleted " + number_deleted); 316 } 317 } 318 319 public void testDeleteWithNoData() 320 { 321 324 deleteAllData(); 325 328 int number_deleted = deleteAllData(); 329 if (number_deleted !=0) 330 { 331 fail("Should have deleted 0, instead deleted " + number_deleted); 332 } 333 } 334 } 335 | Popular Tags |