1 22 package org.jboss.test.jmx.compliance.timer; 23 24 import java.util.ArrayList ; 25 import java.util.Date ; 26 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerFactory ; 29 import javax.management.Notification ; 30 import javax.management.NotificationListener ; 31 import javax.management.ObjectName ; 32 import javax.management.timer.Timer ; 33 import javax.management.timer.TimerNotification ; 34 35 import junit.framework.TestCase; 36 37 42 public class TimerNotificationTestCase 43 extends TestCase 44 implements NotificationListener 45 { 46 48 String TIMER_TYPE = "TimerType"; 49 String MESSAGE = "Message"; 50 String USER_DATA = "UserData"; 51 52 54 57 MBeanServer server; 58 59 62 ObjectName timerName; 63 64 67 Timer timer; 68 69 72 Integer id; 73 74 77 ArrayList notifications = new ArrayList (); 78 79 81 84 public TimerNotificationTestCase(String s) 85 { 86 super(s); 87 } 88 89 91 94 public void testReasonableValues() 95 { 96 initTest(); 97 try 98 { 99 initTimer(); 100 startTimer(); 101 long startTime = timeOffset(0).getTime(); 102 addNotification(TimerSUITE.ZERO_TIME); 103 sync(); 104 stopTimer(); 105 long endTime = timeOffset(0).getTime(); 106 107 assertEquals(1, notifications.size()); 108 TimerNotification tn = (TimerNotification ) notifications.get(0); 109 assertEquals(MESSAGE, tn.getMessage()); 110 assertEquals(1, tn.getSequenceNumber()); 111 assertEquals(timerName, tn.getSource()); 112 assertEquals(TIMER_TYPE, tn.getType()); 113 assertEquals(USER_DATA, tn.getUserData()); 114 assertEquals(id, tn.getNotificationID()); 115 if (tn.getTimeStamp() < startTime + TimerSUITE.ZERO_TIME) 116 fail("Timer notification before start?"); 117 if (tn.getTimeStamp() > endTime) 118 fail("Timer notification after end?"); 119 } 120 finally 121 { 122 MBeanServerFactory.releaseMBeanServer(server); 123 } 124 } 125 126 128 131 private void initTest() 132 { 133 notifications.clear(); 134 server = MBeanServerFactory.createMBeanServer(); 135 } 136 137 140 private void initTimer() 141 { 142 try 143 { 144 timer = new Timer (); 145 timerName = new ObjectName ("test:type=timer"); 146 server.registerMBean(timer, timerName); 147 server.addNotificationListener(timerName, this, null, null); 148 } 149 catch (Exception e) 150 { 151 fail(e.toString()); 152 } 153 } 154 155 158 private void startTimer() 159 { 160 timer.start(); 161 } 162 163 166 private void stopTimer() 167 { 168 sleep(); 169 timer.stop(); 170 } 171 172 175 private void addNotification(long offset) 176 { 177 id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA, 178 timeOffset(TimerSUITE.ZERO_TIME)); 179 } 180 181 184 public void handleNotification(Notification n, Object ignored) 185 { 186 notifications.add(n); 187 synchronized(notifications) 188 { 189 notifications.notifyAll(); 190 } 191 } 192 193 196 private void sync() 197 { 198 synchronized(notifications) 199 { 200 try 201 { 202 notifications.wait(TimerSUITE.MAX_WAIT); 203 } 204 catch (InterruptedException ignored) 205 { 206 } 207 } 208 } 209 210 213 private Date timeOffset(long offset) 214 { 215 return new Date (System.currentTimeMillis() + offset); 216 } 217 218 221 private void sleep() 222 { 223 sleep(TimerSUITE.ZERO_TIME); 224 } 225 226 229 private void sleep(long time) 230 { 231 try 232 { 233 Thread.sleep(time); 234 } 235 catch (InterruptedException ignored) 236 { 237 } 238 } 239 } 240 | Popular Tags |