Return-Path: Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 5462 invoked by uid 500); 9 Jun 2003 18:58:32 -0000 Received: (qmail 5459 invoked from network); 9 Jun 2003 18:58:32 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 9 Jun 2003 18:58:32 -0000 Received: (qmail 22817 invoked by uid 1520); 9 Jun 2003 18:58:31 -0000 Date: 9 Jun 2003 18:58:31 -0000 Message-ID: <20030609185831.22816.qmail@icarus.apache.org> From: olegnitz@apache.org To: db-ojb-cvs@apache.org Subject: cvs commit: db-ojb/src/test/org/apache/ojb/otm OtmExamples.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N olegnitz 2003/06/09 11:58:31 Modified: src/test/org/apache/ojb/otm OtmExamples.java Log: Added test for "update by reachability" (actually lock by reachability and update modified locked objects) Revision Changes Path 1.9 +188 -22 db-ojb/src/test/org/apache/ojb/otm/OtmExamples.java Index: OtmExamples.java =================================================================== RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/otm/OtmExamples.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- OtmExamples.java 18 May 2003 18:15:06 -0000 1.8 +++ OtmExamples.java 9 Jun 2003 18:58:31 -0000 1.9 @@ -40,7 +40,7 @@ _conn = null; } - public void testOtmSession() throws Exception + public void testOtmSession() throws Throwable { Transaction tx = null; @@ -75,10 +75,23 @@ example = (Article) _conn.getObjectByIdentity(oid); assertEquals("should be equal", 7, example.getProductGroupId()); assertEquals("should be equal", 333, example.getStock()); - _conn.deletePersistent(example); tx.commit(); + + try + { + tx = _kit.getTransaction(_conn); + tx.begin(); + example = (Article) _conn.getObjectByIdentity(oid); + _conn.deletePersistent(example); + tx.commit(); + } + catch (Throwable ex) + { + ex.printStackTrace(); + tx.rollback(); + } } - catch (Exception ex) + catch (Throwable ex) { try { @@ -94,7 +107,7 @@ } } - public void testOtmCache() throws Exception + public void testOtmCache() throws Throwable { Transaction tx = null; @@ -132,11 +145,25 @@ article = (Article) _conn.getObjectByIdentity(aOid); pg = (ProductGroup) article.getProductGroup(); assertEquals("should be equal", "1", pg.getName()); - _conn.deletePersistent(article); - _conn.deletePersistent(pg); tx.commit(); + + try + { + tx = _kit.getTransaction(_conn); + tx.begin(); + article = (Article) _conn.getObjectByIdentity(aOid); + pg = (ProductGroup) article.getProductGroup(); + _conn.deletePersistent(article); + _conn.deletePersistent(pg); + tx.commit(); + } + catch (Throwable ex) + { + ex.printStackTrace(); + tx.rollback(); + } } - catch (Exception ex) + catch (Throwable ex) { try { @@ -152,7 +179,7 @@ } } - public void testOtmIsolation() throws Exception + public void testOtmIsolation() throws Throwable { Transaction tx = null; Transaction tx2 = null; @@ -184,11 +211,23 @@ pg = (ProductGroup) conn2.getObjectByIdentity(pgOid); assertEquals("should be equal", "1", pg.getName()); tx2.commit(); - - _conn.deletePersistent(pg); tx.commit(); + + try + { + tx = _kit.getTransaction(_conn); + tx.begin(); + pg = (ProductGroup) _conn.getObjectByIdentity(pgOid); + _conn.deletePersistent(pg); + tx.commit(); + } + catch (Throwable ex) + { + ex.printStackTrace(); + tx.rollback(); + } } - catch (Exception ex) + catch (Throwable ex) { try { @@ -204,7 +243,7 @@ } } - public void testOtmLocks() throws Exception + public void testOtmLocks() throws Throwable { Transaction tx = null; Transaction tx2 = null; @@ -249,11 +288,11 @@ _kit.setLockWaitStrategy(new TimeoutStrategy(2000)); tx2 = _kit.getTransaction(conn2); tx2.begin(); - (new Thread() + (new Thread() { - public void run() + public void run() { - try + try { Thread.sleep(1000); tx3.commit(); @@ -268,7 +307,7 @@ // Third test for the TimeoutStrategy: // test deadlock detection - _kit.setLockWaitStrategy(new TimeoutStrategy(2000)); + _kit.setLockWaitStrategy(new TimeoutStrategy(4000)); tx = _kit.getTransaction(_conn); tx.begin(); _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK); @@ -292,7 +331,7 @@ try { - Thread.sleep(1000); + Thread.sleep(2000); } catch (InterruptedException ex) { @@ -307,11 +346,11 @@ { // ok, deadlock was detected } - + tx2.rollback(); try { - Thread.sleep(1000); + Thread.sleep(2000); } catch (InterruptedException ex) { @@ -319,13 +358,103 @@ tx.commit(); + try + { + tx = _kit.getTransaction(_conn); + tx.begin(); + _conn.deletePersistent(pg); + _conn.deletePersistent(pg2); + tx.commit(); + } + catch (Throwable ex) + { + ex.printStackTrace(); + tx.rollback(); + } + } + catch (Throwable ex) + { + try + { + if (tx != null && tx.isInProgress()) + { + tx.rollback(); + } + } + catch (Exception ex2) + { + } + throw ex; + } + } + + public void testUpdateByReachability() throws Throwable + { + Transaction tx = null; + ProductGroup pg; + Article article; + Article article2; + Identity aOid = null; + Identity aOid2 = null; + Identity pgOid = null; + + //perform transaction + try + { + tx = _kit.getTransaction(_conn); + tx.begin(); + + pg = new ProductGroup(); + pg.setId(77777); + pgOid = _conn.getIdentity(pg); + pg.setName("1"); + article = Article.createInstance(); + article.setArticleId(77777); + aOid = _conn.getIdentity(article); + article.setStock(333); + pg.add(article); + article.setProductGroup(pg); + article2 = Article.createInstance(); + article2.setArticleId(77778); + aOid2 = _conn.getIdentity(article2); + article2.setStock(334); + pg.add(article2); + article2.setProductGroup(pg); + _conn.makePersistent(pg); + _conn.makePersistent(article); + _conn.makePersistent(article2); + tx.commit(); + + + tx = _kit.getTransaction(_conn); + tx.begin(); + article = (Article) _conn.getObjectByIdentity(aOid); + pg = (ProductGroup) article.getProductGroup(); + article2 = (Article) pg.getAllArticles().get(0); + if (!_conn.getIdentity(article2).equals(aOid2)) + { + article2 = (Article) pg.getAllArticles().get(1); + if (!_conn.getIdentity(article2).equals(aOid2)) + { + fail("Missing the second article"); + } + } + article.setStock(433); + article2.setStock(434); + pg.setName("2"); + tx.commit(); + tx = _kit.getTransaction(_conn); + ((BaseConnection) _conn).getKernelBroker().clearCache(); tx.begin(); - _conn.deletePersistent(pg); - _conn.deletePersistent(pg2); + article = (Article) _conn.getObjectByIdentity(aOid); + article2 = (Article) _conn.getObjectByIdentity(aOid2); + assertEquals("should be equal", "2", article.getProductGroup().getName()); + assertEquals("should be equal", 433, article.getStock()); + assertEquals("should be equal", 434, article2.getStock()); tx.commit(); } - catch (Exception ex) + catch (Throwable ex) { try { @@ -338,6 +467,43 @@ { } throw ex; + } + finally + { + try { + tx = _kit.getTransaction(_conn); + tx.begin(); + if (aOid != null) + { + article = (Article) _conn.getObjectByIdentity(aOid); + if (article != null) + { + _conn.deletePersistent(article); + } + } + if (aOid2 != null) + { + article2 = (Article) _conn.getObjectByIdentity(aOid2); + if (article2 != null) + { + _conn.deletePersistent(article2); + } + } + if (pgOid != null) + { + pg = (ProductGroup) _conn.getObjectByIdentity(pgOid); + if (pg != null) + { + _conn.deletePersistent(pg); + } + } + tx.commit(); + } + catch (Throwable ex) + { + ex.printStackTrace(); + tx.rollback(); + } } }