KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > timer > TimerNotificationTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.timer;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Date JavaDoc;
26
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerFactory JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.timer.Timer JavaDoc;
33 import javax.management.timer.TimerNotification JavaDoc;
34
35 import junit.framework.TestCase;
36
37 /**
38  * Timer Notification Tests
39  *
40  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
41  */

42 public class TimerNotificationTestCase
43    extends TestCase
44    implements NotificationListener JavaDoc
45 {
46    // Constants ---------------------------------------------------------------
47

48    String JavaDoc TIMER_TYPE = "TimerType";
49    String JavaDoc MESSAGE = "Message";
50    String JavaDoc USER_DATA = "UserData";
51
52    // Attributes --------------------------------------------------------------
53

54    /**
55     * The MBeanServer
56     */

57    MBeanServer JavaDoc server;
58
59    /**
60     * The object name of the timer
61     */

62    ObjectName JavaDoc timerName;
63
64    /**
65     * The timer
66     */

67    Timer JavaDoc timer;
68
69    /**
70     * The timer notification id
71     */

72    Integer JavaDoc id;
73
74    /**
75     * The notifications
76     */

77    ArrayList JavaDoc notifications = new ArrayList JavaDoc();
78
79    // Constructor -------------------------------------------------------------
80

81    /**
82     * Construct the test
83     */

84    public TimerNotificationTestCase(String JavaDoc s)
85    {
86       super(s);
87    }
88
89    // Tests -------------------------------------------------------------------
90

91    /**
92     * Test a notification gives reasonable values
93     */

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 JavaDoc tn = (TimerNotification JavaDoc) 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    // Support -----------------------------------------------------------------
127

128    /**
129     * Start a new test
130     */

131    private void initTest()
132    {
133       notifications.clear();
134       server = MBeanServerFactory.createMBeanServer();
135    }
136
137    /**
138     * Create a timer and register ourselves as a listener
139     */

140    private void initTimer()
141    {
142       try
143       {
144          timer = new Timer JavaDoc();
145          timerName = new ObjectName JavaDoc("test:type=timer");
146          server.registerMBean(timer, timerName);
147          server.addNotificationListener(timerName, this, null, null);
148       }
149       catch (Exception JavaDoc e)
150       {
151          fail(e.toString());
152       }
153    }
154
155    /**
156     * Start the timer
157     */

158    private void startTimer()
159    {
160       timer.start();
161    }
162
163    /**
164     * Stop the timer, does a small wait to avoid problems with the RI
165     */

166    private void stopTimer()
167    {
168       sleep();
169       timer.stop();
170    }
171
172    /**
173     * Add a notification
174     */

175    private void addNotification(long offset)
176    {
177       id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA,
178                                  timeOffset(TimerSUITE.ZERO_TIME));
179    }
180
181    /**
182     * Handle the notification, just add it to the list
183     */

184    public void handleNotification(Notification JavaDoc n, Object JavaDoc ignored)
185    {
186       notifications.add(n);
187       synchronized(notifications)
188       {
189          notifications.notifyAll();
190       }
191    }
192
193    /**
194     * Sync with the notification handler
195     */

196    private void sync()
197    {
198       synchronized(notifications)
199       {
200          try
201          {
202             notifications.wait(TimerSUITE.MAX_WAIT);
203          }
204          catch (InterruptedException JavaDoc ignored)
205          {
206          }
207       }
208    }
209
210    /**
211     * Get the time using and offset
212     */

213    private Date JavaDoc timeOffset(long offset)
214    {
215       return new Date JavaDoc(System.currentTimeMillis() + offset);
216    }
217
218    /**
219     * Sleep for the default time
220     */

221    private void sleep()
222    {
223       sleep(TimerSUITE.ZERO_TIME);
224    }
225
226    /**
227     * Sleep for a bit
228     */

229    private void sleep(long time)
230    {
231       try
232       {
233          Thread.sleep(time);
234       }
235       catch (InterruptedException JavaDoc ignored)
236       {
237       }
238    }
239 }
240
Popular Tags