/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.eviction;

import junit.framework.TestCase;
import org.jboss.cache.Fqn;

/**
 * Unit tests for MRUAlgorithm.
 *
 * @author Daniel Huang (dhuang@jboss.org)
 * @version $Revision: 1.2 $
 */
public class MRUAlgorithmTest extends TestCase
{
   MRUAlgorithm algorithm;
   RegionManager regionManager;

   public void setUp() throws Exception
   {
      super.setUp();
      algorithm = new MRUAlgorithm();
      MRUConfiguration config = new MRUConfiguration();
      regionManager = new RegionManager();
      regionManager.createRegion("/a/b", new DummyEvictionPolicy(), config);
   }

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

   public void testMaxNodes() throws Exception
   {
      Fqn fqn1 = Fqn.fromString("/a/b/c");
      Fqn fqn2 = Fqn.fromString("/a/b/d");
      Fqn fqn3 = Fqn.fromString("/a/b/e");
      Region region = regionManager.getRegion("/a/b");
      MRUConfiguration config = (MRUConfiguration) region.getEvictionConfiguration();
      config.setMaxNodes(1);
      region.setAddedNode(fqn1);
      region.setAddedNode(fqn2);
      region.setAddedNode(fqn3);

      algorithm.process(region);

      assertEquals(1, algorithm.getEvictionQueue().getNumberOfNodes());

      config.setMaxNodes(100);
      for (int i = 0; i < 150; i++)
      {
         Fqn fqn = Fqn.fromString("/a/b/c/" + Integer.toString(i));
         region.setAddedNode(fqn);
      }

      algorithm.process(region);

      assertEquals(100, algorithm.getEvictionQueue().getNumberOfNodes());
   }

   public void testMRU() throws Exception
   {
      Fqn fqn1 = Fqn.fromString("/a/b/c");
      Fqn fqn2 = Fqn.fromString("/a/b/d");
      Fqn fqn3 = Fqn.fromString("/a/b/e");
      Fqn fqn4 = Fqn.fromString("/a/b/f");
      Fqn fqn5 = Fqn.fromString("/a/b/g");
      Fqn fqn6 = Fqn.fromString("/a/b/h");
      Fqn fqn7 = Fqn.fromString("/a/b/i");
      Fqn fqn8 = Fqn.fromString("/a/b/j");
      Fqn fqn9 = Fqn.fromString("/a/b/k");
      Fqn fqn10 = Fqn.fromString("/a/b/l");
      Region region = regionManager.getRegion("/a/b");
      MRUConfiguration config = (MRUConfiguration) region.getEvictionConfiguration();
      config.setMaxNodes(8);
      region.setAddedNode(fqn1);
      region.setAddedNode(fqn2);
      region.setAddedNode(fqn3);
      region.setAddedNode(fqn4);
      region.setAddedNode(fqn5);
      region.setAddedNode(fqn6);
      region.setAddedNode(fqn7);
      region.setAddedNode(fqn8);

      algorithm.process(region);

      assertEquals(8, algorithm.getEvictionQueue().getNumberOfNodes());

      region.setAddedNode(fqn9);
      region.setAddedNode(fqn10);

      Thread.sleep(5000);

      region.setVisitedNode(fqn2);
      region.setVisitedNode(fqn4);

      algorithm.process(region);

      assertEquals(8, algorithm.getEvictionQueue().getNumberOfNodes());

      assertNull(algorithm.getEvictionQueue().getNodeEntry(fqn2));
      assertNull(algorithm.getEvictionQueue().getNodeEntry(fqn4));

      assertNotNull(algorithm.getEvictionQueue().getNodeEntry(fqn1));
      assertNotNull(algorithm.getEvictionQueue().getNodeEntry(fqn3));
      assertNotNull(algorithm.getEvictionQueue().getNodeEntry(fqn5));
      assertNotNull(algorithm.getEvictionQueue().getNodeEntry(fqn6));
      assertNotNull(algorithm.getEvictionQueue().getNodeEntry(fqn7));
      assertNotNull(algorithm.getEvictionQueue().getNodeEntry(fqn8));
      assertNotNull(algorithm.getEvictionQueue().getNodeEntry(fqn9));
      assertNotNull(algorithm.getEvictionQueue().getNodeEntry(fqn10));
   }

}
