1 7 8 package org.jboss.cache.pojo.event; 9 10 import junit.framework.TestCase; 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.pojo.PojoCache; 16 import org.jboss.cache.pojo.PojoCacheFactory; 17 import org.jboss.cache.pojo.PojoCacheListener; 18 import org.jboss.cache.pojo.test.Person; 19 import org.jboss.cache.pojo.test.Address; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import java.lang.reflect.Field ; 24 25 29 30 public class MapTest extends TestCase 31 { 32 Log log_ = LogFactory.getLog(MapTest.class); 33 PojoCache cache_; 34 static Throwable ex1_; 35 static boolean pre_; 36 static boolean post_; 37 static int counter_; 38 39 public MapTest(String name) 40 { 41 super(name); 42 } 43 44 protected void setUp() throws Exception 45 { 46 super.setUp(); 47 log_.info("setUp() ...."); 48 String configFile = "META-INF/local-service.xml"; 49 boolean toStart = false; 50 cache_ = PojoCacheFactory.createCache(configFile, toStart); 51 cache_.start(); 52 53 reset(); 54 } 55 56 private void reset() 57 { 58 MapTest.ex1_ = null; 59 MapTest.pre_ = false; 60 MapTest.post_ = false; 61 MapTest.counter_ = 0; 62 63 } 64 65 protected void tearDown() throws Exception 66 { 67 super.tearDown(); 68 cache_.stop(); 69 } 70 71 73 public void testAttachNotification1() throws Exception 74 { 75 log_.info("testAttachNotification1() ...."); 76 MapTest.MyListener listener = new MapTest.MyListener(); 77 cache_.addListener(listener); 78 Map map = new HashMap (); 79 map.put("test1","test1"); 80 map.put("test2","test2"); 81 cache_.attach("a", map); 82 assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_); 83 assertTrue("pre-attach event is not emitted", MapTest.pre_); 84 assertTrue("post-attach event is not emitted", MapTest.post_); 85 assertEquals("Total number of event is ", 2, MapTest.counter_); 87 88 map = (Map )cache_.find("a"); 89 map.remove("test2"); 90 map.put("test3", "test3"); 91 assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_); 92 assertTrue("pre-attach event is not emitted", MapTest.pre_); 93 assertTrue("post-attach event is not emitted", MapTest.post_); 94 assertEquals("Total number of event is ", 2, MapTest.counter_); 96 97 cache_.removeListener(listener); 98 } 99 100 public void testAttachNotification2() throws Exception 101 { 102 log_.info("testAttachNotification2() ...."); 103 Person test = new Person(); 104 test.setName("Ben"); 105 test.setAge(10); 106 HashMap map = new HashMap (); 107 map.put("test1", "English"); 108 map.put("test2", "Taiwanese"); 109 test.setHobbies(map); 110 111 MapTest.MyListener listener = new MapTest.MyListener(); 112 cache_.addListener(listener); 113 cache_.attach("a", test); 114 assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_); 115 assertTrue("pre-attach event is not emitted", MapTest.pre_); 116 assertTrue("post-attach event is not emitted", MapTest.post_); 117 assertEquals("Total number of event is ", 4, MapTest.counter_); 118 cache_.removeListener(listener); 119 } 120 121 public void testAttachNotification3() throws Exception 122 { 123 log_.info("testAttachNotification3() ...."); 124 MyListener listener = new MyListener(); 125 cache_.addListener(listener); 126 Map map = new HashMap (); 127 Address addr1 = new Address(); 128 addr1.setCity("Taipei"); 129 130 Address addr2 = new Address(); 131 addr2.setCity("Taipei"); 132 133 map.put("1", addr1); 134 map.put("2", addr2); 135 cache_.attach("a", map); 136 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 137 assertTrue("pre-attach event is not emitted", MapTest.pre_); 138 assertTrue("post-attach event is not emitted", MapTest.post_); 139 assertEquals("Total number of event is ", 6, MapTest.counter_); 141 142 listener.reset(); 143 map = (Map )cache_.find("a"); 144 map.remove("2"); 145 146 Address addr3 = new Address(); 147 addr3.setCity("Taipei"); 148 map.put("3", addr3); 149 150 assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_); 151 assertTrue("pre-attach event is not emitted", MapTest.pre_); 152 assertTrue("post-attach event is not emitted", MapTest.post_); 153 assertEquals("Total number of event is ", 4, MapTest.counter_); 155 156 cache_.removeListener(listener); 157 } 158 159 public void testDetachNotification1() throws Exception 160 { 161 log_.info("testDetachNotification1() ...."); 162 MapTest.MyListener listener = new MapTest.MyListener(); 163 cache_.addListener(listener); 164 HashMap map = new HashMap (); 165 map.put("test1", "test1"); 166 map.put("test2", "test2"); 167 cache_.attach("a", map); 168 assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_); 169 assertTrue("pre-attach event is not emitted", MapTest.pre_); 170 assertTrue("post-attach event is not emitted", MapTest.post_); 171 assertEquals("Total number of event is ", 2, MapTest.counter_); 173 174 listener.reset(); 175 cache_.detach("a"); 176 assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_); 177 assertTrue("pre-attach event is not emitted", MapTest.pre_); 178 assertTrue("post-attach event is not emitted", MapTest.post_); 179 assertEquals("Total number of event is ", 2, MapTest.counter_); 181 182 cache_.removeListener(listener); 183 } 184 185 public void testDetachNotification2() throws Exception 186 { 187 log_.info("testDetachNotification2() ...."); 188 Person test = new Person(); 189 test.setName("Ben"); 190 test.setAge(10); 191 HashMap map = new HashMap (); 192 map.put("test1", "English"); 193 map.put("test2", "Taiwanese"); 194 test.setHobbies(map); 195 196 MapTest.MyListener listener = new MapTest.MyListener(); 197 cache_.addListener(listener); 198 cache_.attach("a", test); 199 assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_); 200 assertTrue("pre-attach event is not emitted", MapTest.pre_); 201 assertTrue("post-attach event is not emitted", MapTest.post_); 202 assertEquals("Total number of event is ", 4, MapTest.counter_); 203 204 listener.reset(); 205 cache_.detach("a"); 206 assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_); 207 assertTrue("pre-attach event is not emitted", MapTest.pre_); 208 assertTrue("post-attach event is not emitted", MapTest.post_); 209 assertEquals("Total number of event is ", 4, MapTest.counter_); 210 211 cache_.removeListener(listener); 212 } 213 214 public void testDetachNotification3() throws Exception 215 { 216 log_.info("testAttachNotification3() ...."); 217 MyListener listener = new MyListener(); 218 cache_.addListener(listener); 219 Map map = new HashMap (); 220 Address addr1 = new Address(); 221 addr1.setCity("Taipei"); 222 223 Address addr2 = new Address(); 224 addr2.setCity("Taipei"); 225 226 map.put("1", addr1); 227 map.put("2", addr2); 228 cache_.attach("a", map); 229 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 230 assertTrue("pre-attach event is not emitted", MapTest.pre_); 231 assertTrue("post-attach event is not emitted", MapTest.post_); 232 assertEquals("Total number of event is ", 6, MapTest.counter_); 234 235 listener.reset(); 236 map = (Map )cache_.find("a"); 237 map.remove("2"); 238 239 Address addr3 = new Address(); 240 addr3.setCity("Taipei"); 241 map.put("3", addr3); 242 243 assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_); 244 assertTrue("pre-attach event is not emitted", MapTest.pre_); 245 assertTrue("post-attach event is not emitted", MapTest.post_); 246 assertEquals("Total number of event is ", 4, MapTest.counter_); 248 249 listener.reset(); 250 cache_.detach("a"); 251 252 assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_); 253 assertTrue("pre-attach event is not emitted", MapTest.pre_); 254 assertTrue("post-attach event is not emitted", MapTest.post_); 255 assertEquals("Total number of event is ", 6, MapTest.counter_); 257 258 cache_.removeListener(listener); 259 } 260 261 public static Test suite() throws Exception 262 { 263 return new TestSuite(MapTest.class); 264 } 265 266 267 public static void main(String [] args) throws Exception 268 { 269 junit.textui.TestRunner.run(MapTest.suite()); 270 } 271 272 public class MyListener implements PojoCacheListener 273 { 274 public MyListener() 275 { 276 } 277 278 public void reset() 279 { 280 MapTest.pre_ = false; 281 MapTest.post_ = false; 282 MapTest.counter_ = 0; 283 } 284 285 public void attach(Object pojo, boolean pre, boolean isLocal) 286 { 287 if(pre) 288 { 289 MapTest.pre_ = true; 290 MapTest.counter_++; 291 } else 292 { 293 MapTest.post_ = true; 294 MapTest.counter_++; 295 } 296 } 297 298 public void detach(Object pojo, boolean pre, boolean isLocal) 299 { 300 if(pre) 301 { 302 MapTest.pre_ = true; 303 MapTest.counter_++; 304 } else 305 { 306 MapTest.post_ = true; 307 MapTest.counter_++; 308 } 309 } 310 311 public void modify(Object pojo, Field field, boolean pre, boolean isLocal) 312 { 313 if(pre) 314 { 315 MapTest.pre_ = true; 316 MapTest.counter_++; 317 } else 318 { 319 MapTest.post_ = true; 320 MapTest.counter_++; 321 } 322 } 323 324 public void passivate(Object pojo, boolean pre) 325 { 326 throw new RuntimeException ("passivate event not yet supported."); 327 } 328 329 public void evict(Object pojo, boolean pre) 330 { 331 throw new RuntimeException ("evict event not yet supported."); 332 } 333 334 public void activate(Object pojo, boolean pre) 335 { 336 throw new RuntimeException ("activate event not yet supported."); 337 } 338 } 339 } 340 | Popular Tags |