KickJava   Java API By Example, From Geeks To Geeks.

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


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 ReplicationServerTest 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         SystemUtil.setJndiPort("16400");
57         server1 = new RegistryServer();
58         Logger logger = EasyMock.createMock(Logger.class);
59         server1.logger = logger;
60         server1.configurationService = new MockConfigurationService();
61         server1.log = EasyMock.createMock(LoggingUtil.class);
62         server1.registryUtil.setLog(EasyMock.createMock(LoggingUtil.class));
63         server1.setGroupName(groupName);
64
65         server2 = new RegistryServer();
66         server2.logger = logger;
67         server2.configurationService = new MockConfigurationService();
68         server2.log = EasyMock.createMock(LoggingUtil.class);
69         server2.registryUtil.setLog(EasyMock.createMock(LoggingUtil.class));
70         server2.setGroupName(groupName);
71
72         server1.startServer();
73     }
74
75     public void testBind() throws Exception JavaDoc {
76         Socket JavaDoc socket = new Socket JavaDoc();
77         // Wait for the servers to start
78
Thread.sleep(1000);
79
80         socket
81             .connect(new InetSocketAddress JavaDoc(SystemUtil.getHost(), 16400), 1000);
82         RegistryRequest request = new BindRequest("/", "key", "value", 0, 0);
83
84         ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(socket
85             .getOutputStream());
86
87         objectOutputStream.writeObject(request);
88         objectOutputStream.flush();
89
90         ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
91             .getInputStream());
92         RegistryResponse jndiResponse = (RegistryResponse) objectInputStream
93             .readObject();
94
95         // Wait for the other server to process the message
96
Thread.sleep(1000);
97
98         SystemUtil.setJndiPort("16401");
99         server2.startServer();
100
101         // Wait for the other server to retrieve the data
102
Thread.sleep(3000);
103
104         if (!jndiResponse.getType().equals(ResponseType.ack)) {
105             fail();
106         } else {
107             assertEquals(server1.data.get("/").get("key"), "value");
108             assertEquals(server2.data.get("/").get("key"), "value");
109         }
110         // Thread.sleep(1000);
111

112     }
113
114     public void tearDown() {
115         server1.stopServer();
116         server2.stopServer();
117     }
118
119 }
120
Popular Tags