KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > registry > ServerAloneTest


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: JNDIServerTest.java 9:42:06 AM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry;
23
24 import java.io.ObjectInputStream JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import java.net.InetSocketAddress JavaDoc;
27 import java.net.Socket JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.naming.Binding JavaDoc;
32 import javax.naming.LinkRef JavaDoc;
33 import javax.naming.NameClassPair JavaDoc;
34
35 import junit.framework.TestCase;
36
37 import org.easymock.classextension.EasyMock;
38 import org.objectweb.petals.kernel.registry.mock.MockConfigurationService;
39 import org.objectweb.petals.kernel.registry.msg.request.BindRequest;
40 import org.objectweb.petals.kernel.registry.msg.request.CreateSubcontextRequest;
41 import org.objectweb.petals.kernel.registry.msg.request.DestroySubcontextRequest;
42 import org.objectweb.petals.kernel.registry.msg.request.ListBindingsRequest;
43 import org.objectweb.petals.kernel.registry.msg.request.ListRequest;
44 import org.objectweb.petals.kernel.registry.msg.request.LookupLinkRequest;
45 import org.objectweb.petals.kernel.registry.msg.request.LookupRequest;
46 import org.objectweb.petals.kernel.registry.msg.request.RebindRequest;
47 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest;
48 import org.objectweb.petals.kernel.registry.msg.request.RenameRequest;
49 import org.objectweb.petals.kernel.registry.msg.request.UnbindRequest;
50 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse;
51 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse.ResponseType;
52 import org.objectweb.petals.util.LoggingUtil;
53 import org.objectweb.petals.util.SystemUtil;
54 import org.objectweb.util.monolog.api.Logger;
55
56 /**
57  * Test of the JNDIServer
58  *
59  * @author ddesjardins - eBMWebsourcing
60  */

61 public class ServerAloneTest extends TestCase {
62
63     private RegistryServer server;
64
65     public void setUp() throws InterruptedException JavaDoc {
66         SystemUtil.setJndiPort("16400");
67         String JavaDoc groupName = "petals"
68             + System.nanoTime();
69         server = new RegistryServer();
70         Logger logger = EasyMock.createMock(Logger.class);
71         server.logger = logger;
72         server.configurationService = new MockConfigurationService();
73         server.log = EasyMock.createMock(LoggingUtil.class);
74         server.registryUtil.setLog(EasyMock.createMock(LoggingUtil.class));
75         server.setGroupName(groupName);
76
77         server.startServer();
78         // Wait for the server to start
79
Thread.sleep(1000);
80     }
81
82     public void tearDown() {
83         server.stopServer();
84     }
85
86     public void testBind() throws Exception JavaDoc {
87         Socket JavaDoc socket = new Socket JavaDoc();
88         socket
89             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
90
91         RegistryRequest request = new BindRequest("/", "key", "value", 0, 0);
92
93         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
94             .getOutputStream());
95         objectOutputStream.writeObject(request);
96         objectOutputStream.flush();
97
98         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
99             .getInputStream());
100         RegistryResponse response = (RegistryResponse) objectInputStream
101             .readObject();
102
103         if (!response.getType().equals(ResponseType.ack)) {
104             fail();
105         } else {
106             assertEquals(server.data.get("/").get("key"), "value");
107         }
108     }
109
110     @SuppressWarnings JavaDoc("unchecked")
111     public void testBindInSubcontext() throws Exception JavaDoc {
112
113         testCreateSubcontext();
114
115         Socket JavaDoc socket = new Socket JavaDoc();
116         socket
117             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
118
119         RegistryRequest request = new BindRequest("/sub/", "key", "value", 0, 0);
120
121         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
122             .getOutputStream());
123         objectOutputStream.writeObject(request);
124         objectOutputStream.flush();
125
126         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
127             .getInputStream());
128         RegistryResponse response = (RegistryResponse) objectInputStream
129             .readObject();
130
131         if (!response.getType().equals(ResponseType.ack)) {
132             fail();
133         } else {
134             assertEquals(server.data.get("/sub/").get("key"), "value");
135         }
136     }
137
138     public void testCreateSubcontext() throws Exception JavaDoc {
139         Socket JavaDoc socket = new Socket JavaDoc();
140         socket
141             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
142
143         RegistryRequest request = new CreateSubcontextRequest("/", "sub", 0, 0);
144
145         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
146             .getOutputStream());
147         objectOutputStream.writeObject(request);
148         objectOutputStream.flush();
149
150         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
151             .getInputStream());
152         RegistryResponse response = (RegistryResponse) objectInputStream
153             .readObject();
154
155         if (!response.getType().equals(ResponseType.createSubcontext)) {
156             fail();
157         } else {
158             assertEquals(response.getArg1(), "/sub/");
159             assertNotNull(server.data.get("/").get("sub"));
160             assertNotNull(server.data.get("/sub/"));
161         }
162     }
163
164     public void testDestroySubcontext() throws Exception JavaDoc {
165         Socket JavaDoc socket = new Socket JavaDoc();
166         socket
167             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
168
169         Map JavaDoc<String JavaDoc, Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
170         map.put("sub", new HashMap JavaDoc<String JavaDoc, Object JavaDoc>());
171
172         server.data.put("/", map);
173
174         Map JavaDoc<String JavaDoc, Object JavaDoc> subMap = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
175         subMap.put("key", "value");
176         server.data.put("/sub/", subMap);
177
178         RegistryRequest request = new DestroySubcontextRequest("/", "sub", 0, 0);
179
180         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
181             .getOutputStream());
182         objectOutputStream.writeObject(request);
183         objectOutputStream.flush();
184
185         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
186             .getInputStream());
187         RegistryResponse response = (RegistryResponse) objectInputStream
188             .readObject();
189
190         if (!response.getType().equals(ResponseType.ack)) {
191             fail();
192         } else {
193             assertEquals(server.data.get("/").size(), 0);
194             assertNull(server.getData().get("/sub/"));
195         }
196     }
197
198     public void testList() throws Exception JavaDoc {
199         Socket JavaDoc socket = new Socket JavaDoc();
200         socket
201             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
202
203         Map JavaDoc<String JavaDoc, Object JavaDoc> sub = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
204         sub.put("key", "value");
205         server.data.put("/sub/", sub);
206
207         RegistryRequest request = new ListRequest("/", "sub", 0, 0);
208
209         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
210             .getOutputStream());
211         objectOutputStream.writeObject(request);
212         objectOutputStream.flush();
213
214         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
215             .getInputStream());
216         RegistryResponse response = (RegistryResponse) objectInputStream
217             .readObject();
218
219         if (!response.getType().equals(ResponseType.list)) {
220             fail();
221         } else {
222             assertEquals("key", ((NameClassPair JavaDoc) ((NameClassPair JavaDoc[]) response
223                 .getArg1())[0]).getName());
224         }
225     }
226
227     public void testListBindings() throws Exception JavaDoc {
228         Socket JavaDoc socket = new Socket JavaDoc();
229         socket
230             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
231
232         Map JavaDoc<String JavaDoc, Object JavaDoc> sub = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
233         sub.put("key", "value");
234         server.data.put("/sub/", sub);
235
236         RegistryRequest request = new ListBindingsRequest("/", "sub", 0, 0);
237
238         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
239             .getOutputStream());
240         objectOutputStream.writeObject(request);
241         objectOutputStream.flush();
242
243         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
244             .getInputStream());
245         RegistryResponse response = (RegistryResponse) objectInputStream
246             .readObject();
247
248         if (!response.getType().equals(ResponseType.listBindings)) {
249             fail();
250         } else {
251             assertEquals("key", ((Binding JavaDoc) ((Binding JavaDoc[]) response.getArg1())[0])
252                 .getName());
253             assertEquals("value",
254                 ((Binding JavaDoc) ((Binding JavaDoc[]) response.getArg1())[0]).getObject());
255         }
256     }
257
258     public void testLookup() throws Exception JavaDoc {
259         Socket JavaDoc socket = new Socket JavaDoc();
260         socket
261             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
262         Map JavaDoc<String JavaDoc, Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
263         map.put("key", "value");
264         server.data.put("/", map);
265
266         RegistryRequest request = new LookupRequest("/", "key", 0, 0);
267
268         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
269             .getOutputStream());
270         objectOutputStream.writeObject(request);
271         objectOutputStream.flush();
272
273         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
274             .getInputStream());
275         RegistryResponse response = (RegistryResponse) objectInputStream
276             .readObject();
277
278         if (!response.getType().equals(ResponseType.lookup)) {
279             fail();
280         } else {
281             assertEquals(response.getArg1(), "value");
282         }
283     }
284
285     public void testLookupLink() throws Exception JavaDoc {
286         Socket JavaDoc socket = new Socket JavaDoc();
287         socket
288             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
289
290         Map JavaDoc<String JavaDoc, Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
291         map.put("key", "value");
292         server.data.put("/", map);
293
294         RegistryRequest request = new LookupLinkRequest("/", "key", 0, 0);
295
296         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
297             .getOutputStream());
298         objectOutputStream.writeObject(request);
299         objectOutputStream.flush();
300
301         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
302             .getInputStream());
303         RegistryResponse response = (RegistryResponse) objectInputStream
304             .readObject();
305
306         if (!response.getType().equals(ResponseType.lookupLink)) {
307             fail();
308         } else {
309             assertEquals(response.getArg1(), "value");
310         }
311     }
312
313     public void testLookupLinkWithLink() throws Exception JavaDoc {
314         Socket JavaDoc socket = new Socket JavaDoc();
315         socket
316             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
317
318         Map JavaDoc<String JavaDoc, Object JavaDoc> subMap = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
319         LinkRef JavaDoc linkRef = new LinkRef JavaDoc("/key");
320         subMap.put("link", linkRef);
321
322         server.data.put("/sub/", subMap);
323         Map JavaDoc<String JavaDoc, Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
324         map.put("key", "value");
325         server.data.put("/", map);
326
327         RegistryRequest request = new LookupLinkRequest("/sub/", "link", 0, 0);
328
329         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
330             .getOutputStream());
331         objectOutputStream.writeObject(request);
332         objectOutputStream.flush();
333
334         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
335             .getInputStream());
336         RegistryResponse response = (RegistryResponse) objectInputStream
337             .readObject();
338
339         if (!response.getType().equals(ResponseType.lookupLink)) {
340             fail();
341         } else {
342             assertEquals(response.getArg1(), "value");
343         }
344     }
345
346     public void testRebind() throws Exception JavaDoc {
347         Socket JavaDoc socket = new Socket JavaDoc();
348         socket
349             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
350
351         Map JavaDoc<String JavaDoc, Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
352         map.put("key", "value");
353         server.data.put("/", map);
354
355         RegistryRequest request = new RebindRequest("/", "key", "value1", 0, 0);
356
357         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
358             .getOutputStream());
359         objectOutputStream.writeObject(request);
360         objectOutputStream.flush();
361
362         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
363             .getInputStream());
364         RegistryResponse response = (RegistryResponse) objectInputStream
365             .readObject();
366
367         if (!response.getType().equals(ResponseType.ack)) {
368             fail();
369         } else {
370             assertEquals(server.data.get("/").get("key"), "value1");
371         }
372     }
373
374     public void testRename() throws Exception JavaDoc {
375         Socket JavaDoc socket = new Socket JavaDoc();
376         socket
377             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
378         Map JavaDoc<String JavaDoc, Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
379         map.put("key", "value");
380         server.data.put("/", map);
381
382         RegistryRequest request = new RenameRequest("/", "key", "key1", 0, 0);
383
384         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
385             .getOutputStream());
386         objectOutputStream.writeObject(request);
387         objectOutputStream.flush();
388
389         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
390             .getInputStream());
391         RegistryResponse response = (RegistryResponse) objectInputStream
392             .readObject();
393
394         if (!response.getType().equals(ResponseType.ack)) {
395             fail();
396         } else {
397             assertEquals(server.data.get("/").get("key1"), "value");
398         }
399     }
400
401     public void testUnbind() throws Exception JavaDoc {
402         Socket JavaDoc socket = new Socket JavaDoc();
403         socket
404             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
405
406         Map JavaDoc<String JavaDoc, Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
407         map.put("key", "value");
408         server.data.put("/", map);
409
410         RegistryRequest request = new UnbindRequest("/", "key", 0, 0);
411
412         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
413             .getOutputStream());
414         objectOutputStream.writeObject(request);
415         objectOutputStream.flush();
416
417         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
418             .getInputStream());
419         RegistryResponse response = (RegistryResponse) objectInputStream
420             .readObject();
421
422         if (!response.getType().equals(ResponseType.ack)) {
423             fail();
424         } else {
425             assertEquals(server.data.get("/").size(), 0);
426         }
427     }
428
429 }
430
Popular Tags