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