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.Set ; 22 import java.util.HashSet ; 23 import java.lang.reflect.Field ; 24 25 29 30 public class SetTest extends TestCase 31 { 32 Log log_ = LogFactory.getLog(SetTest.class); 33 PojoCache cache_; 34 static Throwable ex1_; 35 static boolean pre_; 36 static boolean post_; 37 static int counter_; 38 39 public SetTest(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 SetTest.ex1_ = null; 59 SetTest.pre_ = false; 60 SetTest.post_ = false; 61 SetTest.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 SetTest.MyListener listener = new SetTest.MyListener(); 77 cache_.addListener(listener); 78 Set set = new HashSet (); 79 set.add("test1"); 80 set.add("test2"); 81 cache_.attach("a", set); 82 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 83 assertTrue("pre-attach event is not emitted", SetTest.pre_); 84 assertTrue("post-attach event is not emitted", SetTest.post_); 85 assertEquals("Total number of event is ", 2, SetTest.counter_); 87 88 set = (Set )cache_.find("a"); 89 set.remove("test2"); 90 set.add("test3"); 91 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 92 assertTrue("pre-attach event is not emitted", SetTest.pre_); 93 assertTrue("post-attach event is not emitted", SetTest.post_); 94 assertEquals("Total number of event is ", 2, SetTest.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 HashSet set = new HashSet (); 107 set.add("English"); 108 set.add("Taiwanese"); 109 test.setSkills(set); 110 111 SetTest.MyListener listener = new SetTest.MyListener(); 112 cache_.addListener(listener); 113 cache_.attach("a", test); 114 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 115 assertTrue("pre-attach event is not emitted", SetTest.pre_); 116 assertTrue("post-attach event is not emitted", SetTest.post_); 117 assertEquals("Total number of event is ", 4, SetTest.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 Set set = new HashSet (); 127 Address addr1 = new Address(); 128 addr1.setCity("Taipei"); 129 130 Address addr2 = new Address(); 131 addr2.setCity("Taipei"); 132 133 set.add(addr1); 134 set.add(addr2); 135 cache_.attach("a", set); 136 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 137 assertTrue("pre-attach event is not emitted", SetTest.pre_); 138 assertTrue("post-attach event is not emitted", SetTest.post_); 139 assertEquals("Total number of event is ", 6, SetTest.counter_); 141 142 listener.reset(); 143 set = (Set )cache_.find("a"); 144 set.remove(addr2); 145 146 Address addr3 = new Address(); 147 addr3.setCity("Taipei"); 148 set.add(addr3); 149 150 assertNull("Exception should be null but " + ListTest.ex1_, SetTest.ex1_); 151 assertTrue("pre-attach event is not emitted", SetTest.pre_); 152 assertTrue("post-attach event is not emitted", SetTest.post_); 153 assertEquals("Total number of event is ", 4, SetTest.counter_); 155 156 cache_.removeListener(listener); 157 } 158 159 160 public void testDetachNotification1() throws Exception 161 { 162 log_.info("testDetachNotification1() ...."); 163 SetTest.MyListener listener = new SetTest.MyListener(); 164 cache_.addListener(listener); 165 HashSet set = new HashSet (); 166 set.add("test1"); 167 set.add("test2"); 168 cache_.attach("a", set); 169 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 170 assertTrue("pre-attach event is not emitted", SetTest.pre_); 171 assertTrue("post-attach event is not emitted", SetTest.post_); 172 assertEquals("Total number of event is ", 2, SetTest.counter_); 174 175 listener.reset(); 176 cache_.detach("a"); 177 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 178 assertTrue("pre-attach event is not emitted", SetTest.pre_); 179 assertTrue("post-attach event is not emitted", SetTest.post_); 180 assertEquals("Total number of event is ", 2, SetTest.counter_); 182 183 cache_.removeListener(listener); 184 } 185 186 public void testDetachNotification2() throws Exception 187 { 188 log_.info("testDetachNotification2() ...."); 189 Person test = new Person(); 190 test.setName("Ben"); 191 test.setAge(10); 192 HashSet set = new HashSet (); 193 set.add("English"); 194 set.add("Taiwanese"); 195 test.setSkills(set); 196 197 SetTest.MyListener listener = new SetTest.MyListener(); 198 cache_.addListener(listener); 199 cache_.attach("a", test); 200 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 201 assertTrue("pre-attach event is not emitted", SetTest.pre_); 202 assertTrue("post-attach event is not emitted", SetTest.post_); 203 assertEquals("Total number of event is ", 4, SetTest.counter_); 204 205 listener.reset(); 206 cache_.detach("a"); 207 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 208 assertTrue("pre-attach event is not emitted", SetTest.pre_); 209 assertTrue("post-attach event is not emitted", SetTest.post_); 210 assertEquals("Total number of event is ", 4, SetTest.counter_); 211 212 cache_.removeListener(listener); 213 } 214 215 public void testDetachNotification3() throws Exception 216 { 217 log_.info("testAttachNotification3() ...."); 218 MyListener listener = new MyListener(); 219 cache_.addListener(listener); 220 Set set = new HashSet (); 221 Address addr1 = new Address(); 222 addr1.setCity("Taipei"); 223 224 Address addr2 = new Address(); 225 addr2.setCity("Taipei"); 226 227 set.add(addr1); 228 set.add(addr2); 229 cache_.attach("a", set); 230 assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_); 231 assertTrue("pre-attach event is not emitted", SetTest.pre_); 232 assertTrue("post-attach event is not emitted", SetTest.post_); 233 assertEquals("Total number of event is ", 6, SetTest.counter_); 235 236 listener.reset(); 237 set = (Set )cache_.find("a"); 238 set.remove(addr2); 239 240 Address addr3 = new Address(); 241 addr3.setCity("Taipei"); 242 set.add(addr3); 243 244 assertNull("Exception should be null but " + ListTest.ex1_, SetTest.ex1_); 245 assertTrue("pre-attach event is not emitted", SetTest.pre_); 246 assertTrue("post-attach event is not emitted", SetTest.post_); 247 assertEquals("Total number of event is ", 4, SetTest.counter_); 249 250 listener.reset(); 251 cache_.detach("a"); 252 253 assertNull("Exception should be null but " + ListTest.ex1_, SetTest.ex1_); 254 assertTrue("pre-attach event is not emitted", SetTest.pre_); 255 assertTrue("post-attach event is not emitted", SetTest.post_); 256 assertEquals("Total number of event is ", 6, SetTest.counter_); 258 259 cache_.removeListener(listener); 260 } 261 262 263 public static Test suite() throws Exception 264 { 265 return new TestSuite(SetTest.class); 266 } 267 268 269 public static void main(String [] args) throws Exception 270 { 271 junit.textui.TestRunner.run(SetTest.suite()); 272 } 273 274 public class MyListener implements PojoCacheListener 275 { 276 public MyListener() 277 { 278 } 279 280 public void reset() 281 { 282 SetTest.pre_ = false; 283 SetTest.post_ = false; 284 SetTest.counter_ = 0; 285 } 286 287 public void attach(Object pojo, boolean pre, boolean isLocal) 288 { 289 if(pre) 290 { 291 SetTest.pre_ = true; 292 SetTest.counter_++; 293 } else 294 { 295 SetTest.post_ = true; 296 SetTest.counter_++; 297 } 298 } 299 300 public void detach(Object pojo, boolean pre, boolean isLocal) 301 { 302 if(pre) 303 { 304 SetTest.pre_ = true; 305 SetTest.counter_++; 306 } else 307 { 308 SetTest.post_ = true; 309 SetTest.counter_++; 310 } 311 } 312 313 public void modify(Object pojo, Field field, boolean pre, boolean isLocal) 314 { 315 if(pre) 316 { 317 SetTest.pre_ = true; 318 SetTest.counter_++; 319 } else 320 { 321 SetTest.post_ = true; 322 SetTest.counter_++; 323 } 324 } 325 326 public void passivate(Object pojo, boolean pre) 327 { 328 throw new RuntimeException ("passivate event not yet supported."); 329 } 330 331 public void evict(Object pojo, boolean pre) 332 { 333 throw new RuntimeException ("evict event not yet supported."); 334 } 335 336 public void activate(Object pojo, boolean pre) 337 { 338 throw new RuntimeException ("activate event not yet supported."); 339 } 340 } 341 } 342 | Popular Tags |