Friday, October 26, 2007

Unit Testing Class for Maps

Here is my class for unit testing BizTalk maps:

using Microsoft.XLANGs.BaseTypes;

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace MyNamespace.Test
{
  public class MapsUtil
  {
    public static void CompareMaps(TransformBase map, Stream originStream, Stream expectedStream)
    {
      XslTransform transformation = map.Transform;
      XsltArgumentList transformationArgs = map.TransformArgs;

      MemoryStream outputStream = new MemoryStream();

      XPathDocument xpathDoc = new XPathDocument(originStream);

      transformation.Transform(xpathDoc, transformationArgs, outputStream);

      outputStream.Position = 0;

      StreamUtil.CompareStreams(expectedStream, outputStream);

      expectedStream.Flush();
      expectedStream.Close();
      outputStream.Flush();
      outputStream.Close();
    }
  }
}

And here is an example that uses it:

[Test()]
public void MapTest()
{
  MyMap map = new MyMap();
  TransformBase mapping = (TransformBase)map;

  Stream originStream = DocLoader.LoadStream(MESSAGES_NAMESPACE, "NOOP1.Reply.input.xml");
  Stream expectedStream = DocLoader.LoadStream(MESSAGES_NAMESPACE, "NOOP1.Reply_output.xml");

  MapsUtil.CompareMaps(mapping, originStream, expectedStream);
}

Where CompareStreams is a class for compare two different streams and DocLoader is a class for load resources as streams.

No comments: