/*
 * 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.ConfigureException;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;

/**
 * Unit tests for LRUConfiguration.
 *
 * @author Daniel Huang (dhuang@jboss.org)
 * @version $Revision: 916 $
 */
public class LRUConfigurationTest extends TestCase
{

   public void testXMLParsing() throws Exception
   {
      LRUConfiguration config = new LRUConfiguration();
      String xml =
            "<region name=\"/org/jboss/data\">\n" +
                  "<attribute name=\"maxNodes\">5000</attribute>\n" +
                  "<attribute name=\"timeToLiveSeconds\">1000</attribute>\n" +
                  "</region>";

      Element element = XmlHelper.stringToElement(xml);
      config.parseXMLConfig(element);

      assertEquals(5000, config.getMaxNodes());
      assertEquals(1000, config.getTimeToLiveSeconds());
   }

   public void testXMLParsing2() throws Exception
   {
      LRUConfiguration config = new LRUConfiguration();
      String xml = "<region name=\"/maxAgeTest/\">\n" +
            "<attribute name=\"maxNodes\">10000</attribute>\n" +
            "<attribute name=\"timeToLiveSeconds\">8</attribute>\n" +
            "<attribute name=\"maxAgeSeconds\">10</attribute>\n" +
            "</region>";
      Element element = XmlHelper.stringToElement(xml);
      config.parseXMLConfig(element);

      assertEquals(10000, config.getMaxNodes());
      assertEquals(8, config.getTimeToLiveSeconds());
      assertEquals(10, config.getMaxAgeSeconds());
   }

   public void testXMLParsing3() throws Exception
   {
      LRUConfiguration config = new LRUConfiguration();
      String xml = "<region name=\"/maxAgeTest/\">\n" +
            "<attribute name=\"maxNodes\">10000</attribute>\n" +
            "<attribute name=\"maxAgeSeconds\">10</attribute>\n" +
            "</region>";
      Element element = XmlHelper.stringToElement(xml);
      boolean caught = false;
      try
      {
         config.parseXMLConfig(element);
      }
      catch (ConfigureException ce)
      {
         caught = true;
      }
      assertTrue("Configure exception should have been caught", caught);

      xml = "<region name=\"/maxAgeTest/\">\n" +
            "<attribute name=\"timeToLiveSeconds\">8</attribute>\n" +
            "<attribute name=\"maxAgeSeconds\">10</attribute>\n" +
            "</region>";

      element = XmlHelper.stringToElement(xml);

      config.parseXMLConfig(element);

      assertEquals(0, config.getMaxNodes());
   }
}
