KickJava   Java API By Example, From Geeks To Geeks.

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


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

21
22 package org.jacorb.test.notification.queue;
23
24 import org.easymock.MockControl;
25 import org.jacorb.notification.interfaces.Message;
26 import org.jacorb.notification.queue.BoundedDeadlineEventQueue;
27 import org.jacorb.notification.queue.EventQueueOverflowStrategy;
28
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 /**
34  * @author Alphonse Bendt
35  * @version $Id: BoundedDeadlineEventQueueTest.java,v 1.2 2005/03/20 21:58:51 alphonse.bendt Exp $
36  */

37 public class BoundedDeadlineEventQueueTest extends TestCase
38 {
39     private BoundedDeadlineEventQueue objectUnderTest_;
40
41     private MockControl controlMessage1_;
42
43     private Message mockMessage1_;
44
45     private MockControl controlMessage2_;
46
47     private Message mockMessage2_;
48
49     private MockControl controlMessage3_;
50
51     private Message mockMessage3_;
52
53     public BoundedDeadlineEventQueueTest(String JavaDoc name)
54     {
55         super(name);
56     }
57
58     protected void setUp()
59     {
60         objectUnderTest_ = new BoundedDeadlineEventQueue(2,
61                 EventQueueOverflowStrategy.EARLIEST_TIMEOUT);
62
63         controlMessage1_ = MockControl.createControl(Message.class);
64         controlMessage2_ = MockControl.createControl(Message.class);
65         controlMessage3_ = MockControl.createControl(Message.class);
66
67         mockMessage1_ = (Message) controlMessage1_.getMock();
68         mockMessage2_ = (Message) controlMessage2_.getMock();
69         mockMessage3_ = (Message) controlMessage3_.getMock();
70     }
71
72     public void testGetSingleNonBlocking() throws Exception JavaDoc
73     {
74         mockMessage1_.hasTimeout();
75         controlMessage1_.setDefaultReturnValue(false);
76         controlMessage1_.replay();
77      
78         assertNull(objectUnderTest_.getMessage(false));
79         
80         objectUnderTest_.put(mockMessage1_);
81         
82         assertEquals(mockMessage1_, objectUnderTest_.getMessage(false));
83     }
84     
85     public void testGetAllNonBlocking() throws Exception JavaDoc
86     {
87         mockMessage1_.hasTimeout();
88         controlMessage1_.setDefaultReturnValue(false);
89         controlMessage1_.replay();
90         
91         assertEquals(0, objectUnderTest_.getAllMessages(false).length);
92         
93         objectUnderTest_.put(mockMessage1_);
94         objectUnderTest_.put(mockMessage1_);
95         
96         assertEquals(2, objectUnderTest_.getAllMessages(false).length);
97     }
98     
99     public void testGetAllClearsQueue() throws Exception JavaDoc
100     {
101         mockMessage1_.hasTimeout();
102         controlMessage1_.setDefaultReturnValue(false);
103         controlMessage1_.replay();
104         
105         objectUnderTest_.put(mockMessage1_);
106         
107         assertEquals(mockMessage1_, objectUnderTest_.getAllMessages(false)[0]);
108         
109         assertEquals(0, objectUnderTest_.getAllMessages(false).length);
110     }
111     
112     public void testInsert() throws Exception JavaDoc
113     {
114         mockMessage1_.hasTimeout();
115         controlMessage1_.setReturnValue(true);
116         mockMessage1_.getTimeout();
117         controlMessage1_.setReturnValue(100);
118         controlMessage1_.replay();
119
120         mockMessage2_.hasTimeout();
121         controlMessage2_.setReturnValue(true);
122         mockMessage2_.getTimeout();
123         controlMessage2_.setReturnValue(10);
124         controlMessage2_.replay();
125
126         objectUnderTest_.put(mockMessage1_);
127         objectUnderTest_.put(mockMessage2_);
128
129         assertEquals(mockMessage2_, objectUnderTest_.getMessage(false));
130
131         controlMessage1_.verify();
132         controlMessage2_.verify();
133     }
134
135     public void testOverflow() throws Exception JavaDoc
136     {
137         mockMessage1_.hasTimeout();
138         controlMessage1_.setDefaultReturnValue(true);
139         mockMessage1_.getTimeout();
140         controlMessage1_.setDefaultReturnValue(100);
141         controlMessage1_.replay();
142
143         mockMessage2_.hasTimeout();
144         controlMessage2_.setDefaultReturnValue(true);
145         mockMessage2_.getTimeout();
146         controlMessage2_.setDefaultReturnValue(10);
147         controlMessage2_.replay();
148
149         mockMessage3_.hasTimeout();
150         controlMessage3_.setDefaultReturnValue(true);
151         mockMessage3_.getTimeout();
152         controlMessage3_.setDefaultReturnValue(1);
153         controlMessage3_.replay();
154
155         objectUnderTest_.put(mockMessage1_);
156         objectUnderTest_.put(mockMessage2_);
157         objectUnderTest_.put(mockMessage3_);
158
159         assertEquals(mockMessage3_, objectUnderTest_.getMessage(false));
160         assertEquals(mockMessage1_, objectUnderTest_.getMessage(false));
161
162         assertTrue(objectUnderTest_.isEmpty());
163         assertNull(objectUnderTest_.getMessage(false));
164
165         controlMessage1_.verify();
166         controlMessage2_.verify();
167         controlMessage3_.verify();
168     }
169
170     /**
171      * test to provoke a bug i have found by poking around in the sources.
172      * size of array returned by getEvents was size +1. also entries in queue
173      * could get lost.
174      */

175     public void testGetEvents() throws Exception JavaDoc
176     {
177         mockMessage1_.hasTimeout();
178         controlMessage1_.setDefaultReturnValue(false);
179
180         controlMessage1_.replay();
181     
182         mockMessage2_.hasTimeout();
183         controlMessage2_.setDefaultReturnValue(false);
184
185         controlMessage2_.replay();
186         
187         objectUnderTest_.put(mockMessage1_);
188         objectUnderTest_.put(mockMessage2_);
189                 
190         assertEquals(1, objectUnderTest_.getMessages(1, false).length);
191         assertEquals(1, objectUnderTest_.getMessages(1, false).length);
192     }
193
194     public static Test suite()
195     {
196         return new TestSuite(BoundedDeadlineEventQueueTest.class);
197     }
198 }
Popular Tags