Index: xword/ContentFiltering/ContentFiltering.csproj =================================================================== --- xword/ContentFiltering/ContentFiltering.csproj (revision 21249) +++ xword/ContentFiltering/ContentFiltering.csproj (working copy) @@ -31,6 +31,10 @@ 4 + + False + ..\dependencies\nunit.framework.dll + 3.5 @@ -68,6 +72,13 @@ + + + + + + + Index: xword/ContentFiltering/Office/Word/Filters/GrammarAndSpellingErrorsFilter.cs =================================================================== --- xword/ContentFiltering/Office/Word/Filters/GrammarAndSpellingErrorsFilter.cs (revision 21249) +++ xword/ContentFiltering/Office/Word/Filters/GrammarAndSpellingErrorsFilter.cs (working copy) @@ -9,6 +9,7 @@ public class GrammarAndSpellingErrorsFilter:IDOMFilter { private ConversionManager manager; + public GrammarAndSpellingErrorsFilter(ConversionManager manager) { this.manager = manager; Index: xword/ContentFiltering/Office/Word/Filters/StyleRemoverFilter.cs =================================================================== --- xword/ContentFiltering/Office/Word/Filters/StyleRemoverFilter.cs (revision 21249) +++ xword/ContentFiltering/Office/Word/Filters/StyleRemoverFilter.cs (working copy) @@ -17,7 +17,7 @@ this.manager = manager; } - #region IDOMFilter Members + #region IDOMFilter Myembers /// /// Deletes the style attributes from the Word generated content. /// Index: xword/ContentFiltering/Test/Office/Word/Filters/GrammarAndSpellingErrorsFilterTest.cs =================================================================== --- xword/ContentFiltering/Test/Office/Word/Filters/GrammarAndSpellingErrorsFilterTest.cs (revision 0) +++ xword/ContentFiltering/Test/Office/Word/Filters/GrammarAndSpellingErrorsFilterTest.cs (revision 0) @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using System.Xml; +using ContentFiltering.Test.Util; +using ContentFiltering.Office.Word.Filters; +using XWiki.Office.Word; + +namespace ContentFiltering.Test.Office.Word.Filters +{ + + /// + /// Test class for GrammarAndSpellingErrorsFilter. + /// + [TestFixture] + public class GrammarAndSpellingErrorsFilterTest + { + private ConversionManager manager; + private string initialHTML; + private string cleanedHTML; + private XmlDocument initialXmlDoc; + private XmlDocument cleanedXmlDoc; + + /// + /// Default constructor. + /// + public GrammarAndSpellingErrorsFilterTest() + { + manager = ConversionManagerUtil.DummyConversionManager(); + initialHTML = ""; + cleanedHTML = ""; + initialXmlDoc = new XmlDocument(); + cleanedXmlDoc=new XmlDocument(); + } + + /// + /// Initialize the test. + /// + [TestFixtureSetUp] + public void GlobalSetUp() + { + initialHTML = "" + + "" + + "
" + + "

Page title gos here

" + + "

Page content goes here.

" + + "

And some errrors goes here.

" + + "

We shold have both grammar and spelingserors.

" + + "

He have apples.

" + + "

Un cuvant

" + + "
" + + "" + + ""; + + cleanedHTML ="" + + "" + + "
" + + "

Page title gos here

" + + "

Page content goes here.

" + + "

And some errrors goes here.

" + + "

We shold have both grammar and spelings erors .

" + + "

He have apples.

" + + "

Un cuvant

" + + "
" + + "" + + ""; + + initialXmlDoc.LoadXml(initialHTML); + cleanedXmlDoc.LoadXml(cleanedHTML); + } + + /// + /// Test method for grammar and spelling errors filter. + /// + [Test] + public void TestFilter() + { + new GrammarAndSpellingErrorsFilter(manager).Filter(ref initialXmlDoc); + Assert.IsTrue(XmlDocComparator.Compare(initialXmlDoc, cleanedXmlDoc)); + } + } +} Index: xword/ContentFiltering/Test/Office/Word/Filters/LocalImageAdaptorFilterTest.cs =================================================================== --- xword/ContentFiltering/Test/Office/Word/Filters/LocalImageAdaptorFilterTest.cs (revision 0) +++ xword/ContentFiltering/Test/Office/Word/Filters/LocalImageAdaptorFilterTest.cs (revision 0) @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using ContentFiltering.Office.Word.Filters; +using System.Xml; +using XWiki.Office.Word; +using ContentFiltering.Test.Util; + +namespace ContentFiltering.Test.Office.Word.Filters +{ + /// + /// Test class for LocalImageAdaptorFilter. + /// + [TestFixture] + public class LocalImageAdaptorFilterTest + { + private ConversionManager manager; + private string initialHTML; + private XmlDocument initialXmlDoc; + + /// + /// Default constructor. + /// + public LocalImageAdaptorFilterTest() + { + manager = ConversionManagerUtil.DummyConversionManager(); + initialHTML = ""; + initialXmlDoc = new XmlDocument(); + } + + /// + /// Initialize the test. + /// + [TestFixtureSetUp] + public void GlobalSetUp() + { + initialHTML = "" + + "" + + "

Heading 1

" + + "

" + + "\"DevManager-server.png\"" + + "

" + + "" + + ""; + + initialXmlDoc.LoadXml(initialHTML); + } + + /// + /// Test method for local image adaptor filter. + /// Verifies if images are 'bordered' with comments and image sources start with 'http'. + /// + [Test] + public void TestFilter() + { + new LocalImageAdaptorFilter(manager).Filter(ref initialXmlDoc); + XmlNodeList images=initialXmlDoc.GetElementsByTagName("img"); + foreach (XmlNode image in images) + { + Assert.AreEqual(image.PreviousSibling.NodeType, XmlNodeType.Comment); + Assert.IsTrue(image.PreviousSibling.Value.IndexOf("startimage") >= 0); + Assert.AreEqual(image.NextSibling.NodeType, XmlNodeType.Comment); + Assert.IsTrue(image.NextSibling.Value.IndexOf("stopimage") >= 0); + Assert.IsTrue(image.Attributes["src"].Value.IndexOf("http") >= 0); + } + } + } +} Index: xword/ContentFiltering/Test/Office/Word/Filters/LocalMacrosAdaptorFilterTest.cs =================================================================== --- xword/ContentFiltering/Test/Office/Word/Filters/LocalMacrosAdaptorFilterTest.cs (revision 0) +++ xword/ContentFiltering/Test/Office/Word/Filters/LocalMacrosAdaptorFilterTest.cs (revision 0) @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using System.Xml; +using ContentFiltering.Test.Util; +using ContentFiltering.Office.Word.Filters; +using XWiki.Office.Word; + +namespace ContentFiltering.Test.Office.Word.Filters +{ + /// + /// Test class for LocalMacrosAdaptorFilter. + /// + [TestFixture] + public class LocalMacrosAdaptorFilterTest + { + private ConversionManager manager; + private string initialHTML; + private string expectedHTML; + private XmlDocument initialXmlDoc; + private XmlDocument expectedXmlDoc; + + /// + /// Default constructor. + /// + public LocalMacrosAdaptorFilterTest() + { + manager = ConversionManagerUtil.DummyConversionManager(); + initialHTML = ""; + expectedHTML = ""; + initialXmlDoc = new XmlDocument(); + expectedXmlDoc = new XmlDocument(); + } + + + /// + /// Initialize the test. + /// + [TestFixtureSetUp] + public void GlobalSetUp() + { + //'known' macros + manager.States.Macros.Add("9225601", "generated html content"); + + initialHTML = "" + + "" + + "

Heading 1

" + + "

Normal text

" + + "" + + "

<img border=\"0\" src=\"" + + " http://opi.yahoo.com/online?u=yahoohelper&amp;m=g&amp;t=1\" alt=\"yahoo yahoohelper\" />" + + "" + + "

" + + "
" + + "

Some text goes here" + + "

More text goes here.

" + + "" + + ""; + + expectedHTML = "" + + "" + + "

Heading 1

" + + "

Normal text

" + + "generated html content" + + "

Some text goes here" + + "

More text goes here.

" + + "" + + ""; + + initialXmlDoc = new XmlDocument(); + expectedXmlDoc = new XmlDocument(); + + initialXmlDoc.LoadXml(initialHTML); + expectedXmlDoc.LoadXml(expectedHTML); + } + + /// + /// Test method for local macros adaptor filter. + /// Content controls which represent XWiki macros should be tranformed (in macros) + /// and content controls which belong to Word should not be altered. + /// + [Test] + public void TestFilter() + { + new LocalMacrosAdaptorFilter(manager).Filter(ref initialXmlDoc); + XmlNodeList stds = initialXmlDoc.GetElementsByTagName("Sdt", "urn:schemas-microsoft-com:office:word"); + + bool foundElement9225601 = false; + bool foundElement15075750 = false; + + foreach (XmlNode std in stds) + { + if (std.Attributes["ID"] != null) + { + if (std.Attributes["ID"].Value == "9225601") + { + foundElement9225601 = true; + } + if (std.Attributes["ID"].Value == "15075750") + { + foundElement15075750 = true; + } + } + } + + Assert.IsTrue(foundElement15075750); + Assert.IsFalse(foundElement9225601); + + Assert.IsTrue(initialXmlDoc.InnerText.ToLower().IndexOf("generated html content") >= 0); + } + } +} Index: xword/ContentFiltering/Test/Office/Word/Filters/OfficeAttributesRemoverFilterTest.cs =================================================================== --- xword/ContentFiltering/Test/Office/Word/Filters/OfficeAttributesRemoverFilterTest.cs (revision 0) +++ xword/ContentFiltering/Test/Office/Word/Filters/OfficeAttributesRemoverFilterTest.cs (revision 0) @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using System.Xml; +using ContentFiltering.Test.Util; +using ContentFiltering.Office.Word.Filters; +using XWiki.Office.Word; +using NUnit.Framework; +using System.Xml; +using ContentFiltering.Test.Util; +using ContentFiltering.Office.Word.Filters; +using XWiki.Office.Word; + +namespace ContentFiltering.Test.Office.Word.Filters +{ + /// + /// Test class for OfficeAttributesRemoverFilter. + /// + [TestFixture] + public class OfficeAttributesRemoverFilterTest + { + private ConversionManager manager; + private string initialHTML; + private string expectedHTML; + private XmlDocument initialXmlDoc; + private XmlDocument expectedXmlDoc; + + /// + /// Default constructor. + /// + public OfficeAttributesRemoverFilterTest() + { + manager = ConversionManagerUtil.DummyConversionManager(); + initialHTML = ""; + expectedHTML = ""; + initialXmlDoc = new XmlDocument(); + expectedXmlDoc = new XmlDocument(); + } + + /// + /// Initialize the test. + /// + [TestFixtureSetUp] + public void GlobalSetUp() + { + initialHTML = "" + + "" + + "

o content

" + + "

w content

" + + "" + + "

m content

" + + ""; + + expectedHTML = "" + + "" + + "

o content

" + + "

w content

" + + "" + + "

m content

" + + ""; + + + initialXmlDoc.LoadXml(initialHTML); + expectedXmlDoc.LoadXml(expectedHTML); + } + + /// + /// Test method for office attributes remover filter. + /// + [Test] + public void TestFilter() + { + new OfficeAttributesRemoverFilter(manager).Filter(ref initialXmlDoc); + Assert.IsTrue(XmlDocComparator.Compare(initialXmlDoc, expectedXmlDoc)); + } + } +} Index: xword/ContentFiltering/Test/Util/ConversionManagerUtil.cs =================================================================== --- xword/ContentFiltering/Test/Util/ConversionManagerUtil.cs (revision 0) +++ xword/ContentFiltering/Test/Util/ConversionManagerUtil.cs (revision 0) @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using XWiki.Office.Word; +using XWiki.Clients; + +namespace ContentFiltering.Test.Util +{ + public class ConversionManagerUtil + { + private static string serverURL, localFolder, docFullName, localFileName; + private static IXWikiClient client = null; + private static ConversionManager manager=null; + + /// + /// Creates dummy ConverionManeger to be used by unit tests. + /// + /// Same instance of a dummy ConversionManager + public static ConversionManager DummyConversionManager() + { + if (manager == null) + { + serverURL = "http://127.0.0.1:8080"; + localFolder = "localFolder"; + docFullName = "docFullName"; + localFileName = "localFileName"; + client = XWikiClientUtil.DummyXWikiClient(); + manager = new ConversionManager(serverURL, localFolder, docFullName, localFileName, client); + } + return manager; + } + } +} Index: xword/ContentFiltering/Test/Util/XmlDocComparator.cs =================================================================== --- xword/ContentFiltering/Test/Util/XmlDocComparator.cs (revision 0) +++ xword/ContentFiltering/Test/Util/XmlDocComparator.cs (revision 0) @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml; +using System.Collections; + +namespace ContentFiltering.Test.Util +{ + public class XmlDocComparator + { + /// + /// Compares two XmlDocuments. + /// Return TRUE if the xml dcouments have the same nodes, in the same position with the exact attributes. + /// + /// True if the xml dcouments have the same nodes, in the same position with the exact attributes. + public static bool Compare(XmlDocument xmlDoc1,XmlDocument xmlDoc2) + { + XmlNodeList nodeList1 = xmlDoc1.ChildNodes; + XmlNodeList nodeList2 = xmlDoc2.ChildNodes; + bool same = true; + + if (nodeList1.Count != nodeList2.Count) + { + return false; + } + + IEnumerator enumerator1 = nodeList1.GetEnumerator(); + IEnumerator enumerator2 = nodeList2.GetEnumerator(); + while (enumerator1.MoveNext() && enumerator2.MoveNext() && same) + { + same = CompareNodes((XmlNode)enumerator1.Current, (XmlNode)enumerator2.Current); + } + return same; + } + + private static bool CompareNodes(XmlNode node1, XmlNode node2) + { + //compare properties + if (node1.Attributes == null||node2.Attributes==null) + { + bool nullAttributes = (node1.Attributes == null && node2.Attributes == null); + if (!nullAttributes) + { + return false; + } + } + else + { + if (node1.Attributes.Count != node2.Attributes.Count) + { + Console.WriteLine("Attributes count: " + node1.Attributes.Count + "!=" + node2.Attributes.Count); + Console.WriteLine(node1.ParentNode.InnerXml); + Console.WriteLine(node2.ParentNode.InnerXml); + return false; + } + } + if (node1.ChildNodes.Count != node2.ChildNodes.Count) + { + Console.WriteLine("Child nodes count: " + node1.ChildNodes.Count + " !=" + node2.ChildNodes.Count); + return false; + } + + if (node1.Name != node2.Name) + { + Console.WriteLine("Nodes Name: " + node1.Name + "!=" + node2.Name); + return false; + } + + if (node1.NodeType != node2.NodeType) + { + Console.WriteLine("Nodes Type: " + node1.NodeType + "!=" + node2.NodeType); + return false; + } + + if ((""+node1.Value).Trim() != (""+node2.Value).Trim()) + { + Console.WriteLine("Nodes value: " + node1.Value + "!=" + node2.Value); + return false; + } + + + //compare attributes + XmlAttribute attribute; + if (node1.Attributes != null && node2.Attributes != null) + { + foreach (XmlAttribute attr in node1.Attributes) + { + attribute = node2.Attributes[attr.Name]; + if (attribute == null) + { + Console.WriteLine("Null attribute: " + attr.Name); + return false; + } + if (attribute.Value != attr.Value) + { + Console.WriteLine("Attribute values: " + attribute.Value + "!=" + attr.Value); + return false; + } + } + } + //compare child nodes + IEnumerator enumerator1 = node1.GetEnumerator(); + IEnumerator enumerator2 = node2.GetEnumerator(); + bool childrenOK = true; + while (enumerator1.MoveNext() && enumerator2.MoveNext()) + { + childrenOK = CompareNodes((XmlNode)enumerator1.Current, (XmlNode)enumerator2.Current); + if (!childrenOK) + { + return false; + } + } + + + //same properties, same attributes, same childnodes + return true; + } + } +} Index: xword/ContentFiltering/Test/Util/XWikiClientUtil.cs =================================================================== --- xword/ContentFiltering/Test/Util/XWikiClientUtil.cs (revision 0) +++ xword/ContentFiltering/Test/Util/XWikiClientUtil.cs (revision 0) @@ -0,0 +1,144 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using XWiki.Clients; + +namespace ContentFiltering.Test.Util +{ + public class XWikiClientUtil + { + private static IXWikiClient client=null; + + /// + /// Returns a dummy IXWikiClient implementation to be used by unit tests. + /// + /// The same DummyIXWikiClient instance. + public static IXWikiClient DummyXWikiClient() + { + if (client == null) + { + client = new DummyIXWikiClient(); + } + return client; + } + } + + /// + /// A dummy IXWikiClient implementation. + /// + class DummyIXWikiClient : IXWikiClient + { + #region IXWikiClient Members + + public bool LoggedIn + { + get { return true; } + } + + public string ServerURL + { + get + { + return "http://127.0.0.1:8080"; + } + set + { + + } + } + + public Encoding ServerEncoding + { + get { return Encoding.UTF8; } + } + + public XWikiClientType ClientType + { + get { return XWikiClientType.HTTP_Client; } + } + + public bool Login(string username, string password) + { + return true; + } + + public List GetAvailableSyntaxes() + { + return new List() { "XWiki 2.0", "XHTML"}; + } + + public List GetSpacesNames() + { + return new List() { "Space1" }; + } + + public List GetPagesNames(string spaceName) + { + return new List() { "Page1" }; + } + + public bool SavePageHTML(string docName, string content, string syntax) + { + return true; + } + + public bool AddAttachment(string docName, string filePath) + { + return true; + } + + public bool AddAttachment(string space, string page, string filePath) + { + return true; + } + + public void AddAttachmentAsync(string docName, string filePath) + { + + } + + public void AddAttachmentAsync(string space, string page, string filePath) + { + + } + + public List GetDocumentAttachmentList(string docFullName) + { + return new List() { }; + } + + public string GetAttachmentURL(string docFullName, string attachmentName) + { + return "http://127.0.0.1:8080/xwiki/bin/download/Main/Page1/"+attachmentName; + } + + public int AddObject(string docName, string className, System.Collections.Specialized.NameValueCollection fieldsValues) + { + return 0; + } + + public byte[] GetAttachmentContent(string pageName, string fileName) + { + return new byte[0]; + } + + public string GetRenderedPageContent(string pageFullName) + { + return ""; + } + + public string GetRenderedPageContent(string space, string page) + { + return ""; + } + + public string GetURL(string documentFullName) + { + return "http://127.0.0.1:8080/xwiki/bin/view/Main/Page1/"; + } + + #endregion + } + +}