PageRenderTime 530ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/Core.UnitTests/AttributeOverrideTests.cs

#
C# | 283 lines | 195 code | 83 blank | 5 comment | 0 complexity | 98f2d38a08804f7ee83b4659dfe5046f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. using EnvRide.Core.ObjectModel;
  7. using System.IO;
  8. using System.Xml;
  9. using CodeMadeSimple.Common.Tests;
  10. using CodeMadeSimple.PropertiesManagement;
  11. using CodeMadeSimple.Common.Validation;
  12. using CodeMadeSimple.Common.Tests.Validation;
  13. using log4net.Appender;
  14. using log4net.Core;
  15. namespace EnvRide.Core.UnitTests
  16. {
  17. [TestFixture]
  18. public class AttributeOverrideTests : OverrideTests
  19. {
  20. private const string FILE_NAME = "AttributeOverrideTests.xml";
  21. [Test]
  22. public void Process()
  23. {
  24. AttributeOverride attOver = new AttributeOverride();
  25. Process(attOver);
  26. }
  27. [Test]
  28. public void AppendAttribute()
  29. {
  30. EnvRide.Core.ObjectModel.Environment env = Helper.CreateDummyEnvironment();
  31. AttributeOverride attOver = new AttributeOverride();
  32. attOver.Environment = env;
  33. attOver.OverrideMode = AttributeOverrideMode.AppendAttribute;
  34. attOver.Xpath = "//*";
  35. attOver.AttributeName = "attributeToAppend";
  36. XmlDocument doc = new XmlDocument();
  37. EnvRideProcessArguments args = new EnvRideProcessArguments { Document = doc };
  38. Helper.AddPropertyContext(args);
  39. doc.Load(FILE_NAME);
  40. attOver.Process(args);
  41. AssertHasNoErrors(args);
  42. Assert.IsTrue(doc.DocumentElement.HasAttribute("attributeToAppend"));
  43. Assert.AreEqual(String.Empty, doc.DocumentElement.GetAttribute("attributeToAppend"));
  44. Assert.IsTrue(((XmlElement)doc.DocumentElement.ChildNodes[0]).HasAttribute("attributeToAppend"));
  45. Assert.AreEqual(String.Empty, ((XmlElement)doc.DocumentElement.ChildNodes[0]).GetAttribute("attributeToAppend"));
  46. attOver.NewValue = "attributeValue";
  47. doc.Load(FILE_NAME);
  48. attOver.Process(args);
  49. AssertHasNoErrors(args);
  50. Assert.IsTrue(doc.DocumentElement.HasAttribute("attributeToAppend"));
  51. Assert.AreEqual(attOver.NewValue, doc.DocumentElement.GetAttribute("attributeToAppend"));
  52. Assert.IsTrue(((XmlElement)doc.DocumentElement.ChildNodes[0]).HasAttribute("attributeToAppend"));
  53. Assert.AreEqual(attOver.NewValue, ((XmlElement)doc.DocumentElement.ChildNodes[0]).GetAttribute("attributeToAppend"));
  54. attOver.NewValue = "${" + Helper.PROP_NAME + "}";
  55. doc.Load(FILE_NAME);
  56. attOver.Process(args);
  57. AssertHasNoErrors(args);
  58. Assert.IsTrue(doc.DocumentElement.HasAttribute("attributeToAppend"));
  59. Assert.AreEqual(Helper.PROP_VALUE, doc.DocumentElement.GetAttribute("attributeToAppend"));
  60. Assert.IsTrue(((XmlElement)doc.DocumentElement.ChildNodes[0]).HasAttribute("attributeToAppend"));
  61. Assert.AreEqual(Helper.PROP_VALUE, ((XmlElement)doc.DocumentElement.ChildNodes[0]).GetAttribute("attributeToAppend"));
  62. attOver.If = "false";
  63. doc.Load(FILE_NAME);
  64. attOver.Process(args);
  65. AssertHasNoErrors(args);
  66. Assert.IsFalse(doc.DocumentElement.HasAttribute("attributeToAppend"));
  67. Assert.IsFalse(((XmlElement)doc.DocumentElement.ChildNodes[0]).HasAttribute("attributeToAppend"));
  68. }
  69. [Test]
  70. public void RemoveAttribute()
  71. {
  72. EnvRide.Core.ObjectModel.Environment env = Helper.CreateDummyEnvironment();
  73. AttributeOverride attOver = new AttributeOverride();
  74. attOver.Environment = env;
  75. attOver.Xpath = "//*";
  76. attOver.AttributeName = "attToRemove";
  77. attOver.OverrideMode = AttributeOverrideMode.RemoveAttribute;
  78. XmlDocument doc = new XmlDocument();
  79. EnvRideProcessArguments args = new EnvRideProcessArguments { Document = doc };
  80. Helper.AddPropertyContext(args);
  81. doc.Load(FILE_NAME);
  82. Assert.IsTrue(doc.DocumentElement.HasAttribute("attToRemove"));
  83. Assert.IsTrue(((XmlElement)doc.DocumentElement.ChildNodes[0]).HasAttribute("attToRemove"));
  84. attOver.Process(args);
  85. Assert.IsFalse(doc.DocumentElement.HasAttribute("attToRemove"));
  86. Assert.IsFalse(((XmlElement)doc.DocumentElement.ChildNodes[0]).HasAttribute("attToRemove"));
  87. doc.Load(FILE_NAME);
  88. attOver.AttributeName = "invalidAttToRemove";
  89. attOver.Process(args);
  90. AssertHasErrors(args, new[] { "The attribute \"invalidAttToRemove\" was not found at element \"/root\"!", "The attribute \"invalidAttToRemove\" was not found at element \"/root/child[0]\"!" });
  91. attOver.If = "false";
  92. attOver.Process(args);
  93. Assert.IsTrue(doc.DocumentElement.HasAttribute("attToRemove"));
  94. Assert.IsTrue(((XmlElement)doc.DocumentElement.ChildNodes[0]).HasAttribute("attToRemove"));
  95. }
  96. [Test]
  97. public void ReplaceValue()
  98. {
  99. EnvRide.Core.ObjectModel.Environment env = Helper.CreateDummyEnvironment();
  100. AttributeOverride attOver = new AttributeOverride();
  101. attOver.Environment = env;
  102. attOver.Xpath = "//*";
  103. attOver.AttributeName = "attToReplace";
  104. attOver.NewValue = "replacedValue";
  105. attOver.OverrideMode = AttributeOverrideMode.ReplaceValue;
  106. XmlDocument doc = new XmlDocument();
  107. EnvRideProcessArguments args = new EnvRideProcessArguments { Document = doc };
  108. Helper.AddPropertyContext(args);
  109. doc.Load(FILE_NAME);
  110. Assert.AreEqual("originalValue", doc.DocumentElement.GetAttribute("attToReplace"));
  111. Assert.AreEqual("originalValue", ((XmlElement)doc.DocumentElement.ChildNodes[0]).GetAttribute("attToReplace"));
  112. attOver.Process(args);
  113. AssertHasNoErrors(args);
  114. Assert.AreEqual(attOver.NewValue, doc.DocumentElement.GetAttribute("attToReplace"));
  115. Assert.AreEqual(attOver.NewValue, ((XmlElement)doc.DocumentElement.ChildNodes[0]).GetAttribute("attToReplace"));
  116. doc.Load(FILE_NAME);
  117. attOver.NewValue = "${" + Helper.PROP_NAME + "}";
  118. attOver.Process(args);
  119. AssertHasNoErrors(args);
  120. Assert.AreEqual(Helper.PROP_VALUE, doc.DocumentElement.GetAttribute("attToReplace"));
  121. Assert.AreEqual(Helper.PROP_VALUE, ((XmlElement)doc.DocumentElement.ChildNodes[0]).GetAttribute("attToReplace"));
  122. doc.Load(FILE_NAME);
  123. attOver.AttributeName = "invalidAttToReplace";
  124. attOver.Process(args);
  125. AssertHasErrors(args, new[] { "The attribute \"invalidAttToReplace\" was not found at element \"/root\"!", "The attribute \"invalidAttToReplace\" was not found at element \"/root/child[0]\"!" });
  126. attOver.If = "false";
  127. attOver.Process(args);
  128. Assert.AreEqual("originalValue", doc.DocumentElement.GetAttribute("attToReplace"));
  129. Assert.AreEqual("originalValue", ((XmlElement)doc.DocumentElement.ChildNodes[0]).GetAttribute("attToReplace"));
  130. }
  131. [Test]
  132. public void Serialization()
  133. {
  134. AttributeOverride attOver = new AttributeOverride();
  135. attOver.AttributeName = "AttributeName";
  136. attOver.If = "If";
  137. attOver.Name = "Name";
  138. attOver.NewValue = "NewValue";
  139. attOver.OverrideMode = AttributeOverrideMode.AppendAttribute;
  140. attOver.Xpath = "XPath";
  141. SerializeDeserializeAndCompare(attOver);
  142. attOver.AttributeName = "AttributeName2";
  143. attOver.If = "If2";
  144. attOver.Name = "Name2";
  145. attOver.NewValue = "NewValue2";
  146. attOver.OverrideMode = AttributeOverrideMode.RemoveAttribute;
  147. attOver.Xpath = "XPath2";
  148. SerializeDeserializeAndCompare(attOver);
  149. }
  150. private static void SerializeDeserializeAndCompare(AttributeOverride attOver)
  151. {
  152. AttributeOverride attOver2 = null;
  153. using (MemoryStream ms = new MemoryStream())
  154. {
  155. using (XmlWriter xmlW = XmlWriter.Create(ms))
  156. {
  157. xmlW.WriteStartElement("AttributeOverride");
  158. attOver.WriteXml(xmlW);
  159. xmlW.WriteEndElement();
  160. }
  161. ms.Position = 0;
  162. using (XmlReader xmlR = XmlReader.Create(ms))
  163. {
  164. xmlR.Read();
  165. xmlR.Read();
  166. attOver2 = new AttributeOverride();
  167. attOver2.ReadXml(xmlR);
  168. }
  169. }
  170. AssertEquals(attOver, attOver2);
  171. }
  172. [Test]
  173. public void Validate()
  174. {
  175. AttributeOverride attOver = new AttributeOverride();
  176. ValidationResult result = attOver.Validate();
  177. ValidationResultTests.AssertValidationResult(result, attOver, false, new IValidationMessage[] { new RequiredValueValidationMessage("Xpath"), new RequiredValueValidationMessage("AttributeName") });
  178. attOver.Xpath = "Xpath";
  179. result = attOver.Validate();
  180. ValidationResultTests.AssertValidationResult(result, attOver, false, new IValidationMessage[] { new RequiredValueValidationMessage("AttributeName") });
  181. attOver.AttributeName = "attName";
  182. result = attOver.Validate();
  183. ValidationResultTests.AssertValidationResult(result, attOver, false, false);
  184. }
  185. /// <summary>
  186. /// Asserts two AttributeOverride instances that should be equal.
  187. /// </summary>
  188. /// <param name="attOver1">AttributeOverride instance one.</param>
  189. /// <param name="attOver2">AttributeOverride instance two.</param>
  190. public static void AssertEquals(AttributeOverride attOver1, AttributeOverride attOver2)
  191. {
  192. Assert.AreEqual(attOver1.AttributeName, attOver2.AttributeName);
  193. Assert.AreEqual(attOver1.If, attOver2.If);
  194. Assert.AreEqual(attOver1.Name, attOver2.Name);
  195. Assert.AreEqual(attOver1.NewValue, attOver2.NewValue);
  196. Assert.AreEqual(attOver1.OverrideMode, attOver2.OverrideMode);
  197. Assert.AreEqual(attOver1.Xpath, attOver2.Xpath);
  198. }
  199. }
  200. }