[jboss-cvs] jboss-portal/test/src/main/org/jboss/portal/test/framework/junit ...

Julien Viet julien at jboss.com
Sun Jul 30 08:36:25 EDT 2006


  User: julien  
  Date: 06/07/30 08:36:25

  Added:       test/src/main/org/jboss/portal/test/framework/junit 
                        TransactionAssert.java
  Log:
  JBPORTAL-973 : Portlet instance container integration testing
  JBPORTAL-972 : Portlet stateful invoker testing
  
  Revision  Changes    Path
  1.1      date: 2006/07/30 12:36:25;  author: julien;  state: Exp;jboss-portal/test/src/main/org/jboss/portal/test/framework/junit/TransactionAssert.java
  
  Index: TransactionAssert.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This 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 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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 software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.portal.test.framework.junit;
  
  import junit.framework.Assert;
  
  import javax.transaction.UserTransaction;
  import javax.transaction.Status;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  /**
   * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
   * @version $Revision: 1.1 $
   */
  public class TransactionAssert extends Assert
  {
  
     /**
      * Assert the status of the current transaction.
      *
      * @param expectedStatus the expected status
      */
     public static void assertStatusEquals(int expectedStatus)
     {
        try
        {
           UserTransaction ut = getUserTransaction();
           int status = ut.getStatus();
           if (status != expectedStatus)
           {
              fail("The status of the current transaction is " + status + " was expecting " + expectedStatus);
           }
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Unexpected exception");
        }
     }
  
     /**
      * Commit the transaction or fail.
      */
     public static void commitTransaction()
     {
        try
        {
           getUserTransaction().commit();
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Cannot commit transaction");
        }
     }
  
     /**
      * Rollback the transaction or fail.
      */
     public static void rollbackTransaction()
     {
        rollbackTransaction(true);
     }
  
     public static void rollbackTransaction(boolean marked)
     {
        try
        {
           UserTransaction ut = getUserTransaction();
           int status = ut.getStatus();
           if (status == Status.STATUS_MARKED_ROLLBACK)
           {
              ut.rollback();
           }
           else if (status == Status.STATUS_ACTIVE)
           {
              ut.rollback();
              if (marked)
              {
                 fail("Transaction should have been marked as rollback");
              }
           }
           else
           {
              if (marked)
              {
                 fail("Unexpected transaction status " + status);
              }
           }
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Cannot end transaction");
        }
     }
  
     /**
      * Begin a transaction or fail.
      */
     public static void beginTransaction()
     {
        try
        {
           getUserTransaction().begin();
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Cannot begin transaction");
        }
     }
  
     /**
      * If no transaction do nothing.
      * End the transaction if it is active or marked for rollback otherwise fail.
      */
     public static void endTransaction()
     {
        try
        {
           UserTransaction ut = getUserTransaction();
           int status = ut.getStatus();
           if (status == Status.STATUS_MARKED_ROLLBACK)
           {
              ut.rollback();
           }
           else if (status == Status.STATUS_ACTIVE)
           {
              ut.commit();
           }
           else if (status == Status.STATUS_NO_TRANSACTION)
           {
              // Do nothing
           }
           else
           {
              fail("Unexpected transaction status " + status);
           }
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Cannot end transaction");
        }
     }
  
     public static UserTransaction getUserTransaction()
     {
        try
        {
           return (UserTransaction)new InitialContext().lookup("UserTransaction");
        }
        catch (NamingException e)
        {
           e.printStackTrace();
           fail("Cannot obtain user transaction");
           return null;
        }
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list