[jboss-cvs] JBossCache/old/tests/functional/org/jboss/cache/aop/loader ...

Ben Wang bwang at jboss.com
Tue Oct 31 03:01:12 EST 2006


  User: bwang   
  Date: 06/10/31 03:01:12

  Added:       old/tests/functional/org/jboss/cache/aop/loader       
                        CacheLoaderTestsBase.java
                        FileCacheLoaderAopCollectionsTest.java
                        AbstractCacheLoaderTestBase.java
                        FileCacheLoaderAopTest.java
                        BdbjeCacheLoaderTest.java
                        FileCacheLoaderObjectGraphAopTest.java
                        ObjectGraphTestsBase.java
  Log:
  Deprecated files moved to old dir.
  
  Revision  Changes    Path
  1.1      date: 2006/10/31 08:01:12;  author: bwang;  state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/loader/CacheLoaderTestsBase.java
  
  Index: CacheLoaderTestsBase.java
  ===================================================================
  package org.jboss.cache.aop.loader;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.aop.PojoCache;
  import org.jboss.cache.aop.test.Person;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.loader.CacheLoader;
  
  import javax.transaction.Transaction;
  
  /**
   * Commons tests for all CacheLoaders. Aop version.
   *
   * @author bwang
   * @version $Id: CacheLoaderTestsBase.java,v 1.1 2006/10/31 08:01:12 bwang Exp $
   */
  abstract public class CacheLoaderTestsBase extends AbstractCacheLoaderTestBase
  {
     PojoCache cache;
     CacheLoader loader = null;
     Transaction tx = null;
     static final Fqn FQN = new Fqn("key");
  
     protected void setUp() throws Exception
     {
        super.setUp();
        cache = new PojoCache();
        Configuration c = new Configuration();
        cache.setConfiguration(c);
        c.setCacheMode("local");
        configureCache();
        c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        cache.create();
        cache.start();
        loader = cache.getCacheLoader();
     }
  
     abstract protected void configureCache() throws Exception;
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        if (tx != null)
        {
           try
           {
              tx.commit();
           }
           catch (Throwable e)
           {
              e.printStackTrace();
           }
        }
        cache.remove("/");
        loader.remove(Fqn.fromString("/"));
        cache.stop();
        cache.destroy();
     }
  
     protected void addDelay()
     {
        ; // returns immediately in this case.  Subclasses may override where a delay is needed.
     }
  
     public void testEviction() throws Exception
     {
        cache.putObject("/first/second/third", "val1");   // stored in cache loader
        cache.evict(Fqn.fromString("/first/second/third"));  // removes node, because there are no children
        addDelay();
        assertFalse(cache.exists("/first/second/third"));
        assertTrue(cache.exists("/first/second"));
        assertTrue(cache.exists("/first"));
        String val = (String) cache.getObject("/first/second/third"); // should be loaded from cache loader
        assertEquals("val1", val);
        assertTrue(cache.exists("/first/second/third"));
        assertTrue(cache.exists("/first/second"));
        assertTrue(cache.exists("/first"));
     }
  
  
     public void testPojoEviction() throws Exception
     {
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache.putObject("/first/second/third", test);   // stored in cache loader
        cache.evict(Fqn.fromString("/first/second/third"));  // removes node, because there are no children
        addDelay();
        assertFalse(cache.exists("/first/second/third"));
        assertTrue(cache.exists("/first/second"));
        assertTrue(cache.exists("/first"));
        Person val = (Person) cache.getObject("/first/second/third"); // should be loaded from cache loader
        assertNotNull(val);
        // This will use both original the new pojo to get.
        assertEquals(test.getName(), val.getName());
        assertTrue(cache.exists("/first/second/third"));
        assertTrue(cache.exists("/first/second"));
        assertTrue(cache.exists("/first"));
     }
  
     public void testPojoRemove() throws Exception
     {
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache.putObject("/first/second/third", test);   // stored in cache loader
        cache.remove(Fqn.fromString("/first/second/third"));  // removes node, because there are no children
        addDelay();
        assertFalse(cache.exists("/first/second/third"));
        assertTrue(cache.exists("/first/second"));
        assertTrue(cache.exists("/first"));
        Person val = (Person) cache.getObject("/first/second/third"); // should be loaded from cache loader
        assertNull(val);
     }
  
     /**
      * This test illustrates the problem in JBCACHE-477. That is transactional remove under
      * cacheloader using pessimistic locking.
      */
     public void testTxCollectionRemove() throws Exception
     {
        /*
        Person joe = new Person();
        joe.setName("Joe");
        joe.setAge(10);
        Person ben = new Person();
        ben.setName("Ben");
        ben.setAge(10);
  
        ArrayList list =new ArrayList();
        list.add(joe);
        list.add(ben);
  
        cache.putObject("/list", list);   // stored in cache loader
        list = (ArrayList)cache.getObject("/list");
  
        DummyTransactionManager mgr=DummyTransactionManager.getInstance();
        try {
           mgr.begin();
           tx=mgr.getTransaction();
           list.remove(joe);
        } catch (Exception ex)
        {
          fail("This is currently a known problem (JBCACHE-477) that we need to fix.");
        } finally
        {
           tx.commit();
        }
        */
        // do nothing: override the test which fails.
        // see JBCACHE-477
     }
  
     public void testCollection() throws Exception
     {
        /*
        Map map = new HashMap();
        map.put("1", "1");
        map.put("2", "2");
  
        cache.putObject("/test/map", map);
        map = (Map)cache.getObject("/test/map");
        map.put("3", "3");
        assertEquals("Map size ", 3, map.size());
  
        cache.evict(Fqn.fromString("/test/map"));
        addDelay();
        map = (Map)cache.getObject("/test/map");
        assertEquals("Map 1", "1", map.get("1"));
        assertEquals("Map 2", "2", map.get("2"));
  //      assertEquals("Map 3", "3", map.get("3"));
        assertEquals("Known Failure - see JBCACHE-479 - Map size is not correct now. This is a known bug because of cacheloader does not load children that should be fixed in 1.3. ", 3, map.size());
        */
  
        // do nothing; override the test which fails.
        // See JBCACHE-479
  
     }
  
     public static Test suite()
     {
        return new TestSuite(CacheLoaderTestsBase.class);
     }
  
     public static void main(String[] args)
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  1.1      date: 2006/10/31 08:01:12;  author: bwang;  state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/loader/FileCacheLoaderAopCollectionsTest.java
  
  Index: FileCacheLoaderAopCollectionsTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.loader;
  
  import org.w3c.dom.Element;
  import org.jboss.cache.xml.XmlHelper;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   */
  public class FileCacheLoaderAopCollectionsTest extends CacheLoaderTestsBase {
  
  
      protected Element getCacheLoaderConfig(String loc) throws Exception
      {
          String xml = "            <config>\n" +
                  "                \n" +
                  "                <passivation>false</passivation>\n" +
                  "                <preload></preload>\n" +
                  "\n" +
                  "                <cacheloader>\n" +
                  "                    <class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                  "                    <properties>\n" +
                  "                        location=" + loc + "\n" +
                  "                    </properties>\n" +
                  "                    <async>false</async>\n" +
                  "                    <fetchPersistentState>false</fetchPersistentState>\n" +
                  "                    <ignoreModifications>false</ignoreModifications>\n" +
                  "                </cacheloader>\n" +
                  "                \n" +
                  "            </config>";
          return XmlHelper.stringToElement(xml);
      }
  
     protected void configureCache() throws Exception {
        String tmp_location = System.getProperty("java.io.tmpdir", "c:\\tmp");
        cache.getConfiguration().setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "", false, true, false));
  //       cache.setCacheLoaderConfiguration(getCacheLoaderConfiguration(tmp_location));
     }
  
     public static Test suite() {
        return new TestSuite(FileCacheLoaderAopCollectionsTest.class);
     }
  
     public static void main(String[] args) {
        junit.textui.TestRunner.run(FileCacheLoaderAopCollectionsTest.suite());
     }
  
     // leave in only the collectiosn test - override the rest.
  
     public void testEvictionWithCacheLoader()
     {
     }
  
     public void testPojoEvictionWithCacheLoader()
     {
     }
  
     public void testPojoRemoveWithCacheLoader()
     {
     }
  
  }
  
  
  
  1.1      date: 2006/10/31 08:01:12;  author: bwang;  state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/loader/AbstractCacheLoaderTestBase.java
  
  Index: AbstractCacheLoaderTestBase.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.loader;
  
  import junit.framework.TestCase;
  import org.w3c.dom.Element;
  import org.jboss.cache.xml.XmlHelper;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * Very basic test case that provides methods to create a cache loader config.
   *
   * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
   */
  public abstract class AbstractCacheLoaderTestBase extends TestCase
  {
  
     protected final Log log = LogFactory.getLog(getClass());
  
      protected Element getSingleCacheLoaderConfig(String preload, String cacheloaderClass, String properties, boolean async, boolean fetchPersistentState, boolean shared) throws Exception
      {
          String xml = "<config>\n" +
                  "<passivation>false</passivation>\n" +
                  "<preload>" + preload + "</preload>\n" +
                  "<cacheloader>\n" +
                  "<class>" + cacheloaderClass + "</class>\n" +
                  "<properties>" + properties + "</properties>\n" +
                  "<async>" + async + "</async>\n" +
                  "<shared>" + shared + "</shared>\n" + 
                  "<fetchPersistentState>" + fetchPersistentState + "</fetchPersistentState>\n" +
                  "</cacheloader>\n" +
                  "</config>";
          return XmlHelper.stringToElement(xml);
      }
  }
  
  
  
  1.1      date: 2006/10/31 08:01:12;  author: bwang;  state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/loader/FileCacheLoaderAopTest.java
  
  Index: FileCacheLoaderAopTest.java
  ===================================================================
  package org.jboss.cache.aop.loader;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.jboss.cache.xml.XmlHelper;
  import org.w3c.dom.Element;
  
  /**
   */
  public class FileCacheLoaderAopTest extends CacheLoaderTestsBase {
  
  
      protected Element getCacheLoaderConfig(String loc) throws Exception
      {
          String xml = "            <config>\n" +
                  "                \n" +
                  "                <passivation>false</passivation>\n" +
                  "                <preload></preload>\n" +
                  "\n" +
                  "                <cacheloader>\n" +
                  "                    <class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                  "                    <properties>\n" +
                  "                        location=" + loc + "\n" +
                  "                    </properties>\n" +
                  "                    <async>false</async>\n" +
                  "                    <fetchPersistentState>false</fetchPersistentState>\n" +
                  "                    <ignoreModifications>false</ignoreModifications>\n" +
                  "                </cacheloader>\n" +
                  "                \n" +
                  "            </config>";
          return XmlHelper.stringToElement(xml);
      }
  
     protected void configureCache() throws Exception {
         String tmp_location = System.getProperty("java.io.tmpdir", "c:\\tmp");
         cache.getConfiguration().setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "", false, true, false));
  //       cache.setCacheLoaderConfiguration(getCacheLoaderConfiguration(tmp_location));
     }
  
     public static Test suite() {
        return new TestSuite(FileCacheLoaderAopTest.class);
     }
  
     public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  1.1      date: 2006/10/31 08:01:12;  author: bwang;  state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/loader/BdbjeCacheLoaderTest.java
  
  Index: BdbjeCacheLoaderTest.java
  ===================================================================
  package org.jboss.cache.aop.loader;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import java.io.File;
  import java.io.FileFilter;
  
  /**
   * Runs the same tests as {@link org.jboss.cache.loader.FileCacheLoaderTest}, but with Berkeley DB instead of a file-based CacheLoader
   * @author Bela Ban
   * @version $Id: BdbjeCacheLoaderTest.java,v 1.1 2006/10/31 08:01:12 bwang Exp $
   */
  public class BdbjeCacheLoaderTest extends CacheLoaderTestsBase {
  
      private int counter = 0;
      private String tmp_location = System.getProperty("java.io.tmpdir", "c:\\tmp");
      private File dir = new File(tmp_location);
  
      public BdbjeCacheLoaderTest()
      {
          if (!dir.exists()) dir.mkdirs();
      }
  
      protected void configureCache() throws Exception {
  
        class MyFilter implements FileFilter {
           public boolean accept(File file) {
              return file.getName().endsWith(".jdb");
           }
        }
  
        File[] files=dir.listFiles(new MyFilter());
        if (files != null) {
           for (int i = 0; i < files.length; i += 1) {
              File file = files[i];
              if (file.isFile()) {
                 if (!file.delete()) {
                    System.err.println("Unable to delete: " + file);
                 }
              }
           }
        }
  
      cache.getConfiguration().setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.bdbje.BdbjeCacheLoader", "", false, true, false));
     }
  
     public static Test suite() {
        return new TestSuite(BdbjeCacheLoaderTest.class);
     }
  
      public static void main(String[] args)
      {
          junit.textui.TestRunner.run(suite());
      }
  
  }
  
  
  
  1.1      date: 2006/10/31 08:01:12;  author: bwang;  state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/loader/FileCacheLoaderObjectGraphAopTest.java
  
  Index: FileCacheLoaderObjectGraphAopTest.java
  ===================================================================
  package org.jboss.cache.aop.loader;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.jboss.cache.xml.XmlHelper;
  import org.w3c.dom.Element;
  
  /**
   */
  public class FileCacheLoaderObjectGraphAopTest extends ObjectGraphTestsBase {
     protected void configureCache() throws Exception {
        String tmp_location = System.getProperty("java.io.tmpdir", "c:\\tmp");
        cache.getConfiguration().setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "", false, true, false));
  //      cache.setCacheLoaderConfiguration(getCacheLoaderConfiguration(tmp_location));
     }
  
      protected Element getCacheLoaderConfig(String loc) throws Exception
      {
          String xml = "            <config>\n" +
                  "                \n" +
                  "                <passivation>false</passivation>\n" +
                  "                <preload></preload>\n" +
                  "\n" +
                  "                <cacheloader>\n" +
                  "                    <class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                  "                    <properties>\n" +
                  "                        location=" + loc + "\n" +
                  "                    </properties>\n" +
                  "                    <async>false</async>\n" +
                  "                    <fetchPersistentState>false</fetchPersistentState>\n" +
                  "                    <ignoreModifications>false</ignoreModifications>\n" +
                  "                </cacheloader>\n" +
                  "                \n" +
                  "            </config>";
          return XmlHelper.stringToElement(xml);
      }
  
     public static Test suite() {
        return new TestSuite(FileCacheLoaderObjectGraphAopTest.class);
     }
  
     public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  1.1      date: 2006/10/31 08:01:12;  author: bwang;  state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/loader/ObjectGraphTestsBase.java
  
  Index: ObjectGraphTestsBase.java
  ===================================================================
  package org.jboss.cache.aop.loader;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.aop.PojoCache;
  import org.jboss.cache.aop.test.Address;
  import org.jboss.cache.aop.test.Person;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.loader.CacheLoader;
  
  import javax.transaction.Transaction;
  
  /**
   * Commons tests for all CacheLoaders. Aop version.
   *
   * @author bwang
   * @version $Id: ObjectGraphTestsBase.java,v 1.1 2006/10/31 08:01:12 bwang Exp $
   */
  abstract public class ObjectGraphTestsBase extends AbstractCacheLoaderTestBase
  {
     Log log = LogFactory.getLog(ObjectGraphTestsBase.class);
     PojoCache cache;
     CacheLoader loader = null;
     Transaction tx = null;
     static final Fqn FQN = new Fqn("key");
  
     protected void setUp() throws Exception
     {
        super.setUp();
        cache = new PojoCache();
        Configuration c = new Configuration();
        cache.setConfiguration(c);
        c.setCacheMode("local");
        configureCache();
        c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        cache.create();
        cache.start();
        loader = cache.getCacheLoader();
     }
  
     abstract protected void configureCache() throws Exception;
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        if (tx != null)
        {
           try
           {
              tx.commit();
           }
           catch (Throwable e)
           {
              e.printStackTrace();
           }
        }
        cache.remove("/");
        loader.remove(Fqn.fromString("/"));
        cache.stop();
        cache.destroy();
     }
  
     protected void addDelay()
     {
        ; // returns immediately in this case.  Subclasses may override where a delay is needed.
     }
  
     protected Person createPerson(String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        return p;
     }
  
     public void testMultipleRefence() throws Exception
     {
        log.info("testMultipleReference() ...");
        cache.putObject("/person/joe", createPerson("Joe Black", 31));
        Person joe = (Person) cache.getObject("/person/joe");
        cache.putObject("/person/ben", createPerson("Ben Hogan", 51));
        Person ben = (Person) cache.getObject("/person/ben");
  
        Address addr = new Address();
        addr.setStreet("123 Albert Ave.");
        addr.setCity("Sunnyvale");
        addr.setZip(94087);
  
        // They share the sub-object: address
        log.info("testMultipleReference(): set Joe address");
        joe.setAddress(addr);
        log.info("testMultipleReference(): set Ben address");
        ben.setAddress(addr);
  
        log.info("testMultipleReference(): verify");
        Address add1 = (Address) ((Person) cache.getObject("/person/joe")).getAddress();
        Address add2 = (Address) ((Person) cache.getObject("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
  
        cache.evict(Fqn.fromString("/person/joe"));
        cache.evict(Fqn.fromString("/person/ben"));
  
        log.info("testMultipleReference(): after eviction");
        add1 = (Address) ((Person) cache.getObject("/person/joe")).getAddress();
        add2 = (Address) ((Person) cache.getObject("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        add1.setCity("Santa Clara");
        // Still not working now. Will need to support it. JCACHE-223
  //      assertEquals(add1.getCity(), add2.getCity());
     }
  
     public static Test suite()
     {
        return new TestSuite(ObjectGraphTestsBase.class);
     }
  
     public static void main(String[] args)
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list