1 22 package org.jboss.test.txtimer.test; 23 24 import java.io.Serializable ; 25 import java.util.Date ; 26 import java.util.List ; 27 28 import javax.management.MBeanServerConnection ; 29 import javax.management.MBeanServerInvocationHandler ; 30 import javax.management.ObjectName ; 31 import javax.naming.InitialContext ; 32 33 import junit.framework.TestSuite; 34 35 import org.jboss.ejb.txtimer.DatabasePersistencePolicyMBean; 36 import org.jboss.ejb.txtimer.EJBTimerService; 37 import org.jboss.ejb.txtimer.PersistencePolicy; 38 import org.jboss.ejb.txtimer.TimedObjectId; 39 import org.jboss.ejb.txtimer.TimerHandleImpl; 40 import org.jboss.mx.util.ObjectNameFactory; 41 import org.jboss.test.JBossTestCase; 42 import org.jboss.test.txtimer.interfaces.TimerEntity; 43 import org.jboss.test.txtimer.interfaces.TimerEntityHome; 44 import org.jboss.test.txtimer.interfaces.TimerSession; 45 import org.jboss.test.txtimer.interfaces.TimerSessionHome; 46 47 55 public class PersistenceTestCase extends JBossTestCase 56 { 57 private PersistencePolicy pp; 58 59 public PersistenceTestCase(String name) 60 { 61 super(name); 62 } 63 64 public static TestSuite suite() throws Exception 65 { 66 TestSuite ts = new TestSuite(); 67 ts.addTest(getDeploySetup(PersistenceTestCase.class, "ejb-txtimer.jar")); 68 return ts; 69 } 70 71 protected void setUp() throws Exception 72 { 73 super.setUp(); 74 pp = new PersistencePolicyDelegate(getServer()); 75 } 76 77 80 public void testSingleEventDuration() throws Exception 81 { 82 List timerHandles = pp.listTimerHandles(); 84 assertEquals("unexpected handle count", 0, timerHandles.size()); 85 86 InitialContext iniCtx = getInitialContext(); 87 TimerEntityHome home = (TimerEntityHome)iniCtx.lookup(TimerEntityHome.JNDI_NAME); 88 TimerEntity entity = home.create(new Integer (1)); 89 try 90 { 91 entity.createTimer(500, 0, null); 92 93 timerHandles = pp.listTimerHandles(); 94 assertEquals("unexpected handle count", 1, timerHandles.size()); 95 96 sleep(1000); 97 assertEquals("unexpected call count", 1, entity.getCallCount()); 98 99 timerHandles = pp.listTimerHandles(); 100 assertEquals("unexpected handle count", 0, timerHandles.size()); 101 } 102 finally 103 { 104 entity.remove(); 105 } 106 } 107 108 111 public void testRestoreToEntity() throws Exception 112 { 113 List timerHandles = pp.listTimerHandles(); 115 assertEquals("unexpected handle count", 0, timerHandles.size()); 116 117 InitialContext iniCtx = getInitialContext(); 118 TimerEntityHome home = (TimerEntityHome)iniCtx.lookup(TimerEntityHome.JNDI_NAME); 119 TimerEntity entity = home.create(new Integer (1)); 120 try 121 { 122 ObjectName oname = ObjectNameFactory.create("jboss.j2ee:jndiName=test/txtimer/TimerEntity,service=EJB"); 124 TimedObjectId targetId = new TimedObjectId(oname, new Integer (1)); 125 pp.insertTimer("pk1", targetId, new Date (), 0, null); 126 sleep(500); 127 128 timerHandles = pp.listTimerHandles(); 129 assertEquals("unexpected handle count", 1, timerHandles.size()); 130 131 try 134 { 135 getServer().invoke( 136 EJBTimerService.OBJECT_NAME, 137 "restoreTimers", 138 new Object [] { oname, null }, 139 new String []{"javax.management.ObjectName", "java.lang.ClassLoader" } 140 ); 141 } 142 catch (Exception e) 143 { 144 log.warn("Could not restore ejb timers", e); 145 } 146 sleep(500); 147 assertEquals("unexpected call count", 1, entity.getCallCount()); 148 149 timerHandles = pp.listTimerHandles(); 150 assertEquals("unexpected handle count", 0, timerHandles.size()); 151 } 152 finally 153 { 154 entity.remove(); 155 } 156 } 157 158 161 public void testRestoreToSession() throws Exception 162 { 163 List timerHandles = pp.listTimerHandles(); 165 assertEquals("unexpected handle count", 0, timerHandles.size()); 166 167 InitialContext iniCtx = getInitialContext(); 168 TimerSessionHome home = (TimerSessionHome)iniCtx.lookup(TimerSessionHome.JNDI_NAME); 169 TimerSession session = home.create(); 170 session.resetCallCount(); 171 try 172 { 173 ObjectName oname = ObjectNameFactory.create("jboss.j2ee:jndiName=test/txtimer/TimerSession,service=EJB"); 175 TimedObjectId targetId = new TimedObjectId(oname); 176 pp.insertTimer("pk1", targetId, new Date (), 0, null); 177 sleep(500); 178 179 timerHandles = pp.listTimerHandles(); 180 assertEquals("unexpected handle count", 1, timerHandles.size()); 181 182 try 185 { 186 getServer().invoke( 187 EJBTimerService.OBJECT_NAME, 188 "restoreTimers", 189 new Object [] { oname, null }, 190 new String []{"javax.management.ObjectName", "java.lang.ClassLoader" } 191 ); 192 } 193 catch (Exception e) 194 { 195 log.warn("Could not restore ejb timers", e); 196 } 197 sleep(500); 198 assertEquals("unexpected call count", 1, session.getGlobalCallCount()); 199 200 timerHandles = pp.listTimerHandles(); 201 assertEquals("unexpected handle count", 0, timerHandles.size()); 202 } 203 finally 204 { 205 session.remove(); 206 } 207 } 208 209 212 public void testPersistenceEquality() throws Exception 213 { 214 List timerHandles = pp.listTimerHandles(); 216 assertEquals("unexpected handle count", 0, timerHandles.size()); 217 218 String timerId = "pk1"; 220 ObjectName oname = ObjectNameFactory.create("jboss.j2ee:jndiName=test/txtimer/TimerEntity,service=EJB"); 221 TimedObjectId targetId = new TimedObjectId(oname, new Integer (100)); 222 Date firstEvent = new Date (); 223 String info = "info"; 224 pp.insertTimer(timerId, targetId, firstEvent, 100, info); 225 226 timerHandles = pp.listTimerHandles(); 227 assertEquals("unexpected handle count", 1, timerHandles.size()); 228 229 TimerHandleImpl handle = (TimerHandleImpl)timerHandles.get(0); 230 assertEquals("TimerId is not equal", timerId, handle.getTimerId()); 231 assertEquals("TimedObjectId is not equal", targetId, handle.getTimedObjectId()); 232 assertEquals("firstEvent is not equal", firstEvent, handle.getFirstTime()); 233 assertEquals("periode is not equal", 100, handle.getPeriode()); 234 assertEquals("info is not equal", info, handle.getInfo()); 235 236 pp.clearTimers(); 237 238 timerHandles = pp.listTimerHandles(); 239 assertEquals("unexpected handle count", 0, timerHandles.size()); 240 } 241 242 245 static class PersistencePolicyDelegate implements PersistencePolicy 246 { 247 DatabasePersistencePolicyMBean proxy; 248 249 public PersistencePolicyDelegate(MBeanServerConnection server) 250 { 251 proxy = (DatabasePersistencePolicyMBean) 252 MBeanServerInvocationHandler.newProxyInstance(server, 253 DatabasePersistencePolicyMBean.OBJECT_NAME, 254 DatabasePersistencePolicyMBean.class, false); 255 } 256 257 public void insertTimer(String timerId, TimedObjectId targetId, Date firstEvent, long periode, Serializable info) 258 { 259 try 260 { 261 proxy.insertTimer(timerId, targetId, firstEvent, periode, info); 262 } 263 catch (Exception e) 264 { 265 throw new RuntimeException (e); 266 } 267 } 268 269 public void deleteTimer(String timerId, TimedObjectId targetId) 270 { 271 try 272 { 273 proxy.deleteTimer(timerId, targetId); 274 } 275 catch (Exception e) 276 { 277 throw new RuntimeException (e); 278 } 279 } 280 281 public void clearTimers() 282 { 283 try 284 { 285 proxy.clearTimers(); 286 } 287 catch (Exception e) 288 { 289 throw new RuntimeException (e); 290 } 291 } 292 293 public void restoreTimers() 294 { 295 try 296 { 297 proxy.resetAndRestoreTimers(); 298 } 299 catch (Exception e) 300 { 301 throw new RuntimeException (e); 302 } 303 } 304 305 public List listTimerHandles(ObjectName containerId, ClassLoader loader) 306 { 307 List list = null; 308 try 309 { 310 list = proxy.listTimerHandles(containerId, loader); 311 } 312 catch (Exception e) 313 { 314 throw new RuntimeException (e); 315 } 316 return list; 317 } 318 319 public List listTimerHandles() 320 { 321 List list = null; 322 try 323 { 324 list = proxy.listTimerHandles(); 325 } 326 catch (Exception e) 327 { 328 throw new RuntimeException (e); 329 } 330 return list; 331 } 332 } 333 } 334 | Popular Tags |