KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import junit.framework.TestCase;
30
31 import org.easymock.classextension.EasyMock;
32 import org.objectweb.petals.kernel.registry.mock.MockConfigurationService;
33 import org.objectweb.petals.kernel.registry.msg.request.BindRequest;
34 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest;
35 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse;
36 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse.ResponseType;
37 import org.objectweb.petals.util.LoggingUtil;
38 import org.objectweb.petals.util.SystemUtil;
39 import org.objectweb.util.monolog.api.Logger;
40
41 /**
42  * Test of the JNDIServer
43  *
44  * @author ddesjardins - eBMWebsourcing
45  */

46 public class TwoServerTest extends TestCase {
47
48     private RegistryServer server1;
49
50     private RegistryServer server2;
51
52     public void setUp() {
53         String JavaDoc groupName = "petals"
54             + System.nanoTime();
55
56         server1 = new RegistryServer();
57         Logger logger = EasyMock.createMock(Logger.class);
58         server1.logger = logger;
59         server1.configurationService = new MockConfigurationService();
60         server1.log = EasyMock.createMock(LoggingUtil.class);
61         server1.registryUtil.setLog(EasyMock.createMock(LoggingUtil.class));
62         server1.setGroupName(groupName);
63
64         server2 = new RegistryServer();
65         server2.logger = logger;
66         server2.configurationService = new MockConfigurationService();
67         server2.log = EasyMock.createMock(LoggingUtil.class);
68         server2.registryUtil.setLog(EasyMock.createMock(LoggingUtil.class));
69         server2.setGroupName(groupName);
70
71         SystemUtil.setJndiPort("16400");
72         server2.startServer();
73         try {
74             Thread.sleep(1000);
75         } catch (InterruptedException JavaDoc e) {
76             // Do nothing
77
}
78         SystemUtil.setJndiPort("16401");
79         server1.startServer();
80     }
81
82     public void testBind() {
83         Socket JavaDoc socket = new Socket JavaDoc();
84         try {
85             // Wait for the servers to start
86
Thread.sleep(1000);
87
88             socket.connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400),
89                 1000);
90             RegistryRequest request = new BindRequest("/", "key", "value", 0, 0);
91
92             ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(
93                 socket.getOutputStream());
94
95             objectOutputStream.writeObject(request);
96             objectOutputStream.flush();
97
98             ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
99                 .getInputStream());
100             RegistryResponse jndiResponse = (RegistryResponse) objectInputStream
101                 .readObject();
102
103             // Wait for the other server to process the message
104
Thread.sleep(3000);
105
106             if (!jndiResponse.getType().equals(ResponseType.ack)) {
107                 fail();
108             } else {
109                 assertEquals(server1.data.size(), 1);
110                 assertEquals(server2.data.size(), 1);
111             }
112         } catch (Exception JavaDoc e) {
113             e.printStackTrace();
114         }
115
116     }
117
118     public void tearDown() {
119         server1.stopServer();
120         server2.stopServer();
121     }
122
123 }
124
Popular Tags