package org.jboss.cache.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.TreeCache;
import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.aop.AOPInstance;
import org.jboss.cache.aop.InternalDelegate;

/**
 * @author Ben Wang, Feb 11, 2004
 */
public class ReplicatedAopLRUPolicyTest extends TestCase
{
   TreeCache cache_, cache1_, cache2_;
   int wakeupIntervalMillis_ = 0;

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

   public void setUp() throws Exception
   {
      super.setUp();
      cache_ = new TreeCache();
      cache_.setUseMarshalling(true);
      initCaches(cache_);
//      cache1_ = new TreeCache();
//      initCaches(cache1_);
      cache_.startService();
//      cache1_.startService();
      // cache2 doesn't have eviction policy
      cache2_ = new TreeCache();
      PropertyConfigurator config = new PropertyConfigurator();
      config.configure(cache2_, "META-INF/replSync-service.xml"); // read in generic local xml
      cache2_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
      cache2_.startService();

      wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() *1000;
      log("wakeupInterval is " +wakeupIntervalMillis_);
      if(wakeupIntervalMillis_ <=0)
         fail("testEviction(): eviction thread wake up interval is illegal " +wakeupIntervalMillis_);
   }

   void initCaches(TreeCache cache) throws Exception
   {
      PropertyConfigurator config = new PropertyConfigurator();
      config.configure(cache, "META-INF/replSync-eviction-service.xml"); // read in generic local xml
      cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
   }

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

   /**
    * Test local eviction policy
    */
   public void testEviction() {
      String rootStr = "/org/jboss/test/data/";
      for(int i=0; i < 10; i++) {
         String str = rootStr +i;
         Fqn fqn = Fqn.fromString(str);
         try {
            cache_.put(fqn, str, str);
         } catch (Exception e) {
            fail("Failed to insert data" +e);
            e.printStackTrace();
         }
      }

      TestingUtil.sleepThread(wakeupIntervalMillis_ + 1000);
      try {
         String val = (String)cache_.get(rootStr +"3", rootStr+"3");
         assertNull("DataNode should be evicted already ", val);
         val = (String)cache2_.get(rootStr +"3", rootStr+"3");
         assertNotNull("DataNode should not be evicted here ", val);
      } catch (Exception e) {
         e.printStackTrace();
         fail("Failed to evict" +e);
      }
   }

   public void testEvictionReplication() {
      String rootStr = "/org/jboss/test/data/";
      for(int i=0; i < 10; i++) {
         String str = rootStr +i;
         Fqn fqn = Fqn.fromString(str);
         try {
            cache_.put(fqn, str, str);
         } catch (Exception e) {
            fail("Failed to insert data" +e);
            e.printStackTrace();
         }
      }

      TestingUtil.sleepThread(wakeupIntervalMillis_ -1000);
      String str = rootStr +"7";
      Fqn fqn = Fqn.fromString(str);
      try {
         cache_.get(fqn, str);
      } catch (Exception e) {
         fail("Failed to get data. Exception: " +e);
      }
      TestingUtil.sleepThread(wakeupIntervalMillis_);

      try {
         String val = (String)cache_.get(rootStr +"3", rootStr+"3");
         assertNull("DataNode should be empty ", val);
         val = (String)cache2_.get(rootStr +"7", rootStr+"7");
         assertNotNull("DataNode should not be null", val);
      } catch (Exception e) {
         e.printStackTrace();
         fail("Failed to evict" +e);
      }
   }

   public void testPojoEviction() {
      String rootStr = "/org/jboss/test/data/";
      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.
            cache_.put(fqni, InternalDelegate.CLASS_INTERNAL, String.class);   // signals that this is an aop node.
            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_ +1);
      log("period is " +period);
      TestingUtil.sleepThread(period);  // it really depends on the eviction thread time.

      try {
         String str = rootStr + "0";
         String val = (String)cache2_.get(Fqn.fromString(str), str);
         assertNotNull("DataNode should not be empty ", val);
         str = rootStr + "3";
         val = (String)cache2_.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);
      }

      period = (wakeupIntervalMillis_ +1000);
      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);
      } 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(ReplicatedAopLRUPolicyTest.class);
   }

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

}
