/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.aop.eviction;

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

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

   public AopLRUPolicyUpdateEvictionTest(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/local-aop-eviction-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 testUpdateEviction()
   {
      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.
            cache_.put(fqni, InternalDelegate.CLASS_INTERNAL, String.class);   // 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);
         TestingUtil.sleepThread(period);
         val = (String) cache_.get(rootStr + "3", rootStr + "3");
         assertNull("Known failure.  See JBCACHE-462; DataNode should be empty. But this is broken because of TreeCache._removeData() has disabled " +
               "sendNodeEvent. See the FIXME. ", 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(AopLRUPolicyUpdateEvictionTest.class);
   }

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

}
