PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ConfigGen.Utilities.Tests/Xml/XmlStreamFormatterTests.cs

http://configgen.codeplex.com
C# | 406 lines | 289 code | 74 blank | 43 comment | 2 complexity | 7addfdb736067d3540944155cac3bdd4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0, GPL-3.0, BSD-3-Clause
  1. #region Copyright and License Notice
  2. // Copyright (C)2010-2012 - Rob Levine and other contributors
  3. // http://configgen.codeplex.com
  4. //
  5. // This file is part of ConfigGen.
  6. //
  7. // ConfigGen is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU Lesser General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // ConfigGen is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License and
  18. // the GNU Lesser General Public License along with ConfigGen.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. #endregion
  21. using System;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Text;
  25. using ConfigGen.Utilities.Xml;
  26. using NUnit.Framework;
  27. namespace ConfigGen.Utilities.Tests.Xml
  28. {
  29. [TestFixture]
  30. public class XmlStreamFormatterTests
  31. {
  32. [Test]
  33. public void Format001_SingleChildElement()
  34. {
  35. const string xml = @"<root><child /></root>";
  36. string output = RunCopyTest(xml);
  37. Assert.AreEqual(xml, output);
  38. }
  39. [Test]
  40. public void Format002_TextContents()
  41. {
  42. const string xml = @"<root>SomeText</root>";
  43. string output = RunCopyTest(xml);
  44. Assert.AreEqual(xml, output);
  45. }
  46. [Test]
  47. public void Format003_Attributes()
  48. {
  49. const string xml = @"<root attribute1=""value1"" attribute2=""value2"" />";
  50. string output = RunCopyTest(xml);
  51. Assert.AreEqual(xml, output);
  52. }
  53. [Test]
  54. public void Format004_AttributesAndChildElement()
  55. {
  56. const string xml = @"<root attribute1=""value1"" attribute2=""value2""><child /></root>";
  57. string output = RunCopyTest(xml);
  58. Assert.AreEqual(xml, output);
  59. }
  60. [Test]
  61. public void Format006a_XmlDeclaration_WithEncoding()
  62. {
  63. string xml = @"<?xml version=""1.0"" encoding=""utf-8""?><root><child /></root>";
  64. string output = RunCopyTest(xml);
  65. Assert.AreEqual(xml, output);
  66. }
  67. [Test]
  68. public void Format006b_XmlDeclaration_Windows1252Encoding()
  69. {
  70. string xml = @"<?xml version=""1.0"" encoding=""Windows-1252""?><root><child /></root>";
  71. string output = RunCopyTest(xml);
  72. Assert.AreEqual(xml, output);
  73. }
  74. [Test]
  75. public void Format007_CDATA()
  76. {
  77. const string xml = @"<root><![CDATA[SomeText]]></root>";
  78. string output = RunCopyTest(xml);
  79. Assert.AreEqual(xml, output);
  80. }
  81. [Test]
  82. public void Format008_Comments()
  83. {
  84. const string xml = @"<root><!-- This is my comment --></root>";
  85. string output = RunCopyTest(xml);
  86. Assert.AreEqual(xml, output);
  87. }
  88. [Test]
  89. public void Format009_ProcessingInstruction()
  90. {
  91. const string xml = @"<root><?processing instruction?></root>";
  92. string output = RunCopyTest(xml);
  93. Assert.AreEqual(xml, output);
  94. }
  95. [Test]
  96. public void Format010_Whitespace_Ignored()
  97. {
  98. const string xml = @"<root> </root>";
  99. const string expected = @"<root />";
  100. string output = RunCopyTest(xml);
  101. Assert.AreEqual(expected, output);
  102. }
  103. [Test]
  104. public void Format011_Whitespace()
  105. {
  106. const string xml = @"<root> <child /> </root>";
  107. const string expected = @"<root><child /></root>";
  108. string output = RunCopyTest(xml);
  109. Assert.AreEqual(expected, output);
  110. }
  111. [Test]
  112. public void Format012_ElementWithNamespace()
  113. {
  114. const string xml = @"<root xmlns:ns=""http://test/""><ns:child /></root>";
  115. string output = RunCopyTest(xml);
  116. Console.WriteLine(output);
  117. Assert.AreEqual(xml, output);
  118. }
  119. [Test]
  120. public void Format013_AttributeWithNamespace()
  121. {
  122. const string xml = @"<root xmlns:ns=""http://test/""><child ns:attr1=""value1"" /></root>";
  123. string output = RunCopyTest(xml);
  124. Console.WriteLine(output);
  125. Assert.AreEqual(xml, output);
  126. }
  127. /// <summary>
  128. /// Regression test: escaped entities should be written out as-is, not "unescaped"
  129. /// Issue http://configgen.codeplex.com/workitem/9 - "PrettyPrint xml formatter is erroneously unescaping escaped entities."
  130. /// </summary>
  131. [Test]
  132. public void Format014_EntitiesInAttributesAreWrittenOutVerbatim()
  133. {
  134. const string xml = @"<root attribute1=""&lt;triangular braces&gt;"" />";
  135. string output = RunCopyTest(xml);
  136. Assert.AreEqual(xml, output);
  137. }
  138. /// <summary>
  139. /// Regression test: escaped entities should be written out as-is, not "unescaped"
  140. /// Issue http://configgen.codeplex.com/workitem/9 - "PrettyPrint xml formatter is erroneously unescaping escaped entities."
  141. /// </summary>
  142. [Test]
  143. public void Format015_EntitiesAsTextContentAreWrittenOutVerbatim()
  144. {
  145. const string xml = @"<root>&lt;triangular braces&gt;</root>";
  146. string output = RunCopyTest(xml);
  147. Assert.AreEqual(xml, output);
  148. }
  149. /// <summary>
  150. /// Regression test: escaped entities should be written out as-is, not "unescaped"
  151. /// </summary>
  152. [Test]
  153. public void WrapLongElementLines001_SingleAttribute_BelowWrapThreshold()
  154. {
  155. const string xml = @"<root attribute=""1"" />"; // length=19 to end of attribute
  156. var copierOptions = XmlStreamFormatterOptions.Default;
  157. copierOptions.WrapLongElementLines = true;
  158. copierOptions.MaxElementLineLength = 19;
  159. string output = RunCopyTest(xml, copierOptions);
  160. Console.WriteLine(output);
  161. Assert.AreEqual(xml, output);
  162. }
  163. [Test]
  164. public void WrapLongElementLines002_SingleAttribute_AboveWrapThreshold()
  165. {
  166. const string xml = @"<root attribute=""1"" />"; // length=19 to end of attribute
  167. const string expected =
  168. @"<root
  169. attribute=""1"" />";
  170. var copierOptions = XmlStreamFormatterOptions.Default;
  171. copierOptions.WrapLongElementLines = true;
  172. copierOptions.MaxElementLineLength = 18;
  173. string output = RunCopyTest(xml, copierOptions);
  174. Console.WriteLine(output);
  175. Assert.AreEqual(expected, output);
  176. }
  177. [Test]
  178. public void WrapLongElementLines003_MultipleAttributes_AboveWrapThreshold()
  179. {
  180. const string xml = @"<root a=""1"" b=""2"" longerattribute=""3"" d=""4"" e=""5""/>";
  181. const string expected =
  182. @"<root a=""1"" b=""2""
  183. longerattribute=""3""
  184. d=""4"" e=""5"" />";
  185. var copierOptions = XmlStreamFormatterOptions.Default;
  186. copierOptions.WrapLongElementLines = true;
  187. copierOptions.MaxElementLineLength = 18;
  188. string output = RunCopyTest(xml, copierOptions);
  189. Console.WriteLine(output);
  190. Assert.AreEqual(expected, output);
  191. }
  192. [Test]
  193. public void WrapLongElementLines004_WrapsLineLongLineOnChildElement()
  194. {
  195. const string xml = @"<root xmlns:ns=""http://test/""><child attr1=""value1"" attr2=""value2""/></root>";
  196. const string expected =
  197. @"<root xmlns:ns=""http://test/"">
  198. <child attr1=""value1""
  199. attr2=""value2"" />
  200. </root>";
  201. var copierOptions = XmlStreamFormatterOptions.Default;
  202. copierOptions.WrapLongElementLines = true;
  203. copierOptions.MaxElementLineLength = 30;
  204. string output = RunCopyTest(xml, copierOptions);
  205. Console.WriteLine(output);
  206. Assert.AreEqual(expected, output);
  207. }
  208. /// <summary>
  209. /// Regression test: initially the attribute "data" followed "authentication" without a space
  210. /// </summary>
  211. [Test]
  212. public void WrapLongElementLines005_Regression_SpaceMissingBetweenAttributes()
  213. {
  214. const string xml =
  215. @"<MyService endpoint=""http://somebigdomainname.com/SomeEndpoint"" authentication=""integrated"" data=""datavalue"" data2=""datavalue2"" />";
  216. const string expected =
  217. @"<MyService endpoint=""http://somebigdomainname.com/SomeEndpoint""
  218. authentication=""integrated"" data=""datavalue"" data2=""datavalue2"" />";
  219. var copierOptions = XmlStreamFormatterOptions.Default;
  220. copierOptions.WrapLongElementLines = true;
  221. copierOptions.MaxElementLineLength = 70;
  222. string output = RunCopyTest(xml, copierOptions);
  223. Console.WriteLine(output);
  224. Assert.AreEqual(expected, output);
  225. }
  226. /// <summary>
  227. /// Regression test: Element closing braces were mistakenly wrapping to newline
  228. /// </summary>
  229. [Test]
  230. public void WrapLongElementLines006_Regression_ClosingBracesOnNewline()
  231. {
  232. const string xml =
  233. @"<configuration xmlns:cg=""http://roblevine.co.uk/Namespaces/ConfigGen/1/0/"">
  234. <configSections>
  235. <section name=""MyCustomConfigSection"" type=""MyAssembly.ConfigSections.MyCustomConfigSection, MyAssembly"" />
  236. </configSections>
  237. <appSettings>
  238. <add key=""Environment"" value=""[%Environment%]"" />
  239. </appSettings>
  240. </configuration>";
  241. const string expected =
  242. @"<configuration
  243. xmlns:cg=""http://roblevine.co.uk/Namespaces/ConfigGen/1/0/"">
  244. <configSections>
  245. <section
  246. name=""MyCustomConfigSection""
  247. type=""MyAssembly.ConfigSections.MyCustomConfigSection, MyAssembly"" />
  248. </configSections>
  249. <appSettings>
  250. <add
  251. key=""Environment""
  252. value=""[%Environment%]"" />
  253. </appSettings>
  254. </configuration>";
  255. var copierOptions = XmlStreamFormatterOptions.Default;
  256. copierOptions.WrapLongElementLines = true;
  257. copierOptions.MaxElementLineLength = 18;
  258. string output = RunCopyTest(xml, copierOptions);
  259. Console.WriteLine(output);
  260. Assert.AreEqual(expected, output);
  261. }
  262. /// <summary>
  263. /// Regression test: The indentation was being corrupted as the reader part of the xml stream formatter was not
  264. /// ignoring insigificant whitespace, so this was being carried over to the output.
  265. /// </summary>
  266. [Test]
  267. public void WrapLongElementLines007_WrapIndentationIssue()
  268. {
  269. const string xml =
  270. @"<configuration>
  271. <system.web>
  272. <compilation debug=""true"">
  273. <assemblies><a>
  274. <add assembly=""System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"" />
  275. </a></assemblies>
  276. </compilation>
  277. </system.web>
  278. </configuration>";
  279. // note 3 char indentation of elements and assembly attribute, relative to parents.
  280. // This is because our IndentChars is three characters long
  281. const string expected =
  282. @"<configuration>
  283. <system.web>
  284. <compilation debug=""true"">
  285. <assemblies>
  286. <a>
  287. <add
  288. assembly=""System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"" />
  289. </a>
  290. </assemblies>
  291. </compilation>
  292. </system.web>
  293. </configuration>";
  294. var copierOptions = XmlStreamFormatterOptions.Default;
  295. copierOptions.WrapLongElementLines = true;
  296. copierOptions.MaxElementLineLength = 100;
  297. string output = RunCopyTest(xml, copierOptions);
  298. Console.WriteLine(output);
  299. Assert.AreEqual(expected, output);
  300. }
  301. public string RunCopyTest(string input)
  302. {
  303. var copierOptions = XmlStreamFormatterOptions.Default;
  304. copierOptions.Indent = false;
  305. return RunCopyTest(input, copierOptions);
  306. }
  307. public string RunCopyTest(string input, XmlStreamFormatterOptions formatterOptions)
  308. {
  309. var encoding = Encoding.UTF8;
  310. using (var readerStream = new MemoryStream(encoding.GetBytes(input).ToArray()))
  311. {
  312. using (var writerStream = new MemoryStream())
  313. {
  314. var copier = new XmlStreamFormatter();
  315. copier.Initialise(readerStream, writerStream);
  316. copier.FormatterOptions = formatterOptions;
  317. copier.Format();
  318. writerStream.Position = 0;
  319. var byteArray = writerStream.ToArray();
  320. var ret = encoding.GetString(byteArray);
  321. // TODO: RJL: this isn't right - this shouldn't be necessary. Is this a side effect of doing this via the encoding class?
  322. if (ret[0] != '<')
  323. {
  324. // remove BOM if present. RJL - could be a little more scientific here!
  325. ret = ret.Substring(1);
  326. }
  327. return ret;
  328. }
  329. }
  330. }
  331. }
  332. }