/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.cache.passivation;

import junit.framework.TestCase;
import org.jboss.cache.TreeCache;
import org.jboss.cache.TreeCacheListener;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.Fqn;
import org.jboss.cache.ExtendedTreeCacheListener;
import org.jboss.cache.misc.TestingUtil;
import org.jgroups.View;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.logging.Logger;

/**
 * @author Ben Wang, Feb 11, 2004
 */
public class LocalPassivationIntegrationTest extends TestCase
{
   TreeCache cache_;
   protected final static Log log = LogFactory.getLog(LocalPassivationIntegrationTest.class);
   int wakeupIntervalMillis_ = 0;
   PassivationListener listener_ ;

   public LocalPassivationIntegrationTest(String s)
   {
      super(s);
      listener_ = new PassivationListener();
   }

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

      cache_.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/local-passivation-service.xml"); // read in generic local xml
      cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
   }

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

   /**
    */
   public void testActivationEvent() throws Exception {
     String rootStr = "/org/jboss/test/data/";
     String str = rootStr +"0";
     cache_.remove("/");
     listener_.resetCounter();

     cache_.put(str, str, str);

     TestingUtil.sleepThread(20000);
     assertFalse("Node should not exist", cache_.exists(str, str));
     String val = (String)cache_.get(str, str);
     assertNotNull("DataNode should be activated ", val);
     assertEquals("Eviction counter ", 1, listener_.getCounter());
   }

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

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

      public int getCounter()
      { return counter; }

      public void resetCounter()
      {
         counter = 0;
      }

      public void nodeEvict(Fqn fqn, boolean pre) {
      }

      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) {
         if(!pre)
         {
            counter++;
            log.info("nodeActivate(): counter: " +counter);
         }
      }

      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.
      }
   }
}
