TransactionTests.java :  » Database-ORM » beankeeper » hu » netmind » persistence » Java Open Source

Java Open Source » Database ORM » beankeeper 
beankeeper » hu » netmind » persistence » TransactionTests.java
/**
 * Copyright (C) 2006 NetMind Consulting Bt.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package hu.netmind.persistence;

import java.util.List;
import org.apache.log4j.Logger;

/**
 * Test the transaction framework.
 * @author Brautigam Robert
 * @version Revision: $Revision$
 */
public class TransactionTests extends AbstractPersistenceTest
{
   private static Logger logger = Logger.getLogger(TransactionTests.class);
   
   private boolean called = false; // Helper
   
   public TransactionTests(String name)
      throws Exception
   {
      super(name);
   }

   public void testAtomicOperations()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Create stuff
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Wayne's World","1"));
      store.save(new Book("Wayne's World II.","2"));
      tx.rollback();
      // Test
      List result = store.find("find book");
      assertEquals(0,result.size());
   }
   
   public void testMultilevelTransactionInnerCommit()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Create stuff
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Wayne's World","1"));
      tx.begin();
      store.save(new Book("Wayne's World II.","2"));
      tx.commit();
      tx.rollback();
      // Test
      List result = store.find("find book");
      assertEquals(0,result.size());
   }
   
   public void testMultilevelTransactionOuterCommit()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Create stuff
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Wayne's World","1"));
      tx.begin();
      store.save(new Book("Wayne's World II.","2"));
      tx.rollback();
      tx.commit();
      // Test
      List result = store.find("find book");
      assertEquals(0,result.size());
   }
   
   public void testTransactionRequiredSameThread()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Create stuff
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Wayne's World","1"));
      Transaction tx2 = store.getTransactionTracker().getTransaction(TransactionTracker.TX_REQUIRED);
      assertSame(tx,tx2);
      tx2.begin();
      store.save(new Book("Wayne's World II.","2"));
      tx2.rollback();
      tx.commit();
      // Test
      List result = store.find("find book");
      assertEquals(0,result.size());
   }
   
   public void testTransactionNewSameThread()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Create stuff
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Wayne's World","1"));
      Transaction tx2 = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx2.begin();
      store.save(new Book("Wayne's World II.","2"));
      tx2.rollback();
      tx.commit();
      // Test
      List result = store.find("find book");
      assertEquals(1,result.size());
   }
   
   public void testListenerCommit()
      throws Exception
   {
      // Register listener
      called = false;
      TransactionTracker tt = store.getTransactionTracker();
      tt.addListener( new TransactionListener()
            {
               public void transactionCommited(Transaction t)
               {
                  called=true;
               }
               public void transactionRolledback(Transaction t)
               {
               }
            } );
      // Do a transaction
      store.save(new Book("Book Of Bokonon","1-2-3-4"));
      // Check
      assertTrue(called);
   }

   public void testListenerRollback()
      throws Exception
   {
      // Register listener
      called = false;
      TransactionTracker tt = store.getTransactionTracker();
      tt.addListener( new TransactionListener()
            {
               public void transactionCommited(Transaction t)
               {
               }
               public void transactionRolledback(Transaction t)
               {
                  called=true;
               }
            } );
      // Do a transaction
      Transaction tx = tt.getTransaction(TransactionTracker.TX_REQUIRED);
      tx.begin();
      store.save(new Book("Book Of Bokonon","1-2-3-4"));
      tx.rollback();
      // Check
      assertTrue(called);
   }

   public void testListenerRecursion()
      throws Exception
   {
      called = false;
      // Register listener
      final TransactionTracker tt = store.getTransactionTracker();
      tt.addListener( new TransactionListener()
            {
               public void transactionCommited(Transaction t)
               {
                  // Determine whether internal
                  logger.debug("received commit for transaction: "+t+", with keys: "+t.keySet());
                  if ( t.get("Internal") != null )
                  {
                     called = true;
                     return; // Avoid recursion
                  }
                  // Do transaction
                  Transaction tx = tt.getTransaction(TransactionTracker.TX_NEW);
                  tx.put("Internal","true");
                  logger.debug("starting internal transaction: "+tx+", with keys: "+tx.keySet());
                  tx.begin();
                  store.save(new Book("Book Of Bokonon","1-2-3-4"));
                  tx.commit();
                  logger.debug("internal transaction ended: "+tx);
               }
               public void transactionRolledback(Transaction t)
               {
               }
            } );
      // Do a transaction
      store.save(new Book("Book Of Bokonon","1-2-3-4"));
      // Check
      assertFalse(called);
   }

   public void testNewVisibility()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Create stuff
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Wayne's World","1"));
      Transaction tx2 = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx2.begin();
      assertEquals(0,store.find("find book").size());
      tx2.commit();
      tx.commit();
   }

   public void testNewParameters()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Create stuff
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Wayne's World","1"));
      tx.put("Book","yes");
      Transaction tx2 = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx2.begin();
      assertNull(tx2.get("Book"));
      tx2.commit();
      tx.commit();
   }

   public void testNewParametersBack()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Create stuff
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Wayne's World","1"));
      Transaction tx2 = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx2.begin();
      tx2.put("Book","no");
      tx2.commit();
      assertNull(tx.get("Book"));
      tx.commit();
   }
      
   public void testListOutOfActiveTransaction()
      throws Exception
   {
      // Drop tables
      dropTables("book");
      // Register class
      store.save(new Book("Wayne's World","1"));
      // Get a list which has transaction bindings
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      store.save(new Book("Bill and Ted","2"));
      List result = store.find("find book");
      // Before closing try to use the list in another transaction
      Transaction tx2 = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx2.begin();
      try
      {
         result.size();
         fail("could query a list outside it's still open transaction, that should be prohibited");
      } catch ( StoreException e ) {
         // Ok
      }
      tx2.commit();
      // Close all
      tx.commit();
   }

   public void testDoubleSave()
      throws Exception
   {
      // Drop tables
      dropTables("referrer");
      dropTables("referrersubclass");
      // Create
      ReferrerSubclass obj = new ReferrerSubclass(1,1);
      store.save(obj);
      // Double save
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      obj.setIdentity(2);
      store.save(obj);
      obj.setIdentity(3);
      store.save(obj);
      tx.commit();
      // Test
      List result = store.find("find referrersubclass");
      assertEquals(1,result.size());
   }
   
   public void testCombinedSaveInsert()
      throws Exception
   {
      // Drop tables
      dropTables("referrer");
      dropTables("referrersubclass");
      // Create
      ReferrerSubclass obj = new ReferrerSubclass(1,1);
      store.save(obj);
      // Double save
      Transaction tx = store.getTransactionTracker().getTransaction(TransactionTracker.TX_NEW);
      tx.begin();
      obj.setIdentity(2);
      store.save(obj);
      store.save(new ReferrerSubclass(2,2));
      obj.setIdentity(3);
      store.save(obj);
      tx.commit();
      // Test
      List result = store.find("find referrersubclass");
      assertEquals(2,result.size());
   }
   
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.