示例#1
0
        public void ReceiveNewMessagesWhenValidSocketShouldReceiveNewMessagesShowThemToTheUserAndUpdateLastMessage()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");
            mockSocket.ListToReceive.Add("otherUser: Unrelated message.<eof>");
            mockSocket.ListToReceive.Add("userName: Hello!<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add("Hello!");
            dataReader.ListToRead.Add("exit");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            client.Start();

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("userName<sep>Hello!<sep>server: userName joined the chat.<eof>", item));

            Assert.Collection(mockSocket.ReceivedMessages,
                              item => Assert.Equal("server: userName joined the chat.<eof>", item),
                              item => Assert.Equal("otherUser: Unrelated message.<eof>", item),
                              item => Assert.Equal("userName: Hello!<eof>", item));
        }
示例#2
0
        public void StartWhenValidSocketAndDataReaderShouldLogOnAndProcessChat()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");
            mockSocket.ListToReceive.Add("otherUser: Unrelated message.<eof>");
            mockSocket.ListToReceive.Add("userName: Hello!<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add("Hello!");
            dataReader.ListToRead.Add("exit");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.True(mockSocket.Connected);

            client.Start();

            Assert.False(mockSocket.Connected);
            Assert.True(mockSocket.Closed);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("userName<sep>Hello!<sep>server: userName joined the chat.<eof>", item));

            Assert.Collection(mockSocket.ReceivedMessages,
                              item => Assert.Equal("server: userName joined the chat.<eof>", item),
                              item => Assert.Equal("otherUser: Unrelated message.<eof>", item),
                              item => Assert.Equal("userName: Hello!<eof>", item));
        }
示例#3
0
        public void StartWhenNullSocketShouldThrowException()
        {
            MockClientSocket mockSocket = null;

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.Throws <ArgumentNullException>(() => client.Start());
        }
示例#4
0
        public void StartWhenNullDataReaderShouldThrowException()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");

            MockDataReader dataReader = null;

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.Throws <ArgumentNullException>(() => client.Start());
        }
示例#5
0
        public void TestMethod1()
        {
            var manager = new MockClientManager();
            var socket  = new MockClientSocket();

            var img = new Bitmap("test1.png");

            using var ms = new MemoryStream();
            img.Save(ms, ImageFormat.Png);
            var sendByte = ms.GetBuffer();

            var sender = ShareAlgorithmBuilder.NewBuilder()
                         .SetShareAlgorithmManager(manager)
                         .SetSocket(socket)
                         .BuildSend(new IPEndPoint(0, 0));

            sender.Send(sendByte);

            var Receiver = ShareAlgorithmBuilder.NewBuilder()
                           .SetShareAlgorithmManager(manager)
                           .SetSocket(socket)
                           .BuildRecieve(new IPEndPoint(0, 0));

            byte[] recieveData = null;
            var    task        = Receiver.RecieveAsync((data) => recieveData = data);

            while (!task.IsCompleted)
            {
                Thread.Yield();
            }

            var size1 = manager.SendDataSize[0];
            var size2 = manager.RecieveDataSize[0];

            Assert.AreEqual(size1, size2);

            Assert.AreEqual(sendByte.Length, recieveData.Length);

            for (int i = 0; i < sendByte.Length; i++)
            {
                Assert.AreEqual(sendByte[i], recieveData[i]);
            }
        }
示例#6
0
        public void LogOnWhenValidUserNameShouldLogOnReceiveGreetingMessageAndDisconnect()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.True(mockSocket.Connected);

            Assert.Equal("userName", client.LogOn());

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item));
        }
示例#7
0
        public void LogOnWhenUserNameContainsEOFTagShouldSendNothingShowErrorMessageAndRequestForNewUserName()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("user<eof>Name");
            dataReader.ListToRead.Add("userName");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.True(mockSocket.Connected);

            Assert.Equal("userName", client.LogOn());

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item));
        }
示例#8
0
        public void SendMessageWhenValidMessageShouldConnectSendMessageToServerAndDisconnect()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");
            mockSocket.ListToReceive.Add("userName: Hello!<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add("Hello!");
            dataReader.ListToRead.Add("exit");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            client.Start();

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("userName<sep>Hello!<sep>server: userName joined the chat.<eof>", item));
        }
示例#9
0
        public void LogOnWhenAlreadyExistingUserNameShouldReceiveErrorMessageAndRequestForNewUserName()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName already exist, choose a different user name.<eof>");
            mockSocket.ListToReceive.Add("server: newUserName joined the chat.<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add("newUserName");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.True(mockSocket.Connected);

            Assert.Equal("newUserName", client.LogOn());

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("newUserName<sep>logon<sep>NoLastMessage<eof>", item));
        }
示例#10
0
        public void SendMessageWhenNullStringShouldSendNothingShowErrorMessageAndRequestForNewMessage()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");
            mockSocket.ListToReceive.Add("userName: valid message<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add(null);
            dataReader.ListToRead.Add("valid message");
            dataReader.ListToRead.Add("exit");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            client.Start();

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("userName<sep>valid message<sep>server: userName joined the chat.<eof>", item));
        }