PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/BurnSystems/tests/BurnSystems.UnitTests/XmlHelperTests.cs

https://bitbucket.org/mbrenn/entityconnector
C# | 162 lines | 135 code | 24 blank | 3 comment | 0 complexity | 0ff25237919db08c3216fdbfab618ea1 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 BurnSystems;
  7. using System.Xml.Linq;
  8. namespace BurnSystems.UnitTests
  9. {
  10. /// <summary>
  11. /// Contains some test checking the XmlHelper-class
  12. /// </summary>
  13. [TestFixture]
  14. public class XmlHelperTests
  15. {
  16. [Test]
  17. public void GetExistingElementViaGetOrCreate()
  18. {
  19. var xDocument = new XDocument(
  20. new XElement(
  21. "tests",
  22. new XElement(
  23. "existing",
  24. new XAttribute(
  25. "Wert",
  26. "12"))));
  27. var xmlTests = xDocument.Element("tests");
  28. var xmlExisting = xmlTests.Element("existing");
  29. var xmlExistingCheck = xDocument.Elements("tests").GetOrCreateLastElement("existing");
  30. Assert.That(xmlExistingCheck, Is.SameAs(xmlExisting));
  31. }
  32. [Test]
  33. public void GetExistingAttributeViaGetOrCreate()
  34. {
  35. var xDocument = new XDocument(
  36. new XElement(
  37. "tests",
  38. new XElement(
  39. "existing",
  40. new XAttribute(
  41. "Wert",
  42. "12"))));
  43. var xmlTests = xDocument.Element("tests");
  44. var xmlExisting = xmlTests.Element("existing");
  45. var xmlAttribute = xmlExisting.Attribute("Wert");
  46. var xmlAttributeCheck = xDocument.Elements("tests").Elements("existing").GetOrCreateLastAttribute("Wert");
  47. Assert.That(xmlAttributeCheck, Is.SameAs(xmlAttribute));
  48. }
  49. [Test]
  50. public void CreateAttributeViaGetOrCreate()
  51. {
  52. var xDocument = new XDocument(
  53. new XElement(
  54. "tests",
  55. new XElement(
  56. "existing",
  57. new XAttribute(
  58. "Wert",
  59. "12"))));
  60. var xmlTests = xDocument.Element("tests");
  61. var xmlExisting = xmlTests.Element("existing");
  62. var xmlAttribute = xmlExisting.Attribute("Wert");
  63. var xmlAttributeCheck = xDocument.Elements("tests").Elements("existing").GetOrCreateLastAttribute("Value", "test");
  64. Assert.That(xmlAttributeCheck, Is.Not.Null);
  65. Assert.That(xmlAttributeCheck, Is.Not.SameAs(xmlAttribute));
  66. var xmlAttributeCorrect = xmlExisting.Attribute("Value");
  67. Assert.That(xmlAttributeCorrect, Is.SameAs(xmlAttributeCheck));
  68. Assert.That(xmlAttributeCorrect.Value, Is.EqualTo("test"));
  69. Assert.That(xmlAttributeCheck.Value, Is.EqualTo("test"));
  70. }
  71. [Test]
  72. public void CreateElementViaGetOrCreate()
  73. {
  74. var xDocument = new XDocument(
  75. new XElement(
  76. "tests",
  77. new XElement(
  78. "existing",
  79. new XAttribute(
  80. "Wert",
  81. "12"))));
  82. var xmlTests = xDocument.Element("tests");
  83. var xmlExisting = xmlTests.Element("existing");
  84. var xmlElementCheck = xDocument.Elements("tests").GetOrCreateLastElement("notexisting");
  85. Assert.That(xmlElementCheck, Is.Not.Null);
  86. Assert.That(xmlElementCheck, Is.Not.SameAs(xmlExisting));
  87. var xmlElementCorrect = xmlTests.Element("notexisting");
  88. Assert.That(xmlElementCorrect, Is.SameAs(xmlElementCheck));
  89. }
  90. [Test]
  91. public void GetLastElementViaGetOrCreate()
  92. {
  93. var xDocument = new XDocument(
  94. new XElement(
  95. "tests",
  96. new XElement(
  97. "existing",
  98. new XAttribute(
  99. "Wert",
  100. "12")),
  101. new XElement(
  102. "existing",
  103. new XAttribute(
  104. "Wert",
  105. "25"))));
  106. var xmlTests = xDocument.Element("tests");
  107. var xmlExisting = xmlTests.Elements("existing").Last();
  108. var xmlElementCheck = xDocument.Elements("tests").GetOrCreateLastElement("existing");
  109. Assert.That(xmlElementCheck, Is.Not.Null);
  110. Assert.That(xmlElementCheck, Is.SameAs(xmlExisting));
  111. Assert.That(xmlElementCheck.Attribute("Wert").Value, Is.EqualTo("25"));
  112. }
  113. [Test]
  114. public void GetLastAttributeViaGetOrCreate()
  115. {
  116. var xDocument = new XDocument(
  117. new XElement(
  118. "tests",
  119. new XElement(
  120. "existing",
  121. new XAttribute(
  122. "Wert",
  123. "12")),
  124. new XElement(
  125. "existing",
  126. new XAttribute(
  127. "Wert",
  128. "25"))));
  129. var xmlTests = xDocument.Element("tests");
  130. var xmlExisting = xmlTests.Elements("existing").Last();
  131. var xmlAttribute = xmlExisting.Attribute("Wert");
  132. var xmlAttributeCheck = xDocument.Elements("tests").Elements("existing").GetOrCreateLastAttribute("Wert");
  133. Assert.That(xmlAttributeCheck, Is.Not.Null);
  134. Assert.That(xmlAttributeCheck, Is.SameAs(xmlAttribute));
  135. Assert.That(xmlAttributeCheck.Value, Is.EqualTo("25"));
  136. }
  137. }
  138. }