Beispiel #1
0
    public void ObservableQueueSerializationTest()
    {
        // ARRANGE
        // =======

        // A random generator (we'll test random values to avoid hard-codings)
        var r = new Random();

        // The data contract serializer we'll use to serialize and deserialize
        var dcs = new DataContractSerializer(typeof(ObservableQueue <DummyDataContract>));

        // The dummy data
        var ddc1 = new DummyDataContract {
            RandomValue = r.Next()
        };
        var ddc2 = new DummyDataContract {
            RandomValue = r.Next()
        };
        var ddc3 = new DummyDataContract {
            RandomValue = r.Next()
        };
        var ddc4 = new DummyDataContract {
            RandomValue = r.Next()
        };

        // The original observable list
        using (var l1 = new ObservableQueue <DummyDataContract>
        {
            ddc1,
            ddc2,
            ddc3,
            ddc4,
        })
        {
            // The deserialized list
            ObservableQueue <DummyDataContract> l2;

            // The serialization content
            string content;

            // ACT
            // ===
            using (var ms = new MemoryStream())
            {
                dcs.WriteObject(
                    ms,
                    l1);

                _ = ms.Seek(
                    0,
                    SeekOrigin.Begin);

                using (var textReader = new StreamReader(
                           ms,
                           Encoding.UTF8,
                           false,
                           32768,
                           true))
                {
                    content = textReader.ReadToEnd();
                }

                _ = ms.Seek(
                    0,
                    SeekOrigin.Begin);

                l2 = dcs.ReadObject(ms) as ObservableQueue <DummyDataContract>;
            }

            try
            {
                // ASSERT
                // ======

                // Serialization content is OK
                Assert.False(string.IsNullOrWhiteSpace(content));
                Assert.Equal(
                    $@"<ObservableDDCQueue xmlns=""http://ns.ixiancorp.com/IX/IX.Observable"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:a=""http://schemas.datacontract.org/2004/07/IX.UnitTests.Observable""><Item><a:RandomValue>{ddc1.RandomValue}</a:RandomValue></Item><Item><a:RandomValue>{ddc2.RandomValue}</a:RandomValue></Item><Item><a:RandomValue>{ddc3.RandomValue}</a:RandomValue></Item><Item><a:RandomValue>{ddc4.RandomValue}</a:RandomValue></Item></ObservableDDCQueue>",
                    content);

                // Deserialized object is OK
                Assert.NotNull(l2);
                Assert.Equal(
                    l1.Count,
                    l2.Count);
                Assert.True(l1.SequenceEquals(l2));
            }
            finally
            {
                l2?.Dispose();
            }
        }
    }