package org.jboss.cache.aop.eviction;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.Fqn;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.aop.AOPInstance;
import org.jboss.cache.aop.InternalDelegate;
import org.jboss.cache.aop.PojoCache;

/**
 * @author Ben Wang, Feb 11, 2004
 * @version $Revision: 1838 $
 */
public class AopLRUPolicyTest extends TestCase
{
   PojoCache cache_;
   int wakeupIntervalMillis_ = 0;

   public AopLRUPolicyTest(String s)
   {
      super(s);
   }

   public void setUp() throws Exception
   {
      super.setUp();
      initCaches();
      wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() * 1000;
      log("wakeupInterval is " + wakeupIntervalMillis_);
      if (wakeupIntervalMillis_ <= 0)
         fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);

   }

   void initCaches() throws Exception
   {
//      sleep(10000);
      cache_ = new PojoCache();
      PropertyConfigurator config = new PropertyConfigurator();
      config.configure(cache_, "META-INF/pojocache-service.xml"); // read in generic local xml
      cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
      cache_.startService();
   }

   public void tearDown() throws Exception
   {
      super.tearDown();
      cache_.stopService();
   }

   public void testDummy()
   {
      // nothing
   }

   /**
    * We are disabling these tests now becuase of the switch to EvictionInterceptor makes these
    * tests obslete.
    */
   public void XtestSimpleEviction()
   {
      String rootStr = "/aop/";
      AOPInstance aop = new AOPInstance();
      Fqn internalFqn = new Fqn(InternalDelegate.JBOSS_INTERNAL, "test");
      try
      {
         for (int i = 0; i < 4; i++)
         {
            String stri = rootStr + i;
            Fqn fqni = Fqn.fromString(stri);
            cache_.put(fqni, stri, stri);
            cache_.put(fqni, AOPInstance.KEY, aop);   // signals that this is an aop node.
            cache_.put(fqni, InternalDelegate.CLASS_INTERNAL, String.class);   // signals that this is an aop node.
            cache_.put(internalFqn, "test", "test"); // this will always be around since it is internal.

            for (int j = 0; j < 3; j++)
            {
               String strj = stri + "/" + j;
               Fqn fqnj = Fqn.fromString(strj);
               cache_.put(fqnj, strj, strj);
            }
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
         fail("Failed to insert data" + e);
      }

      int period = (wakeupIntervalMillis_ + 500);
      log("period is " + period);
      TestingUtil.sleepThread(period);  // it really depends on the eviction thread time.

      try
      {
         String str = rootStr + "0";
         String val = (String) cache_.get(Fqn.fromString(str), str);
         assertNull("DataNode should be empty ", val);
         str = rootStr + "3";
         val = (String) cache_.get(Fqn.fromString(str), str);
         assertNotNull("DataNode should not be empty if maxElements is 4 ", val);
      }
      catch (Exception e)
      {
         e.printStackTrace();
         fail("Failed to evict" + e);
      }
   }

   public void XtestRemoveEviction()
   {
      String rootStr = "/aop/";
      AOPInstance aop = new AOPInstance();
      try
      {
         for (int i = 0; i < 4; i++)
         {
            String stri = rootStr + i;
            Fqn fqni = Fqn.fromString(stri);
            cache_.put(fqni, stri, stri);
            cache_.put(fqni, AOPInstance.KEY, aop);   // signals that this is an aop node.
            for (int j = 0; j < 2; j++)
            {
               String strj = stri + "/" + j;
               Fqn fqnj = Fqn.fromString(strj);
               cache_.put(fqnj, strj, strj);
            }
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
         fail("Failed to insert data" + e);
      }

      int period = (wakeupIntervalMillis_ + 500);
      log("period is " + period);
      TestingUtil.sleepThread(period);  // it really depends on the eviction thread time.
      String str = rootStr + "3";
      Fqn fqn = Fqn.fromString(str);
      try
      {
         cache_.get(fqn, str);   // just to keep it fresh
         TestingUtil.sleepThread(period);  // it really depends the eviction thread time.
         cache_.get(fqn, str);   // just to keep it fresh
         TestingUtil.sleepThread(period);  // it really depends the eviction thread time.
         String val = (String) cache_.get(rootStr + "3/1", rootStr + "3/1");
         assertNotNull("DataNode should not be empty ", val);
         cache_.remove(rootStr + "3");
         val = (String) cache_.get(rootStr + "3", rootStr + "3");
         assertNull("DataNode should be empty ", val);
         TestingUtil.sleepThread(wakeupIntervalMillis_ + 500);
      }
      catch (Exception e)
      {
         e.printStackTrace();
         fail("Failed to evict" + e);
      }
   }

   void log(String msg)
   {
      System.out.println("-- " + msg);
   }

   public static Test suite()
   {
      return new TestSuite(AopLRUPolicyTest.class);
   }

   public static void main(String[] args)
   {
      junit.textui.TestRunner.run(suite());
   }

}
