1 22 package org.objectweb.petals.kernel.registry; 23 24 import java.io.ObjectInputStream ; 25 import java.io.ObjectOutputStream ; 26 import java.net.InetSocketAddress ; 27 import java.net.Socket ; 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 46 public class ReplicationServerTest extends TestCase { 47 48 private RegistryServer server1; 49 50 private RegistryServer server2; 51 52 public void setUp() { 53 String 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 { 76 Socket socket = new Socket (); 77 Thread.sleep(1000); 79 80 socket 81 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 82 RegistryRequest request = new BindRequest("/", "key", "value", 0, 0); 83 84 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 85 .getOutputStream()); 86 87 objectOutputStream.writeObject(request); 88 objectOutputStream.flush(); 89 90 ObjectInputStream objectInputStream = new ObjectInputStream (socket 91 .getInputStream()); 92 RegistryResponse jndiResponse = (RegistryResponse) objectInputStream 93 .readObject(); 94 95 Thread.sleep(1000); 97 98 SystemUtil.setJndiPort("16401"); 99 server2.startServer(); 100 101 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 112 } 113 114 public void tearDown() { 115 server1.stopServer(); 116 server2.stopServer(); 117 } 118 119 } 120 | Popular Tags |