コード例 #1
0
        public void ExceptionFullyMixedDictionarySerialization()
        {
            SerializableException sException;

            System.Exception exception;
            string           message      = "test";
            string           innerMessage = "inner test";

            Guid   guid         = Guid.NewGuid();
            Stream memoryStream = new MemoryStream();
            SerializableException       serializableException = new SerializableException(new InvalidOperationException(message));
            Dictionary <object, object> data = new Dictionary <object, object>
            {
                { guid, 16 },
                { "test", serializableException },
                { "foo", memoryStream }
            };

            try
            {
                throw new CustomException(4, "a stack trace", message, data, new CustomException(4, "a stack trace", innerMessage, null, null));
            }
            catch (CustomException ex)
            {
                exception  = ex;
                sException = new SerializableException(exception, true, true);
            }

            Assert.Equal(16, (int)(sException.Data[guid]));
            Assert.Null(sException.Data["foo"]);
            Assert.Equal((object)serializableException, sException.Data["test"]);
            AssertEqualExceptions(sException, exception, true, true);

            string jsonString = sException.SerializeJson();

            Assert.NotNull(jsonString);
            Assert.NotEmpty(jsonString);
            SerializableException deserialized = jsonString.DeserializeJson <SerializableException>();

            Assert.NotNull(deserialized);
            // the data can't be verified this way (they are new instances when deserialized)
            AssertEqualExceptionsSerializable(sException, deserialized, true, false);
            Assert.Equal(sException.Data.Count, deserialized.Data.Count);
            string guidKeyString = deserialized.Data.Keys.OfType <string>().FirstOrDefault(k => k == guid.ToString());

            Assert.NotNull(guidKeyString);
            Assert.NotEmpty(guidKeyString);
            Guid guidKey = Guid.Parse(guidKeyString);

            Assert.NotNull(deserialized.Data[guidKeyString]);
            Assert.Equal(sException.Data[guid], deserialized.Data[guidKeyString]);
            AssertEqualExceptionsSerializable(serializableException, (SerializableException)deserialized.Data["test"], true, true);
            Assert.Null(deserialized.Data["foo"]);

            memoryStream.Dispose();
        }