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 TwoServerTest 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 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 e) { 76 } 78 SystemUtil.setJndiPort("16401"); 79 server1.startServer(); 80 } 81 82 public void testBind() { 83 Socket socket = new Socket (); 84 try { 85 Thread.sleep(1000); 87 88 socket.connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 89 1000); 90 RegistryRequest request = new BindRequest("/", "key", "value", 0, 0); 91 92 ObjectOutputStream objectOutputStream = new ObjectOutputStream ( 93 socket.getOutputStream()); 94 95 objectOutputStream.writeObject(request); 96 objectOutputStream.flush(); 97 98 ObjectInputStream objectInputStream = new ObjectInputStream (socket 99 .getInputStream()); 100 RegistryResponse jndiResponse = (RegistryResponse) objectInputStream 101 .readObject(); 102 103 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 e) { 113 e.printStackTrace(); 114 } 115 116 } 117 118 public void tearDown() { 119 server1.stopServer(); 120 server2.stopServer(); 121 } 122 123 } 124 | Popular Tags |