1 package org.sapia.ubik.rmi.server.transport.nio.tcp; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.DataInputStream ; 6 import java.io.DataOutputStream ; 7 import java.nio.ByteBuffer ; 8 9 import org.sapia.ubik.net.ServerAddress; 10 import org.sapia.ubik.net.nio.TestChannelManager; 11 import org.sapia.ubik.net.nio.TestCycle; 12 import org.sapia.ubik.net.nio.TestReadChannel; 13 import org.sapia.ubik.net.nio.TestWriteChannel; 14 import org.sapia.ubik.net.nio.util.ReadHelper; 15 import org.sapia.ubik.net.nio.util.WriteHelper; 16 17 import junit.framework.TestCase; 18 19 28 public class RmiChannelHandlerTest extends TestCase implements AddressProvider { 29 30 public RmiChannelHandlerTest(String name){ 31 super(name); 32 } 33 34 37 public ServerAddress getAddress() { 38 return new NioAddress("localhost", 0); 39 } 40 41 public void testReadWrite() throws Exception { 42 ByteArrayOutputStream out = new ByteArrayOutputStream (); 43 StringBuffer buf = new StringBuffer (); 44 int from = (int)'a'; 45 int to = ((int)'z')+1; 46 47 for(int i = 0; i < 5; i++){ 48 for(int j = from; j < to; j++){ 49 buf.append((char)j); 50 } 51 } 52 String dataStr = buf.toString(); 53 byte[] data = dataStr.getBytes(); 54 DataOutputStream dos = new DataOutputStream (out); 55 dos.writeInt(data.length); 56 dos.flush(); 57 dos.close(); 58 out.write(data); 59 TestReadChannel channel = 60 new TestReadChannel(out.toByteArray(), 10); 61 RmiChannelHandler handler = new RmiChannelHandler(new RmiChannelHandlerFactory(this, 15), 15); 62 TestChannelManager manager = new TestChannelManager(); 63 ByteBuffer buffer = ByteBuffer.allocate(10); 64 TestCycle cycle = new TestCycle(channel, handler, manager); 65 cycle.setByteBuffer(buffer); 66 ReadHelper helper = new ReadHelper(); 67 helper.read(cycle); 68 byte[] read = handler.getContent().toByteArray(); 69 String readStr = new String (read); 70 super.assertEquals(data.length, read.length); 71 super.assertEquals(dataStr, readStr); 72 73 buffer.clear(); 75 WriteHelper whelper = new WriteHelper(); 76 TestWriteChannel wchannel = new TestWriteChannel(); 77 cycle = new TestCycle(wchannel, handler, manager); 78 cycle.setByteBuffer(buffer); 79 whelper.write(cycle); 80 byte[] written = wchannel.getBytes(); 81 DataInputStream dis = new DataInputStream (new ByteArrayInputStream (written)); 82 int expectedPayloadSize = dis.readInt(); 83 super.assertEquals(data.length, expectedPayloadSize); 84 byte[] payload = new byte[data.length]; 85 int payloadSize = dis.read(payload); 86 super.assertEquals(data.length, payloadSize); 87 String payloadStr = new String (payload); 88 super.assertEquals(dataStr, payloadStr); 89 90 } 91 92 93 } 94 | Popular Tags |