KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > queue > BoundedPriorityEventQueueTest


1 package org.jacorb.test.notification.queue;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2003 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 import org.easymock.MockControl;
33 import org.jacorb.notification.interfaces.Message;
34 import org.jacorb.notification.queue.AbstractBoundedEventQueue;
35 import org.jacorb.notification.queue.BoundedPriorityEventQueue;
36 import org.jacorb.notification.queue.MessageQueue;
37 import org.jacorb.notification.queue.EventQueueOverflowStrategy;
38
39 /**
40  * @author Alphonse Bendt
41  */

42
43 public class BoundedPriorityEventQueueTest extends TestCase
44 {
45     public BoundedPriorityEventQueueTest(String JavaDoc name)
46     {
47         super(name);
48     }
49
50     public static Test suite() throws Exception JavaDoc
51     {
52         return new TestSuite(BoundedPriorityEventQueueTest.class);
53     }
54
55     public void testPriorityOrder_ascendingInsert() throws Exception JavaDoc
56     {
57         BoundedPriorityEventQueue _queue = new BoundedPriorityEventQueue(20,
58                 EventQueueOverflowStrategy.FIFO);
59
60         for (int x = 0; x < 10; ++x)
61         {
62             Message mockMessage = newMessage(x);
63             _queue.put(mockMessage);
64         }
65
66         for (int x = 9; x >= 0; --x)
67         {
68             Message _event = _queue.getMessage(false);
69             assertEquals(x, _event.getPriority());
70         }
71     }
72
73     public void testPriorityOrder_descendingInsert() throws Exception JavaDoc
74     {
75         MessageQueue _queue = new BoundedPriorityEventQueue(20, EventQueueOverflowStrategy.FIFO);
76         
77         for (int x = 0; x < 10; ++x)
78         {
79             int prio = 10 - x;
80
81             Message mockMessage = newMessage(prio);
82             _queue.put(mockMessage);
83         }
84
85         for (int x = 0; x < 10; ++x)
86         {
87             Message _event = _queue.getMessage(false);
88             assertEquals(10 - x, _event.getPriority());
89         }
90
91     }
92
93     public void testFIFOOverflow() throws Exception JavaDoc
94     {
95         DelegatingOverflowStrategy strategy = new DelegatingOverflowStrategy(EventQueueOverflowStrategy.FIFO);
96
97         List JavaDoc _events = new ArrayList JavaDoc();
98
99         Message e1 = newMessage();
100         Message e2 = newMessage();
101         Message e3 = newMessage();
102         Message e4 = newMessage();
103         Message e5 = newMessage();
104
105         _events.add(e1);
106
107         _events.add(e2);
108
109         _events.add(e3);
110
111         _events.add(e4);
112
113         _events.add(e5);
114
115         addEventsToEventQueue(strategy, _events);
116
117         assertEquals(1, strategy.getRemovedElements().size());
118
119         assertTrue(strategy.getRemovedElements().contains(e1));
120     }
121
122     public void testLIFOOverflow() throws Exception JavaDoc
123     {
124         DelegatingOverflowStrategy strategy = new DelegatingOverflowStrategy(EventQueueOverflowStrategy.LIFO);
125         
126         List JavaDoc _events = new ArrayList JavaDoc();
127
128         _events.add(newMessage());
129
130         _events.add(newMessage());
131
132         _events.add(newMessage());
133
134         Message e1 = newMessage();
135
136         Message e2 = newMessage();
137
138         _events.add(e1);
139
140         _events.add(e2);
141
142         _events.add(newMessage());
143
144         addEventsToEventQueue(strategy, _events);
145
146         assertEquals(2, strategy.getRemovedElements().size());
147
148         assertTrue(strategy.getRemovedElements().contains(e1));
149
150         assertTrue(strategy.getRemovedElements().contains(e2));
151     }
152     
153     public void testGetAllClearsQueue() throws Exception JavaDoc
154     {
155         BoundedPriorityEventQueue queue = new BoundedPriorityEventQueue(10, EventQueueOverflowStrategy.LEAST_PRIORITY);
156         
157         Message m = newMessage();
158         
159         assertEquals(0, queue.getAllMessages(false).length);
160         
161         queue.put(m);
162         
163         Message[] mesgs = queue.getAllMessages(false);
164         
165         assertEquals(1, mesgs.length);
166         assertEquals(m, mesgs[0]);
167         
168         queue.getAllMessages(false);
169         
170         assertEquals(0, queue.getAllMessages(false).length);
171     }
172
173     private void addEventsToEventQueue(EventQueueOverflowStrategy strategy, List JavaDoc events)
174     {
175         AbstractBoundedEventQueue queue = new BoundedPriorityEventQueue(4, strategy);
176
177         Iterator JavaDoc i = events.iterator();
178
179         while (i.hasNext())
180         {
181             queue.put((Message) i.next());
182         }
183     }
184
185     private Message newMessage()
186     {
187         return newMessage(0);
188     }
189     
190     private Message newMessage(int priority)
191     {
192         MockControl controlMessage = MockControl.createControl(Message.class);
193         Message mockMessage = (Message) controlMessage.getMock();
194         mockMessage.getPriority();
195         controlMessage.setDefaultReturnValue(priority);
196         controlMessage.replay();
197         
198         return mockMessage;
199     }
200 }
Popular Tags