db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker OptimisticLockingTest.java
Date Tue, 09 Sep 2003 13:37:14 GMT
arminw      2003/09/09 06:37:14

  Modified:    src/test/org/apache/ojb/broker OptimisticLockingTest.java
  Log:
  add more checks, remove try/catch block
  
  Revision  Changes    Path
  1.6       +115 -129  db-ojb/src/test/org/apache/ojb/broker/OptimisticLockingTest.java
  
  Index: OptimisticLockingTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/OptimisticLockingTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- OptimisticLockingTest.java	5 Feb 2003 20:03:41 -0000	1.5
  +++ OptimisticLockingTest.java	9 Sep 2003 13:37:14 -0000	1.6
  @@ -3,7 +3,7 @@
   import junit.framework.TestCase;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  -/** 
  +/**
    * This TestClass tests OJB facilities for optimistic locking.
    */
   public class OptimisticLockingTest extends TestCase
  @@ -54,144 +54,130 @@
   	}
   
   	/** Test optimistic Lock by version.*/
  -	public void testVersionLock()
  +	public void testVersionLock() throws Exception
   	{
  -		try
  -		{
  -		    LockedByVersion obj = new LockedByVersion();
  -		    obj.setValue("original");
  -		    Identity oid = new Identity(obj, broker);		    
  -		    broker.store(obj);		    
  -		    
  -		    broker.clearCache();
  -		    LockedByVersion copy1 = (LockedByVersion) broker.getObjectByIdentity(oid);
  -		    broker.clearCache();
  -		    LockedByVersion copy2 = (LockedByVersion) broker.getObjectByIdentity(oid);
  -		    
  -		    copy1.setValue("copy 1");
  -		    broker.store(copy1);
  -		    
  -		    copy2.setValue("copy 2");
  -		    try
  -		    {
  -		        // as copy1 has already been stored the version info of copy2 
  -		        // is out of sync with the database !		        
  -		    	broker.store(copy2);
  -		    }
  -		    catch (OptimisticLockException ex)
  -		    {
  -		        //LoggerFactory.getDefaultLogger().debug(ex);
  -		    	return;    
  -		    }
  -		    
  -		    fail("Should throw an Optimistic Lock exception");
  -		    
  -
  -		}
  -		catch (Throwable t)
  -		{
  -			fail(t.getMessage());
  -		}
  +        LockedByVersion obj = new LockedByVersion();
  +        obj.setValue("original");
  +        Identity oid = new Identity(obj, broker);
  +        broker.beginTransaction();
  +        broker.store(obj);
  +        broker.commitTransaction();
  +
  +        broker.clearCache();
  +        LockedByVersion copy1 = (LockedByVersion) broker.getObjectByIdentity(oid);
  +        broker.clearCache();
  +        LockedByVersion copy2 = (LockedByVersion) broker.getObjectByIdentity(oid);
  +
  +        copy1.setValue("copy 1");
  +        copy2.setValue("copy 2");
  +        assertEquals("Expect same version number", copy1.getVersion(), copy2.getVersion());
  +
  +        broker.beginTransaction();
  +        broker.store(copy1);
  +        broker.commitTransaction();
  +        assertTrue("Expect different version number", copy1.getVersion() != copy2.getVersion());
  +        try
  +        {
  +            // as copy1 has already been stored the version info of copy2
  +            // is out of sync with the database !
  +            broker.store(copy2);
  +        }
  +        catch (OptimisticLockException ex)
  +        {
  +            assertTrue(true);
  +            //LoggerFactory.getDefaultLogger().debug(ex);
  +            return;
  +        }
  +        fail("Should throw an Optimistic Lock exception");
   	}
  -	
  -	/** 
  +
  +	/**
   	 * demonstrates how OptimisticLockExceptions can be used
   	 * to handle resynchronization of conflicting instances.
   	 */
  -	public void testLockHandling()
  +	public void testLockHandling() throws Exception
   	{
  -		try
  -		{
  -		    LockedByVersion obj = new LockedByVersion();
  -		    obj.setValue("original");
  -		    Identity oid = new Identity(obj, broker);
  -		    
  -		    broker.store(obj);
  -		    
  -		    broker.clearCache();
  -		    LockedByVersion copy1 = (LockedByVersion) broker.getObjectByIdentity(oid);
  -		    broker.clearCache();
  -		    LockedByVersion copy2 = (LockedByVersion) broker.getObjectByIdentity(oid);
  -		    
  -		    copy1.setValue("copy 1");
  -		    broker.store(copy1);
  -		    
  -		    copy2.setValue("copy 2");
  -		    try
  -		    {
  -		        // as copy1 has already been stored the version info of copy2 
  -		        // is out of sync with the database !
  -		    	broker.store(copy2);
  -		    }
  -		    catch (OptimisticLockException ex)
  -		    {
  -		        // obtain conflicting object from exception
  -		        Object conflictingObject = ex.getSourceObject();
  -		        
  -		        // get a synchronized instance
  -		        broker.removeFromCache(conflictingObject);
  -		        Object syncronizedObject = broker.getObjectByIdentity(new Identity(conflictingObject,
broker));
  -		        
  -		        // modify synchronized copy and call store again without trouble
  -		        ((LockedByVersion) syncronizedObject).setValue("copy 3");
  -		       	broker.store(syncronizedObject);		       
  -		    	return;    
  -		    }
  -		    
  -		    fail("Should throw an Optimistic Lock exception");
  -		    
  +        LockedByVersion obj = new LockedByVersion();
  +        obj.setValue("original");
  +        Identity oid = new Identity(obj, broker);
  +        broker.beginTransaction();
  +        broker.store(obj);
  +        broker.commitTransaction();
  +
  +        broker.clearCache();
  +        LockedByVersion copy1 = (LockedByVersion) broker.getObjectByIdentity(oid);
  +        broker.clearCache();
  +        LockedByVersion copy2 = (LockedByVersion) broker.getObjectByIdentity(oid);
  +
  +        copy1.setValue("copy 1");
  +        copy2.setValue("copy 2");
  +        assertEquals("Expect same version number", copy1.getVersion(), copy2.getVersion());
  +
  +        broker.beginTransaction();
  +        broker.store(copy1);
  +        broker.commitTransaction();
  +        assertTrue("Expect different version number", copy1.getVersion() != copy2.getVersion());
  +        try
  +        {
  +            // as copy1 has already been stored the version info of copy2
  +            // is out of sync with the database !
  +            broker.store(copy2);
  +        }
  +        catch (OptimisticLockException ex)
  +        {
  +            // obtain conflicting object from exception
  +            Object conflictingObject = ex.getSourceObject();
  +
  +            // get a synchronized instance
  +            broker.removeFromCache(conflictingObject);
  +            Object syncronizedObject = broker.getObjectByIdentity(new Identity(conflictingObject,
broker));
  +
  +            // modify synchronized copy and call store again without trouble
  +            ((LockedByVersion) syncronizedObject).setValue("copy 3");
  +            broker.store(syncronizedObject);
  +            return;
  +        }
  +        fail("Should throw an Optimistic Lock exception");
  +	}
   
  -		}
  -		catch (Throwable t)
  -		{
  -			fail(t.getMessage());
  -		}
  -	}	
  -	
   
   /** Test optimistic Lock by timestamp.*/
  -	public void testTimestampLock()
  +	public void testTimestampLock() throws Exception
   	{
  -		try
  -		{
  -		    LockedByTimestamp obj = new LockedByTimestamp();
  -		    obj.setValue("original");
  -		    Identity oid = new Identity(obj, broker);
  -		    
  -		    broker.store(obj);
  -		    
  -		    broker.clearCache();
  -		    LockedByTimestamp copy1 = (LockedByTimestamp) broker.getObjectByIdentity(oid);
  -		    broker.clearCache();
  -		    LockedByTimestamp copy2 = (LockedByTimestamp) broker.getObjectByIdentity(oid);
  -		    
  -		    Thread.sleep(1000);	//mysql timestamp does not support milliseconds
  -
  -		    copy1.setValue("copy 1");
  -		    broker.store(copy1);
  -		    
  -		    copy2.setValue("copy 2");
  -		    try
  -		    {
  -		        // as copy1 has already been stored the timestamp info 
  -		        // of copy2  is out of sync with the database !
  -		    	broker.store(copy2);
  -		    }
  -		    catch (OptimisticLockException ex)
  -		    {
  -		        LoggerFactory.getDefaultLogger().debug(ex);
  -		    	return;    
  -		    }
  -		    
  -		    fail("Should throw an Optimistic Lock exception");
  -		    
  +		LockedByTimestamp obj = new LockedByTimestamp();
  +        obj.setValue("original");
  +        Identity oid = new Identity(obj, broker);
  +        broker.beginTransaction();
  +        broker.store(obj);
  +        broker.commitTransaction();
  +
  +        broker.clearCache();
  +        LockedByTimestamp copy1 = (LockedByTimestamp) broker.getObjectByIdentity(oid);
  +        broker.clearCache();
  +        LockedByTimestamp copy2 = (LockedByTimestamp) broker.getObjectByIdentity(oid);
  +
  +        Thread.sleep(1000);	//mysql timestamp does not support milliseconds
  +
  +        copy1.setValue("copy 1");
  +        copy2.setValue("copy 2");
  +        assertEquals("Expect same version number", copy1.getTimestamp(), copy2.getTimestamp());
  +
  +        broker.beginTransaction();
  +        broker.store(copy1);
  +        broker.commitTransaction();
  +        assertTrue("Expect different version number", copy1.getTimestamp() != copy2.getTimestamp());
  +        try
  +        {
  +            // as copy1 has already been stored the timestamp info
  +            // of copy2  is out of sync with the database !
  +            broker.store(copy2);
  +        }
  +        catch (OptimisticLockException ex)
  +        {
  +            LoggerFactory.getDefaultLogger().debug(ex);
  +            return;
  +        }
   
  -		}
  -		catch (Throwable t)
  -		{
  -			fail(t.getMessage());
  -		}
  +        fail("Should throw an Optimistic Lock exception");
   	}
  -
  -
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message