1 22 package org.jboss.test.timer.test; 23 24 import java.rmi.RemoteException ; 25 import java.rmi.ServerException ; 26 import java.util.Date ; 27 import java.util.HashMap ; 28 import javax.ejb.EJBHome ; 29 import javax.ejb.NoSuchObjectLocalException ; 30 import javax.jms.Message ; 31 import javax.jms.Queue ; 32 import javax.jms.QueueConnection ; 33 import javax.jms.QueueConnectionFactory ; 34 import javax.jms.QueueReceiver ; 35 import javax.jms.QueueSender ; 36 import javax.jms.QueueSession ; 37 import javax.jms.TextMessage ; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 import javax.security.auth.login.LoginContext ; 41 42 import junit.framework.Test; 43 import junit.framework.TestSuite; 44 import org.jboss.test.JBossTestCase; 45 import org.jboss.test.JBossTestSetup; 46 import org.jboss.test.util.AppCallbackHandler; 47 import org.jboss.test.timer.interfaces.TimerEntity; 48 import org.jboss.test.timer.interfaces.TimerEntityHome; 49 import org.jboss.test.timer.interfaces.TimerSFSB; 50 import org.jboss.test.timer.interfaces.TimerSFSBHome; 51 import org.jboss.test.timer.interfaces.TimerSLSBHome; 52 import org.jboss.test.timer.interfaces.TimerSLSB; 53 54 57 public class BasicTimerUnitTestCase extends JBossTestCase 58 { 59 60 private static final String EJB_TIMER_XAR = "ejb-timer.ear"; 61 62 private static final int SHORT_PERIOD = 1 * 1000; private static final int LONG_PERIOD = 20 * 1000; private LoginContext lc; 65 66 69 public static Test suite() throws Exception 70 { 71 TestSuite suite = new TestSuite(); 72 73 suite.addTest(new JBossTestSetup(new TestSuite(BasicTimerUnitTestCase.class)) 74 { 75 protected void setUp() throws Exception 76 { 77 super.setUp(); 78 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 79 deploy (loader.getResource("messaging/test-destinations-full-service.xml").toString()); 80 deploy (EJB_TIMER_XAR); 81 } 82 protected void tearDown() throws Exception 83 { 84 super.tearDown(); 85 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 86 undeploy (EJB_TIMER_XAR); 87 undeploy (loader.getResource("messaging/test-destinations-full-service.xml").toString()); 88 } 89 }); 90 91 return suite; 92 } 93 94 97 public BasicTimerUnitTestCase(String pName) 98 { 99 super(pName); 100 } 101 102 public void tearDown() 103 { 104 } 105 106 111 public void testStatefulSessionBeanTimer() 112 throws Exception 113 { 114 TimerSFSBHome lHome = (TimerSFSBHome) getEJBHome(TimerSFSBHome.JNDI_NAME); 115 TimerSFSB lBean = lHome.create(); 116 try 117 { 118 lBean.checkTimerService(); 119 fail("Stateful Session Bean is not allowed to get a Timer Service"); 120 } 121 catch (RemoteException re) 122 { 123 Throwable lCause = re.detail; 124 if (lCause instanceof ServerException ) 125 { 126 lCause = ((ServerException ) lCause).detail; 127 if (lCause instanceof IllegalStateException ) 128 { 129 } 131 else 132 { 133 throw re; 134 } 135 } 136 } 137 } 138 139 144 public void testStatelessSessionBeanTimer() 145 throws Exception 146 { 147 TimerSLSBHome home = (TimerSLSBHome) getEJBHome(TimerSLSBHome.JNDI_NAME); 148 TimerSLSB bean = home.create(); 149 byte[] handle = bean.startTimer(SHORT_PERIOD); 150 Thread.sleep(12 * SHORT_PERIOD + SHORT_PERIOD); 151 int count = bean.getTimeoutCount(handle); 152 bean.stopTimer(handle); 153 assertTrue("Timeout was expected to be called at least 10 times but was " 154 + "only called: " + count + " times", 155 count >= 10); 156 Thread.sleep(5 * SHORT_PERIOD); 157 int count2 = bean.getTimeoutCount(handle); 158 assertTrue("After the timer was stopped no timeout should happen but " 159 + "it was called " + count2 + " more times", 160 count2 == 0); 161 bean.remove(); 162 } 163 164 174 public void testStatelessSessionBeanTimerRetry() 175 throws Exception 176 { 177 log.info("testStatelessSessionBeanTimerRetry(): start"); 178 TimerSLSBHome home = (TimerSLSBHome) getEJBHome(TimerSLSBHome.JNDI_NAME); 179 TimerSLSB bean = home.create(); 180 181 final long retryMs = bean.getRetryTimeoutPeriod(); 184 log.info("testStatelessSessionBeanTimerRetry():GOT RETRY TIME:" + retryMs); 185 assertFalse("Failed to get valid retry timeout!", retryMs == -1); 186 final HashMap info = new HashMap (); 187 info.put(TimerSLSB.INFO_EXEC_FAIL_COUNT,new Integer (1)); final int taskTime = SHORT_PERIOD * 2; 194 info.put(TimerSLSB.INFO_TASK_RUNTIME,new Integer (taskTime)); 196 final byte[] handle = bean.startTimer(SHORT_PERIOD,info); 197 Thread.sleep(SHORT_PERIOD + retryMs + taskTime + 200); 201 int count = bean.getTimeoutCount(handle); 202 bean.stopTimer(handle); 203 assertEquals("Timeout was called too many times. Should have been once for the initial" + 204 ", and once for the retry during the time allotted.",2,count); 205 206 bean.remove(); 207 } 208 209 210 215 public void testStatelessSessionBeanSingleTimer() 216 throws Exception 217 { 218 TimerSLSBHome home = (TimerSLSBHome) getEJBHome(TimerSLSBHome.JNDI_NAME); 219 TimerSLSB bean = home.create(); 220 byte[] handle = bean.startSingleTimer(SHORT_PERIOD); 221 Thread.sleep(5 * SHORT_PERIOD); 222 int lCount = bean.getTimeoutCount(handle); 223 assertTrue("Timeout was expected to be called only once but was called: " 224 + lCount + " times", 225 lCount == 1); 226 try 227 { 228 bean.stopTimer(handle); 229 fail("A single timer should expire after the first event and therefore this " 230 + "has to throw an NoSuchObjectLocalException"); 231 } 232 catch (RemoteException re) 233 { 234 Throwable lCause = re.detail; 235 if (lCause instanceof ServerException ) 236 { 237 lCause = ((ServerException ) lCause).detail; 238 if (lCause instanceof NoSuchObjectLocalException ) 239 { 240 } 242 else 243 { 244 throw re; 245 } 246 } 247 } 248 249 log.info("testStatelessSessionBeanSingleTimer(): Testing retry on timer."); 254 final HashMap info = new HashMap (1); 255 info.put(TimerSLSB.INFO_EXEC_FAIL_COUNT,new Integer (1)); 256 handle = bean.startSingleTimer(SHORT_PERIOD,info); 257 Thread.sleep(5 * SHORT_PERIOD); 258 assertEquals("Timeout was expected to be called twice, once inititially, one once for the retry.", 259 2,bean.getTimeoutCount(handle)); 260 261 262 } 263 264 269 public void testEntityBeanTimer() 270 throws Exception 271 { 272 TimerEntityHome home = (TimerEntityHome) getEJBHome(TimerEntityHome.JNDI_NAME); 273 TimerEntity entity = home.create(new Integer (111)); 274 entity.startTimer(SHORT_PERIOD); 275 Thread.sleep(12 * SHORT_PERIOD); 276 entity.stopTimer(); 277 int lCount = entity.getTimeoutCount(); 278 assertTrue("Timeout was expected to be called at least 10 times but was " 279 + "only called: " + lCount + " times", 280 lCount >= 10); 281 Thread.sleep(5 * SHORT_PERIOD); 282 int lCount2 = entity.getTimeoutCount(); 283 assertTrue("After the timer was stopped no timeout should happen but " 284 + "it was called " + (lCount2 - lCount) + " more times", 285 lCount == lCount2); 286 entity.remove(); 287 } 288 289 294 public void testEntityBeanSingleTimer() 295 throws Exception 296 { 297 TimerEntityHome home = (TimerEntityHome) getEJBHome(TimerEntityHome.JNDI_NAME); 298 TimerEntity entity = home.create(new Integer (222)); 299 entity.startSingleTimer(SHORT_PERIOD); 300 Thread.sleep(5 * SHORT_PERIOD); 301 int lCount = entity.getTimeoutCount(); 302 assertTrue("Timeout was expected to be called only once but was called: " 303 + lCount + " times", 304 lCount == 1); 305 try 306 { 307 entity.stopTimer(); 308 fail("A single timer should expire after the first event and therefore this " 309 + "has to throw an NoSuchObjectLocalException"); 310 } 311 catch (RemoteException re) 312 { 313 Throwable lCause = re.detail; 314 if (lCause instanceof ServerException ) 315 { 316 lCause = ((ServerException ) lCause).detail; 317 if (lCause instanceof NoSuchObjectLocalException ) 318 { 319 } 321 else 322 { 323 throw re; 324 } 325 } 326 } 327 entity.remove(); 328 } 329 330 333 public void testMDBTimer() throws Exception 334 { 335 InitialContext ctx = new InitialContext (); 336 QueueConnectionFactory factory = (QueueConnectionFactory ) ctx.lookup("ConnectionFactory"); 337 QueueConnection queConn = factory.createQueueConnection(); 338 queConn.start(); 339 340 Queue queueA = (Queue ) ctx.lookup("queue/A"); 341 Queue queueB = (Queue ) ctx.lookup("queue/B"); 342 343 QueueSession session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 344 QueueSender sender = session.createSender(queueA); 345 TextMessage message = session.createTextMessage(); 346 message.setText("testMDBTimer"); 347 message.setIntProperty("UNIQUE_ID", 123456789); 348 message.setJMSReplyTo(queueB); 349 sender.send(message); 350 QueueReceiver receiver = session.createReceiver(queueB); 352 Message reply = receiver.receive(15000); 353 log.info("onMessage reply: " + reply); 354 assertTrue("onMessage reply != null", reply != null); 355 int id = reply.getIntProperty("UNIQUE_ID"); 356 assertTrue("onMessage reply.id = 123456789", id == 123456789); 357 reply = receiver.receive(15000); 359 log.info("ejbTimeout reply: " + reply); 360 assertTrue("ejbTimeout reply != null", reply != null); 361 id = reply.getIntProperty("UNIQUE_ID"); 362 assertTrue("onMessage reply.id = 123456789", id == 123456789); 363 364 session.close(); 365 queConn.close(); 366 } 367 368 371 public void testOnCreateMDBTimer() throws Exception 372 { 373 log.info("+++ testOnCreateMDBTimer"); 374 InitialContext ctx = new InitialContext (); 375 QueueConnectionFactory factory = (QueueConnectionFactory ) ctx.lookup("ConnectionFactory"); 376 QueueConnection queConn = factory.createQueueConnection(); 377 queConn.start(); 378 379 Queue queueA = (Queue ) ctx.lookup("queue/C"); 380 Queue queueB = (Queue ) ctx.lookup("queue/D"); 381 382 QueueSession session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 383 QueueSender sender = session.createSender(queueA); 384 TextMessage message = session.createTextMessage(); 385 message.setText("testOnCreateMDBTimer"); 386 message.setIntProperty("UNIQUE_ID", 123456788); 387 message.setJMSReplyTo(queueB); 388 sender.send(message); 389 QueueReceiver receiver = session.createReceiver(queueB); 391 Message reply = receiver.receive(15000); 392 log.info("onMessage reply: " + reply); 393 assertTrue("onMessage reply != null", reply != null); 394 int id = reply.getIntProperty("UNIQUE_ID"); 395 assertTrue("onMessage reply.id = 123456788", id == 123456788); 396 397 for(int n = 0; n < 10; n ++) 399 { 400 reply = receiver.receive(15000); 401 log.info("ejbTimeout reply: " + reply); 402 assertTrue("ejbTimeout reply != null", reply != null); 403 id = reply.getIntProperty("UNIQUE_ID"); 404 assertTrue("onMessage reply.id = 123456788", id == 123456788); 405 long elapsed = reply.getLongProperty("Elapsed"); 406 log.info("Elapsed: "+elapsed); 407 } 408 409 session.close(); 410 queConn.close(); 411 } 412 413 418 public void testTimerImplementation() 419 throws Exception 420 { 421 TimerSLSBHome home = (TimerSLSBHome) getEJBHome(TimerSLSBHome.JNDI_NAME); 422 TimerSLSB bean = home.create(); 423 byte[] handle = bean.startTimer(LONG_PERIOD); 424 Date lNextEvent = bean.getNextTimeout(handle); 425 long lUntilNextEvent = lNextEvent.getTime() - new Date ().getTime(); 426 Thread.sleep(SHORT_PERIOD); 427 long lTimeRemaining = bean.getTimeRemaining(handle); 428 Object info = bean.getInfo(handle); 429 assertTrue("Date of the next event must be greater than 0", lUntilNextEvent > 0); 430 assertTrue("Period until next event must be greater than 0", lTimeRemaining > 0); 431 assertTrue("Period until next event must be smaller than time until next even because it " 432 + "it is called later", lUntilNextEvent > lTimeRemaining); 433 assertTrue("Info("+info+") must be 'TimerSLSBean.startTimer'", 434 "TimerSLSBean.startTimer".equals(info)); 435 assertTrue("Must be able to get a handle", handle != null); 436 bean.stopTimer(handle); 437 } 438 439 445 public void testBadStatelessSessionBeanTimer() 446 throws Exception 447 { 448 TimerSLSBHome home = (TimerSLSBHome) getEJBHome("ejb/test/timer/NoTimedObjectBean"); 449 TimerSLSB bean = home.create(); 450 try 451 { 452 bean.startTimer(SHORT_PERIOD); 453 fail("Was able to call NoTimedObjectBean.startTimer"); 454 } 455 catch(RemoteException e) 456 { 457 log.info("Saw exception as expected", e); 458 } 459 bean.remove(); 460 } 461 462 private EJBHome getEJBHome(String pJNDIName) 463 throws NamingException 464 { 465 InitialContext lContext = new InitialContext (); 466 return (EJBHome ) lContext.lookup(pJNDIName); 467 } 468 469 private void login() throws Exception 470 { 471 lc = null; 472 String username = "jduke"; 473 char[] password = "theduke".toCharArray(); 474 AppCallbackHandler handler = new AppCallbackHandler(username, password); 475 log.debug("Creating LoginContext(ejb-timers)"); 476 lc = new LoginContext ("ejb-timers", handler); 477 lc.login(); 478 log.debug("Created LoginContext, subject="+lc.getSubject()); 479 } 480 private void logout() throws Exception 481 { 482 if( lc != null ) 483 { 484 lc.logout(); 485 lc = null; 486 } 487 } 488 } 489 | Popular Tags |