KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mx4j > remote > RemoteNotificationServerHandlerTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.mx4j.remote;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13 import javax.management.Notification JavaDoc;
14 import javax.management.NotificationListener JavaDoc;
15 import javax.management.remote.NotificationResult JavaDoc;
16
17 import mx4j.remote.DefaultRemoteNotificationServerHandler;
18 import mx4j.remote.MX4JRemoteConstants;
19 import mx4j.remote.RemoteNotificationServerHandler;
20 import test.MX4JTestCase;
21
22 /**
23  * @version $Revision: 1.6 $
24  */

25 public class RemoteNotificationServerHandlerTest extends MX4JTestCase
26 {
27    public RemoteNotificationServerHandlerTest(String JavaDoc s)
28    {
29       super(s);
30    }
31
32    public void testFirstFetch() throws Exception JavaDoc
33    {
34       RemoteNotificationServerHandler handler = new DefaultRemoteNotificationServerHandler(null);
35
36       // First call is with sequence number negative; no notification waiting
37
NotificationResult JavaDoc result = handler.fetchNotifications(-1, 10, 100);
38
39       assertEquals(result.getEarliestSequenceNumber(), 0);
40       assertEquals(result.getNextSequenceNumber(), 0);
41       assertNotNull(result.getTargetedNotifications());
42       assertEquals(result.getTargetedNotifications().length, 0);
43    }
44
45    public void testNotificationMissBetweenAddAndFetch() throws Exception JavaDoc
46    {
47       RemoteNotificationServerHandler handler = new DefaultRemoteNotificationServerHandler(null);
48
49       // Add a notification
50
NotificationListener JavaDoc listener = handler.getServerNotificationListener();
51       Notification JavaDoc notification = new Notification JavaDoc("dummy", this, 0);
52       Integer JavaDoc listenerID = new Integer JavaDoc(1);
53       listener.handleNotification(notification, listenerID);
54
55       // Fetch notifications
56
NotificationResult JavaDoc result = handler.fetchNotifications(-1, 10, 100);
57       assertEquals(result.getEarliestSequenceNumber(), 0);
58       assertEquals(result.getNextSequenceNumber(), 1);
59       assertNotNull(result.getTargetedNotifications());
60       assertEquals(result.getTargetedNotifications().length, 0);
61
62       // Add another notification
63
listener.handleNotification(notification, listenerID);
64
65       // Fetch again
66
result = handler.fetchNotifications(result.getNextSequenceNumber(), 10, 100);
67       assertEquals(result.getEarliestSequenceNumber(), 0);
68       assertEquals(result.getNextSequenceNumber(), 2);
69       assertNotNull(result.getTargetedNotifications());
70       assertEquals(result.getTargetedNotifications().length, 1);
71    }
72
73    public void testBufferWithSmallCapacity() throws Exception JavaDoc
74    {
75       int bufferCapacity = 1;
76       Map JavaDoc environment = new HashMap JavaDoc();
77       environment.put(MX4JRemoteConstants.NOTIFICATION_BUFFER_CAPACITY, new Integer JavaDoc(bufferCapacity));
78       RemoteNotificationServerHandler handler = new DefaultRemoteNotificationServerHandler(environment);
79
80       // Fill the buffer
81
NotificationListener JavaDoc listener = handler.getServerNotificationListener();
82       Notification JavaDoc notification = new Notification JavaDoc("dummy", this, 0);
83       Integer JavaDoc listenerID = new Integer JavaDoc(1);
84       for (int i = 0; i < bufferCapacity; ++i) listener.handleNotification(notification, listenerID);
85
86       // Fetch first time
87
NotificationResult JavaDoc result = handler.fetchNotifications(-1, bufferCapacity + 1, 100);
88       assertEquals(result.getEarliestSequenceNumber(), 0);
89       assertEquals(result.getNextSequenceNumber(), bufferCapacity);
90       assertNotNull(result.getTargetedNotifications());
91       assertEquals(result.getTargetedNotifications().length, 0);
92
93       // Add another notification: the buffer is full, the earliest sequence number must change
94
listener.handleNotification(notification, listenerID);
95
96       // Fetch again
97
result = handler.fetchNotifications(result.getNextSequenceNumber(), bufferCapacity + 1, 100);
98       assertEquals(result.getEarliestSequenceNumber(), bufferCapacity);
99       assertEquals(result.getNextSequenceNumber(), bufferCapacity + 1);
100       assertNotNull(result.getTargetedNotifications());
101       assertEquals(result.getTargetedNotifications().length, 1);
102    }
103
104    public void testPurgeNotifications() throws Exception JavaDoc
105    {
106       int distance = 2;
107       Map JavaDoc environment = new HashMap JavaDoc();
108       environment.put(MX4JRemoteConstants.NOTIFICATION_PURGE_DISTANCE, new Integer JavaDoc(distance));
109       RemoteNotificationServerHandler handler = new DefaultRemoteNotificationServerHandler(environment);
110
111       // Fetch
112
NotificationResult JavaDoc result = handler.fetchNotifications(-1, 10, 100);
113
114       // Add notifications
115
NotificationListener JavaDoc listener = handler.getServerNotificationListener();
116       Notification JavaDoc notification = new Notification JavaDoc("dummy", this, 0);
117       Integer JavaDoc listenerID = new Integer JavaDoc(1);
118       int count = distance + 1;
119       for (int i = 0; i < count; ++i) listener.handleNotification(notification, listenerID);
120
121       // Fetch again
122
result = handler.fetchNotifications(result.getNextSequenceNumber(), count + 1, 100);
123       assertEquals(result.getEarliestSequenceNumber(), 0);
124       assertEquals(result.getNextSequenceNumber(), count);
125       assertNotNull(result.getTargetedNotifications());
126       assertEquals(result.getTargetedNotifications().length, count);
127
128       // Fetch again: this call triggers purge of old notifications, which changes the earliest sequence number
129
result = handler.fetchNotifications(result.getNextSequenceNumber(), count + 1, 100);
130
131       // Check that the earliest sequence number has changed
132
long oldEarliest = result.getEarliestSequenceNumber();
133       result = handler.fetchNotifications(result.getNextSequenceNumber(), count + 1, 100);
134       if (oldEarliest >= result.getEarliestSequenceNumber()) fail();
135    }
136
137    public void testPurgeNotificationsWithInitialNotificationMiss() throws Exception JavaDoc
138    {
139       int distance = 2;
140       Map JavaDoc environment = new HashMap JavaDoc();
141       environment.put(MX4JRemoteConstants.NOTIFICATION_PURGE_DISTANCE, new Integer JavaDoc(distance));
142       RemoteNotificationServerHandler handler = new DefaultRemoteNotificationServerHandler(environment);
143
144       // Add notifications (will be missed)
145
NotificationListener JavaDoc listener = handler.getServerNotificationListener();
146       Notification JavaDoc notification = new Notification JavaDoc("dummy", this, 0);
147       Integer JavaDoc listenerID = new Integer JavaDoc(1);
148       int count = distance + 1;
149       for (int i = 0; i < count; ++i) listener.handleNotification(notification, listenerID);
150
151       // First fetch
152
NotificationResult JavaDoc result = handler.fetchNotifications(-1, count + 1, 100);
153
154       // Add notifications
155
for (int i = 0; i < count; ++i) listener.handleNotification(notification, listenerID);
156
157       // Fetch again: this call triggers purge of old notifications, which changes the earliest sequence number
158
result = handler.fetchNotifications(result.getNextSequenceNumber(), count + 1, 100);
159       assertEquals(result.getEarliestSequenceNumber(), 0);
160       assertEquals(result.getNextSequenceNumber(), count + count);
161       assertNotNull(result.getTargetedNotifications());
162       assertEquals(result.getTargetedNotifications().length, count);
163
164       // Check that the earliest sequence number has changed
165
long oldEarliest = result.getEarliestSequenceNumber();
166       result = handler.fetchNotifications(result.getNextSequenceNumber(), count + 1, 100);
167       if (oldEarliest >= result.getEarliestSequenceNumber()) fail();
168    }
169
170    public void testBufferOverflowWhileFetching() throws Exception JavaDoc
171    {
172       int bufferCapacity = 5;
173       Map JavaDoc environment = new HashMap JavaDoc();
174       environment.put(MX4JRemoteConstants.NOTIFICATION_BUFFER_CAPACITY, new Integer JavaDoc(bufferCapacity));
175       RemoteNotificationServerHandler handler = new DefaultRemoteNotificationServerHandler(environment);
176
177       // First Fetch
178
NotificationResult JavaDoc result = handler.fetchNotifications(-1, bufferCapacity + 1, 100);
179
180       // Add some notifications, but don't fill the buffer
181
long count = bufferCapacity - 2;
182       NotificationListener JavaDoc listener = handler.getServerNotificationListener();
183       Notification JavaDoc notification = new Notification JavaDoc("dummy", this, 0);
184       Integer JavaDoc listenerID = new Integer JavaDoc(1);
185       for (int i = 0; i < count; ++i) listener.handleNotification(notification, listenerID);
186
187       // Fetch again
188
result = handler.fetchNotifications(result.getNextSequenceNumber(), bufferCapacity + 1, 100);
189       assertEquals(result.getEarliestSequenceNumber(), 0);
190       assertEquals(result.getNextSequenceNumber(), count);
191       assertNotNull(result.getTargetedNotifications());
192       assertEquals(result.getTargetedNotifications().length, count);
193
194       // Add some notification overflowing the buffer by a number that will exceed the
195
// next sequence number sent by the client.
196
// The client will fetch with its own sequence number, but the server
197
// has discarded that number already (it overflew), be sure the system it does
198
// not screw up (IndexOutOfBoundsException)
199
int overflow = 7;
200       // Note that count == result.getNextSequenceNumber() always yield true at this point (see assertion above),
201
// so we actually overflew the buffer by a quantity q == count + overflow (see assertion below).
202
long overflowCount = (bufferCapacity - count) + result.getNextSequenceNumber() + overflow;
203       for (int i = 0; i < overflowCount; ++i) listener.handleNotification(notification, listenerID);
204       result = handler.fetchNotifications(result.getNextSequenceNumber(), bufferCapacity + 1, 100);
205
206       // After algebraic semplification, overflowCount == bufferCapacity + overflow, so
207
// the buffer is full, and we fetched all notifications
208
assertEquals(count + overflow, result.getEarliestSequenceNumber());
209       assertEquals(count + overflow + bufferCapacity, result.getNextSequenceNumber());
210       assertNotNull(result.getTargetedNotifications());
211       assertEquals(result.getTargetedNotifications().length, bufferCapacity);
212    }
213 }
214
Popular Tags