KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > registry > thread > TribeThreadTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: TribeThreadTest.java 1:43:39 PM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry.thread;
23
24 import javax.naming.NamingException JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.easymock.classextension.EasyMock;
29 import org.objectweb.petals.kernel.registry.RegistryServer;
30 import org.objectweb.petals.kernel.registry.msg.request.BindRequest;
31 import org.objectweb.petals.kernel.registry.msg.request.CreateSubcontextRequest;
32 import org.objectweb.petals.kernel.registry.msg.request.DestroySubcontextRequest;
33 import org.objectweb.petals.kernel.registry.msg.request.PingRequest;
34 import org.objectweb.petals.kernel.registry.msg.request.RebindRequest;
35 import org.objectweb.petals.kernel.registry.msg.request.RenameRequest;
36 import org.objectweb.petals.kernel.registry.msg.request.UnbindRequest;
37 import org.objectweb.petals.kernel.registry.msg.response.MyStartTimeResponse;
38 import org.objectweb.tribe.channel.ReliableGroupChannelWithGms;
39 import org.objectweb.tribe.exceptions.ChannelException;
40 import org.objectweb.tribe.exceptions.NotConnectedException;
41
42 /**
43  * Tests of the TribeThread
44  *
45  * @author ddesjardins - eBMWebsourcing
46  */

47 public class TribeThreadTest extends TestCase {
48
49     public void testRunNullMessage() throws ChannelException,
50         NotConnectedException {
51         RegistryServer registryServer = EasyMock
52             .createMock(RegistryServer.class);
53         ReliableGroupChannelWithGms channel = EasyMock
54             .createMock(ReliableGroupChannelWithGms.class);
55         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
56
57         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
58             .anyTimes();
59         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
60             .anyTimes();
61         EasyMock.expect(registryServer.getChannel()).andReturn(channel)
62             .anyTimes();
63         EasyMock.expect(channel.receive()).andReturn(null).anyTimes();
64
65         EasyMock.replay(registryServer);
66         EasyMock.replay(channel);
67
68         TribeThread tribeThread = new TribeThread(registryServer);
69         tribeThread.start();
70         try {
71             Thread.sleep(500);
72         } catch (InterruptedException JavaDoc e) {
73             // do nothing
74
}
75         tribeThread.terminate();
76         assertTrue(mockLoggingUtil.isWarning());
77     }
78
79     public void testRunNotRegistryMessage() throws ChannelException,
80         NotConnectedException {
81         RegistryServer registryServer = EasyMock
82             .createMock(RegistryServer.class);
83         ReliableGroupChannelWithGms channel = EasyMock
84             .createMock(ReliableGroupChannelWithGms.class);
85         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
86
87         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
88             .anyTimes();
89         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
90             .anyTimes();
91         EasyMock.expect(registryServer.getChannel()).andReturn(channel)
92             .anyTimes();
93         EasyMock.expect(channel.receive()).andReturn("test").anyTimes();
94
95         EasyMock.replay(registryServer);
96         EasyMock.replay(channel);
97
98         TribeThread tribeThread = new TribeThread(registryServer);
99         tribeThread.start();
100         try {
101             Thread.sleep(500);
102         } catch (InterruptedException JavaDoc e) {
103             // do nothing
104
}
105         tribeThread.terminate();
106         assertTrue(mockLoggingUtil.isWarning());
107     }
108
109     public void testRunException() throws ChannelException,
110         NotConnectedException {
111         RegistryServer registryServer = EasyMock
112             .createMock(RegistryServer.class);
113         ReliableGroupChannelWithGms channel = EasyMock
114             .createMock(ReliableGroupChannelWithGms.class);
115         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
116
117         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
118             .anyTimes();
119         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
120             .anyTimes();
121         EasyMock.expect(registryServer.getChannel()).andReturn(channel)
122             .anyTimes();
123         EasyMock.expect(channel.receive()).andThrow(new RuntimeException JavaDoc())
124             .anyTimes();
125
126         EasyMock.replay(registryServer);
127         EasyMock.replay(channel);
128
129         TribeThread tribeThread = new TribeThread(registryServer);
130         tribeThread.start();
131         try {
132             Thread.sleep(500);
133         } catch (InterruptedException JavaDoc e) {
134             // do nothing
135
}
136         tribeThread.terminate();
137         assertTrue(mockLoggingUtil.isError());
138     }
139
140     public void testProcessRequestCreateSubcontextRequest()
141         throws NamingException JavaDoc {
142         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
143         CreateSubcontextRequest request = new CreateSubcontextRequest("/",
144             "test", 0, 0);
145         RegistryServer registryServer = EasyMock
146             .createMock(RegistryServer.class);
147
148         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
149             .anyTimes();
150         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
151             .anyTimes();
152         EasyMock.expect(registryServer.processRequest(request)).andReturn(null)
153             .anyTimes();
154
155         EasyMock.replay(registryServer);
156
157         TribeThread tribeThread = new TribeThread(registryServer);
158
159         tribeThread.processRequest(request);
160     }
161
162     public void testProcessRequestDestroySubcontextRequest()
163         throws NamingException JavaDoc {
164         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
165         DestroySubcontextRequest request = new DestroySubcontextRequest("/",
166             "test", 0, 0);
167         RegistryServer registryServer = EasyMock
168             .createMock(RegistryServer.class);
169
170         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
171             .anyTimes();
172         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
173             .anyTimes();
174         EasyMock.expect(registryServer.processRequest(request)).andReturn(null)
175             .anyTimes();
176
177         EasyMock.replay(registryServer);
178
179         TribeThread tribeThread = new TribeThread(registryServer);
180
181         tribeThread.processRequest(request);
182     }
183
184     public void testProcessRequestRebindRequest() throws NamingException JavaDoc {
185         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
186         RebindRequest request = new RebindRequest("/", "test", null, 0, 0);
187         RegistryServer registryServer = EasyMock
188             .createMock(RegistryServer.class);
189
190         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
191             .anyTimes();
192         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
193             .anyTimes();
194         EasyMock.expect(registryServer.processRequest(request)).andReturn(null)
195             .anyTimes();
196
197         EasyMock.replay(registryServer);
198
199         TribeThread tribeThread = new TribeThread(registryServer);
200
201         tribeThread.processRequest(request);
202     }
203
204     public void testProcessRequestRenameRequest() throws NamingException JavaDoc {
205         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
206         RenameRequest request = new RenameRequest("/", "test", null, 0, 0);
207         RegistryServer registryServer = EasyMock
208             .createMock(RegistryServer.class);
209
210         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
211             .anyTimes();
212         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
213             .anyTimes();
214         EasyMock.expect(registryServer.processRequest(request)).andReturn(null)
215             .anyTimes();
216
217         EasyMock.replay(registryServer);
218
219         TribeThread tribeThread = new TribeThread(registryServer);
220
221         tribeThread.processRequest(request);
222     }
223
224     public void testProcessRequestUnbindRequest() throws NamingException JavaDoc {
225         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
226         UnbindRequest request = new UnbindRequest("/", "test", 0, 0);
227         RegistryServer registryServer = EasyMock
228             .createMock(RegistryServer.class);
229
230         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
231             .anyTimes();
232         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
233             .anyTimes();
234         EasyMock.expect(registryServer.processRequest(request)).andReturn(null)
235             .anyTimes();
236
237         EasyMock.replay(registryServer);
238
239         TribeThread tribeThread = new TribeThread(registryServer);
240
241         tribeThread.processRequest(request);
242     }
243
244     public void testProcessRequestAnything() throws NamingException JavaDoc {
245         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
246         PingRequest request = new PingRequest();
247         RegistryServer registryServer = EasyMock
248             .createMock(RegistryServer.class);
249
250         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
251             .anyTimes();
252         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
253             .anyTimes();
254         EasyMock.expect(registryServer.processRequest(request)).andReturn(null)
255             .anyTimes();
256
257         EasyMock.replay(registryServer);
258
259         TribeThread tribeThread = new TribeThread(registryServer);
260
261         tribeThread.processRequest(request);
262     }
263
264     public void testProcessRequestException() throws NamingException JavaDoc {
265         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
266         BindRequest request = new BindRequest("/", "key", "value", 0, 0);
267         RegistryServer registryServer = EasyMock
268             .createMock(RegistryServer.class);
269         NamingException JavaDoc e = new NamingException JavaDoc("error");
270
271         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
272             .anyTimes();
273         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
274             .anyTimes();
275         EasyMock.expect(registryServer.processRequest(request)).andThrow(e)
276             .anyTimes();
277         EasyMock.expect(registryServer.getChannel()).andThrow(
278             new RuntimeException JavaDoc("error")).anyTimes();
279
280         EasyMock.replay(registryServer);
281
282         TribeThread tribeThread = new TribeThread(registryServer);
283
284         tribeThread.processRequest(request);
285         assertTrue(mockLoggingUtil.isError());
286     }
287
288     public void testProcessResponseMyStartTimeResponseException()
289         throws NamingException JavaDoc {
290         MockLoggingUtil mockLoggingUtil = new MockLoggingUtil();
291         RegistryServer registryServer = EasyMock
292             .createMock(RegistryServer.class);
293         MyStartTimeResponse myStartTimeResponse = new MyStartTimeResponse(
294             (long) 1, 0, 0);
295
296         EasyMock.expect(registryServer.getStartTime()).andReturn((long) 1)
297             .anyTimes();
298         EasyMock.expect(registryServer.getLog()).andReturn(mockLoggingUtil)
299             .anyTimes();
300         EasyMock.expect(registryServer.processResponse(myStartTimeResponse))
301             .andReturn("0").anyTimes();
302         EasyMock.expect(registryServer.getChannel()).andThrow(
303             new RuntimeException JavaDoc()).anyTimes();
304
305         EasyMock.replay(registryServer);
306
307         TribeThread tribeThread = new TribeThread(registryServer);
308         tribeThread.processResponse(myStartTimeResponse);
309         assertTrue(mockLoggingUtil.isError());
310     }
311
312 }
313
Popular Tags