/*
 * JBoss, the OpenSource EJB server
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3.test.txexceptions.unit;

import javax.ejb.EJBException;
import org.jboss.ejb3.test.txexceptions.AnnotatedAppException;
import org.jboss.ejb3.test.txexceptions.AppException;
import org.jboss.ejb3.test.txexceptions.CheckedRollbackException;
import org.jboss.ejb3.test.txexceptions.Dao;
import org.jboss.ejb3.test.txexceptions.NoRollbackRemoteException;
import org.jboss.ejb3.test.txexceptions.NoRollbackRuntimeException;
import org.jboss.ejb3.test.txexceptions.RollbackRemoteException;
import org.jboss.ejb3.test.txexceptions.RollbackRuntimeException;
import org.jboss.test.JBossTestCase;
import junit.framework.Test;

/**
 * @author <a href="mailto:bdecoste@jboss.com">William DeCoste</a>
 * @version $Revision: 1.1.2.5 $
 */
public class TxExceptionsTestCase extends JBossTestCase
{
   org.apache.log4j.Category log = getLog();

   static boolean deployed = false;
   static int test = 0;

   public TxExceptionsTestCase(String name)
   {
      super(name);
   }

   public void testAnnotatedAppException() throws Exception
   {
      Dao dao = (Dao) getInitialContext().lookup(Dao.class.getName());

      boolean exceptionThrown = false;
      try
      {
         dao.createThrowAnnotatedAppException(1);
      }
      catch (AnnotatedAppException e)
      {
         exceptionThrown = true;
      }
      assertTrue(exceptionThrown);
      assertNotNull(dao.get(1));
      dao.remove(1);
   }

   public void testAppException() throws Exception
   {
      Dao dao = (Dao) getInitialContext().lookup(Dao.class.getName());

      boolean exceptionThrown = false;
      try
      {
         dao.createThrowAppException(1);
      }
      catch (AppException e)
      {
         exceptionThrown = true;
      }
      assertTrue(exceptionThrown);
      assertNotNull(dao.get(1));
      dao.remove(1);
   }

   public void testNoRollbackRemoteException() throws Exception
   {
      Dao dao = (Dao) getInitialContext().lookup(Dao.class.getName());

      boolean exceptionThrown = false;
      try
      {
         dao.createThrowNoRollbackRemoteException(1);
      }
      catch (NoRollbackRemoteException e)
      {
         exceptionThrown = true;
      }
      assertTrue(exceptionThrown);
      assertNotNull(dao.get(1));
      dao.remove(1);
   }

   public void testNoRollbackRuntimexception() throws Exception
   {
      Dao dao = (Dao) getInitialContext().lookup(Dao.class.getName());

      boolean exceptionThrown = false;
      try
      {
         dao.createThrowNoRollbackRuntimeException(1);
      }
      catch (NoRollbackRuntimeException e)
      {
         exceptionThrown = true;
      }
      assertTrue(exceptionThrown);
      assertNotNull(dao.get(1));
      dao.remove(1);
   }

   public void testCheckedRollbackException() throws Exception
   {
      Dao dao = (Dao) getInitialContext().lookup(Dao.class.getName());

      boolean exceptionThrown = false;
      try
      {
         dao.createThrowCheckedRollbackException(1);
      }
      catch (CheckedRollbackException e)
      {
         exceptionThrown = true;
      }
      assertTrue(exceptionThrown);

      exceptionThrown = false;

      try
      {
         dao.get(1);
      }
      catch (Exception e)
      {
         exceptionThrown = true;
         assertTrue(e instanceof EJBException);
         EJBException top = (EJBException) e;
         //todo assertTrue(top.getCausedByException() instanceof EntityNotFoundException);
      }

      if (!exceptionThrown)
      {
         dao.remove(1);
      }
   }

   public void testRollbackRemoteException() throws Exception
   {
      Dao dao = (Dao) getInitialContext().lookup(Dao.class.getName());

      boolean exceptionThrown = false;
      try
      {
         dao.createThrowRollbackRemoteException(1);
      }
      catch (RollbackRemoteException e)
      {
         exceptionThrown = true;
      }
      assertTrue(exceptionThrown);

      exceptionThrown = false;
      try
      {
         dao.get(1);
      }
      catch (Exception e)
      {
         exceptionThrown = true;
         assertTrue(e instanceof EJBException);
         EJBException top = (EJBException) e;
         //todo assertTrue(top.getCausedByException() instanceof EntityNotFoundException);
      }

      if (!exceptionThrown)
      {
         dao.remove(1);
      }
   }

   public void testRollbackRuntimeException() throws Exception
   {
      Dao dao = (Dao) getInitialContext().lookup(Dao.class.getName());

      boolean exceptionThrown = false;
      try
      {
         dao.createThrowRollbackRuntimeException(1);
      }
      catch (EJBException e)
      {
         exceptionThrown = true;
         assertTrue(e.getCausedByException() instanceof RollbackRuntimeException);
      }
      assertTrue(exceptionThrown);

      exceptionThrown = false;
      try
      {
         dao.get(1);
      }
      catch (Exception e)
      {
         exceptionThrown = true;
         assertTrue(e instanceof EJBException);
         EJBException top = (EJBException) e;
         // todo assertTrue(top.getCausedByException() instanceof EntityNotFoundException);
      }

      if (!exceptionThrown)
      {
         dao.remove(1);
      }
   }

   public static Test suite() throws Exception
   {
      return getDeploySetup(TxExceptionsTestCase.class, "txexceptions-test.ejb3");
   }
}