Exemplo n.º 1
0
        public void Dispose_true()
        {
            MemoryStream   memoryStream = new MemoryStream();
            JsonStreamMock jsonStream   = new JsonStreamMock(memoryStream);

            jsonStream.Dispose(true);
            Assert.IsFalse(memoryStream.CanRead);
            Assert.IsFalse(memoryStream.CanWrite);
            Assert.IsFalse(memoryStream.CanSeek);
        }
Exemplo n.º 2
0
        public void ReadBytesAsync_in_write_only_mode()
        {
            using (JsonStreamMock jsonStream = new JsonStreamMock(new MemoryStream(), Modes.WriteOnly))
            {
                ForbiddenOperationException exception = Assert.ThrowsException <ForbiddenOperationException>(() =>
                {
                    jsonStream.ReadBytesAsync().GetAwaiter().GetResult();
                });

                Assert.AreEqual("Can't read in WriteOnly mode.", exception.Message);
            }
        }
Exemplo n.º 3
0
        public void ReadBytesAsync_using_optimized_constructor()
        {
            using (JsonStreamMock jsonStream = new JsonStreamMock(Modes.ReadAndWrite, GetTestFileName()))
            {
                ForbiddenOperationException exception = Assert.ThrowsException <ForbiddenOperationException>(() =>
                {
                    jsonStream.ReadBytesAsync().GetAwaiter().GetResult();
                });

                Assert.AreEqual("Do not call any async method when using optimized constructor.", exception.Message);
            }
        }
Exemplo n.º 4
0
 public void Constructor_with_stream_pass()
 {
     using (JsonStreamMock jsonStream = new JsonStreamMock(new MemoryStream()))
     {
         Assert.IsInstanceOfType(jsonStream.Stream, typeof(MemoryStream));
         Assert.IsTrue(jsonStream.Stream.CanWrite);
         Assert.IsTrue(jsonStream.Stream.CanRead);
         Assert.IsNotNull(jsonStream.BinaryWriter);
         Assert.IsNotNull(jsonStream.BinaryReader);
         Assert.IsFalse(jsonStream.IsUsingOptimizedConstructor);
         Assert.AreEqual(Modes.ReadAndWrite, jsonStream.Mode);
     }
 }
Exemplo n.º 5
0
 public void Constructor_with_filePath_when_mode_is_ReadWrite()
 {
     using (var fs = File.OpenWrite(GetTestFileName())) { }
     using (JsonStreamMock jsonStream = new JsonStreamMock(Modes.ReadAndWrite, GetTestFileName()))
     {
         Assert.IsInstanceOfType(jsonStream.Stream, typeof(FileStream));
         Assert.IsTrue(jsonStream.Stream.CanWrite);
         Assert.IsTrue(jsonStream.Stream.CanRead);
         Assert.IsNotNull(jsonStream.BinaryWriter);
         Assert.IsNotNull(jsonStream.BinaryReader);
         Assert.IsTrue(jsonStream.IsUsingOptimizedConstructor);
         Assert.AreEqual(Modes.ReadAndWrite, jsonStream.Mode);
     }
 }
Exemplo n.º 6
0
        public void GetNextDocumentSize_changing_default_length_when_end_of_stream_was_reached()
        {
            Stream stream = new MemoryStream();

            stream.Write(Encoding.UTF8.GetBytes("000001024"), 0, 9);
            stream.Position = stream.Length;

            using (JsonStreamMock jsonStream = new JsonStreamMock(stream, Modes.ReadAndWrite, 9))
            {
                int  size     = jsonStream.GetNextDocumentSize();
                long position = stream.Position;

                Assert.AreEqual(0, size);
                Assert.AreEqual(stream.Length, position);
            }
        }
Exemplo n.º 7
0
        public void GetNextDocumentSize_changing_default_length_value_pass()
        {
            Stream stream = new MemoryStream();

            stream.Write(Encoding.UTF8.GetBytes("000001024"), 0, 9);
            stream.Position = 0;

            using (JsonStreamMock jsonStream = new JsonStreamMock(stream, Modes.ReadAndWrite, 9))
            {
                int  size     = jsonStream.GetNextDocumentSize();
                long position = stream.Position;

                Assert.AreEqual(1024, size);
                Assert.AreEqual(jsonStream.DocumentSizeLengthInBytes, position);
            }
        }
Exemplo n.º 8
0
        public void GetNextDocumentSizeAsync_with_default_value_when_end_of_stream_was_reached()
        {
            Stream stream = new MemoryStream();

            stream.Write(Encoding.UTF8.GetBytes("00001024"), 0, 8);
            stream.Position = stream.Length;

            using (JsonStreamMock jsonStream = new JsonStreamMock(stream))
            {
                int  size     = jsonStream.GetNextDocumentSizeAsync().GetAwaiter().GetResult();
                long position = stream.Position;

                Assert.AreEqual(0, size);
                Assert.AreEqual(stream.Length, position);
            }
        }
Exemplo n.º 9
0
        public void GetNextDocumentSizeAsync_with_default_value_pass()
        {
            Stream stream = new MemoryStream();

            stream.Write(Encoding.UTF8.GetBytes("00001024"), 0, 8);
            stream.Position = 0;

            using (JsonStreamMock jsonStream = new JsonStreamMock(stream))
            {
                int  size     = jsonStream.GetNextDocumentSizeAsync().GetAwaiter().GetResult();
                long position = stream.Position;

                Assert.AreEqual(1024, size);
                Assert.AreEqual(jsonStream.DocumentSizeLengthInBytes, position);
            }
        }
Exemplo n.º 10
0
        public void GetNexDocumentSize_when_size_descriptor_is_invalid()
        {
            Stream stream = new MemoryStream();

            stream.Write(Encoding.UTF8.GetBytes("AsWedbUp"), 0, 8);
            stream.Position = 0;

            using (JsonStreamMock jsonStream = new JsonStreamMock(stream, Modes.ReadAndWrite, 8))
            {
                InvalidDocumentSizeLengthException exception = Assert.ThrowsException <InvalidDocumentSizeLengthException>(() =>
                {
                    int size = jsonStream.GetNextDocumentSize();
                });

                Assert.AreEqual(default(int), exception.ExpectedDocumentSizeLength);
                Assert.AreEqual(default(int), exception.ReadDocumentSizeLength);
                Assert.AreEqual("Error interpreting document size.", exception.Message);
            }
        }
Exemplo n.º 11
0
        public void GetNextDocumentSize_changing_default_length_when_cant_read_all_expected_buffer()
        {
            Stream stream = new MemoryStream();

            stream.Write(Encoding.UTF8.GetBytes("000001024"), 0, 9);
            stream.Position = 1;

            using (JsonStreamMock jsonStream = new JsonStreamMock(stream, Modes.ReadAndWrite, 9))
            {
                InvalidDocumentSizeLengthException exception = Assert.ThrowsException <InvalidDocumentSizeLengthException>(() =>
                {
                    int size = jsonStream.GetNextDocumentSize();
                });

                Assert.AreEqual(jsonStream.DocumentSizeLengthInBytes, exception.ExpectedDocumentSizeLength);
                Assert.AreNotEqual(exception.ReadDocumentSizeLength, exception.ExpectedDocumentSizeLength);
                Assert.IsTrue(exception.ReadDocumentSizeLength < exception.ExpectedDocumentSizeLength);
            }
        }
Exemplo n.º 12
0
        public void GetNextDocumentSizeAsync_with_default_value_when_cant_read_all_expected_buffer()
        {
            Stream stream = new MemoryStream();

            stream.Write(Encoding.UTF8.GetBytes("00001024"), 0, 8);
            stream.Position = 1;

            using (JsonStreamMock jsonStream = new JsonStreamMock(stream))
            {
                InvalidDocumentSizeLengthException exception = Assert.ThrowsException <InvalidDocumentSizeLengthException>(() =>
                {
                    int size = jsonStream.GetNextDocumentSizeAsync().GetAwaiter().GetResult();
                });

                Assert.AreEqual(jsonStream.DocumentSizeLengthInBytes, exception.ExpectedDocumentSizeLength);
                Assert.AreNotEqual(exception.ReadDocumentSizeLength, exception.ExpectedDocumentSizeLength);
                Assert.IsTrue(exception.ReadDocumentSizeLength < exception.ExpectedDocumentSizeLength);
            }
        }