KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > timer > TimerNotificationTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 /**
24  * Timer Notification Tests
25  *
26  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
27  */

28 public class TimerNotificationTestCase
29    extends TestCase
30    implements NotificationListener
31 {
32    // Constants ---------------------------------------------------------------
33

34    String TIMER_TYPE = "TimerType";
35    String MESSAGE = "Message";
36    String USER_DATA = "UserData";
37
38    // Attributes --------------------------------------------------------------
39

40    /**
41     * The MBeanServer
42     */

43    MBeanServer server;
44
45    /**
46     * The object name of the timer
47     */

48    ObjectName timerName;
49
50    /**
51     * The timer
52     */

53    Timer timer;
54
55    /**
56     * The timer notification id
57     */

58    Integer id;
59
60    /**
61     * The notifications
62     */

63    ArrayList notifications = new ArrayList();
64
65    // Constructor -------------------------------------------------------------
66

67    /**
68     * Construct the test
69     */

70    public TimerNotificationTestCase(String s)
71    {
72       super(s);
73    }
74
75    // Tests -------------------------------------------------------------------
76

77    /**
78     * Test a notification gives reasonable values
79     */

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    // Support -----------------------------------------------------------------
113

114    /**
115     * Start a new test
116     */

117    private void initTest()
118    {
119       notifications.clear();
120       server = MBeanServerFactory.createMBeanServer();
121    }
122
123    /**
124     * Create a timer and register ourselves as a listener
125     */

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    /**
142     * Start the timer
143     */

144    private void startTimer()
145    {
146       timer.start();
147    }
148
149    /**
150     * Stop the timer, does a small wait to avoid problems with the RI
151     */

152    private void stopTimer()
153    {
154       sleep();
155       timer.stop();
156    }
157
158    /**
159     * Add a notification
160     */

161    private void addNotification(long offset)
162    {
163       id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA,
164                                  timeOffset(TimerSUITE.ZERO_TIME));
165    }
166
167    /**
168     * Handle the notification, just add it to the list
169     */

170    public void handleNotification(Notification n, Object ignored)
171    {
172       notifications.add(n);
173       synchronized(notifications)
174       {
175          notifications.notifyAll();
176       }
177    }
178
179    /**
180     * Sync with the notification handler
181     */

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    /**
197     * Get the time using and offset
198     */

199    private Date timeOffset(long offset)
200    {
201       return new Date(System.currentTimeMillis() + offset);
202    }
203
204    /**
205     * Sleep for the default time
206     */

207    private void sleep()
208    {
209       sleep(TimerSUITE.ZERO_TIME);
210    }
211
212    /**
213     * Sleep for a bit
214     */

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