public void TestNullStreamEntryResultsInExpectedBehaviour()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            streamMessage.WriteObject(null);
            streamMessage.Reset();

            // expect an NFE from the primitive integral, float, double, and char <type>.valueOf(null) conversions
            AssertGetStreamEntryThrowsNullReferenceException <byte>(streamMessage);
            AssertGetStreamEntryThrowsNullReferenceException <short>(streamMessage);
            AssertGetStreamEntryThrowsNullReferenceException <int>(streamMessage);
            AssertGetStreamEntryThrowsNullReferenceException <long>(streamMessage);
            AssertGetStreamEntryThrowsNullReferenceException <float>(streamMessage);
            AssertGetStreamEntryThrowsNullReferenceException <double>(streamMessage);
            AssertGetStreamEntryThrowsNullReferenceException <char>(streamMessage);

            // expect null
            Assert.Null(streamMessage.ReadObject());
            streamMessage.Reset(); // need to reset as read was a success
            Assert.Null(streamMessage.ReadString());
            streamMessage.Reset(); // need to reset as read was a success

            // expect completion value.
            Assert.AreEqual(-1, streamMessage.ReadBytes(new byte[1]));
            streamMessage.Reset(); // need to reset as read was a success

            // expect false
            Assert.False(streamMessage.ReadBoolean());
        }
        public void TestReadObjectAfterPartialReadBytesThrowsMFE()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            byte[] bytes = { 4, 44, 99 };
            streamMessage.WriteBytes(bytes);

            streamMessage.Reset();

            // start reading via readBytes
            int partialLength = 2;

            byte[] retrievedByteArray = new byte[partialLength];
            int    readBytesLength    = streamMessage.ReadBytes(retrievedByteArray);

            Assert.AreEqual(partialLength, readBytesLength);
            CollectionAssert.AreEqual(bytes.Take(partialLength), retrievedByteArray, "Expected array subset to equal retrieved bytes");

            // check that using readObject does not return the full/remaining bytes as a new array

            Assert.Throws <MessageFormatException>(() => streamMessage.ReadObject());

            // finish reading via reaBytes to ensure it can be completed
            readBytesLength = streamMessage.ReadBytes(retrievedByteArray);
            Assert.AreEqual(bytes.Length - partialLength, readBytesLength);
            CollectionAssert.AreEqual(bytes.Skip(partialLength).Take(bytes.Length), retrievedByteArray.Take(readBytesLength), "Expected array subset to equal retrieved bytes");
        }
        public void TestReadBytesFullWithUndersizedDestinationArrayUsingMultipleReads()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            byte[] bytes = { 3, 78, 253, 26, 8 };
            Assert.AreEqual(1, bytes.Length % 2, "bytes should be odd length");

            int undersizedLength = 2;
            int remaining        = 1;

            streamMessage.WriteBytes(bytes);
            streamMessage.Reset();

            byte[] undersizedDestination = new byte[undersizedLength];
            byte[] fullRetrievedBytes    = new byte[bytes.Length];

            Assert.AreEqual(undersizedLength, streamMessage.ReadBytes(undersizedDestination), "Number of bytes read did not match destination array length");
            int read = undersizedLength;

            Array.Copy(undersizedDestination, 0, fullRetrievedBytes, 0, undersizedLength);
            Assert.AreEqual(undersizedLength, streamMessage.ReadBytes(undersizedDestination), "Number of bytes read did not match destination array length");
            Array.Copy(undersizedDestination, 0, fullRetrievedBytes, read, undersizedLength);
            read += undersizedLength;
            Assert.AreEqual(remaining, streamMessage.ReadBytes(undersizedDestination), "Number of bytes read did not match expectation");
            Array.Copy(undersizedDestination, 0, fullRetrievedBytes, read, remaining);
            read += remaining;
            CollectionAssert.AreEqual(bytes, fullRetrievedBytes, "Expected array to equal retrieved bytes");
        }
        public void TestIllegalTypeConversionFailureDoesNotIncrementPosition1()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            byte[] bytes = { 0, 255, 78 };

            streamMessage.WriteBytes(bytes);
            streamMessage.Reset();

            AssertGetStreamEntryThrowsMessageFormatException <bool>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <byte>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <short>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <char>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <int>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <long>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <float>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <double>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <string>(streamMessage);

            byte[] retrievedByteArray = new byte[bytes.Length];
            int    readBytesLength    = streamMessage.ReadBytes(retrievedByteArray);

            Assert.AreEqual(bytes.Length, readBytesLength, "Number of bytes read did not match original array length");
            CollectionAssert.AreEqual(bytes, retrievedByteArray, "Expected array to equal retrieved bytes");
            Assert.AreEqual(-1, streamMessage.ReadBytes(retrievedByteArray), "Expected completion return value");
        }
        public void TestReadWithEmptyStreamThrowsMEOFE()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            streamMessage.Reset();

            Assert.Throws <MessageEOFException>(() => streamMessage.ReadBoolean(), "Expected exception to be thrown as message has no content");
        }
        public void TestReadBytesWithNullArrayThrowsArgumentNullException()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            streamMessage.Reset();

            Assert.Throws <ArgumentNullException>(() => streamMessage.ReadBytes(null));
        }
        public void TestWriteStringReadLegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            string integralValue = Convert.ToString(byte.MaxValue);

            streamMessage.WriteString(integralValue);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <string>(streamMessage, true, integralValue);
            AssertGetStreamEntryEquals <bool>(streamMessage, true, false);
            AssertGetStreamEntryEquals <byte>(streamMessage, true, byte.MaxValue);

            streamMessage.ClearBody();
            integralValue = Convert.ToString(short.MaxValue);
            streamMessage.WriteString(integralValue);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <short>(streamMessage, true, short.MaxValue);

            streamMessage.ClearBody();
            integralValue = Convert.ToString(int.MaxValue);
            streamMessage.WriteString(integralValue);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <int>(streamMessage, true, int.MaxValue);

            streamMessage.ClearBody();
            integralValue = Convert.ToString(long.MaxValue);
            streamMessage.WriteString(integralValue);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <long>(streamMessage, true, long.MaxValue);

            streamMessage.ClearBody();
            string fpValue = Convert.ToString(float.MaxValue, CultureInfo.InvariantCulture);

            streamMessage.WriteString(fpValue);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <float>(streamMessage, true, float.Parse(fpValue));

            // TODO: make following pass
            //AssertGetStreamEntryEquals<double>(streamMessage, true, double.Parse(fpValue));
        }
        public void TestWriteReadBoolean()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();
            bool             value         = true;

            streamMessage.WriteBoolean(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadBoolean(), "Value not as expected");
        }
        public void TestReadBytesWithNullSignalsCompletion()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            streamMessage.WriteObject(null);

            streamMessage.Reset();

            Assert.AreEqual(-1, streamMessage.ReadBytes(new byte[1]), "Expected immediate completion signal");
        }
        public void TestWriteReadString()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            string value = "myString";

            streamMessage.WriteString(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadString(), "Value not as expected");
        }
        public void TestReadObjectGetsInvalidObjectThrowsMFE()
        {
            NmsTestStreamMessageFacade facade = new NmsTestStreamMessageFacade();

            facade.Put(new Uri("test://test"));
            NmsStreamMessage streamMessage = new NmsStreamMessage(facade);

            streamMessage.Reset();

            Assert.Throws <MessageFormatException>(() => streamMessage.ReadObject());
        }
        private void AssertGetStreamEntryEquals <T>(NmsStreamMessage testMessage, bool resetStreamAfter, object expectedValue)
        {
            object actualValue = GetStreamEntryUsingTypeMethod(testMessage, typeof(T), null);

            Assert.AreEqual(expectedValue, actualValue);

            if (resetStreamAfter)
            {
                testMessage.Reset();
            }
        }
        public void TestWriteReadChar()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            char value = 'c';

            streamMessage.WriteChar(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadChar(), "Value not as expected correctly");
        }
        public void TestWriteReadShort()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            short value = 6;

            streamMessage.WriteInt16(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadInt16(), "Value not as expected");
        }
        public void TestWriteReadLong()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            long value = long.MaxValue;

            streamMessage.WriteInt64(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadInt64(), "Value not as expected");
        }
        public void TestWriteReadFloat()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            float value = float.MaxValue;

            streamMessage.WriteSingle(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadSingle(), "Value not as expected");
        }
        public void TestWriteReadDouble()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            double value = double.MaxValue;

            streamMessage.WriteDouble(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadDouble(), "Value not as expected");
        }
        public void TestWriteReadByte()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            byte value = 6;

            streamMessage.WriteByte(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadByte(), "Value not as expected");
        }
        public void TestWriteReadInt()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            int value = int.MaxValue;

            streamMessage.WriteInt32(value);
            streamMessage.Reset();

            Assert.AreEqual(value, streamMessage.ReadInt32(), "Value not as expected");
        }
        public void TestWriteCharReadLegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            char value = 'c';

            streamMessage.WriteChar(value);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <char>(streamMessage, true, value);
            AssertGetStreamEntryEquals <string>(streamMessage, true, value.ToString());
        }
        public void TestWriteStringReadIllegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            string stringValue = "myString";

            streamMessage.WriteString(stringValue);
            streamMessage.Reset();

            AssertGetStreamEntryThrowsMessageFormatException <char>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <byte[]>(streamMessage);
        }
        public void TestWriteBooleanReadLegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            bool value = true;

            streamMessage.WriteBoolean(value);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <bool>(streamMessage, true, value);
            AssertGetStreamEntryEquals <string>(streamMessage, true, value.ToString());
        }
        public void TestWriteDoubleReadLegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            double value = double.MaxValue;

            streamMessage.WriteDouble(value);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <double>(streamMessage, true, value);
            AssertGetStreamEntryEquals <string>(streamMessage, true, value.ToString(CultureInfo.InvariantCulture));
        }
        public void TestReadBytesWithZeroLengthSource()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            streamMessage.WriteBytes(new byte[0]);

            streamMessage.Reset();

            byte[] fullRetrievedBytes = new byte[1];

            Assert.AreEqual(0, streamMessage.ReadBytes(fullRetrievedBytes), "Expected no bytes to be read, as none were written");
        }
        public void TestWriteLongReadLegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            long value = long.MaxValue;

            streamMessage.WriteInt64(value);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <long>(streamMessage, true, value);
            AssertGetStreamEntryEquals <string>(streamMessage, true, value.ToString());
        }
        public void TestIllegalTypeConversionFailureDoesNotIncrementPosition2()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            String stringVal = "myString";

            streamMessage.WriteString(stringVal);
            streamMessage.Reset();

            AssertGetStreamEntryThrowsMessageFormatException <byte[]>(streamMessage);

            Assert.AreEqual(stringVal, streamMessage.ReadString(), "Expected written string");
        }
        public void TestWriteByteReadLegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();
            byte             value         = 6;

            streamMessage.WriteByte(value);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <byte>(streamMessage, true, value);
            AssertGetStreamEntryEquals <short>(streamMessage, true, value);
            AssertGetStreamEntryEquals <int>(streamMessage, true, value);
            AssertGetStreamEntryEquals <long>(streamMessage, true, value);
            AssertGetStreamEntryEquals <string>(streamMessage, true, value.ToString());
        }
        public void TestWriteByteReadIllegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();
            byte             value         = 6;

            streamMessage.WriteByte(value);
            streamMessage.Reset();

            AssertGetStreamEntryThrowsMessageFormatException <bool>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <char>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <float>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <double>(streamMessage);
            AssertGetStreamEntryThrowsMessageFormatException <byte[]>(streamMessage);
        }
        public void TestReadObjectForBytesReturnsNewArray()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            byte[] bytes = { 11, 44, 99 };
            streamMessage.WriteBytes(bytes);

            streamMessage.Reset();

            byte[] retrievedBytes = (byte[])streamMessage.ReadObject();

            Assert.AreNotSame(bytes, retrievedBytes, "Expected different array objects");
            CollectionAssert.AreEqual(bytes, retrievedBytes, "Expected arrays to be equal");
        }
        public void TestWriteShortReadLegal()
        {
            NmsStreamMessage streamMessage = factory.CreateStreamMessage();

            short value = 302;

            streamMessage.WriteInt16(value);
            streamMessage.Reset();

            AssertGetStreamEntryEquals <short>(streamMessage, true, value);
            AssertGetStreamEntryEquals <int>(streamMessage, true, value);
            AssertGetStreamEntryEquals <long>(streamMessage, true, value);
            AssertGetStreamEntryEquals <string>(streamMessage, true, value.ToString());
        }