Biztalk 映射单元测试异常
Biztalk Map Unit Testing Exception
我正在尝试使用以下代码对地图进行单元测试,
protected string Map(TransformBase map, string xml)
{
StringWriter str = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(str);
map.Transform.Transform(new XPathDocument(new StringReader(xml)), new XsltArgumentList(), writer);
return str.ToString();
}
调用如下,
[Test]
public void Map_Test()
{
var result = Map(new TestMap(),File.ReadAllText(_dataDir.GetFiles("TestRequest.xml")[0].FullName));
Assert.IsTrue(result.Contains("4323432"));
}
这对大多数地图都适用,但是如果我使用外部程序集中的函数,这将不起作用并失败并显示错误
Result Message: System.Xml.Xsl.XslTransformException : Cannot find a script or an extension object associated with namespace 'http://schemas.microsoft.com/BizTalk/2003/ScriptNS0'.
Result StackTrace:
at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
at <xsl:template match="/workOrderRequest">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at <xsl:template match="/">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
at System.Xml.Xsl.XslCompiledTransform.Transform(XmlReader input, XsltArgumentList arguments, XmlWriter results)
无法参考外部程序集调试自定义 xslt。
查看 this 话题以获取更多信息
编辑: this 您可能也会感兴趣。
最后,我用下面的代码(BT 2013 R2)做到了这一点,
方法 Map
进行实际映射,returns 结果 xml,
参数,
map -> new object of the map type instance
xml -> source xml content
extObjects -> *_extxml.xml file content generated when executing validate instance on the map
代码
protected string Map(TransformBase map, string xml, string extObjects = null)
{
string result = string.Empty;
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings setting = new XsltSettings(false, true);
transform.Load(XmlReader.Create(new StringReader(map.XmlContent)), setting, new XmlUrlResolver());
using (StringWriter writer = new StringWriter())
{
transform.Transform(XmlReader.Create(new StringReader(xml)),
GetExtensionObjects(extObjects), XmlWriter.Create(writer));
result = writer.ToString();
}
return result;
}
protected XsltArgumentList GetExtensionObjects(string extObjects)
{
XsltArgumentList arguments = new XsltArgumentList();
if (extObjects == null)
return arguments;
XDocument extObjectsXDoc = XDocument.Parse(extObjects);
foreach (XElement node in extObjectsXDoc.Descendants("ExtensionObject"))
{
string assembly_qualified_name = String.Format("{0}, {1}", node.Attribute("ClassName").Value, node.Attribute("AssemblyName").Value);
object extension_object = Activator.CreateInstance(Type.GetType(assembly_qualified_name));
arguments.AddExtensionObject(node.Attribute("Namespace").Value, extension_object);
}
return arguments;
}
示例用法
[Test]
public void Map_Test()
{
var result = Map(new A_To_B()
, File.ReadAllText("A.xml")
, File.ReadAllText("A_To_B_extxml.xml"));
Assert.IsNotNullOrEmpty(result);
}
我正在尝试使用以下代码对地图进行单元测试,
protected string Map(TransformBase map, string xml)
{
StringWriter str = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(str);
map.Transform.Transform(new XPathDocument(new StringReader(xml)), new XsltArgumentList(), writer);
return str.ToString();
}
调用如下,
[Test]
public void Map_Test()
{
var result = Map(new TestMap(),File.ReadAllText(_dataDir.GetFiles("TestRequest.xml")[0].FullName));
Assert.IsTrue(result.Contains("4323432"));
}
这对大多数地图都适用,但是如果我使用外部程序集中的函数,这将不起作用并失败并显示错误
Result Message: System.Xml.Xsl.XslTransformException : Cannot find a script or an extension object associated with namespace 'http://schemas.microsoft.com/BizTalk/2003/ScriptNS0'.
Result StackTrace:
at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
at <xsl:template match="/workOrderRequest">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at <xsl:template match="/">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
at System.Xml.Xsl.XslCompiledTransform.Transform(XmlReader input, XsltArgumentList arguments, XmlWriter results)
无法参考外部程序集调试自定义 xslt。
查看 this 话题以获取更多信息
编辑: this 您可能也会感兴趣。
最后,我用下面的代码(BT 2013 R2)做到了这一点,
方法 Map
进行实际映射,returns 结果 xml,
参数,
map -> new object of the map type instance
xml -> source xml content
extObjects -> *_extxml.xml file content generated when executing validate instance on the map
代码
protected string Map(TransformBase map, string xml, string extObjects = null)
{
string result = string.Empty;
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings setting = new XsltSettings(false, true);
transform.Load(XmlReader.Create(new StringReader(map.XmlContent)), setting, new XmlUrlResolver());
using (StringWriter writer = new StringWriter())
{
transform.Transform(XmlReader.Create(new StringReader(xml)),
GetExtensionObjects(extObjects), XmlWriter.Create(writer));
result = writer.ToString();
}
return result;
}
protected XsltArgumentList GetExtensionObjects(string extObjects)
{
XsltArgumentList arguments = new XsltArgumentList();
if (extObjects == null)
return arguments;
XDocument extObjectsXDoc = XDocument.Parse(extObjects);
foreach (XElement node in extObjectsXDoc.Descendants("ExtensionObject"))
{
string assembly_qualified_name = String.Format("{0}, {1}", node.Attribute("ClassName").Value, node.Attribute("AssemblyName").Value);
object extension_object = Activator.CreateInstance(Type.GetType(assembly_qualified_name));
arguments.AddExtensionObject(node.Attribute("Namespace").Value, extension_object);
}
return arguments;
}
示例用法
[Test]
public void Map_Test()
{
var result = Map(new A_To_B()
, File.ReadAllText("A.xml")
, File.ReadAllText("A_To_B_extxml.xml"));
Assert.IsNotNullOrEmpty(result);
}