db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/otm OtmExamples.java
Date Mon, 09 Jun 2003 18:58:31 GMT
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();
  +            }
           }
       }
   
  
  
  

Mime
View raw message