// // System.Xml.Xsl.XslTransformTests.cs // // Author: // Atsushi Enomoto // // (C) 2002 Atsushi Enomoto // using System; using System.Globalization; using System.IO; using System.Text; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; using NUnit.Framework; namespace MonoTests.System.Xml.Xsl { [TestFixture] public class XslTransformTests : Assertion { XmlDocument doc; XslTransform xslt; XmlDocument result; [SetUp] public void GetReady() { doc = new XmlDocument (); xslt = new XslTransform (); result = new XmlDocument (); } [Test] public void TestBasicTransform () { doc.LoadXml (""); xslt.Load ("Test/XmlFiles/xsl/empty.xsl"); xslt.Transform ("Test/XmlFiles/xsl/empty.xsl", "Test/XmlFiles/xsl/result.xml"); result.Load ("Test/XmlFiles/xsl/result.xml"); AssertEquals ("count", 2, result.ChildNodes.Count); } [Test] [ExpectedException (typeof (XsltCompileException))] public void InvalidStylesheet () { XmlDocument doc = new XmlDocument (); doc.LoadXml (""); XslTransform t = new XslTransform (); t.Load (doc); } [Test] [ExpectedException (typeof (XsltCompileException))] public void EmptyStylesheet () { XmlDocument doc = new XmlDocument (); XslTransform t = new XslTransform (); t.Load (doc); } [Test] [ExpectedException (typeof (XsltCompileException))] public void InvalidStylesheet2 () { string xml = @"text"; string xsl = @" "; XslTransform xslt = new XslTransform (); xslt.Load (new XPathDocument (new XmlTextReader (xsl, XmlNodeType.Document, null))); } [Test()] [Category ("NotWorking")] // it depends on "mcs" existence public void MsxslTest() { string _styleSheet = @" "; StringReader stringReader = new StringReader(_styleSheet); XslTransform transform = new XslTransform(); XmlTextReader reader = new XmlTextReader(stringReader); transform.Load(reader, new XmlUrlResolver(), AppDomain.CurrentDomain.Evidence); StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb, CultureInfo.InvariantCulture); XsltArgumentList arguments = new XsltArgumentList(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(""); // Do transformation transform.Transform(xmlDoc, new XsltArgumentList(), writer, new XmlUrlResolver()); AssertEquals("test".PadRight(20), sb.ToString()); } [Test] public void MSXslNodeSet () { string xsl = @" : "; StringWriter sw = new StringWriter (); XslTransform t = new XslTransform (); t.Load (new XPathDocument (new StringReader (xsl))); t.Transform (new XPathDocument (new XmlTextReader (new StringReader (""))), null, sw); AssertEquals (@"foo: Afoo: Bfoo: C", sw.ToString ()); } [Test] [Category ("NotDotNet")] // Actually MS.NET here throws XsltException, but Mono returns // XPathException (since XPath evaluation engine generally // catches (should catch) static error. It is implementation // dependent matter. [ExpectedException (typeof (XPathException))] public void MSXslNodeSetRejectsNodeSet () { string xsl = @" : "; StringWriter sw = new StringWriter (); XslTransform t = new XslTransform (); t.Load (new XPathDocument (new StringReader (xsl))); t.Transform (new XPathDocument (new XmlTextReader (new StringReader (""))), null, sw); } [Test] public void EvaluateEmptyVariableAsBoolean () { string xsl = @" true "; XslTransform t = new XslTransform (); t.Load (new XPathDocument (new StringReader (xsl))); StringWriter sw = new StringWriter (); t.Transform ( new XPathDocument (new StringReader ("")), null, sw); Assert (sw.ToString ().IndexOf ("true") > 0); } [Test] [ExpectedException (typeof (XsltCompileException))] public void NotAllowedPatternAxis () { string xsl = @" "; new XslTransform ().Load (new XPathDocument ( new StringReader (xsl))); } [Test] [ExpectedException (typeof (XsltCompileException))] public void ImportIncorrectlyLocated () { string xsl = @" "; new XslTransform ().Load (new XPathDocument ( new StringReader (xsl))); } [Test] [Category ("NotDotNet")] public void DontHoldStylesheetReference () { // This test is optional. Examines whether Load() does // not hold loaded stylesheet XPathNavigator internally. XslTransform t = new XslTransform (); WeakReference wr = StylesheetLoad (t, ""); GC.Collect (0); GC.Collect (2); Assert ("load", !wr.IsAlive); wr = StylesheetTransform (t, ""); GC.Collect (0); GC.Collect (2); Assert ("transform", !wr.IsAlive); } private WeakReference StylesheetLoad (XslTransform t, string xsl) { XPathDocument doc = new XPathDocument ( new StringReader (xsl)); WeakReference wr = new WeakReference (doc); t.Load (doc); return wr; } private WeakReference StylesheetTransform (XslTransform t, string xml) { XPathDocument doc = new XPathDocument ( new StringReader (xml)); WeakReference wr = new WeakReference (doc); t.Transform (doc, null, TextWriter.Null, null); return wr; } } }