1 package org.jboss.cache.tests.aop; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.aop.proxy.ClassProxy; 9 import org.jboss.cache.Fqn; 10 import org.jboss.cache.aop.TreeCacheAop; 11 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 19 20 24 25 public class CachedMapAopTest extends TestCase 26 { 27 Log log=LogFactory.getLog(CachedMapAopTest.class); 28 TreeCacheAopTester tester; 29 Map hobbies; 30 31 public CachedMapAopTest(String name) 32 { 33 super(name); 34 } 35 36 protected void setUp() throws Exception 37 { 38 super.setUp(); 39 log.info("setUp() ...."); 40 String configFile = "META-INF/replSync-service.xml"; 41 tester = new TreeCacheAopTester(configFile); 42 tester.createPerson("/person/test7", "p7", 27); 43 tester.setHobby("/person/test7", "1", "golf"); 44 tester.setHobby("/person/test7", "2", "tennis"); 45 tester.setHobby("/person/test7", "3", "polo"); 46 hobbies = tester.getHobbies("/person/test7"); 47 System.out.println("Cache: "+ tester.getCache().printDetails()); 48 } 49 50 protected void tearDown() throws Exception 51 { 52 super.tearDown(); 53 tester.stop(); 54 tester = null; 55 } 56 57 61 public void testPut() throws Throwable { 62 Map map = new HashMap (); 63 map.put("4", "pingpong"); 64 map.put("5", "handball"); 65 66 tester.setHobbies("/person/test7", map); 67 hobbies = tester.getHobbies("/person/test7"); 68 if( !(hobbies instanceof ClassProxy || hobbies instanceof Map ) ) { 69 fail("testPut(): hobbies is not instance of ClassProxy nor Map"); 70 } 71 72 int size = hobbies.size(); 73 assertEquals("Size is ", 2, size); 74 75 hobbies.put("6", "baseball"); 76 hobbies = tester.getHobbies("/person/test7"); 77 size = hobbies.size(); 78 assertEquals("Size is ", 3, size); 79 80 } 81 82 public void testAddAndRemoveIndex() throws Throwable 83 { 84 int size = hobbies.size(); 85 assertEquals("Size is ", 3, size); 86 87 hobbies.put("4", "baseball"); 88 size = hobbies.size(); 89 assertEquals("Size is ", 4, size); 90 91 assertTrue("Skill contain Golf ", hobbies.containsKey("3")); 92 93 hobbies.remove("3"); 94 size = hobbies.size(); 95 assertEquals("Size is ", 3, size); 96 assertFalse("Skill does not contain 3 anymore ", hobbies.containsKey("3")); 97 98 hobbies.clear(); 99 size = hobbies.size(); 100 assertEquals("Size is ", 0, size); 101 102 assertTrue("Should be empty", hobbies.isEmpty()); 103 } 104 105 public void testPutAllEtc() throws Throwable { 106 int size = hobbies.size(); 107 assertEquals("Size is ", 3, size); 108 109 Map map = new HashMap (); 110 map.put("4", "pingpong"); 111 map.put("5", "handball"); 112 113 hobbies.putAll(map); 114 size = hobbies.size(); 115 assertEquals("Size is ", 5, size); 116 117 assertTrue("Key is ", hobbies.containsKey("4")); 118 119 Set keys = hobbies.keySet(); 120 assertEquals("Key size ", 5, keys.size()); 121 122 Set entries = hobbies.entrySet(); 123 assertEquals("Entry size ", 5, entries.size()); 124 125 } 126 127 public void testEntrySet() throws Throwable { 128 Fqn fqn = Fqn.fromString("/person/test7"); 129 TreeCacheAop cache = tester.getCache(); 130 Map map = tester.getHobbies("/person/test7"); 131 System.out.println("Map "+ map.toString()); 132 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 133 Map.Entry entry = (Map.Entry ) i.next(); 134 System.out.println("Entry key and value "+ entry.getKey()+ " "+ entry.getValue()); 135 } 136 } 137 138 public void testEquals() throws Throwable { 139 Map map = tester.getHobbies("/person/test7"); 140 assertTrue("Map should be the same ", map.equals(hobbies)); 141 map = new HashMap (); 142 map.put("1", "test"); 143 map.put("4", "test"); 144 map.put("2", "tennis"); 145 assertFalse("Map should not be the same ", map.equals(hobbies)); 146 } 147 148 public static Test suite() throws Exception 149 { 150 return new TestSuite(CachedMapAopTest.class); 151 } 152 153 public static void main(String [] args) throws Exception 154 { 155 junit.textui.TestRunner.run(suite()); 156 } 157 158 } 159 160 | Popular Tags |