1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Xml.Serialization;
namespace Microsoft.TestCommon
{
/// <summary>
/// MSTest utility for testing code operating against a stream.
/// </summary>
public class SerializerAssert
{
private static SerializerAssert singleton = new SerializerAssert();
public static SerializerAssert Singleton { get { return singleton; } }
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="XmlSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingXmlSerializer(Type type, object objectInstance, Action<Stream> codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
XmlSerializer serializer = new XmlSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="XmlSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <typeparam name="T">The type to serialize.</typeparam>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingXmlSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
{
UsingXmlSerializer(typeof(T), objectInstance, codeThatChecks);
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="DataContractSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingDataContractSerializer(Type type, object objectInstance, Action<Stream> codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
DataContractSerializer serializer = new DataContractSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="DataContractSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <typeparam name="T">The type to serialize.</typeparam>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingDataContractSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
{
UsingDataContractSerializer(typeof(T), objectInstance, codeThatChecks);
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="DataContractJsonSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public static void UsingDataContractJsonSerializer(Type type, object objectInstance, Action<Stream> codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="DataContractJsonSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <typeparam name="T">The type to serialize.</typeparam>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingDataContractJsonSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
{
UsingDataContractJsonSerializer(typeof(T), objectInstance, codeThatChecks);
}
}
}
|