1 package org.apache.ojb.odmg; 2 3 import java.util.List ; 4 5 import org.apache.ojb.broker.Fish; 6 import org.apache.ojb.broker.Salad; 7 import org.apache.ojb.junit.ODMGTestCase; 8 import org.apache.ojb.odmg.shared.ODMGGourmet; 9 import org.odmg.OQLQuery; 10 import org.odmg.Transaction; 11 12 public class ManyToManyTest extends ODMGTestCase 13 { 14 public static void main(String [] args) 15 { 16 String [] arr = {ManyToManyTest.class.getName()}; 17 junit.textui.TestRunner.main(arr); 18 } 19 20 public ManyToManyTest(String name) 21 { 22 super(name); 23 } 24 25 29 public void testPolymorphMToN() throws Exception 30 { 31 String postfix = "testPolymorphMToN_" + System.currentTimeMillis(); 32 ODMGGourmet james = new ODMGGourmet(postfix + "_james"); 33 ODMGGourmet doris = new ODMGGourmet(postfix + "_doris"); 34 35 Fish tuna = new Fish(postfix + "_tuna", 242, "salt"); 36 Fish trout = new Fish(postfix + "_trout", 52, "fresh water"); 37 38 Salad radiccio = new Salad(postfix + "_Radiccio", 7, "red"); 39 Salad lolloverde = new Salad(postfix + "_Lollo verde", 7, "green"); 40 41 james.addFavoriteFood(tuna); 42 james.addFavoriteFood(radiccio); 43 44 doris.addFavoriteFood(tuna); 45 doris.addFavoriteFood(trout); 46 doris.addFavoriteFood(lolloverde); 47 48 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 49 tx.begin(); 50 database.makePersistent(james); 51 database.makePersistent(doris); 52 tx.commit(); 53 54 int dorisId = doris.getGourmetId(); 55 int jamesId = james.getGourmetId(); 56 57 tx = (TransactionExt) odmg.newTransaction(); 58 tx.begin(); 59 tx.getBroker().clearCache(); 60 61 OQLQuery query = odmg.newOQLQuery(); 62 query.create("select gourmets from " + ODMGGourmet.class.getName() + 63 " where gourmetId=$1"); 64 query.bind(new Integer (dorisId)); 65 List gourmets = (List ) query.execute(); 66 tx.commit(); 67 assertEquals(1, gourmets.size()); 68 ODMGGourmet loadedDoris = (ODMGGourmet) gourmets.get(0); 69 assertEquals(3, loadedDoris.getFavoriteFood().size()); 71 72 tx.begin(); 73 query = odmg.newOQLQuery(); 74 query.create("select gourmets from " + ODMGGourmet.class.getName() + 75 " where gourmetId=$1"); 76 query.bind(new Integer (jamesId)); 77 gourmets = (List ) query.execute(); 78 tx.commit(); 79 assertEquals(1, gourmets.size()); 80 ODMGGourmet loadedJames = (ODMGGourmet) gourmets.get(0); 81 assertEquals(2, loadedJames.getFavoriteFood().size()); 83 } 84 85 107 public void testMtoNSeparate_I() throws Exception 108 { 109 ODMGGourmet paula = new ODMGGourmet("a_testMtoNSeparate_I"); 110 ODMGGourmet candy = new ODMGGourmet("b_testMtoNSeparate_I"); 111 112 long timestamp = System.currentTimeMillis(); 113 Fish tuna = new Fish("tuna_" + timestamp, 242, "salt"); 114 Fish trout = new Fish("trout_" + timestamp, 52, "fresh water"); 115 116 paula.addFavoriteFood(trout); 117 candy.addFavoriteFood(tuna); 118 119 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 120 tx.begin(); 121 database.makePersistent(paula); 122 database.makePersistent(candy); 123 tx.commit(); 124 125 OQLQuery query = odmg.newOQLQuery(); 126 query.create("select fishs from " + Fish.class.getName() + 127 " where (name=$1 or name=$2)"); 128 query.bind(tuna.getName()); 129 query.bind(trout.getName()); 130 List fishs = (List ) query.execute(); 131 134 assertEquals(2, fishs.size()); 135 } 136 137 public void testMtoNSeparate_II() throws Exception 138 { 139 ODMGGourmet james = new ODMGGourmet("a_testMtoNSeparate_II"); 140 ODMGGourmet doris = new ODMGGourmet("b_testMtoNSeparate_II"); 141 142 long timestamp = System.currentTimeMillis(); 143 Fish tuna = new Fish("tuna_" + timestamp, 242, "salt"); 144 Fish trout = new Fish("trout_" + timestamp, 52, "fresh water"); 145 146 james.addFavoriteFood(tuna); 147 148 doris.addFavoriteFood(tuna); 149 doris.addFavoriteFood(trout); 150 151 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 152 tx.begin(); 153 database.makePersistent(james); 154 database.makePersistent(doris); 155 tx.commit(); 156 157 OQLQuery query = odmg.newOQLQuery(); 158 query.create("select fishs from " + Fish.class.getName() + 159 " where (name=$1 or name=$2)"); 160 query.bind(tuna.getName()); 161 query.bind(trout.getName()); 162 List fishs = (List ) query.execute(); 163 166 assertEquals(2, fishs.size()); 167 } 168 169 public void testMtoNTogether() throws Exception 170 { 171 long timestamp = System.currentTimeMillis(); 172 Fish tuna = new Fish("tuna_" + timestamp, 242, "salt"); 173 Fish trout = new Fish("trout_" + timestamp, 52, "fresh water"); 174 175 ODMGGourmet paula = new ODMGGourmet("a_testMtoNTogether"); 176 ODMGGourmet candy = new ODMGGourmet("b_testMtoNTogether"); 177 ODMGGourmet james = new ODMGGourmet("c_testMtoNTogether"); 178 ODMGGourmet doris = new ODMGGourmet("d_testMtoNTogether"); 179 180 paula.addFavoriteFood(trout); 181 candy.addFavoriteFood(tuna); 182 james.addFavoriteFood(tuna); 183 doris.addFavoriteFood(tuna); 184 doris.addFavoriteFood(trout); 185 186 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 187 tx.begin(); 188 database.makePersistent(james); 189 database.makePersistent(doris); 190 database.makePersistent(candy); 191 database.makePersistent(paula); 192 tx.commit(); 193 194 OQLQuery query = odmg.newOQLQuery(); 195 query.create("select fishs from " + Fish.class.getName() + 196 " where (name=$1 or name=$2)"); 197 query.bind(tuna.getName()); 198 query.bind(trout.getName()); 199 List fishs = (List ) query.execute(); 200 203 assertEquals(2, fishs.size()); 204 } 205 206 211 public void testMtoNPolymorphUpdate() throws Exception 212 { 213 long timestamp = System.currentTimeMillis(); 214 Fish tuna = new Fish("tuna_" + timestamp, 242, "salt"); 215 Fish trout = new Fish("trout_" + timestamp, 52, "fresh water"); 216 Fish goldfish = new Fish("goldfish_" + timestamp, 10, "brackish water"); 217 218 ODMGGourmet paula = new ODMGGourmet("a_testMtoNTogether"+ timestamp); 219 ODMGGourmet candy = new ODMGGourmet("b_testMtoNTogether"+ timestamp); 220 ODMGGourmet james = new ODMGGourmet("c_testMtoNTogether"+ timestamp); 221 ODMGGourmet doris = new ODMGGourmet("d_testMtoNTogether"+ timestamp); 222 223 paula.addFavoriteFood(trout); 224 candy.addFavoriteFood(tuna); 225 james.addFavoriteFood(tuna); 226 doris.addFavoriteFood(tuna); 227 doris.addFavoriteFood(trout); 228 229 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 230 tx.begin(); 231 database.makePersistent(james); 232 database.makePersistent(doris); 233 database.makePersistent(candy); 234 database.makePersistent(paula); 235 tx.commit(); 236 237 OQLQuery query = odmg.newOQLQuery(); 238 query.create("select fishs from " + Fish.class.getName() + 239 " where (name=$1 or name=$2)"); 240 query.bind(tuna.getName()); 241 query.bind(trout.getName()); 242 List fishs = (List ) query.execute(); 243 246 assertEquals(2, fishs.size()); 247 248 tx = (TransactionExt) odmg.newTransaction(); 249 tx.begin(); 250 query = odmg.newOQLQuery(); 251 query.create("select gourmets from " + ODMGGourmet.class.getName() + 252 " where name=$1"); 253 query.bind(doris.getName()); 254 List result = (List ) query.execute(); 255 assertEquals("We should found a gourmet", 1, result.size()); 256 ODMGGourmet gourmet = (ODMGGourmet)result.get(0); 257 258 261 tx.lock(gourmet, Transaction.WRITE); 262 gourmet.addFavoriteFood(goldfish); 263 tx.commit(); 264 265 query = odmg.newOQLQuery(); 266 query.create("select fishs from " + Fish.class.getName() + 267 " where (name=$1 or name=$2 or name=$3)"); 268 query.bind(tuna.getName()); 269 query.bind(trout.getName()); 270 query.bind(goldfish.getName()); 271 272 tx = (TransactionExt) odmg.newTransaction(); 273 tx.begin(); 274 fishs = (List ) query.execute(); 275 tx.commit(); 276 assertEquals("seems referenced object was not added (if found <3) ",3, fishs.size()); 277 } 278 279 284 public void testMtoNPolymorphDelete() throws Exception 285 { 286 long timestamp = System.currentTimeMillis(); 287 String name = "testMtoNPolymorphDelete_" + timestamp; 288 Fish tuna = new Fish(name + "_tuna", 242, "salt"); 289 Fish trout = new Fish(name + "_trout", 52, "fresh water"); 290 Fish goldfish = new Fish(name + "_goldfish", 10, "brackish water"); 291 292 ODMGGourmet paula = new ODMGGourmet(name + "_paula"); 293 ODMGGourmet candy = new ODMGGourmet(name + "_candy"); 294 ODMGGourmet james = new ODMGGourmet(name + "_james"); 295 ODMGGourmet doris = new ODMGGourmet(name + "_doris"); 296 297 paula.addFavoriteFood(trout); 298 candy.addFavoriteFood(tuna); 299 james.addFavoriteFood(tuna); 300 doris.addFavoriteFood(tuna); 301 doris.addFavoriteFood(trout); 302 doris.addFavoriteFood(goldfish); 303 304 307 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 308 tx.begin(); 309 database.makePersistent(paula); 310 database.makePersistent(james); 311 database.makePersistent(candy); 312 database.makePersistent(doris); 313 tx.commit(); 314 315 tx.begin(); 316 OQLQuery query = odmg.newOQLQuery(); 317 query.create("select fishs from " + Fish.class.getName() + 318 " where (name=$1 or name=$2 or name=$3)"); 319 query.bind(tuna.getName()); 320 query.bind(trout.getName()); 321 query.bind(goldfish.getName()); 322 323 List fishs = (List ) query.execute(); 324 tx.commit(); 325 328 assertEquals(3, fishs.size()); 329 330 tx = (TransactionExt) odmg.newTransaction(); 331 tx.begin(); 332 query = odmg.newOQLQuery(); 333 query.create("select gourmets from " + ODMGGourmet.class.getName() + 334 " where name=$1"); 335 query.bind(doris.getName()); 336 List result = (List ) query.execute(); 337 assertEquals("We should found a gourmet_doris", 1, result.size()); 338 ODMGGourmet gourmet_doris = (ODMGGourmet)result.get(0); 339 assertEquals(name + "_doris", gourmet_doris.getName()); 340 assertEquals(3, gourmet_doris.getFavoriteFood().size()); 341 342 345 tx.lock(gourmet_doris, Transaction.WRITE); 346 List foodList = gourmet_doris.getFavoriteFood(); 347 foodList.remove(0); 348 tx.commit(); 350 351 query = odmg.newOQLQuery(); 352 query.create("select gourmets from " + ODMGGourmet.class.getName() + 353 " where name=$1"); 354 query.bind(doris.getName()); 355 356 tx = (TransactionExt) odmg.newTransaction(); 357 tx.begin(); 358 result = (List ) query.execute(); 359 assertEquals("We should found a gourmet_doris", 1, result.size()); 360 gourmet_doris = (ODMGGourmet)result.get(0); 361 tx.commit(); 362 assertEquals( 363 "We removed one fish, so doris should only have two entries left", 364 2, gourmet_doris.getFavoriteFood().size()); 365 } 366 } 367 | Popular Tags |