package dslab.rmi; import dslab.rmi.channel.SocketChannel; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import static org.junit.Assert.assertEquals; public class SocketChannelTest { private ServerSocket serverSocket; private Socket clientSocket; private SocketChannel clientChannel; private SocketChannel serverChannel; @Before public void init() throws IOException { serverSocket = new ServerSocket(1234); clientSocket = new Socket((String) null, serverSocket.getLocalPort()); clientChannel = new SocketChannel(clientSocket); serverChannel = new SocketChannel(serverSocket.accept()); } @After public void cleanup() throws IOException { clientChannel.close(); serverChannel.close(); serverSocket.close(); } @Test public void shouldReadMultiLineCommands() throws IOException { var twoLinedInput = "First line\nSecond line"; clientChannel.write(twoLinedInput); assertEquals(serverChannel.readLinesWhileReady(), twoLinedInput); } @Test public void shouldReadEmptyLines() throws IOException { clientChannel.write("first line"); clientChannel.write(""); clientChannel.write("second line"); var readMessage = serverChannel.readLinesWhileReady(); assertEquals(readMessage, "first line\n\nsecond line"); } @Test(expected = NullPointerException.class) public void shouldThrowOnNulls() throws IOException { clientChannel.write(null); } }