1 package org.apache.ojb.odmg; 2 3 import java.util.List ; 4 5 import org.apache.ojb.broker.Identity; 6 import org.apache.ojb.broker.PersistenceBroker; 7 import org.apache.ojb.broker.TestHelper; 8 import org.apache.ojb.broker.metadata.ClassDescriptor; 9 import org.apache.ojb.junit.OJBTestCase; 10 import org.apache.ojb.odmg.collections.DListImpl; 11 import org.apache.ojb.odmg.shared.Article; 12 import org.apache.ojb.odmg.shared.ProductGroup; 13 import org.apache.ojb.odmg.states.ModificationState; 14 import org.apache.ojb.odmg.states.StateNewClean; 15 import org.apache.ojb.odmg.states.StateNewDirty; 16 import org.odmg.DCollection; 17 import org.odmg.Database; 18 import org.odmg.Implementation; 19 import org.odmg.ODMGException; 20 import org.odmg.OQLQuery; 21 import org.odmg.Transaction; 22 23 26 public class OdmgExamples extends OJBTestCase 27 { 28 public static void main(String [] args) 29 { 30 String [] arr = {OdmgExamples.class.getName()}; 31 junit.textui.TestRunner.main(arr); 32 } 33 34 private String databaseName; 35 36 public OdmgExamples(String name) 37 { 38 super(name); 39 } 40 41 public void setUp() 42 { 43 databaseName = TestHelper.DEF_DATABASE_NAME; 44 } 45 46 public void tearDown() 47 { 48 databaseName = null; 49 } 50 51 52 public void testModificationStates() 53 { 54 Implementation odmg = OJB.getInstance(); 56 Database db = odmg.newDatabase(); 57 try 59 { 60 db.open(databaseName, Database.OPEN_READ_WRITE); 61 } 62 catch (ODMGException ex) 63 { 64 fail("ODMGException: " + ex.getMessage()); 65 } 66 ModificationState oldState = StateNewClean.getInstance(); 67 ModificationState newState = oldState.markDirty(); 68 assertEquals(StateNewDirty.getInstance(), newState); 69 70 oldState = newState; 71 newState = oldState.markDirty(); 72 assertEquals(oldState, newState); 73 74 try 76 { 77 db.close(); 78 } 79 catch (ODMGException ex) 80 { 81 fail("ODMGException: " + ex.getMessage()); 82 } 83 84 } 85 86 public void testOdmgSession() 87 { 88 Implementation odmg = OJB.getInstance(); 90 Database db = odmg.newDatabase(); 91 try 93 { 94 db.open(databaseName, Database.OPEN_READ_WRITE); 95 } 96 catch (ODMGException ex) 97 { 98 fail("ODMGException: " + ex.getMessage()); 99 } 100 Transaction tx = odmg.newTransaction(); 101 102 try 104 { 105 tx.begin(); 106 107 ProductGroup pg = new ProductGroup(); 108 pg.setName("PG A"); 109 Article example = new Article(); 110 example.setProductGroup(pg); 111 pg.addArticle(example); 112 db.makePersistent(pg); 113 114 example.setStock(333); 116 example.addToStock(47); 117 example.addToStock(7); 118 example.addToStock(4); 119 120 tx.commit(); 122 } 123 catch (Exception ex) 124 { 125 tx.abort(); 126 } 127 128 try 130 { 131 db.close(); 132 } 133 catch (ODMGException ex) 134 { 135 fail("ODMGException: " + ex.getMessage()); 136 } 137 } 138 139 public void testOQLQuery() 140 { 141 Implementation odmg = OJB.getInstance(); 143 Database db = odmg.newDatabase(); 144 try 146 { 147 db.open(databaseName, Database.OPEN_READ_WRITE); 148 } 149 catch (ODMGException ex) 150 { 151 fail("ODMGException: " + ex.getMessage()); 152 } 153 Transaction tx = odmg.newTransaction(); 154 155 try 157 { 158 tx.begin(); 159 160 OQLQuery query = odmg.newOQLQuery(); 161 query.create("select anArticle from " + Article.class.getName() + " where articleId = 60"); 162 List results = (List ) query.execute(); 163 164 Article a = (Article) results.get(0); 165 166 Article example = new Article(); 169 example.setArticleId(60); 170 Identity oid = new Identity(example, ((TransactionImpl) tx).getBroker()); 171 PersistenceBroker broker = ((TransactionImpl) tx).getBroker(); 173 broker.clearCache(); 174 Article b = (Article) broker.getObjectByIdentity(oid); 175 176 assertEquals("should be same object", a, b); 177 178 tx.commit(); 180 } 181 catch (Exception ex) 182 { 183 tx.abort(); 184 fail("ODMGException: " + ex.getMessage()); 185 } 186 187 try 189 { 190 db.close(); 191 } 192 catch (ODMGException ex) 193 { 194 fail("ODMGException: " + ex.getMessage()); 195 } 196 } 197 198 public void testPathExpressionOqlQuery() throws Exception 199 { 200 Implementation odmg = OJB.getInstance(); 202 Database db = odmg.newDatabase(); 203 try 205 { 206 db.open(databaseName, Database.OPEN_READ_WRITE); 207 } 208 catch (ODMGException ex) 209 { 210 fail("ODMGException: " + ex.getMessage()); 211 } 212 Transaction tx = odmg.newTransaction(); 213 214 tx.begin(); 216 217 OQLQuery query = odmg.newOQLQuery(); 218 query.create( 220 "select anArticle from " + Article.class.getName() + " where productGroup.groupName like \"Fruit*\""); 221 List results = (List ) query.execute(); 222 223 query = odmg.newOQLQuery(); 225 query.create("select aPG from " + ProductGroup.class.getName() + " where groupName like \"Fruit*\""); 226 List check = (List ) query.execute(); 227 if (check.size() < 1) 228 fail("Could not found ProductGroup's for: " + 229 "select aPG from " + ProductGroup.class.getName() + " where groupName like \"Fruit*\""); 230 ProductGroup pg = (ProductGroup) check.get(0); 231 232 assertEquals(pg.getAllArticlesInGroup().size(), results.size()); 233 assertTrue((results.size() > 0)); 234 235 tx.commit(); 236 237 238 240 db.close(); 241 } 242 243 public void testNrmAndDlists() throws Exception 244 { 245 Implementation odmg = OJB.getInstance(); 247 Database db = odmg.newDatabase(); 248 try 250 { 251 db.open(databaseName, Database.OPEN_READ_WRITE); 252 } 253 catch (ODMGException ex) 254 { 255 fail("ODMGException: " + ex.getMessage()); 256 } 257 Transaction tx = odmg.newTransaction(); 258 259 try 261 { 262 ((ImplementationImpl) odmg).setOqlCollectionClass(DListImpl.class); 265 267 tx.begin(); 268 269 OQLQuery query = odmg.newOQLQuery(); 270 query.create("select x from " + Article.class.getName() + " where productGroupId = 7"); 271 List results = (List ) query.execute(); 272 273 int originalSize = results.size(); 274 assertTrue("result count have to be > 0", originalSize > 0); 275 276 278 String name = "gimme fruits_" + System.currentTimeMillis(); 279 280 db.bind(results, name); 281 tx.commit(); 282 283 tx = odmg.newTransaction(); 284 tx.begin(); 285 286 ((TransactionImpl) tx).getBroker().clearCache(); 287 288 List newResults = (List ) db.lookup(name); 290 291 assertEquals(originalSize, newResults.size()); 292 Article art = (Article) newResults.get(0); 293 assertNotNull(art); 294 296 tx.commit(); 297 298 } 299 catch (Exception e) 300 301 { 302 tx.abort(); 303 throw e; 304 } 305 306 try 308 { 309 db.close(); 310 } 311 catch (ODMGException ex) 312 { 313 fail("ODMGException: " + ex.getMessage()); 314 } 315 } 316 317 public void testOQLQueryBind() 318 { 319 Implementation odmg = OJB.getInstance(); 321 Database db = odmg.newDatabase(); 322 try 324 { 325 db.open(databaseName, Database.OPEN_READ_WRITE); 326 } 327 catch (ODMGException ex) 328 { 329 fail("ODMGException: " + ex.getMessage()); 330 } 331 Transaction tx = odmg.newTransaction(); 332 333 try 335 { 336 tx.begin(); 337 338 OQLQuery query = odmg.newOQLQuery(); 339 query.create("select anArticle from " + Article.class.getName() + " where articleId = $678"); 340 query.bind(new Integer (30)); 341 342 List results = (List ) query.execute(); 343 344 Article a = (Article) results.get(0); 345 346 Article example = new Article(); 349 example.setArticleId(30); 350 Identity oid = new Identity(example, ((TransactionImpl) tx).getBroker()); 351 352 PersistenceBroker broker = ((TransactionImpl) tx).getBroker(); 354 broker.clearCache(); 355 Article b = (Article) broker.getObjectByIdentity(oid); 356 357 assertEquals("should be same object", a, b); 358 359 tx.commit(); 361 } 362 catch (Exception ex) 363 364 { 365 tx.abort(); 366 fail("ODMGException: " + ex.getMessage()); 367 } 368 369 try 371 { 372 db.close(); 373 } 374 catch (ODMGException ex) 375 { 376 fail("ODMGException: " + ex.getMessage()); 377 } 378 } 379 380 public void YYYtestOQLQueryOnCollections() throws Exception 381 { 382 Implementation odmg = OJB.getInstance(); 384 Database db = odmg.newDatabase(); 385 db.open(databaseName, Database.OPEN_READ_WRITE); 387 Transaction tx = odmg.newTransaction(); 388 389 try 391 { 392 tx.begin(); 393 OQLQuery query = odmg.newOQLQuery(); 394 query.create("select aLotOfArticles from " + Article.class.getName() + " where productGroupId = 4"); 395 396 DCollection results = (DCollection) query.execute(); 397 results = results.query("price > 35"); 398 399 query = odmg.newOQLQuery(); 401 query.create( 402 "select aLotOfArticles from " 403 + Article.class.getName() 404 + " where productGroupId = 4 and price > 35"); 405 406 DCollection check = (DCollection) query.execute(); 407 408 assertEquals(results, check); 409 410 tx.commit(); 411 } 412 finally 414 { 415 db.close(); 416 } 417 } 418 419 420 public void YYYtestWrongDbName() 421 { 422 Implementation objectserver = OJB.getInstance(); 424 Database db = objectserver.newDatabase(); 425 426 String wrongDatabaseName = "ThereIsNoSuchFile"; 428 try 429 { 430 db.open(wrongDatabaseName, Database.OPEN_READ_WRITE); 431 fail("should not be able to open database " + wrongDatabaseName); 432 } 433 catch (ODMGException ex) 434 { 435 return; 436 } 437 } 438 439 440 public void YYYtestBrokerCrash() 441 { 442 Implementation odmg = OJB.getInstance(); 444 Database db = odmg.newDatabase(); 445 PersistenceBroker broker = null; 446 ClassDescriptor cld = null; 447 String tablename = null; 448 449 try 451 { 452 db.open(databaseName, Database.OPEN_READ_WRITE); 453 } 454 catch (ODMGException ex) 455 { 456 fail("ODMGException: " + ex.getMessage()); 457 } 458 try 459 { 460 Transaction tx = odmg.newTransaction(); 461 tx.begin(); 462 463 OQLQuery query = odmg.newOQLQuery(); 465 query.create("select anArticle from " + Article.class.getName() + " where articleId = $678"); 466 query.bind(new Integer (30)); 467 List results = (List ) query.execute(); 468 Article a = (Article) results.get(0); 469 470 broker = ((TransactionImpl) tx).getBroker(); 472 cld = broker.getClassDescriptor(Article.class); 473 tablename = cld.getFullTableName(); 474 cld.setTableName("ELVIS"); 475 broker.getDescriptorRepository().setClassDescriptor(cld); 476 477 a.addToStock(5); 479 tx.commit(); 480 fail("Can commit tx with corrupt metadata"); 481 } 482 catch (Throwable t) 483 { 484 } 486 finally 487 { 488 cld.setTableName(tablename); 489 broker.getDescriptorRepository().setClassDescriptor(cld); 490 } 491 492 } 493 } 494 | Popular Tags |