/*
 * 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.Test;
import junit.framework.TestSuite;
import org.jboss.cache.Fqn;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.TreeCacheMBean;
import org.jboss.cache.config.Option;
import org.jboss.cache.loader.DummyInMemoryCacheLoader;
import org.jboss.cache.loader.AbstractCacheLoaderTestBase;
import org.jboss.cache.misc.TestingUtil;

/**
 * Test for http://jira.jboss.com/jira/browse/JBCACHE-1139.
 * 
 * Issue is not really specific to passivation. I wrote the test before the exact
 * scope of JBCACHE-1139 was clear. But, it's a valid test, so no reason
 * to discard it.
 *
 * @author Brian Stansberry
 * @version $Revision: 4141 $
 */
public class OptionSurvivalTest extends AbstractCacheLoaderTestBase
{
   private TreeCache cache0;
   private TreeCache cache1;
   private int wakeupIntervalMillis_ = 0;

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

   public void setUp() throws Exception
   {
      super.setUp();
      cache0 = createCache();
      cache1 = createCache();
      wakeupIntervalMillis_ = cache0.getEvictionThreadWakeupIntervalSeconds() * 1000;
      if (wakeupIntervalMillis_ < 0)
         fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
      TestingUtil.blockUntilViewsReceived(new TreeCacheMBean[] { cache0, cache1}, 5000);
   }

   private TreeCache createCache() throws Exception
   {
      TreeCache cache = new TreeCache();
      PropertyConfigurator config = new PropertyConfigurator();
      config.configure(cache, "META-INF/local-passivation-service.xml"); // read in generic local xml
      cache.setCacheMode("REPL_SYNC");
      cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
      cache.setCacheLoaderConfiguration(getSingleCacheLoaderConfig(true, "/", DummyInMemoryCacheLoader.class.getName(), null, false, true, false, false));
      cache.startService();
      return cache;
   }
   
   public void tearDown() throws Exception
   {
      super.tearDown();
      cache0.stop();
      cache0 = null;
      cache1.stop();
      cache1 = null;
   }

   /**
    * Confirm that a "cacheModeLocal=true" survives the cache loader.
    * 
    * @throws Exception
    */
   public void testOptionSurvival() throws Exception
   {
      Fqn fqn = Fqn.fromString("/org/jboss/test");
      
      Option opt = new Option();
      opt.setCacheModeLocal(true);
      cache0.put(fqn, "test", "test", opt);
      
      assertNull(cache1.get(fqn, "test"));
   }

   public static Test suite()
   {
      return new TestSuite(org.jboss.cache.passivation.OptionSurvivalTest.class);
   }

   public static void main(String[] args)
   {
      junit.textui.TestRunner.run(org.jboss.cache.passivation.OptionSurvivalTest.suite());
   }
}
