Geeks With Blogs

News

View Anthony Trudeau's profile on LinkedIn

Add to Technorati Favorites


Anthony Trudeau

I'm created a simple utility class for unit testing serialization for my first post of the year.  I originally created the class to test data contract serialization, but I extended it for IFormatter based serializers for another class I needed to test.  I hope it proves useful.

You'll need the following using statements:

using System;

using System.IO;

using System.Runtime.Serialization;


The following utility class will help you with unit testing serialization for your data contracts and serialization using a standard IFormatter:

internal static class SerializationHelper

{

    public static string SerializeDataContract<T>(T testObject) where T: class

    {

        string fileName = Path.GetTempFileName();

 

        using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate))

        {

            DataContractSerializer serializer =
                new DataContractSerializer(typeof(T));

            serializer.WriteObject(stream, testObject);

        }

 

        return fileName;

    }

 

    public static string SerializeObject<TObject, TFormatter>(TObject testObject)

        where TObject : class

        where TFormatter : IFormatter

    {

        string fileName = Path.GetTempFileName();

 

        using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate))

        {

            IFormatter serializer = Activator.CreateInstance<TFormatter>();

            serializer.Serialize(stream, testObject);

        }

 

        return fileName;

    }

 

    public static T DeserializeDataContract<T>(string fileName) where T : class

    {

        using (FileStream stream = new FileStream(fileName, FileMode.Open))

        {

            DataContractSerializer serializer =
                new DataContractSerializer(typeof(T));

            return (serializer.ReadObject(stream) as T);

        }

    }

 

    public static TObject DeserializeObject<TObject, TFormatter>(string fileName)

        where TObject : class

        where TFormatter : IFormatter

    {

        using (FileStream stream = new FileStream(fileName, FileMode.Open))

        {

            IFormatter serializer = Activator.CreateInstance<TFormatter>();

            return (serializer.Deserialize(stream) as TObject);

        }

    }

}


The following is a sample of how you can use the utility class to test a data contract based class (source and target are both objects of the same data contract type):

string serializedFile = SerializationHelper.SerializeDataContract(source);

target = SerializationHelper

    .DeserializeDataContract<WorkflowRequest>(serializedFile);

Posted on Sunday, January 4, 2009 4:54 PM .NET | Back to top


Comments on this post: Unit testing for serialization

# re: Unit testing for serialization
Requesting Gravatar...
I save your class and try it next time I have to Unit test a serialization class.

At first look, it's loko simple but should made the job!
Left by Marky on Jan 05, 2009 8:34 AM

Your comment:
 (will show your gravatar)


Copyright © Anthony Trudeau | Powered by: GeeksWithBlogs.net