KickJava   Java API By Example, From Geeks To Geeks.

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


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.BoundedFifoEventQueue;
35 import org.jacorb.notification.queue.EventQueueOverflowStrategy;
36
37 /**
38  * @author Alphonse Bendt
39  */

40
41 public class BoundedFifoEventQueueTest extends TestCase
42 {
43     public BoundedFifoEventQueueTest(String JavaDoc name)
44     {
45         super(name);
46     }
47
48     private void addEventsToEventQueue(EventQueueOverflowStrategy strategy, List JavaDoc events)
49     {
50         BoundedFifoEventQueue queue = new BoundedFifoEventQueue(4, strategy);
51
52         Iterator JavaDoc i = events.iterator();
53
54         while (i.hasNext())
55         {
56             queue.put((Message) i.next());
57         }
58     }
59
60     public void testFIFOOverflow() throws Exception JavaDoc
61     {
62         DelegatingOverflowStrategy strategy = new DelegatingOverflowStrategy(EventQueueOverflowStrategy.FIFO);
63         
64         List JavaDoc _events = new ArrayList JavaDoc();
65
66         Message mockMessage1 = newMessage();
67
68         Message mockMessage2 = newMessage();
69
70         _events.add(mockMessage1);
71
72         _events.add(mockMessage2);
73
74         _events.add(newMessage());
75
76         _events.add(newMessage());
77
78         _events.add(newMessage());
79
80         _events.add(newMessage());
81
82         addEventsToEventQueue(strategy, _events);
83
84         assertEquals(2, strategy.getRemovedElements().size());
85
86         assertTrue(strategy.getRemovedElements().contains(mockMessage1));
87
88         assertTrue(strategy.getRemovedElements().contains(mockMessage2));
89     }
90
91     public void testLIFOOverflow() throws Exception JavaDoc
92     {
93         DelegatingOverflowStrategy strategy = new DelegatingOverflowStrategy(EventQueueOverflowStrategy.LIFO);
94         
95         List JavaDoc _events = new ArrayList JavaDoc();
96
97         _events.add(newMessage());
98
99         _events.add(newMessage());
100
101         _events.add(newMessage());
102
103         Message e1 = newMessage();
104
105         Message e2 = newMessage();
106
107         _events.add(e1);
108
109         _events.add(e2);
110
111         _events.add(newMessage());
112
113         addEventsToEventQueue(strategy, _events);
114
115         assertEquals(2, strategy.getRemovedElements().size());
116
117         assertTrue(strategy.getRemovedElements().contains(e1));
118
119         assertTrue(strategy.getRemovedElements().contains(e2));
120     }
121
122     public void testGetAllClearsQueue() throws Exception JavaDoc
123     {
124         BoundedFifoEventQueue queue = new BoundedFifoEventQueue(10, EventQueueOverflowStrategy.LIFO);
125         
126         assertEquals(0, queue.getAllMessages(false).length );
127         
128         Message m = newMessage();
129         queue.put(m);
130         
131         Message[] mesgs = queue.getAllMessages(false);
132         
133         assertEquals(1, mesgs.length);
134         assertEquals(m, mesgs[0]);
135         
136         assertEquals(0, queue.getAllMessages(false).length);
137     }
138     
139     private Message newMessage()
140     {
141         MockControl controlMessage = MockControl.createControl(Message.class);
142         Message mockMessage = (Message) controlMessage.getMock();
143         controlMessage.replay();
144         return mockMessage;
145     }
146
147     public static Test suite()
148     {
149         TestSuite suite = new TestSuite(BoundedFifoEventQueueTest.class);
150
151         return suite;
152     }
153 }
154
155
Popular Tags