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 23 28 29 public class LocalTest extends TestCase 30 { 31 Log log_ = LogFactory.getLog(LocalTest.class); 32 PojoCache cache_; 33 static Throwable ex1_; 34 static boolean pre_; 35 static boolean post_; 36 static int counter_; 37 38 public LocalTest(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 ex1_ = null; 58 pre_ = false; 59 post_ = false; 60 counter_ = 0; 61 62 } 63 64 protected void tearDown() throws Exception 65 { 66 super.tearDown(); 67 cache_.stop(); 68 } 69 70 72 public void testAttachNotification() throws Exception 73 { 74 log_.info("testAttachNotification() ...."); 75 Person test = new Person(); 76 test.setName("Ben"); 77 test.setAge(10); 78 MyListener listener = new MyListener(test); 79 cache_.addListener(listener); 80 cache_.attach("/a", test); 81 assertNull("Exception should be null but " +ex1_, ex1_); 82 assertTrue("pre-attach event is not emitted", pre_); 83 assertTrue("post-attach event is not emitted", post_); 84 assertEquals("Total number of event is ", 2, counter_); 85 cache_.removeListener(listener); 86 } 87 88 public void testAttachNotification2() throws Exception 89 { 90 log_.info("testAttachNotification2() ...."); 91 Person test = new Person(); 92 test.setName("Ben"); 93 test.setAge(10); 94 Address addr = new Address(); 95 test.setAddress(addr); 96 97 MyListener listener = new MyListener(test); 98 cache_.addListener(listener); 99 cache_.attach("/a", test); 100 assertNull("Exception should be null but " +ex1_, ex1_); 101 assertTrue("pre-attach event is not emitted", pre_); 102 assertTrue("post-attach event is not emitted", post_); 103 104 assertEquals("Total number of event is ", 4, counter_); 105 cache_.removeListener(listener); 106 } 107 108 public void testDetachNotification() throws Exception 109 { 110 log_.info("testDetachNotification() ...."); 111 Person test = new Person(); 112 test.setName("Ben"); 113 test.setAge(10); 114 MyListener listener = new MyListener(test); 115 cache_.addListener(listener); 116 cache_.attach("/a", test); 117 assertNull("Exception should be null but " +ex1_, ex1_); 118 assertTrue("pre-attach event is not emitted", pre_); 119 assertTrue("post-attach event is not emitted", post_); 120 reset(); 121 122 cache_.detach("/a"); 123 assertNull("Exception should be null but " +ex1_, ex1_); 124 assertTrue("pre-detach event is not emitted", pre_); 125 assertTrue("post-detach event is not emitted", post_); 126 cache_.removeListener(listener); 127 } 128 129 public void testFieldNotification() throws Exception 130 { 131 log_.info("testAttachNotification() ...."); 132 Person test = new Person(); 133 test.setName("Ben"); 134 test.setAge(10); 135 MyListener listener = new MyListener(test); 136 cache_.addListener(listener); 137 cache_.attach("/a", test); 138 assertNull("Exception should be null but " +ex1_, ex1_); 139 assertTrue("pre-attach event is not emitted", pre_); 140 assertTrue("post-attach event is not emitted", post_); 141 142 reset(); 143 test.setAge(20); 145 assertNull("Exception should be null but " +ex1_, ex1_); 146 assertTrue("pre-attach event is not emitted", pre_); 147 assertTrue("post-attach event is not emitted", post_); 148 assertEquals("Total number of event is ", 2, counter_); 149 150 cache_.removeListener(listener); 151 } 152 153 public static Test suite() throws Exception 154 { 155 return new TestSuite(LocalTest.class); 156 } 157 158 159 public static void main(String [] args) throws Exception 160 { 161 junit.textui.TestRunner.run(LocalTest.suite()); 162 } 163 164 public class MyListener implements PojoCacheListener 165 { 166 Object pojo; 167 168 public MyListener(Object pojo) 169 { 170 this.pojo = pojo; 171 } 172 173 public void attach(Object pojo, boolean pre, boolean isLocal) 174 { 175 if(pre) 176 { 177 pre_ = true; 178 counter_++; 179 } else 180 { 181 post_ = true; 182 counter_++; 183 } 184 } 185 186 public void detach(Object pojo, boolean pre, boolean isLocal) 187 { 188 if(pre) 189 { 190 pre_ = true; 191 counter_++; 192 } else 193 { 194 post_ = true; 195 counter_++; 196 } 197 } 198 199 public void modify(Object pojo, Field field, boolean pre, boolean isLocal) 200 { 201 if(pre) 202 { 203 pre_ = true; 204 counter_++; 205 } else 206 { 207 post_ = true; 208 counter_++; 209 } 210 } 211 212 public void passivate(Object pojo, boolean pre) 213 { 214 throw new RuntimeException ("passivate event not yet supported."); 215 } 216 217 public void evict(Object pojo, boolean pre) 218 { 219 throw new RuntimeException ("evict event not yet supported."); 220 } 221 222 public void activate(Object pojo, boolean pre) 223 { 224 throw new RuntimeException ("activate event not yet supported."); 225 } 226 } 227 } 228 | Popular Tags |