/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.ejb3.test.entityexception;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision: 1.4.2.5 $
 */
@Stateless
@Remote(ExceptionTest.class)
public class ExceptionTestBean implements ExceptionTest
{
   @PersistenceContext
   EntityManager manager;

   
   public Person createEntry(Person person)
   {
      manager.persist(person);
      return person;
   }
   
   public Person removeEntry(Person person)
   {
      manager.remove(person);
      return person;
   }
   
   @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)   
   public void testTransactionRequiredException()
   {
      System.out.println("*** testEMPersistTransactionRequiredException");
      Person person = new Person(100, "No Tx");
      
      try
      {
         manager.persist(person);
         throw new RuntimeException("TransactionRequiredException not thrown for persist()");
      }
      catch(TransactionRequiredException e)
      {
      }
      
      try
      {
         manager.merge(person);
         throw new RuntimeException("TransactionRequiredException not thrown for merge()");
      }
      catch(TransactionRequiredException e)
      {
      }
      
      try
      {
         manager.remove(person);
         throw new RuntimeException("TransactionRequiredException not thrown for remove()");
      }
      catch(TransactionRequiredException e)
      {
      }

      try
      {
         manager.refresh(person);
         throw new RuntimeException("TransactionRequiredException not thrown for refresh()");
      }
      catch(TransactionRequiredException e)
      {
      }
      
      try
      {
         manager.contains(person);
         throw new RuntimeException("TransactionRequiredException not thrown for contains()");
      }
      catch(TransactionRequiredException e)
      {
      }
      
      try
      {
         manager.flush();
         throw new RuntimeException("TransactionRequiredException not thrown for flush()");
      }
      catch(TransactionRequiredException e)
      {
      }
      
      
   }
   
   public void testEMPersistExceptions()
   {
      System.out.println("*** testEMPersistIllegalArgumentExceptions");
      NonEntity nonEntity = new NonEntity();
      try
      {
         manager.persist(nonEntity);
         throw new RuntimeException("IllegalArgumentException not thrown when saving non-entity");
      }
      catch(IllegalArgumentException e)
      {
      }

      //TODO - IllegalArgumentException should be thrown if entity is in detached state
   }
   
   public void testEMMergeExceptions()
   {
      System.out.println("*** testEMMergeIllegalArgumentException");
      try
      {
         NonEntity nonEntity = new NonEntity();
         manager.merge(nonEntity);
         throw new RuntimeException("IllegalArgumentException not thrown when merging non-entity");
      }
      catch(IllegalArgumentException e)
      {
      }
      
      //TODO - IllegalArgumentException should be thrown if entity is in removed state
   }

   public void testEMRemoveIllegalArgumentException()
   {
      System.out.println("*** testEMRemoveIllegalArgumentException");
      try
      {
         NonEntity nonEntity = new NonEntity();
         manager.remove(nonEntity);
         throw new RuntimeException("IllegalArgumentException not thrown when merging non-entity");
      }
      catch(IllegalArgumentException e)
      {
      }
      
      //TODO - IllegalArgumentException should be thrown if entity is in detached or removed state
   }
   
   public boolean testEMFindExceptions()
   {
      System.out.println("*** testEMFindExceptions");
      manager.find(Person.class, 1);
      try
      {

         Person person = manager.getReference(Person.class, 2);
         person.getId();
         throw new RuntimeException("EntityNotFoundException not thrown: " + person);
      }
      catch(EntityNotFoundException e) 
      {
      }
      
      try
      {
         manager.find(NonEntity.class, 1);
         throw new RuntimeException("IllegalArgumentException not thrown");
      }
      catch(IllegalArgumentException e)
      {
      }

      try
      {
         manager.find(Person.class, "abc");
         throw new RuntimeException("IllegalArgumentException not thrown");
      }
      catch(IllegalArgumentException e)
      {
      }


      return true;
   } 
   
   public void testEMCreateQueryExceptions()
   {
      try
      {
         manager.createQuery("This is all nonsense");
         //TODO, according to spec invalid EJBQL should throw IllegalArgumentExceptions
         //throw new RuntimeException("IllegalArgumentException not thrown");
      }
      catch(IllegalArgumentException e)
      {
      }
   }
   
   public void testEMRefreshExceptions()
   {
      try
      {
         manager.refresh(new NonEntity());
         throw new RuntimeException("IllegalArgumentException not thrown when refreshing a non-entity");
      }
      catch(IllegalArgumentException e)
      {
      }

      //TODO - IllegalArgumentException should be thrown if argument is not in managed state
   }
 
   public void testEMContainsExceptions()
   {
      try
      {
         manager.contains(new NonEntity());
         //TODO - IllegalArgumentException should be thrown 
         //throw new RuntimeException("IllegalArgumentException not thrown for contains a non-entity");
      }
      catch(IllegalArgumentException e)
      {
      }
   }
   
   public void testQuerySingleResultExceptions()
   {
      createEntry(new Person(11, "A"));
      createEntry(new Person(12, "B"));
      createEntry(new Person(13, "C"));
      
      Query query = manager.createQuery("from Person");
      try
      {
         query.getSingleResult();
         throw new RuntimeException("NonUniqueResultException not thrown for getSingleResult");
      }
      catch(NonUniqueResultException e)
      {
      }
      
      Query query2 = manager.createQuery("from Person where id=999");
      try
      {
         query2.getSingleResult();
         throw new RuntimeException("EntityNotFoundException not thrown for getSingleResult returning no results");
      }
      catch(EntityNotFoundException e)
      {
      }
   }
   
   public void testQuerySetHintAndParameter()
   {
      
      try
      {
         Query query = manager.createQuery("from Person");
         query.setHint("org.hibernate.timeout", "Not an integer");
         throw new RuntimeException("IllegalArgumentException not thrown for setHint");
      }
      catch(IllegalArgumentException e)
      {
      }
      
      Query query = manager.createQuery("from Person where id=:id and name=:name");
      try
      {
         query.setParameter("nosuchparam", "Whateverrrr");
         throw new RuntimeException("IllegalArgumentException not thrown for setParameter (Wrong name)");
      }
      catch(IllegalArgumentException e)
      {
      }
      
      query.setParameter("name", "Kabir");
      try
      {
         query.setParameter("id", "HELLO");
         //Acceptable not to throw an exception when using wrong type (query will fail)
      }
      catch(IllegalArgumentException e)
      {
      }
      
      query = manager.createQuery("from Person where id=?0 and name=?1");
      query.setParameter(0, 1);
      query.setParameter(1, "XXX");
      try
      {
         query.setParameter(-1, "HELLO");
         throw new RuntimeException("IllegalArgumentException not thrown for setParameter (Wrong index)");
      }
      catch(IllegalArgumentException e)
      {
      }

      try
      {
         query.setParameter(10, "HELLO");
         throw new RuntimeException("IllegalArgumentException not thrown for setParameter (Wrong index)");
      }
      catch(IllegalArgumentException e)
      {
      }
   }

}
