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 TestKit.java OtmExamples.java
Date Sun, 18 May 2003 15:11:58 GMT
olegnitz    2003/05/18 08:11:58

  Modified:    src/test/org/apache/ojb/otm OtmExamples.java
  Added:       src/test/org/apache/ojb/otm TestKit.java
  Log:
  Added test for TimeoutStrategy
  
  Revision  Changes    Path
  1.7       +89 -15    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- OtmExamples.java	18 May 2003 09:50:02 -0000	1.6
  +++ OtmExamples.java	18 May 2003 15:11:58 -0000	1.7
  @@ -8,9 +8,10 @@
   import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.otm.core.BaseConnection;
   import org.apache.ojb.otm.core.Transaction;
  -import org.apache.ojb.otm.kit.SimpleKit;
   import org.apache.ojb.otm.lock.LockType;
   import org.apache.ojb.otm.lock.LockingException;
  +import org.apache.ojb.otm.lock.wait.NoWaitStrategy;
  +import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
   
   /**
    * Demo Application that shows basic concepts for Applications
  @@ -19,7 +20,7 @@
   public class OtmExamples extends TestCase
   {
       private static Class CLASS = OtmExamples.class;
  -    private OTMKit _kit;
  +    private TestKit _kit;
       private OTMConnection _conn;
   
       public OtmExamples(String name)
  @@ -29,7 +30,7 @@
   
       public void setUp()
       {
  -        _kit = SimpleKit.getInstance();
  +        _kit = TestKit.getTestInstance();
           _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
       }
   
  @@ -179,24 +180,81 @@
   
               tx2 = _kit.getTransaction(conn2);
               tx2.begin();
  -            try {
  -                pg = (ProductGroup) conn2.getObjectByIdentity(pgOid,
  -                        LockType.WRITE_LOCK);
  -                fail("LockingException was not thrown");
  -            } catch (LockingException ex) {
  -                // ok
  -                tx2.rollback();
  -            } catch (Throwable ex) {
  -                tx2.rollback();
  -System.err.println("ex " + ex.getClass() + " " + (ex instanceof LockingException));
  +            pg = (ProductGroup) conn2.getObjectByIdentity(pgOid);
  +            assertEquals("should be equal", "1", pg.getName());
  +            tx2.commit();
  +
  +            _conn.deletePersistent(pg);
  +            tx.commit();
  +        }
  +        catch (Exception ex)
  +        {
  +            try
  +            {
  +                if (tx != null && tx.isInProgress())
  +                {
  +                    tx.rollback();
  +                }
               }
  +            catch (Exception ex2)
  +            {
  +            }
  +            throw ex;
  +        }
  +    }
  +
  +    public void testOtmLocks() throws Exception
  +    {
  +        Transaction tx = null;
  +        Transaction tx2 = null;
  +        OTMConnection conn2;
   
  +        conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +
  +        try
  +        {
  +            tx = _kit.getTransaction(_conn);
  +            tx.begin();
  +
  +            ProductGroup pg = new ProductGroup();
  +            pg.setId(77777);
  +            pg.setName("1");
  +            _conn.makePersistent(pg);
  +            tx.commit();
  +
  +            Identity pgOid = _conn.getIdentity(pg);
  +
  +            final Transaction tx3 = _kit.getTransaction(_conn);
  +            tx3.begin();
  +            _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
  +            // we can write lock twice from the same tx
  +            _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
  +
  +            // test different LockWaitStrategies
  +            _kit.setLockWaitStrategy(new NoWaitStrategy());
  +            failIfLockForWrite(conn2, pgOid);
  +            _kit.setLockWaitStrategy(new TimeoutStrategy(1));
  +            failIfLockForWrite(conn2, pgOid);
  +
  +            // Second test for the TimeoutStrategy:
  +            // let the second tx to lock
  +            _kit.setLockWaitStrategy(new TimeoutStrategy(2000));
               tx2 = _kit.getTransaction(conn2);
               tx2.begin();
  -            pg = (ProductGroup) conn2.getObjectByIdentity(pgOid);
  -            assertEquals("should be equal", "1", pg.getName());
  +            (new Thread() {
  +                public void run() {
  +                    try {
  +                        Thread.sleep(1000);
  +                        tx3.commit();
  +                    } catch (InterruptedException ex) {
  +                    }
  +                }
  +            }).start();
  +            conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
               tx2.commit();
   
  +            tx = _kit.getTransaction(_conn);
  +            tx.begin();
               _conn.deletePersistent(pg);
               tx.commit();
           }
  @@ -213,6 +271,22 @@
               {
               }
               throw ex;
  +        }
  +    }
  +
  +    private void failIfLockForWrite(OTMConnection conn2, Identity oid)
  +            throws Exception
  +    {
  +        Transaction tx2 = null;
  +
  +        tx2 = _kit.getTransaction(conn2);
  +        tx2.begin();
  +        try {
  +            conn2.getObjectByIdentity(oid, LockType.WRITE_LOCK);
  +            fail("LockingException was not thrown");
  +        } catch (LockingException ex) {
  +            // ok: we cannot write lock from another tx
  +            tx2.rollback();
           }
       }
   
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/otm/TestKit.java
  
  Index: TestKit.java
  ===================================================================
  package org.apache.ojb.otm;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.ojb.otm.kit.SimpleKit;
  import org.apache.ojb.otm.lock.wait.LockWaitStrategy;
  
  /**
   * @author <a href="mailto:olegnitz@apache.org">Oleg Nitz</a>
   *
   */
  public class TestKit extends SimpleKit
  {
  
      private static TestKit _instance;
  
      /**
       * Constructor for SimpleKit.
       */
      protected TestKit()
      {
          super();
      }
  
      public static TestKit getTestInstance()
      {
          if (_instance == null)
          {
              _instance = new TestKit();
          }
          return _instance;
      }
  
      /**
      * This allows to test different LockWaitStrategies
      */
      public void setLockWaitStrategy(LockWaitStrategy lockWaitStrategy)
      {
          _lockWaitStrategy = lockWaitStrategy;
      }
  
  }
  
  
  

Mime
View raw message