1 7 8 package test.compliance.timer; 9 10 import java.util.ArrayList; 11 import java.util.Date; 12 13 import javax.management.MBeanServer; 14 import javax.management.MBeanServerFactory; 15 import javax.management.Notification; 16 import javax.management.NotificationListener; 17 import javax.management.ObjectName; 18 import javax.management.timer.Timer; 19 import javax.management.timer.TimerNotification; 20 21 import junit.framework.TestCase; 22 23 28 public class TimerNotificationTestCase 29 extends TestCase 30 implements NotificationListener 31 { 32 34 String TIMER_TYPE = "TimerType"; 35 String MESSAGE = "Message"; 36 String USER_DATA = "UserData"; 37 38 40 43 MBeanServer server; 44 45 48 ObjectName timerName; 49 50 53 Timer timer; 54 55 58 Integer id; 59 60 63 ArrayList notifications = new ArrayList(); 64 65 67 70 public TimerNotificationTestCase(String s) 71 { 72 super(s); 73 } 74 75 77 80 public void testReasonableValues() 81 { 82 initTest(); 83 try 84 { 85 initTimer(); 86 startTimer(); 87 long startTime = timeOffset(0).getTime(); 88 addNotification(TimerSUITE.ZERO_TIME); 89 sync(); 90 stopTimer(); 91 long endTime = timeOffset(0).getTime(); 92 93 assertEquals(1, notifications.size()); 94 TimerNotification tn = (TimerNotification) notifications.get(0); 95 assertEquals(MESSAGE, tn.getMessage()); 96 assertEquals(1, tn.getSequenceNumber()); 97 assertEquals(timerName, tn.getSource()); 98 assertEquals(TIMER_TYPE, tn.getType()); 99 assertEquals(USER_DATA, tn.getUserData()); 100 assertEquals(id, tn.getNotificationID()); 101 if (tn.getTimeStamp() < startTime + TimerSUITE.ZERO_TIME) 102 fail("Timer notification before start?"); 103 if (tn.getTimeStamp() > endTime) 104 fail("Timer notification after end?"); 105 } 106 finally 107 { 108 MBeanServerFactory.releaseMBeanServer(server); 109 } 110 } 111 112 114 117 private void initTest() 118 { 119 notifications.clear(); 120 server = MBeanServerFactory.createMBeanServer(); 121 } 122 123 126 private void initTimer() 127 { 128 try 129 { 130 timer = new Timer(); 131 timerName = new ObjectName("test:type=timer"); 132 server.registerMBean(timer, timerName); 133 server.addNotificationListener(timerName, this, null, null); 134 } 135 catch (Exception e) 136 { 137 fail(e.toString()); 138 } 139 } 140 141 144 private void startTimer() 145 { 146 timer.start(); 147 } 148 149 152 private void stopTimer() 153 { 154 sleep(); 155 timer.stop(); 156 } 157 158 161 private void addNotification(long offset) 162 { 163 id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA, 164 timeOffset(TimerSUITE.ZERO_TIME)); 165 } 166 167 170 public void handleNotification(Notification n, Object ignored) 171 { 172 notifications.add(n); 173 synchronized(notifications) 174 { 175 notifications.notifyAll(); 176 } 177 } 178 179 182 private void sync() 183 { 184 synchronized(notifications) 185 { 186 try 187 { 188 notifications.wait(TimerSUITE.MAX_WAIT); 189 } 190 catch (InterruptedException ignored) 191 { 192 } 193 } 194 } 195 196 199 private Date timeOffset(long offset) 200 { 201 return new Date(System.currentTimeMillis() + offset); 202 } 203 204 207 private void sleep() 208 { 209 sleep(TimerSUITE.ZERO_TIME); 210 } 211 212 215 private void sleep(long time) 216 { 217 try 218 { 219 Thread.sleep(time); 220 } 221 catch (InterruptedException ignored) 222 { 223 } 224 } 225 } 226 | Popular Tags |