[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/eviction ...

Ben Wang bwang at jboss.com
Mon Oct 9 22:46:36 EDT 2006


  User: bwang   
  Date: 06/10/09 22:46:36

  Added:       tests/functional/org/jboss/cache/eviction 
                        ProgrammaticLRUPolicyTest.java
  Log:
  Progrmmatic eviction policy test case
  
  Revision  Changes    Path
  1.1      date: 2006/10/10 02:46:36;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/eviction/ProgrammaticLRUPolicyTest.java
  
  Index: ProgrammaticLRUPolicyTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  
  package org.jboss.cache.eviction;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.jboss.cache.TreeCache;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.xml.XmlHelper;
  import org.jboss.cache.misc.TestingUtil;
  import org.jboss.cache.lock.IsolationLevel;
  import org.jboss.cache.factories.XmlConfigurationParser;
  import org.w3c.dom.Element;
  
  /**
   * Unit tests for programmatic configuration of LRU policy
   *
   * @author Ben Wang, Oct, 2006
   * @version $Revision: 1.1 $
   */
  public class ProgrammaticLRUPolicyTest extends TestCase
  {
     TreeCache cache_;
     int wakeupIntervalMillis_ = 0;
  
     public ProgrammaticLRUPolicyTest(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_);
  
     }
  
     public void initCaches() throws Exception
     {
        cache_ = new TreeCache();
        cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-lru-eviction-service.xml")); // read in generic local xml
        cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        cache_.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
  
        cache_.create();
        cache_.start();
     }
  
     public void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     private void addStringBasedRegion() throws Exception
     {
        // region name is ignored here.
        String xml = "<region name=\"/dummy\">" +
              "<attribute name=\"maxNodes\">10000</attribute>" +
              "<attribute name=\"timeToLiveSeconds\">4</attribute>" +
              "</region>";
        Element element = XmlHelper.stringToElement(xml);
        RegionManager regionManager = cache_.getEvictionRegionManager();
        // Fqn is the region name
        Fqn fqn = Fqn.fromString("/programmatic");
        Region region = regionManager.createRegion("/programmatic", element);
     }
  
     public void testStringBasedFqnEviction() throws Exception
     {
        addStringBasedRegion();
  
        String rootStr = "/programmatic/";
        for (int i = 0; i < 10; i++)
        {
           String str = rootStr + i;
           Fqn fqn = Fqn.fromString(str);
           try
           {
              cache_.put(fqn, str, str);
           }
           catch (Exception e)
           {
              fail("Failed to insert data" + e);
              e.printStackTrace();
           }
        }
  
        try
        {
           String val = (String) cache_.get(rootStr + "3", rootStr + "3");
           assertNotNull("DataNode should be empty ", val);
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Failed to get" + e);
        }
  
        System.out.println(cache_.toString());
        TestingUtil.sleepThread(2*wakeupIntervalMillis_ + 500);
        System.out.println(cache_.toString());
        try
        {
           String val = (String) cache_.get(rootStr + "3", rootStr + "3");
           assertNull("DataNode should be empty ", val);
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Failed to get" + e);
        }
     }
  
     private void addObjectBasedRegion() throws Exception
     {
        // region name is ignored here.
        String xml = "<region name=\"/dummy\">" +
              "<attribute name=\"maxNodes\">10000</attribute>" +
              "<attribute name=\"timeToLiveSeconds\">4</attribute>" +
              "</region>";
        Element element = XmlHelper.stringToElement(xml);
        RegionManager regionManager = cache_.getEvictionRegionManager();
        // Fqn is the region name
        Integer ii = new Integer(1);
        Fqn fqn = new Fqn(ii);
        Region region = regionManager.createRegion(fqn, element);
     }
  
     public void testObjectBasedFqnEviction1() throws Exception
     {
        addStringBasedRegion();
  
        String rootStr = "/programmatic/";
        for (int i = 0; i < 10; i++)
        {
           String str = rootStr;
           Integer in = new Integer(i);
           Fqn fqn = new Fqn(Fqn.fromString(str), in);
           try
           {
              cache_.put(fqn, str, str);
           }
           catch (Exception e)
           {
              fail("Failed to insert data" + e);
              e.printStackTrace();
           }
        }
  
        Integer in = new Integer(3);
        Fqn fqn = new Fqn(Fqn.fromString(rootStr), in);
        try
        {
           String val = (String) cache_.get(fqn, in);
           assertNull("DataNode should be empty ", val);
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Failed to get" + e);
        }
  
        System.out.println(cache_.toString());
        TestingUtil.sleepThread(2*wakeupIntervalMillis_ + 500);
        System.out.println(cache_.toString());
  
        try
        {
           String val = (String) cache_.get(fqn, in);
           assertNull("DataNode should be empty ", val);
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Failed to get" + e);
        }
     }
  
     public void testObjectBasedFqnEviction2() throws Exception
     {
        addObjectBasedRegion();
  
        Integer ii = new Integer(1);
        Fqn rootfqn = new Fqn(ii);
        for (int i = 0; i < 10; i++)
        {
           Integer in = new Integer(i);
           Fqn fqn = new Fqn(rootfqn, in);
           try
           {
              cache_.put(fqn, in, in);
           }
           catch (Exception e)
           {
              fail("Failed to insert data" + e);
              e.printStackTrace();
           }
        }
  
        try
        {
           Integer in = new Integer(3);
           Fqn fqn = new Fqn(rootfqn, in);
           Object val = cache_.get(fqn, in);
           assertNotNull("DataNode should be empty ", val);
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Failed to get" + e);
        }
  
        System.out.println(cache_.toString());
        TestingUtil.sleepThread(2*wakeupIntervalMillis_ + 500);
        System.out.println(cache_.toString());
        try
        {
           Integer in = new Integer(3);
           Fqn fqn = new Fqn(rootfqn, in);
           String val = (String) cache_.get(fqn, in);
           assertNull("DataNode should be empty ", val);
        }
        catch (Exception e)
        {
           e.printStackTrace();
           fail("Failed to get" + e);
        }
     }
  
     void log(String msg)
     {
        System.out.println("-- " + msg);
     }
  
     public static Test suite()
     {
        return new TestSuite(ProgrammaticLRUPolicyTest.class);
     }
  
     public static void main(String[] args)
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list