package org.zanata;

import org.jboss.seam.mock.DBUnitSeamTest;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

@Test(groups = { "seam-tests" })
public abstract class ZanataDBUnitSeamTest extends DBUnitSeamTest
{

   // begin config methods from DBUnitSeamTest
   @BeforeClass
   @Parameters("datasourceJndiName")
   public void setDatasourceJndiName(@Optional("java:/zanataTestDatasource") String datasourceJndiName)
   {
      super.setDatasourceJndiName(datasourceJndiName);
   }

   @BeforeClass
   @Parameters("binaryDir")
   public void setBinaryDir(@Optional("") String binaryDir)
   {
      super.setBinaryDir(binaryDir);
   }

   @BeforeClass
   @Parameters("database")
   public void setDatabase(@Optional("hsql") String database)
   {
      super.setDatabase(database);
   }

   // end of config methods from DBUnitSeamTest

   // begin setup methods from DBUnitSeamTest

   @BeforeClass(groups = { "seam-tests" })
   @Override
   public void setupClass() throws Exception
   {
      super.setupClass();
   }

   @BeforeMethod(groups = { "seam-tests" })
   @Override
   public void prepareDataBeforeTest()
   {
      super.prepareDataBeforeTest();
   }

   @AfterMethod(groups = { "seam-tests" })
   @Override
   public void cleanDataAfterTest()
   {
      super.cleanDataAfterTest();
   }

   // end of setup methods from DBUnitSeamTest

   // begin setup methods from SeamTest

   @BeforeMethod(groups = { "seam-tests" })
   @Override
   public void begin()
   {
      super.begin();
   }

   @AfterMethod(groups = { "seam-tests" })
   @Override
   public void end()
   {
      super.end();
   }

   @Override
   @AfterClass(groups = { "seam-tests" })
   public void cleanupClass() throws Exception
   {
      super.cleanupClass();
   }

   @Override
   @BeforeSuite(groups = { "seam-tests" })
   public void startSeam() throws Exception
   {
      super.startSeam();
   }

   @Override
   @AfterSuite(groups = { "seam-tests" })
   protected void stopSeam() throws Exception
   {
      super.stopSeam();
   }
   // end of setup methods from SeamTest

}

@@ -25,42 +25,632 @@
 ort 
-org.jboss.seam.mock.DBUnitSeamTest
+java.util.Map;%0A%0Aimport org.hibernate.Session;%0Aimport org.hibernate.SessionFactory;%0Aimport org.hibernate.persister.collection.AbstractCollectionPersister;%0Aimport org.hibernate.persister.entity.EntityPersister;%0Aimport org.jboss.seam.contexts.TestLifecycle;%0Aimport org.jboss.seam.mock.DBUnitSeamTest;%0Aimport org.jboss.seam.mock.MockHttpSession;%0Aimport org.jboss.seam.mock.MockServletContext;%0Aimport org.jboss.seam.mock.ResourceRequestEnvironment;%0Aimport org.jboss.seam.mock.ResourceRequestEnvironment.Method;%0Aimport org.jboss.seam.mock.ResourceRequestEnvironment.ResourceRequest;%0Aimport org.jboss.seam.servlet.ServletSessionMap
 ;%0Aim
@@ -1918,32 +1918,115 @@
 r.setupClass();%0A
+      this.clearHibernateCache(); // Clear the Hibernate cache after initial setup%0A
    %7D%0A%0A   @Before
@@ -2145,32 +2145,134 @@
 taBeforeTest();%0A
+      this.clearHibernateCache(); // Clear the hibernate cache after data is modified before the test%0A
    %7D%0A%0A   @AfterM
@@ -3118,16 +3118,16 @@
 );%0A   %7D%0A
-
    // en
@@ -3158,12 +3158,1420 @@
 SeamTest
+%0A   %0A   /**%0A    * Clears the Hibernate Cache.%0A    * This method starts a Test lifecycle just for the purpose of clearing the Hibernate session factory's%0A    * second level cache.%0A    * TODO Maybe there is a way of doing this without intruding into TestNGs internals.%0A    */%0A   protected void clearHibernateCache()%0A   %7B%0A      TestLifecycle.beginTest(servletContext, new ServletSessionMap(new MockHttpSession(servletContext)));%0A      try%0A      %7B%0A         Session session = (Session)getInstance(%22session%22);%0A         SessionFactory sessionFactory = session.getSessionFactory();%0A         %0A         // Clear the Entity cache%0A         Map classMetadata = sessionFactory.getAllClassMetadata();%0A         for( Object obj : classMetadata.values() )%0A         %7B%0A            EntityPersister p = (EntityPersister)obj;%0A            if( p.hasCache() )%0A            %7B%0A               sessionFactory.evictEntity( p.getEntityName() );%0A            %7D%0A         %7D%0A         %0A         // Clear the Collection cache%0A         Map collMetadata = sessionFactory.getAllCollectionMetadata();%0A         for( Object obj : collMetadata.values() )%0A         %7B%0A            AbstractCollectionPersister p = (AbstractCollectionPersister)obj;%0A            if( p.hasCache() )%0A            %7B%0A               sessionFactory.evictCollection( p.getRole() );%0A            %7D%0A         %7D%0A      %7D%0A      finally%0A      %7B%0A         TestLifecycle.endTest();%0A      %7D%0A   %7D   
 %0A%0A%7D%0A

Add test listener to clear hibernate cache between test executions.