[jboss-cvs] JBossCache/tests-50/functional/org/jboss/cache/pojo/collection ...

Ben Wang bwang at jboss.com
Mon Jul 17 10:41:28 EDT 2006


  User: bwang   
  Date: 06/07/17 10:41:28

  Added:       tests-50/functional/org/jboss/cache/pojo/collection                
                        CachedListImplTest.java CachedListTest.java
                        CachedListTxTest.java CachedMapNullTest.java
                        CachedMapTest.java CachedSetTest.java
                        CollectionTest.java ObjectGraphTest.java
  Removed:     tests-50/functional/org/jboss/cache/pojo/collection                
                        CachedListAopTest.java CachedListAopTxTest.java
                        CachedListImplAopTest.java
                        CachedMapAopNullTest.java CachedMapAopTest.java
                        CachedSetAopTest.java CollectionAopTest.java
                        ObjectGraphAopTest.java
  Log:
  Name changes
  
  Revision  Changes    Path
  1.1      date: 2006/07/17 14:41:28;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/collection/CachedListImplTest.java
  
  Index: CachedListImplTest.java
  ===================================================================
  package org.jboss.cache.pojo.collection;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.PropertyConfigurator;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  
  /**
   * List implementation testing.
   *
   * @author Ben Wang
   */
  
  public class CachedListImplTest extends TestCase
  {
     Log log = LogFactory.getLog(CachedListImplTest.class);
     PojoCache cache_;
  
     public CachedListImplTest(String name)
     {
        super(name);
     }
  
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        cache_ = PojoCacheFactory.createInstance(configFile);
        PropertyConfigurator config = new PropertyConfigurator();
        config.configure(cache_, configFile);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     public void testDummy()
     {
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CachedListImplTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2006/07/17 14:41:28;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/collection/CachedListTest.java
  
  Index: CachedListTest.java
  ===================================================================
  package org.jboss.cache.pojo.collection;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.ListIterator;
  import java.util.NoSuchElementException;
  
  /**
   * List interface testing.
   *
   * @author Ben Wang
   */
  
  public class CachedListTest extends TestCase
  {
     Log log = LogFactory.getLog(CachedListTest.class);
     PojoCache cache_;
     List languages;
     List languages2;
  
     public CachedListTest(String name)
     {
        super(name);
     }
  
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        cache_ = PojoCacheFactory.createInstance(configFile);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     public void testAddAndRemoveIndex() throws Throwable
     {
        stage();
  
        languages.add(1, "Taiwanese");
        assertEquals("Languages size ", 4, languages.size());
        assertEquals("Language ", (Object) "Taiwanese", (Object) languages.get(1));
        languages.remove(2);
        assertEquals("Languages size ", 3, languages.size());
        assertEquals("Language ", (Object) "English", (Object) languages.get(2));
  
        languages.add("Mandarin");
        assertEquals("Languages size ", 4, languages.size());
        languages.remove("Mandarin");
        assertEquals("Languages size ", 3, languages.size());
     }
  
     protected void stage() throws Throwable
     {
        languages = new ArrayList();
        languages.add("English");
        languages.add("French");
        languages.add("English");
        cache_.attach("/person/test6", languages);
        languages = (List) cache_.find("/person/test6");
        int size = languages.size();
        assertEquals("Size of list ", 3, size);
  
        languages2 = new ArrayList();
        languages2.addAll(languages);
        assertEquals("New ArrayList().addAll(CachedList)", languages, languages2);
     }
  
     public void testAddAllAndClear() throws Throwable
     {
        stage();
        List list = new ArrayList();
        list.add("Taiwanese");
        list.add("Madarin");
  
        assertTrue("Language is Taiwanese ", list.contains("Taiwanese"));
  
        languages.addAll(list);
        assertEquals("Languages size ", 5, languages.size());
  
        languages.removeAll(list);
        assertEquals("Languages size ", 3, languages.size());
  
        assertEquals("Index of French ", 1, languages.indexOf("French"));
  
        languages.clear();
        assertEquals("Languages size ", 0, languages.size());
  
        assertTrue("Languages empty ", languages.isEmpty());
     }
  
     public void testEquals() throws Throwable
     {
        stage();
  
        List list = (List) cache_.find("/person/test6");
        assertTrue("List should be the same ", list.equals(languages));
        list = new ArrayList();
        list.add("German");
        list.add("test");
        list.add("English");
        assertFalse("List should not be the same ", languages.equals(list));
        assertFalse("List should not be the same ", list.equals(languages));
     }
  
     public void testIterator() throws Throwable
     {
        stage();
  
        Iterator it = languages.iterator();
        Iterator it2 = languages2.iterator();
        int counter = 0;
        while (it.hasNext())
        {
           counter++;
           assertEquals(it.next(), it2.next());
           it.remove();
           it2.remove();
        }
  
        assertEquals("Size should be ", 3, counter);
        assertEquals("Skills should be empty ", 0, languages.size());
     }
  
     public void testListIterator() throws Throwable
     {
        stage();
  
        ListIterator li = languages.listIterator();
        ListIterator li2 = languages2.listIterator();
        assertFalse("LI has no previous element ", li.hasPrevious());
        assertFalse("LI2 has no previous element ", li2.hasPrevious());
        assertTrue("LI has next element ", li.hasNext());
        assertTrue("LI2 has next element ", li2.hasNext());
        assertEquals(li.next(), li2.next());
        assertEquals("Index is ", 1, li.nextIndex());
        assertEquals("Index is ", 1, li2.nextIndex());
        assertEquals("Index is ", 0, li.previousIndex());
        assertEquals("Index is ", 0, li2.previousIndex());
        assertEquals(li.next(), li2.next());
        assertEquals(li.next(), li2.next()); // the end
        try
        {
           li.next();
           fail("Should throw an exception here ");
        }
        catch (NoSuchElementException ex)
        {
           ;
        }
        try
        {
           li2.next();
           fail("Should throw an exception here ");
        }
        catch (NoSuchElementException ex)
        {
           ;
        }
  
        assertEquals("Index is ", 3, li.nextIndex());
        assertEquals("Index is ", 3, li2.nextIndex());
        assertEquals("Index is ", 2, li.previousIndex());
        assertEquals("Index is ", 2, li2.previousIndex());
        li.previous();
        li2.previous();
        assertEquals("Index is ", 2, li.nextIndex());
        assertEquals("Index is ", 2, li2.nextIndex());
        assertEquals("Index is ", 1, li.previousIndex());
        assertEquals("Index is ", 1, li2.previousIndex());
        li.previous();
        li2.previous();
        li.previous();
        li2.previous();
  
        try
        {
           li.previous();
           fail("Should throw an exception here ");
        }
        catch (NoSuchElementException ex)
        {
           ;
        }
  
        try
        {
           li2.previous();
           fail("Should throw an exception here ");
        }
        catch (NoSuchElementException ex)
        {
           ;
        }
  
        try
        {
           assertEquals(li.next(), li2.next());
           li.remove();
           li2.remove();
        }
        catch (Exception e)
        {
           fail("ListIterator.remove failed" + e);
        }
  
  
        try
        {
           assertEquals(li.next(), li2.next());
           li.remove();
           li2.remove();
        }
        catch (Exception e)
        {
           fail("ListIterator.remove failed" + e);
        }
  
        try
        {
           assertEquals(li.next(), li2.next());
           assertEquals("ListIterator.remove test problem with nextIndex, cache next index=" + li.nextIndex() +
                   ", jdk next index=" + li2.nextIndex() + "cache list size = " + languages.size() + ", jdk list size = " + languages.size(),
                   li.nextIndex(), li2.nextIndex());
           li2.set("German");
           li.set("German");
           String s1 = (String) li.previous();
           String s2 = (String) li2.previous();
           assertEquals(s1, s2);
           assertEquals(s2, "German");
        }
        catch (Exception e)
        {
           fail("ListIterator.remove failed" + e + ", cache list size = " + languages.size() + ", jdk list size = " + languages.size());
        }
  
        try
        {
           assertEquals(li.next(), li2.next());
           li2.add("Vulcan");
           li.add("Vulcan");
           String s1 = (String) li.previous();
           String s2 = (String) li2.previous();
           assertEquals(s1, s2);
           assertEquals(s2, "Vulcan");
        }
        catch (Exception e)
        {
           fail("ListIterator.add failed" + e + ", cache list size = " + languages.size() + ", jdk list size = " + languages.size());
        }
  
     }
  
  
     public void testAttachAndDetach() throws Exception
     {
        List list = new ArrayList();
        list.add("English");
        list.add("French");
        list.add("Taiwanese");
  
        cache_.attach("/test", list); // attach
        list = (List) cache_.find("/test");
        assertEquals("Size ", 3, list.size());
  
        list = (List) cache_.detach("/test");
        assertEquals("Size ", 3, list.size());
  
        System.out.println("**** End of cache content **** ");
        list.remove(2);
        list.add("Hoklo");
        assertEquals("Size ", 3, list.size());
        assertEquals("Content ", "Hoklo", list.get(2));
  
        // Try to re-attach
        cache_.attach("/test", list);
        list.remove(2);
        assertEquals("Size ", 2, list.size());
     }
  
     public void testPojoAttachAndDetach() throws Exception
     {
        Address add1 = new Address();
        add1.setCity("San Jose");
        add1.setZip(95123);
  
        Address add2 = new Address();
        add2.setCity("Sunnyvale");
        add2.setZip(94086);
  
        Address add3 = new Address();
        add3.setCity("Santa Clara");
        add3.setZip(951131);
  
        List list = new ArrayList();
        list.add(add1);
        list.add(add2);
        list.add(add3);
  
        cache_.attach("/test", list); // attach
        list = (List) cache_.find("/test");
        assertEquals("Size ", 3, list.size());
  
        list = (List) cache_.detach("/test");
        assertEquals("Size ", 3, list.size());
  
        System.out.println("**** End of cache content **** ");
        list.remove(2);
        list.add(add2);
        assertEquals("Size ", 3, list.size());
        assertEquals("Content ", add2, list.get(2));
  
        // Try to re-attach
        cache_.attach("/test", list);
        list.remove(2);
        assertEquals("Size ", 2, list.size());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CachedListTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2006/07/17 14:41:28;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/collection/CachedListTxTest.java
  
  Index: CachedListTxTest.java
  ===================================================================
  package org.jboss.cache.pojo.collection;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  
  /**
   * List interface testing with transaction control.
   *
   * @author Scott Marlow
   */
  
  public class CachedListTxTest extends TestCase
  {
     Log log = LogFactory.getLog(CachedListTxTest.class);
     PojoCache cache_;
  
  
     public CachedListTxTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/cache-config.xml";
        cache_ = PojoCacheFactory.createInstance(configFile);
        cache_.start();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     public void testDummy()
     {
        // No op now.
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CachedListTxTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  
  1.1      date: 2006/07/17 14:41:28;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/collection/CachedMapNullTest.java
  
  Index: CachedMapNullTest.java
  ===================================================================
  package org.jboss.cache.pojo.collection;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.aop.proxy.ClassProxy;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  
  /**
   * Set interface testing.
   *
   * @author Scott Marlow
   */
  
  public class CachedMapNullTest extends TestCase
  {
     Log log = LogFactory.getLog(CachedMapNullTest.class);
     PojoCache cache_;
     Map hobbies;
  
     public CachedMapNullTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        cache_ = PojoCacheFactory.createInstance(configFile);
        cache_.start();
  
        stage();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     static final int NUMBER_OF_STAGED_HOBBIES = 5;
  
     protected void stage() throws Exception
     {
        hobbies = new HashMap();
        hobbies.put("1", "golf");
        hobbies.put("2", "tennis");
        hobbies.put("3", "polo");
        hobbies.put(null, "Non-null value but the key is null");
        hobbies.put("key is non-null but value is null", null);
  
        cache_.attach("/person/test7", hobbies);
        hobbies = (Map) cache_.find("/person/test7");
        assertEquals("Map size", NUMBER_OF_STAGED_HOBBIES, hobbies.size());
  
        if (!(hobbies instanceof ClassProxy || hobbies instanceof Map))
        {
           fail("testPut(): hobbies is not instance of ClassProxy nor Map");
        }
     }
  
     /**
      * Test simple put
      *
      * @throws Throwable
      */
     public void testPut() throws Throwable
     {
        int size = hobbies.size();
        assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES, size);
  
        hobbies.put("6", "baseball");
        size = hobbies.size();
        assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 1, size);
  
     }
  
     public void testAddAndRemoveIndex() throws Throwable
     {
        hobbies.put("4", "baseball");
        int size = hobbies.size();
        assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 1, size);
  
        assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
  
        hobbies.remove("3");
        size = hobbies.size();
        assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES, size);
        assertFalse("Skill does not contain " + NUMBER_OF_STAGED_HOBBIES + " anymore ", hobbies.containsKey("3"));
  
        assertTrue("search for null key returned non-null value " + hobbies.get(null), hobbies.get(null) != null);
  
        hobbies.remove(null);
        size = hobbies.size();
        assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES - 1, size);
        assertFalse("Skill does not contain " + (NUMBER_OF_STAGED_HOBBIES - 1) + " ", hobbies.containsKey(null));
  
        hobbies.clear();
        size = hobbies.size();
        assertEquals("Size is ", 0, size);
  
        assertTrue("Should be empty", hobbies.isEmpty());
     }
  
     public void testPutAllEtc() throws Throwable
     {
        Map map = new HashMap();
        map.put("4", "pingpong");
        map.put("5", "handball");
  
        hobbies.putAll(map);
        int size = hobbies.size();
        assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 2, size);
  
        assertTrue("Key is ", hobbies.containsKey("4"));
  
        Set keys = hobbies.keySet();
        assertEquals("Key size ", NUMBER_OF_STAGED_HOBBIES + 2, keys.size());
  
        Set entries = hobbies.entrySet();
        assertEquals("Entry size ", NUMBER_OF_STAGED_HOBBIES + 2, entries.size());
  
     }
  
     public void testEntrySet() throws Throwable
     {
        System.out.println("Map " + hobbies.toString());
        for (Iterator i = hobbies.entrySet().iterator(); i.hasNext();)
        {
           Map.Entry entry = (Map.Entry) i.next();
           System.out.println("Entry key and value " + entry.getKey() + " " + entry.getValue());
        }
     }
  
     public void testValues() throws Throwable
     {
        System.out.println("Map " + hobbies.toString());
  
        Set correct = new HashSet();
        correct.add("golf");
        correct.add("tennis");
        correct.add("polo");
        correct.add("Non-null value but the key is null");
        correct.add(null);
  
        Collection values = hobbies.values();
        assertEquals("Correct number of elements in value collection",
                correct.size(), values.size());
  
        Iterator iter = null;
        for (iter = correct.iterator(); iter.hasNext();)
           assertTrue(values.contains(iter.next()));
  
        for (iter = values.iterator(); iter.hasNext();)
        {
           Object value = iter.next();
           assertTrue(value + " expected", correct.remove(value));
        }
        assertTrue("No missing elements from iterator", correct.size() == 0);
  
        iter.remove();
        assertTrue("2 elements left after remove via iter", values.size() == NUMBER_OF_STAGED_HOBBIES - 1);
        assertTrue("Iter removal reflected in map", hobbies.size() == NUMBER_OF_STAGED_HOBBIES - 1);
  
        Object[] data = values.toArray();
        assertTrue("2 elements in values array", data.length == NUMBER_OF_STAGED_HOBBIES - 1);
  
        values.remove(data[0]);
        assertTrue("1 element left after remove", values.size() == NUMBER_OF_STAGED_HOBBIES - 2);
        assertTrue("Removal reflected in map", hobbies.size() == NUMBER_OF_STAGED_HOBBIES - 2);
  
        values.clear();
        assertTrue("0 elements left after clear", values.size() == 0);
        assertTrue("Clear reflected in map", hobbies.size() == 0);
     }
  
     public void testContainsValue() throws Throwable
     {
        System.out.println("Map " + hobbies.toString());
        assertTrue("contains golf", hobbies.containsValue("golf"));
        assertTrue("contains tennis", hobbies.containsValue("tennis"));
        assertTrue("contains polo", hobbies.containsValue("polo"));
        assertFalse("does not contain squash", hobbies.containsValue("squash"));
     }
  
     public void testEquals() throws Throwable
     {
        Map map = new HashMap();
        map.put("1", "test");
        map.put("4", "test");
        map.put("2", "tennis");
        assertFalse("Map should not be the same ", map.equals(hobbies));
  
        map.clear();
        map.put("1", "golf");
        map.put("2", "tennis");
        map.put("3", "polo");
        map.put(null, "Non-null value but the key is null");
        map.put("key is non-null but value is null", null);
        assertTrue("Map should be the same ", map.equals(hobbies));
        assertTrue("Map should be the same, hobbies=" + hobbies.toString() + ", map=" + map.toString(), hobbies.equals(map));
        assertTrue("Map should be the same ", hobbies.equals(hobbies));
     }
  
     public void testAttachAndDetach() throws Exception
     {
        Map map = new HashMap();
        map.put("1", "English");
        map.put("2", "French");
        map.put("3", "Taiwanese");
  
        cache_.attach("/test", map); // attach
        map = (Map) cache_.find("/test");
        assertEquals("Size ", 3, map.size());
  
        map = (Map) cache_.detach("/test");  // detach
        assertEquals("Size ", 3, map.size());
  
        System.out.println("**** End of cache content **** ");
        map.remove("2");
        map.put("2", "Hoklo");
        assertEquals("Size ", 3, map.size());
        assertEquals("Content ", "Hoklo", map.get("2"));
  
        // Try to re-attach
        cache_.attach("/test", map);
        map.remove("3");
        assertEquals("Size ", 2, map.size());
     }
  
     public void testPojoAttachAndDetach() throws Exception
     {
        Address add1 = new Address();
        add1.setCity("San Jose");
        add1.setZip(95123);
  
        Address add2 = new Address();
        add1.setCity("Sunnyvale");
        add1.setZip(94086);
  
        Address add3 = new Address();
        add1.setCity("Santa Clara");
        add1.setZip(951131);
  
        Map map = new HashMap();
        map.put("1", add1);
        map.put("2", add2);
        map.put("3", add3);
  
        cache_.attach("/test", map); // attach
        map = (Map) cache_.find("/test");
        assertEquals("Size ", 3, map.size());
  
        map = (Map) cache_.detach("/test");
        assertEquals("Size ", 3, map.size());
  
        System.out.println("**** End of cache content **** ");
        map.remove("2");
        map.put("2", add2);
        assertEquals("Size ", 3, map.size());
        assertEquals("Content ", add2, map.get("2"));
  
        // Try to re-attach
        cache_.attach("/test", map);
        map.remove("2");
        assertEquals("Size ", 2, map.size());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CachedMapNullTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  }
  
  
  
  
  1.1      date: 2006/07/17 14:41:28;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/collection/CachedMapTest.java
  
  Index: CachedMapTest.java
  ===================================================================
  package org.jboss.cache.pojo.collection;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.aop.proxy.ClassProxy;
  import org.jboss.cache.PropertyConfigurator;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  import java.io.Serializable;
  
  /**
   * Map interface testing.
   *
   * @author Ben Wang
   */
  
  public class CachedMapTest extends TestCase
  {
     Log log = LogFactory.getLog(CachedMapTest.class);
     PojoCache cache_;
     Map hobbies;
  
     public CachedMapTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        cache_ = PojoCacheFactory.createInstance(configFile);
        PropertyConfigurator config = new PropertyConfigurator();
        config.configure(cache_, configFile);
        cache_.start();
  
        stage();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     protected void stage() throws Exception
     {
        hobbies = new HashMap();
        hobbies.put("1", "golf");
        hobbies.put("2", "tennis");
        hobbies.put("3", "polo");
  
        cache_.attach("/person/test7", hobbies);
  
        hobbies = (Map) cache_.find("/person/test7");
        assertEquals("Map size", 3, hobbies.size());
  
        if (!(hobbies instanceof ClassProxy || hobbies instanceof Map))
        {
           fail("testPut(): hobbies is not instance of ClassProxy nor Map");
        }
     }
  
     /**
      * Test simple put
      *
      * @throws Throwable
      */
     public void testPut() throws Throwable
     {
        int size = hobbies.size();
        assertEquals("Size is ", 3, size);
  
        hobbies.put("6", "baseball");
        size = hobbies.size();
        assertEquals("Size is ", 4, size);
  
     }
  
     public void testAddAndRemoveIndex() throws Throwable
     {
        hobbies.put("4", "baseball");
        int size = hobbies.size();
        assertEquals("Size is ", 4, size);
  
        assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
  
        hobbies.remove("3");
        size = hobbies.size();
        assertEquals("Size is ", 3, size);
        assertFalse("Skill does not contain 3 anymore ", hobbies.containsKey("3"));
  
        hobbies.clear();
        size = hobbies.size();
        assertEquals("Size is ", 0, size);
  
        assertTrue("Should be empty", hobbies.isEmpty());
     }
  
     public void testPutAllEtc() throws Throwable
     {
        Map map = new HashMap();
        map.put("4", "pingpong");
        map.put("5", "handball");
  
        hobbies.putAll(map);
        int size = hobbies.size();
        assertEquals("Size is ", 5, size);
  
        assertTrue("Key is ", hobbies.containsKey("4"));
  
        Set keys = hobbies.keySet();
        assertEquals("Key size ", 5, keys.size());
  
        Set entries = hobbies.entrySet();
        assertEquals("Entry size ", 5, entries.size());
  
     }
  
     public void testLongValue() throws Throwable
     {
        Long val = new Long("8225676592564383");
        Long val2 = new Long(8225676592564383L);
        assertTrue(0 == val.compareTo(val2));   // sanity check
        hobbies.put(val, "prateek");
        assertTrue(hobbies.containsKey(val));
        assertTrue(hobbies.get(val).equals("prateek"));
     }
  
     public void testEntrySet() throws Throwable
     {
        System.out.println("Map " + hobbies.toString());
        for (Iterator i = hobbies.entrySet().iterator(); i.hasNext();)
        {
           Map.Entry entry = (Map.Entry) i.next();
           System.out.println("Entry key and value " + entry.getKey() + " " + entry.getValue());
        }
     }
  
     public void testValues() throws Throwable
     {
        System.out.println("Map " + hobbies.toString());
  
        Set correct = new HashSet();
        correct.add("golf");
        correct.add("tennis");
        correct.add("polo");
  
        Collection values = hobbies.values();
        assertEquals("Correct number of elements in value collection",
                correct.size(), values.size());
  
        Iterator iter = null;
        for (iter = correct.iterator(); iter.hasNext();)
           assertTrue(values.contains(iter.next()));
  
        for (iter = values.iterator(); iter.hasNext();)
        {
           Object value = iter.next();
           assertTrue(value + " expected", correct.remove(value));
        }
        assertTrue("No missing elements from iterator", correct.size() == 0);
  
        iter.remove();
        assertTrue("2 elements left after remove via iter", values.size() == 2);
        assertTrue("Iter removal reflected in map", hobbies.size() == 2);
  
        Object[] data = values.toArray();
        assertTrue("2 elements in values array", data.length == 2);
  
        values.remove(data[0]);
        assertTrue("1 element left after remove", values.size() == 1);
        assertTrue("Removal reflected in map", hobbies.size() == 1);
  
        values.clear();
        assertTrue("0 elements left after clear", values.size() == 0);
        assertTrue("Clear reflected in map", hobbies.size() == 0);
     }
  
     public void testContainsValue() throws Throwable
     {
        System.out.println("Map " + hobbies.toString());
        assertTrue("contains golf", hobbies.containsValue("golf"));
        assertTrue("contains tennis", hobbies.containsValue("tennis"));
        assertTrue("contains polo", hobbies.containsValue("polo"));
        assertFalse("does not contain squash", hobbies.containsValue("squash"));
     }
  
     public void testEquals() throws Throwable
     {
        Map map = new HashMap();
        map.put("1", "test");
        map.put("4", "test");
        map.put("2", "tennis");
        assertFalse("Map should not be the same ", map.equals(hobbies));
     }
  
     public void testAttachAndDetach() throws Exception
     {
        Map map = new HashMap();
        map.put("1", "English");
        map.put("2", "French");
        map.put("3", "Taiwanese");
  
        cache_.attach("/test", map); // attach
        map = (Map) cache_.find("/test");
        assertEquals("Size ", 3, map.size());
  
        map = (Map) cache_.detach("/test");  // detach
        assertEquals("Size ", 3, map.size());
  
        System.out.println("**** End of cache content **** ");
        map.remove("2");
        map.put(new Integer(2), "Hoklo");
        assertEquals("Size ", 3, map.size());
        assertEquals("Content ", "Hoklo", map.get(new Integer(2)));
  
        // Try to re-attach
        cache_.attach("/test", map);
        map.remove("3");
        assertEquals("Size ", 2, map.size());
     }
  
     public void testPojoAttachAndDetach() throws Exception
     {
        Address add1 = new Address();
        add1.setCity("San Jose");
        add1.setZip(95123);
  
        Address add2 = new Address();
        add2.setCity("Sunnyvale");
        add2.setZip(94086);
  
        Address add3 = new Address();
        add3.setCity("Santa Clara");
        add3.setZip(951131);
  
        Map map = new HashMap();
        map.put("1", add1);
        map.put("2", add2);
        map.put("3", add3);
  
        cache_.attach("/test", map); // attach
        map = (Map) cache_.find("/test");
        assertEquals("Size ", 3, map.size());
  
        map = (Map) cache_.detach("/test");
        assertEquals("Size ", 3, map.size());
  
        System.out.println("**** End of cache content **** ");
        map.remove("2");
        map.put("2", add2);
        assertEquals("Size ", 3, map.size());
        assertEquals("Content ", add2, map.get("2"));
  
        // Try to re-attach
        cache_.attach("/test", map);
        map.remove("2");
        assertEquals("Size ", 2, map.size());
     }
     public void testKeyAsObject() throws Exception
     {
        SerializableKeyValue key = new SerializableKeyValue(42,"Novell");
        Address add1 = new Address();
        add1.setCity("Waltham");
        add1.setStreet("404 Wyman Street");
        add1.setZip(02451);
  
        Map map = new HashMap();
        map.put(key,add1);
        cache_.attach("/keytest", map);
        Map ref = (Map)cache_.find("/keytest");
  
        Iterator iter = ref.keySet().iterator();
        assertTrue(iter.hasNext());
        Object keyFromMap = iter.next();
        assertEquals("original key is same as key in pojocache map, key class=" +
              key.getClass().getName() +", keyFromMap class=" + keyFromMap.getClass().getName(),
              key , keyFromMap);
        assertEquals("local map is equal to pojocache map",map, ref);
        }
  
     public void testNonSerializableKeyAsObject() throws Exception
     {
        // negative test as we should get a java.io.NotSerializableException
        NotSerializableKeyValue key = new NotSerializableKeyValue(42,"Novell");
        Address add1 = new Address();
        add1.setCity("Waltham");
        add1.setStreet("404 Wyman Street");
        add1.setZip(02451);
  
        Map map = new HashMap();
        map.put(key,add1);
        try {
           cache_.attach("/keytest", map);  // this should throw RuntimeException with cause of java.io.NotSerializableException
           fail("failed to get expected runtimeException when adding nonserializable key to pojocache map");
        }
        catch(RuntimeException e) {
           Throwable t = e;
           while((t.getCause() instanceof RuntimeException )) // get through wrapped runtimeexceptions
              t = t.getCause();
           assertTrue("we got expected RuntimeException, test that cause is java.io.NotSerializableException, t.getCause()= "+t.getCause().getClass().getName(), t.getCause() instanceof java.io.NotSerializableException);
        }
        map.clear();
        cache_.attach("/keytest", map);  // this should succeed
        Map ref = (Map)cache_.find("/keytest");
  
        // try adding nonserializable key to pojocache based map
        try {
           ref.put(key, add1);  // this should throw RuntimeException with cause of java.io.NotSerializableException
           fail("failed to get expected runtimeException when putting nonserializable key to pojocache map");
        }
        catch(RuntimeException e) {
           Throwable t = e;
           while((t.getCause() instanceof RuntimeException )) // get through wrapped runtimeexceptions
              t = t.getCause();
           assertTrue("we got expected RuntimeException, test that cause is java.io.NotSerializableException, t.getCause()= "+t.getCause().getClass().getName(), t.getCause() instanceof java.io.NotSerializableException);
        }
  
  
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CachedMapTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
     static class SerializableKeyValue implements Serializable
     {
         Integer ivalue;
         String svalue;
  
         SerializableKeyValue(Integer ivalue, String svalue)
         {
             this.ivalue = ivalue;
             this.svalue= svalue;
         }
  
         public int hashCode()
         {
             return ivalue.hashCode() + svalue.hashCode();
         }
  
         public boolean equals(Object o)
         {
             if(o == this)
             {
                 return true;
             }
             if(! (o instanceof SerializableKeyValue)) {
                 System.out.println("equals: not instanceof SerializableKeyValue , it is a " + o.getClass().getName());
                 return false;
             }
             SerializableKeyValue val = (SerializableKeyValue)o;
             return val.ivalue == this.ivalue && val.svalue == this.svalue;
         }
     }
  
     static class NotSerializableKeyValue
        {
            Integer ivalue;
            String svalue;
  
            NotSerializableKeyValue(Integer ivalue, String svalue)
            {
                this.ivalue = ivalue;
                this.svalue= svalue;
            }
  
            public int hashCode()
            {
                return ivalue.hashCode() + svalue.hashCode();
            }
  
            public boolean equals(Object o)
            {
                if(o == this)
                {
                    return true;
                }
                if(! (o instanceof SerializableKeyValue)) {
                    System.out.println("equals: not instanceof SerializableKeyValue , it is a " + o.getClass().getName());
                    return false;
                }
                SerializableKeyValue val = (SerializableKeyValue)o;
                return val.ivalue == this.ivalue && val.svalue == this.svalue;
            }
        }
  
  }
  
  
  
  
  1.1      date: 2006/07/17 14:41:28;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/collection/CachedSetTest.java
  
  Index: CachedSetTest.java
  ===================================================================
  package org.jboss.cache.pojo.collection;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.PropertyConfigurator;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.NoSuchElementException;
  import java.util.Set;
  
  /**
   * Set interface testing.
   *
   * @author Ben Wang
   */
  
  public class CachedSetTest extends TestCase
  {
     Log log = LogFactory.getLog(CachedSetTest.class);
     PojoCache cache_;
     Set skills;
  
     public CachedSetTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        cache_ = PojoCacheFactory.createInstance(configFile);
        PropertyConfigurator config = new PropertyConfigurator();
        config.configure(cache_, configFile);
        cache_.start();
  
        stage();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     protected void stage() throws Exception
     {
        skills = new HashSet();
        skills.add("Java");
        skills.add("C++");
        skills.add("Perl");
  
        cache_.attach("/person/test7", skills);
        skills = (Set) cache_.find("/person/test7");
        int size = skills.size();
        assertEquals("Size is ", 3, size);
     }
  
     public void testClear() throws Throwable
     {
        int size = skills.size();
        assertEquals("Size is ", 3, size);
  
        skills.clear();
        size = skills.size();
        assertEquals("Size is ", 0, size);
  
        assertTrue("Should be empty", skills.isEmpty());
     }
  
     public void testAddAndRemoveIndex() throws Throwable
     {
        skills.add("Golf");
        int size = skills.size();
        assertEquals("Size is ", 4, size);
  
        skills.add("Golf");
        size = skills.size();
        assertEquals("Size is ", 4, size);
  
        assertTrue("Skill shuold contain Golf ", skills.contains("Golf"));
        skills.remove("C++");
        size = skills.size();
        assertEquals("Size is ", 3, size);
        assertFalse("Skill does not contain C++ anymore ", skills.contains("C++"));
  
        skills.add("Golf");
        size = skills.size();
        assertEquals("Size is ", 3, size);
        skills.clear();
        size = skills.size();
        assertEquals("Size is ", 0, size);
  
        assertTrue("Should be empty", skills.isEmpty());
     }
  
     public void testAddAndRemoveAll() throws Throwable
     {
        List list = new ArrayList();
        list.add("Tennis");
        list.add("Polo");
        list.add("Baseball");
  
        skills.addAll((Collection) list);
        int size = skills.size();
        assertEquals("Size is ", 6, size);
        assertTrue("Skill contains Polo ", skills.contains("Polo"));
  
        skills.removeAll((Collection) list);
        size = skills.size();
        assertEquals("Size is ", 3, size);
        assertFalse("Skill does not contain Polo ", skills.contains("Polo"));
        assertTrue("Skill contains C++ ", skills.contains("C++"));
  
     }
  
     public void testRemoveAndAdd()
     {
        assertTrue(skills.remove("C++"));
        assertTrue(skills.add("C++"));
        assertEquals("removeAndAdd: size is 3", 3, skills.size());
     }
  
     public void testIterator()
     {
        Iterator it = skills.iterator();
        int counter = 0;
        while (it.hasNext())
        {
           counter++;
           it.next();
           it.remove();
        }
  
        assertEquals("iterator: Size should be ", 3, counter);
        assertEquals("iterator: Skills should be empty ", 0, skills.size());
  
        List list = new ArrayList();
        list.add("Tennis");
        list.add("Polo");
        list.add("Baseball");
        list.add("Soccer");
        list.add("Hockey");
        list.add("Lacrosse");
        skills.addAll(list);
        it = skills.iterator();
        while (it.hasNext())
        {
           counter++;
           if ("Polo".equals(it.next()))
           {
              it.remove();
           }
        }
        assertFalse("iterator: item removed via iterator", skills.contains("Polo"));
        assertTrue("iterator: item not removed via iterator", skills.contains("Lacrosse"));
  
        // Check for proper relationship between hasNext and next
        list = new ArrayList();
        list.add("Tennis");
        list.add("Polo");
        list.add("Baseball");
        skills.addAll(list);
        it = skills.iterator();
        while (it.hasNext())
        {
           it.next();
        }
        try
        {
           it.next();
           fail("iterator: Didn't fail on next() when hasNext() == false");
        }
        catch (NoSuchElementException good)
        {
        }
  
        // Check for proper relationship between next and remove
        it = skills.iterator();
        try
        {
           it.remove();
        }
        catch (IllegalStateException good)
        {
           // behaves correctly per Iterator contract
        }
        catch (Exception e)
        {
           StringWriter stackTrace = new StringWriter();
           e.printStackTrace(new PrintWriter(stackTrace));
           fail("iterator: failed with incorrect exception type when removing " +
                   "without first calling next() -- " + e.getClass().getName() + ".  stackTrace=" + stackTrace);
        }
  
  
     }
  
     public void testEquals() throws Throwable
     {
        Set set = (Set) cache_.find("/person/test7");
        assertTrue("iterator: List should be the same ", set.equals(skills));
        set = new HashSet();
        set.add("German");
        set.add("test");
        set.add("English");
        assertFalse("List should not be the same ", set.equals(skills));
        assertFalse("List should not be the same ", skills.equals(set));
     }
  
  
     public void testAttachAndDetach() throws Exception
     {
        Set set = new HashSet();
        set.add("English");
        set.add("French");
        set.add("Taiwanese");
  
        cache_.attach("/test", set); // attach
        set = (Set) cache_.find("/test");
        assertEquals("Size ", 3, set.size());
  
        set = (Set) cache_.detach("/test");
        assertEquals("Size ", 3, set.size());
  
        System.out.println("**** End of cache content **** ");
        set.remove("French");
        set.add("Hoklo");
        assertEquals("Size ", 3, set.size());
        assertTrue("Content ", set.contains("Hoklo"));
  
        // Try to re-attach
        cache_.attach("/test", set);
        set.remove("Taiwanese");
        assertEquals("Size ", 2, set.size());
     }
  
     public void testPojoAttachAndDetach() throws Exception
     {
        Address add1 = new Address();
        add1.setCity("San Jose");
        add1.setZip(95123);
  
        Address add2 = new Address();
        add2.setCity("Sunnyvale");
        add2.setZip(94086);
  
        Address add3 = new Address();
        add3.setCity("Santa Clara");
        add3.setZip(951131);
  
        Set set = new HashSet();
        set.add(add1);
        set.add(add2);
        set.add(add3);
  
        cache_.attach("/test", set); // attach
        set = (Set) cache_.find("/test");
        assertEquals("Size ", 3, set.size());
  
        set = (Set) cache_.detach("/test");
        assertEquals("Size ", 3, set.size());
  
        System.out.println("**** End of cache content **** ");
        set.remove(add2);
        set.add(add2);
        assertEquals("Size ", 3, set.size());
        assertTrue("Content ", set.contains(add2));
  
        // Try to re-attach
        cache_.attach("/test", set);
        set.remove(add2);
        assertEquals("Size ", 2, set.size());
     }
  
     public void testToArray() throws Exception
     {
        Object[] arr = skills.toArray();
        assertEquals("toArray: array length is correct", 3, arr.length);
        testLanguagesFound(arr);
  
        Object[] arr1 = skills.toArray(arr);
        assertTrue("toArray: arrays match", Arrays.equals(arr, arr1));
  
        arr = new Object[5];
        arr = skills.toArray(arr);
        assertEquals("toArray: array length is correct", 5, arr.length);
        testLanguagesFound(arr);
        assertEquals(null, arr[3]);
        assertEquals(null, arr[4]);
  
        arr = new Object[2];
        arr = skills.toArray(arr);
        assertEquals("toArray: array length is correct", 3, arr.length);
        testLanguagesFound(arr);
  
     }
  
     private void testLanguagesFound(Object[] arr)
     {
        boolean[] found = new boolean[3];
        for (int i = 0; i < arr.length; i++)
        {
           if ("Java".equals(arr[i]))
              found[0] = true;
           else if ("C++".equals(arr[i]))
              found[1] = true;
           else if ("Perl".equals(arr[i]))
              found[2] = true;
        }
        assertTrue("toArray: all elements found", found[0] && found[1] && found[2]);
  
     }
  
     public void testRetainAll() throws Exception
     {
        LinkedList list2 = new LinkedList();
        list2.add("Java");
  
        assertTrue("testRetainAll", skills.retainAll(list2));
        // should only have Java left
        assertTrue("testRetainAll, skills size should be 1 but is " + skills.size(), skills.size() == 1);
        assertTrue("testRetainAll", skills.contains("Java"));
  
     }
  
     public void testContainsAll() throws Exception
     {
        LinkedList list2 = new LinkedList();
        list2.add("Java");
        list2.add("C++");
        list2.add("Perl");
  
        skills.clear();
  
        assertTrue(skills.addAll(list2));
        list2.remove("Java");
        assertTrue("testContainsAll", skills.containsAll(list2));
        skills.remove("C++");
        assertFalse("testContainsAll", skills.containsAll(list2));
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CachedSetTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  
  }
  
  
  
  
  1.1      date: 2006/07/17 14:41:28;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/collection/CollectionTest.java
  
  Index: CollectionTest.java
  ===================================================================
  package org.jboss.cache.pojo.collection;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.LinkedHashMap;
  import java.util.LinkedHashSet;
  import java.util.LinkedList;
  import java.util.Map;
  
  /**
   * Generic Collection class support testing.
   *
   * @author Ben Wang
   */
  
  public class CollectionTest extends TestCase
  {
     Log log_ = LogFactory.getLog(CollectionTest.class);
     PojoCache cache_;
  
     public CollectionTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        cache_ = PojoCacheFactory.createInstance(configFile);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     /**
      * Testing using LinkedList proxy.
      *
      * @throws Exception
      */
     public void testLinkedList() throws Exception
     {
        LinkedList list = new LinkedList();
        LinkedList list1;
        list.add("English");
        try
        {
           cache_.attach("/aop/list", list);
           list = (LinkedList) cache_.find("/aop/list");
           list.add("French");
           list1 = (LinkedList) cache_.find("/aop/list");
           assertEquals("Size of list ", 2, list1.size());
        } catch (Exception e)
        {
           fail("pubtObject fails");
           throw e;
        }
     }
  
     /**
      * Testing using LinkedMap proxy.
      *
      * @throws Exception
      */
     public void testLinkedMap() throws Exception
     {
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap map1;
        map.put("1", "English");
        try
        {
           cache_.attach("/aop/map", map);
           map = (LinkedHashMap) cache_.find("/aop/map");
           map.put("2", "French");
           map1 = (LinkedHashMap) cache_.find("/aop/map");
           assertEquals("Size of map ", 2, map1.size());
        } catch (Exception e)
        {
           fail("pubtObject fails");
           throw e;
        }
     }
  
     /**
      * Testing using LinkedSet proxy.
      *
      * @throws Exception
      */
     public void testLinkedSet() throws Exception
     {
        LinkedHashSet set = new LinkedHashSet();
        LinkedHashSet set1;
        set.add("English");
        Map map;
        try
        {
           cache_.attach("/aop/set", set);
           set = (LinkedHashSet) cache_.find("/aop/set");
           set.add("French");
           set1 = (LinkedHashSet) cache_.find("/aop/set");
           assertEquals("Size of set ", 2, set1.size());
        } catch (Exception e)
        {
           fail("pubtObject fails");
           throw e;
        }
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CollectionTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
     // tests for each of the methods in Collection interface
     public void testSize() throws Exception
     {
  
        LinkedList list = new LinkedList();
        list.add("java");
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           assertEquals("size of collection", 1, list.size());
           list.add("c");
           list.add("lisp");
           assertEquals("size of collection", 3, list.size());
           list.remove("lisp");
           assertEquals("size of collection", 2, list.size());
           list.remove("c");
           assertEquals("size of collection", 1, list.size());
           list.clear();
           assertEquals("size of collection", 0, list.size());
        }
        catch (Exception e)
        {
           fail("testSize " + e.getMessage());
           throw e;
        }
  
     }
  
     public void testIsEmpty() throws Exception
     {
        LinkedList list = new LinkedList();
  
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           assertTrue("collection is empty", list.isEmpty());
           list.add("c");
           list.add("lisp");
           assertFalse("collection is not empty", list.isEmpty());
           list.remove("lisp");
           assertFalse("collection is not empty", list.isEmpty());
           list.remove("c");
           assertTrue("collection is empty", list.isEmpty());
        }
        catch (Exception e)
        {
           fail("testIsEmpty " + e.getMessage());
           throw e;
        }
     }
  
     public void testContains() throws Exception
     {
        LinkedList list = new LinkedList();
        list.add("java");
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           assertFalse("collection doesn't contains", list.contains("lisp"));
           list.add("c");
           list.add("lisp");
           assertTrue("collection contains", list.contains("lisp"));
           list.remove("lisp");
           assertFalse("collection doesn't contain", list.contains("lisp"));
           list.remove("c");
           list.clear();
           assertFalse("collection doesn't contains", list.contains("c"));
        }
        catch (Exception e)
        {
           fail("testContains " + e.getMessage());
           throw e;
        }
  
     }
  
     public void testIterator() throws Exception
     {
        LinkedList list = new LinkedList();
        LinkedList list2 = new LinkedList();
        list2.add("java");
        list2.add("c");
        list2.add("lisp");
        list2.add("c++");
        list2.add("forth");
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           list.addAll(list2);
           Iterator iter = list.iterator();
           while (iter.hasNext())
           {
              Object o = iter.next();
              assertTrue("Iteration test", list2.contains(o));
           }
        }
        catch (Exception e)
        {
           fail("testIterator " + e.getMessage());
           throw e;
        }
     }
  
     public void testToArray() throws Exception
     {
        LinkedList list = new LinkedList();
        LinkedList list2 = new LinkedList();
        list2.add("java");
        list2.add("c");
        list2.add("lisp");
        list2.add("c++");
        list2.add("forth");
        list.addAll(list2);
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           Object[] values = list.toArray();
  
           for (int looper = 0; looper < values.length; looper ++)
           {
              assertTrue("toArray test", list2.contains(values[looper]));
           }
  
           values = new Object[1];
           values = list.toArray(values);  // test with too small of an array
           for (int looper = 0; looper < values.length; looper ++)
           {
              assertTrue("toArray test", list2.contains(values[looper]));
           }
  
           values = new Object[10];
           values = list.toArray(values);  // test with too large of an array, result should be null padded
           for (int looper = 0; looper < values.length; looper ++)
           {
              assertTrue("toArray test", (values[looper] == null || list2.contains(values[looper])));
           }
  
        }
        catch (Exception e)
        {
           fail("testToArray " + e.getMessage());
           throw e;
        }
     }
  
     public void testAdd() throws Exception
     {
        LinkedList list = new LinkedList();
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           list.add("java");
           assertTrue("add test", list.contains("java"));
           assertFalse("add test", list.contains("c#"));
        }
        catch (Exception e)
        {
           fail("testAdd " + e.getMessage());
           throw e;
        }
  
     }
  
     public void testRemove() throws Exception
     {
        LinkedList list = new LinkedList();
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           list.add("java");
           assertTrue(list.remove("java"));
           assertFalse("remove test", list.contains("java"));
        }
        catch (Exception e)
        {
           fail("testRemove " + e.getMessage());
           throw e;
        }
  
     }
  
     public void testAddAll() throws Exception
     {
        LinkedList list = new LinkedList();
        LinkedList list2 = new LinkedList();
        list2.add("java");
        list2.add("c");
        list2.add("lisp");
        list2.add("c++");
        list2.add("forth");
  
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           assertTrue(list.addAll(list2));
           Object[] values = list.toArray();
  
           for (int looper = 0; looper < values.length; looper ++)
           {
              assertTrue("testAddAll", list2.contains(values[looper]));
           }
        }
        catch (Exception e)
        {
           fail("testAddAll " + e.getMessage());
           throw e;
        }
  
     }
  
     public void testClear() throws Exception
     {
        LinkedList list = new LinkedList();
        LinkedList list2 = new LinkedList();
        list2.add("java");
        list2.add("c");
        list2.add("lisp");
        list2.add("c++");
        list2.add("forth");
  
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           assertTrue(list.addAll(list2));
           assertTrue("testClear", list.size() > 0);
           list.clear();
           assertTrue("testClear", list.size() == 0);
        }
        catch (Exception e)
        {
           fail("testClear " + e.getMessage());
           throw e;
        }
  
     }
  
     public void testRetainAll() throws Exception
     {
        LinkedList list = new LinkedList();
        LinkedList list2 = new LinkedList();
        list2.add("java");
        list2.add("c");
        list2.add("lisp");
        list2.add("c++");
        list2.add("forth");
  
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           assertTrue(list.addAll(list2));
           list2.remove("c");
           list2.remove("lisp");
           list2.remove("c++");
           list2.remove("forth");
           assertTrue("testRetainAll", list.retainAll(list2));
           // should only have java left
           assertTrue("testRetainAll, list size should be 1 but is " + list.size(), list.size() == 1);
           assertTrue("testRetainAll", list.contains("java"));
        }
        catch (Exception e)
        {
           fail("testRetainAll " + e.getMessage());
           throw e;
        }
  
  
     }
  
     public void testRemoveAll() throws Exception
     {
        LinkedList list = new LinkedList();
        LinkedList list2 = new LinkedList();
        list2.add("java");
        list2.add("c");
        list2.add("lisp");
        list2.add("c++");
        list2.add("forth");
  
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           assertTrue(list.addAll(list2));
           list2.remove("java");
           assertTrue("testRemoveAll", list.removeAll(list2));
           // should only have java left
           assertTrue("testRemoveAll", list.size() == 1);
           assertTrue("testRemoveAll", list.contains("java"));
        }
  
        catch (Exception e)
        {
           fail("testRemoveAll " + e.getMessage());
           throw e;
        }
  
  
     }
  
     public void testContainsAll() throws Exception
     {
        LinkedList list = new LinkedList();
        LinkedList list2 = new LinkedList();
        list2.add("java");
        list2.add("c");
        list2.add("lisp");
        list2.add("c++");
        list2.add("forth");
  
        try
        {
           cache_.attach("/language/list", list);
           list = (LinkedList) cache_.find("/language/list");
           assertTrue(list.addAll(list2));
           list2.remove("java");
           assertTrue("testContainsAll", list.containsAll(list2));
           list.remove("c");
           assertFalse("testContainsAll", list.containsAll(list2));
        }
        catch (Exception e)
        {
           fail("testContainsAll " + e.getMessage());
           throw e;
        }
     }
  
     /**
      * Commented it out now. We should move this to perf directory.
      *
      * @throws Exception
      */
     public void XtestListAddPerformance() throws Exception
     {
        Collection cache_List = new LinkedList();
        Collection jdkList = new LinkedList();
        addPerformance(cache_List, jdkList, "List");
     }
  
     public void XtestSetAddPerformance() throws Exception
     {
        Collection cache_List = new HashSet();
        Collection jdkList = new HashSet();
        addPerformance(cache_List, jdkList, "Set");
     }
  
     public void XtestMapAddPerformance() throws Exception
     {
        Map cacheList = new HashMap();
        Map jdkList = new HashMap();
        addPerformance(cacheList, jdkList, "Map");
     }
  
     public void XtestListIterPerformance() throws Exception
     {
        Collection cacheList = new LinkedList();
        Collection jdkList = new LinkedList();
        iterPerformance(cacheList, jdkList, "List");
     }
  
     public void XtestSetIterPerformance() throws Exception
     {
        Collection cacheList = new HashSet();
        Collection jdkList = new HashSet();
        iterPerformance(cacheList, jdkList, "Set");
     }
  
     public void XtestMapIterPerformance() throws Exception
     {
        Map cacheList = new HashMap();
        Map jdkList = new HashMap();
        iterPerformance(cacheList, jdkList, "Map");
     }
  
     public void addPerformance(Collection cacheList, Collection jdkList, String label) throws Exception
     {
        try
        {
           cache_.attach("/language/list", cacheList);
           cacheList = (Collection) cache_.find("/language/list");
  
           long start = System.currentTimeMillis();
           collectionAdd(1000, jdkList);
           long jdkTime = System.currentTimeMillis() - start;
  
           start = System.currentTimeMillis();
           collectionAdd(1000, cacheList);
           long cacheTime = System.currentTimeMillis() - start;
           //assertTrue(label + " 1000 add performance, jdk time=" + jdkTime +", cache time=" + cacheTime, goodEnough(cacheTime,jdkTime));
           log_.info(label + " 1000 add performance, jdk time=" + jdkTime + ", cache time=" + cacheTime);
        }
        catch (Exception e)
        {
           fail("testAddAll " + e.getMessage());
           throw e;
        }
     }
  
     public void addPerformance(Map cacheList, Map jdkList, String label) throws Exception
     {
        try
        {
           cache_.attach("/language/list", cacheList);
           cacheList = (Map) cache_.find("/language/list");
  
           long start = System.currentTimeMillis();
           collectionAdd(1000, jdkList);
           long jdkTime = System.currentTimeMillis() - start;
  
           start = System.currentTimeMillis();
           collectionAdd(1000, cacheList);
           long cacheTime = System.currentTimeMillis() - start;
           //assertTrue(label + " 1000 add performance, jdk time=" + jdkTime +", cache time=" + cacheTime, goodEnough(cacheTime,jdkTime));
           log_.info(label + " 1000 add performance, jdk time=" + jdkTime + ", cache time=" + cacheTime);
        }
        catch (Exception e)
        {
           fail("testAddAll " + e.getMessage());
           throw e;
        }
     }
  
     public void iterPerformance(Collection cacheList, Collection jdkList, String label) throws Exception
     {
        cache_.attach("/language/list", cacheList);
        cacheList = (Collection) cache_.find("/language/list");
  
        collectionAdd(1000, jdkList);
        collectionAdd(1000, cacheList);
  
        long start = System.currentTimeMillis();
        Iterator iter = jdkList.iterator();
        while (iter.hasNext())
           iter.next();
        long jdkTime = System.currentTimeMillis() - start;
  
        start = System.currentTimeMillis();
        iter = cacheList.iterator();
        while (iter.hasNext())
           iter.next();
        long cacheTime = System.currentTimeMillis() - start;
        //assertTrue(label + " 1000 iter performance, jdk time=" + jdkTime +", cache time=" + cacheTime, goodEnough(cacheTime,jdkTime));
        log_.info(label + " 1000 iter performance, jdk time=" + jdkTime + ", cache time=" + cacheTime);
     }
  
     public void iterPerformance(Map cacheList, Map jdkList, String label) throws Exception
     {
        cache_.attach("/language/list", cacheList);
        cacheList = (Map) cache_.find("/language/list");
  
        collectionAdd(1000, jdkList);
        collectionAdd(1000, cacheList);
  
        long start = System.currentTimeMillis();
        Iterator iter = jdkList.keySet().iterator();
        while (iter.hasNext())
           iter.next();
        long jdkTime = System.currentTimeMillis() - start;
  
        start = System.currentTimeMillis();
        iter = cacheList.keySet().iterator();
        while (iter.hasNext())
           iter.next();
        long cacheTime = System.currentTimeMillis() - start;
  
        //assertTrue(label + " 1000 iter performance, jdk time=" + jdkTime +", cache time=" + cacheTime, goodEnough(cacheTime,jdkTime));
        log_.info(label + " 1000 iter performance, jdk time=" + jdkTime + ", cache time=" + cacheTime);
        System.out.println(label + " 1000 iter performance, jdk time=" + jdkTime + ", cache time=" + cacheTime);
     }
  
     public void collectionAdd(int count, Collection c) throws Exception
     {
        for (int looper = 0; looper < count; looper ++)
        {
           String value = Integer.toString(looper);
           c.add(value);
        }
     }
  
     public void collectionAdd(int count, Map c) throws Exception
     {
        for (int looper = 0; looper < count; looper ++)
        {
           String value = Integer.toString(looper);
           c.put(value, value);
        }
     }
  
     public boolean goodEnough(double cacheTime, double jdkTime)
     {
        if (jdkTime == 0)
           jdkTime = 1;
  
        double percent = ((cacheTime - jdkTime) / jdkTime) * 100.0;
  
        log_.info("percent =" + percent + " cache=" + cacheTime + " jdk =" + jdkTime);
        return percent < 100.0;
     }
  
  
  }
  
  
  
  
  1.1      date: 2006/07/17 14:41:28;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/collection/ObjectGraphTest.java
  
  Index: ObjectGraphTest.java
  ===================================================================
  package org.jboss.cache.pojo.collection;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.PropertyConfigurator;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.pojo.test.Person;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  /**
   * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
   *
   * @author Ben Wang
   */
  
  public class ObjectGraphTest extends TestCase
  {
     Log log = LogFactory.getLog(ObjectGraphTest.class);
     PojoCache cache_;
  
     public ObjectGraphTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        cache_ = PojoCacheFactory.createInstance(configFile);
        PropertyConfigurator config = new PropertyConfigurator();
        config.configure(cache_, configFile);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     protected Person createPerson(String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        return p;
     }
  
  
     public void testListWithMultipleSharedObjccts() throws Exception
     {
        log.info("testListWithMultipleSharedObjects() ....");
        List list1 = new ArrayList();
        Address addr = new Address();
        addr.setCity("San Jose");
        addr.setZip(95123);
        list1.add(addr);
  
        List list2 = new ArrayList();
        List list3 = new ArrayList();
        list2.add(addr);
        list3.add(addr);
  
        cache_.attach("/list1", list1);
        cache_.attach("/list2", list2);
        cache_.attach("/list3", list3);
  
        list3 = (List) cache_.find("/list3");
        assertTrue("List size should not be 0 ", (list3.size() != 0));
        assertEquals("Address ", addr.getZip(), ((Address) list3.get(0)).getZip());
        addr.setCity("Sunnyvale");
        assertEquals("Address ", addr.getCity(), ((Address) list3.get(0)).getCity());
  
        cache_.detach("/list1");
        cache_.detach("/list2");
        cache_.detach("/list3");
     }
  
     public void testRelationshipWithSharedList1() throws Exception
     {
        log.info("testRelationshipWithList() ....");
        List list1 = new ArrayList();
        Address addr = new Address();
        addr.setCity("San Jose");
        addr.setZip(95123);
        list1.add(addr);
  
        cache_.attach("/list", list1);
        list1 = (List) cache_.find("/list");
        assertEquals("List size should be ", 1, list1.size());
        cache_.attach("/alias", list1);
  
        list1 = (List) cache_.find("/list");
        assertEquals("List size should be ", 1, list1.size());
        List list2 = (List) cache_.find("/alias");
  //      System.out.println(cache_.printDetails());
        assertEquals("List size should be ", 1, list2.size());
        assertEquals("Both lists should be equal ", list1, list2);
        assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
     }
  
     public void testRelationshipWithSharedList2() throws Exception
     {
        log.info("testRelationshipWithList2() ....");
        List list1 = new ArrayList();
        Address addr = new Address();
        addr.setCity("San Jose");
        addr.setZip(95123);
        list1.add(addr);
  
        List list2 = new ArrayList();
        list2.add(addr);
  
        cache_.attach("/list1", list1);
        cache_.attach("/list2", list2);
        Address add2 = (Address) ((List) cache_.find("/list2")).get(0);
        Address add1 = (Address) ((List) cache_.find("/list1")).get(0);
        assertEquals("Address should be the same", add1, add2);
        assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
     }
  
     public void testListWithAttachAndDetach() throws Exception
     {
        log.info("testListWithAttachAndDetach() ....");
        List list1 = new ArrayList();
        Address addr1 = new Address();
        addr1.setCity("San Jose");
        addr1.setZip(95123);
  
        Address addr2 = new Address();
        addr2.setCity("Sunnyvale");
        addr2.setZip(94086);
        list1.add(addr2);
  
        cache_.attach("/list", list1);
        list1 = (List) cache_.find("/list");
        assertEquals("List size should be ", 1, list1.size());
        cache_.attach("/alias", list1);
  
        list1 = (List) cache_.find("/list");
        assertEquals("List size should be ", 1, list1.size());
        List list2 = (List) cache_.find("/alias");
  //      System.out.println(cache_.printDetails());
        assertEquals("List size should be ", 1, list2.size());
        assertEquals("Both lists should be equal ", list1, list2);
        assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
     }
  
     public void testRelationshipWithSharedSet1() throws Exception
     {
        log.info("testRelationshipWithSet() ....");
        Set set1 = new HashSet();
        Address addr = new Address();
        addr.setCity("San Jose");
        addr.setZip(95123);
        set1.add(addr);
  
        // Pure set
        cache_.attach("/set", set1);
        // We specifically need to use Proxy otherwise it won't work with multiple references
        set1 = (Set) cache_.find("/set");
        cache_.attach("/alias", set1);
  
        set1 = (Set) cache_.find("/set");
        Set set2 = (Set) cache_.find("/alias");
        assertTrue("Set size should not be 0 ", (set2.size() != 0));
        assertEquals("Both sets should be equal ", set1, set2);
        Address add1 = (Address) set2.iterator().next();
        assertNotNull("Address should not be null", add1);
        assertEquals("Zip ", 95123, add1.getZip());
     }
  
     public void testRelationshipWithSharedSet2() throws Exception
     {
        log.info("testRelationshipWithSet2() ....");
        Set set1 = new HashSet();
        Address addr = new Address();
        addr.setCity("San Jose");
        addr.setZip(95123);
        set1.add(addr);
  
        Set set2 = new HashSet();
        set2.add(addr);
  
        cache_.attach("/set1", set1);
        cache_.attach("/set2", set2);
  
        Address add2 = (Address) ((Set) cache_.find("/set2")).iterator().next();
        Address add1 = (Address) ((Set) cache_.find("/set1")).iterator().next();
        assertEquals("Address should be the same", add1, add2);
        assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
     }
  
     public void testRelationshipWithSharedMap1() throws Exception
     {
        log.info("testRelationshipWithMap() ....");
        Map map1 = new HashMap();
        Address addr = new Address();
        addr.setCity("San Jose");
        addr.setZip(95123);
        map1.put("key1", addr);
  
        cache_.attach("/map", map1);
        cache_.attach("/alias", map1);
  
        map1 = (Map) cache_.find("/map");
        Map map2 = (Map) cache_.find("/alias");
        assertTrue("Map size should not be 0 ", (map2.size() != 0));
        assertEquals("Both maps should be equal ", map1, map2);
        Address add1 = (Address) ((Map.Entry) map2.entrySet().iterator().next()).getValue();
        assertNotNull("Address should not be null", add1);
        assertEquals("Zip ", 95123, add1.getZip());
     }
  
     public void testRelationshipWithSharedMap2() throws Exception
     {
        log.info("testRelationshipWithMap2() ....");
        Map map1 = new HashMap();
        Address addr = new Address();
        addr.setCity("San Jose");
        addr.setZip(95123);
        map1.put("key1", addr);
  
        Map map2 = new HashMap();
        map2.put("key2", addr);
  
        // Pure map
        cache_.attach("/map", map1);
        cache_.attach("/alias", map2);
  
        map1 = (Map) cache_.find("/map");
        map2 = (Map) cache_.find("/alias");
        assertTrue("Map size should not be 0 ", (map2.size() != 0));
        Address add1 = (Address) ((Map.Entry) map2.entrySet().iterator().next()).getValue();
        assertNotNull("Address should not be null", add1);
        assertEquals("Zip ", 95123, add1.getZip());
     }
  
  
     public void testObjectIdentity() throws Exception
     {
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ObjectGraphTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  



More information about the jboss-cvs-commits mailing list