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.Collection ; 14 import java.util.Iterator ; 15 16 19 public class ComplexMultiMappedTableTest extends PBTestCase 20 { 21 private int COUNT = 10; 22 23 public static void main(String [] args) 24 { 25 String [] arr = {ComplexMultiMappedTableTest.class.getName()}; 26 junit.textui.TestRunner.main(arr); 27 } 28 29 33 public ComplexMultiMappedTableTest(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 Collection result; 51 Iterator iter; 52 int number_deleted = 0; 53 54 broker.beginTransaction(); 55 Criteria crit = new Criteria(); 56 Query q = QueryFactory.newQuery(ComplexMultiMapped.PersistentA.class, crit); 57 result = broker.getCollectionByQuery(q); 58 iter = result.iterator(); 59 while (iter.hasNext()) 60 { 61 broker.delete(iter.next()); 62 number_deleted++; 63 } 64 67 crit = new Criteria(); 68 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentB.class, crit); 69 result = broker.getCollectionByQuery(q); 70 iter = result.iterator(); 71 while (iter.hasNext()) 72 { 73 broker.delete(iter.next()); 74 number_deleted++; 75 } 76 79 crit = new Criteria(); 80 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentC.class, crit); 81 result = broker.getCollectionByQuery(q); 82 iter = result.iterator(); 83 while (iter.hasNext()) 84 { 85 broker.delete(iter.next()); 86 number_deleted++; 87 } 88 broker.commitTransaction(); 89 return number_deleted; 90 } 91 92 private void createTestData() 93 { 94 broker.beginTransaction(); 95 98 for (int i = 0; i < COUNT; i++) 99 { 100 ComplexMultiMapped.PersistentA a = new ComplexMultiMapped.PersistentA(); 101 a.setValue1("a"); 102 a.setValue2(i); 103 a.setValue3(new Timestamp (System.currentTimeMillis())); 104 broker.store(a); 105 106 ComplexMultiMapped.PersistentB b = new ComplexMultiMapped.PersistentB(); 107 b.setValue4("b"); 108 b.setValue5(i); 109 b.setValue6(new Timestamp (System.currentTimeMillis())); 110 broker.store(b); 111 112 ComplexMultiMapped.PersistentC c = new ComplexMultiMapped.PersistentC(); 113 c.setValue1("c"); 114 c.setValue2(i); 115 c.setValue3(new Timestamp (System.currentTimeMillis())); 116 c.setValue4("c"); 117 c.setValue5(i); 118 c.setValue6(new Timestamp (System.currentTimeMillis())); 119 broker.store(c); 120 121 ComplexMultiMapped.PersistentD d = new ComplexMultiMapped.PersistentD(); 122 d.setValue1("d"); 123 d.setValue2(i); 124 d.setValue3(new Timestamp (System.currentTimeMillis())); 125 d.setValue4("d"); 126 d.setValue5(i); 127 d.setValue6(new Timestamp (System.currentTimeMillis())); 128 broker.store(d); 129 130 ComplexMultiMapped.PersistentE e = new ComplexMultiMapped.PersistentE(); 131 e.setValue1("e"); 132 e.setValue2(i); 133 e.setValue3(new Timestamp (System.currentTimeMillis())); 134 e.setValue4("e"); 135 e.setValue5(i); 136 e.setValue6(new Timestamp (System.currentTimeMillis())); 137 broker.store(e); 138 139 ComplexMultiMapped.PersistentF f = new ComplexMultiMapped.PersistentF(); 140 f.setValue1("f"); 141 f.setValue2(i); 142 f.setValue3(new Timestamp (System.currentTimeMillis())); 143 f.setValue4("f"); 144 f.setValue5(i); 145 f.setValue6(new Timestamp (System.currentTimeMillis())); 146 broker.store(f); 147 } 148 broker.commitTransaction(); 149 } 150 151 public void testCreate() 152 { 153 createTestData(); 154 } 155 156 public void testGet() 157 { 158 161 deleteAllData(); 162 165 createTestData(); 166 167 Criteria crit = new Criteria(); 168 Query q; 169 Iterator iter; 170 int count; 171 174 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentA.class, crit); 175 iter = broker.getIteratorByQuery(q); 176 ComplexMultiMapped.PersistentA a = null; 177 count = 0; 178 while (iter.hasNext()) 179 { 180 a = (ComplexMultiMapped.PersistentA) iter.next(); 181 if (!a.getValue1().equals("a")) 182 { 183 fail("getValue1 should have returned 'a', it in fact returned '" + a.getValue1() + "'"); 184 } 185 count++; 186 } 187 if (count != COUNT) 188 fail("should have found " + COUNT + " ComplexMultiMapped.PersistentA's, in fact found " + count); 189 190 193 crit = new Criteria(); 194 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentB.class, crit); 195 iter = broker.getIteratorByQuery(q); 196 ComplexMultiMapped.PersistentB b = null; 197 count = 0; 198 while (iter.hasNext()) 199 { 200 b = (ComplexMultiMapped.PersistentB) iter.next(); 201 if (!b.getValue4().equals("b") && !b.getValue4().equals("d") && !b.getValue4().equals("e") && !b.getValue4().equals("f")) 202 { 203 fail("getValue4 should have returned 'b' or 'd' or 'e' or 'f' (from extent), it in fact returned '" + b.getValue4() + "'"); 204 } 205 count++; 206 } 207 210 if (count != COUNT*4) 211 fail("should have found " + (COUNT *4) + " ComplexMultiMapped.PersistentB's, in fact found " + count); 212 213 216 crit = new Criteria(); 217 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentC.class, crit); 218 iter = broker.getIteratorByQuery(q); 219 ComplexMultiMapped.PersistentC c = null; 220 count = 0; 221 while (iter.hasNext()) 222 { 223 c = (ComplexMultiMapped.PersistentC) iter.next(); 224 if (!c.getValue1().equals("c")) 225 { 226 fail("getValue1 should have returned 'c', it in fact returned '" + c.getValue1() + "'"); 227 } 228 count++; 229 } 230 if (count != COUNT) 231 fail("should have found " + COUNT + " ComplexMultiMapped.PersistentC's, in fact found " + count); 232 233 236 crit = new Criteria(); 237 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentD.class, crit); 238 iter = broker.getIteratorByQuery(q); 239 ComplexMultiMapped.PersistentD d = null; 240 count = 0; 241 while (iter.hasNext()) 242 { 243 d = (ComplexMultiMapped.PersistentD) iter.next(); 244 if (!d.getValue1().equals("d")) 245 { 246 fail("getValue1 should have returned 'd', it in fact returned '" + d.getValue1() + "'"); 247 } 248 count++; 249 } 250 if (count != COUNT) 251 fail("should have found " + COUNT + " ComplexMultiMapped.PersistentD's, in fact found " + count); 252 253 256 crit = new Criteria(); 257 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentE.class, crit); 258 iter = broker.getIteratorByQuery(q); 259 ComplexMultiMapped.PersistentE e = null; 260 count = 0; 261 while (iter.hasNext()) 262 { 263 e = (ComplexMultiMapped.PersistentE) iter.next(); 264 if (!e.getValue1().equals("e") && !e.getValue1().equals("f")) 265 { 266 fail("getValue1 should have returned 'e' or 'f' (extent), it in fact returned '" + e.getValue1() + "'"); 267 } 268 count++; 269 } 270 if (count != COUNT *2) 271 fail("should have found " + (COUNT*2) + " ComplexMultiMapped.PersistentE's, in fact found " + count); 272 273 292 293 } 294 295 public void testDeleteWithData() 296 { 297 300 createTestData(); 301 304 int number_deleted = deleteAllData(); 305 308 if (number_deleted < (5*COUNT)) 309 { 310 fail("Should have deleted at least " + (4*COUNT) + " actually deleted " + number_deleted); 311 } 312 } 313 314 public void testDeleteWithNoData() 315 { 316 319 deleteAllData(); 320 323 int number_deleted = deleteAllData(); 324 if (number_deleted !=0) 325 { 326 fail("Should have deleted 0, instead deleted " + number_deleted); 327 } 328 329 } 330 331 } 332 | Popular Tags |