package org.jboss.cache.eviction;

import junit.framework.TestCase;
import org.jboss.cache.Fqn;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.ExtendedTreeCacheListener;
import org.jboss.cache.TreeCacheListener;
import org.jboss.cache.misc.TestingUtil;
import org.jgroups.View;

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

   public ReplicatedLRUPolicyTest(String s)
   {
      super(s);
      listener_ = new EvictionListener();
   }

   public void setUp() throws Exception
   {
      super.setUp();
      cache_ = new TreeCache();
      initCaches(cache_);
//      cache1_ = new TreeCache();
//      initCaches(cache1_);
      cache_.setUseMarshalling(true);
      cache_.addTreeCacheListener((TreeCacheListener)listener_);
      listener_.resetCounter();

      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_.setUseMarshalling(true);
      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 that failed for eviction event.
    */
   public void testBasic() throws Exception {
     String rootStr = "/org/jboss/test/data/";
     String str = rootStr +"0";
     cache_.put(str, str, str);

      TestingUtil.sleepThread(30000);
      String val = (String)cache_.get(str, str);
      assertNull("DataNode should be evicted already ", val);
      assertEquals("Eviction counter ", 1, listener_.getCounter());
      val = (String)cache2_.get(str, str);
      assertNotNull("DataNode should not be evicted here ", val);
      assertEquals("Eviction counter ", 1, listener_.getCounter());
   }

   public void testEviction() throws Exception {
      String rootStr = "/org/jboss/test/data/";
      for(int i=0; i < 10; i++) {
         String str = rootStr +i;
         Fqn fqn = Fqn.fromString(str);
         cache_.put(fqn, str, str);
      }

      TestingUtil.sleepThread(2 *wakeupIntervalMillis_);
      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);
   }

   public void testEvictionReplication() throws Exception {
      String rootStr = "/org/jboss/test/data/";
      for(int i=0; i < 10; i++) {
         String str = rootStr +i;
         Fqn fqn = Fqn.fromString(str);
         cache_.put(fqn, str, str);
      }

      TestingUtil.sleepThread(wakeupIntervalMillis_ -1000);
      String str = rootStr +"7";
      Fqn fqn = Fqn.fromString(str);
      cache_.get(fqn, str);
      TestingUtil.sleepThread(wakeupIntervalMillis_);

      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);
   }

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

   class EvictionListener implements ExtendedTreeCacheListener, TreeCacheListener
   {
      int counter = 0;

      public int getCounter()
      { return counter; }

      public void resetCounter()
      {
         counter = 0;
      }

      public void nodeEvict(Fqn fqn, boolean pre) {
         if(pre)
            counter++;
      }

      public void nodeRemove(Fqn fqn, boolean pre, boolean isLocal) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodeModify(Fqn fqn, boolean pre, boolean isLocal) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodeActivate(Fqn fqn, boolean pre) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodePassivate(Fqn fqn, boolean pre) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodeCreated(Fqn fqn) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodeRemoved(Fqn fqn) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodeLoaded(Fqn fqn) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodeEvicted(Fqn fqn) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodeModified(Fqn fqn) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void nodeVisited(Fqn fqn) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void cacheStarted(TreeCache cache) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void cacheStopped(TreeCache cache) {
         //To change body of implemented methods use File | Settings | File Templates.
      }

      public void viewChange(View new_view)  // might be MergeView after merging
      {
         //To change body of implemented methods use File | Settings | File Templates.
      }
   }
}
