XmlAttributesTests.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // System.Xml.XmlAttributesTests
  3. //
  4. // Author:
  5. // Atsushi Enomoto
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. using System.Reflection;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using System.Xml.Serialization;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.XmlSerialization
  18. {
  19. [TestFixture]
  20. public class XmlAttributesTests
  21. {
  22. StringWriter sw;
  23. XmlTextWriter xtw;
  24. XmlSerializer xs;
  25. private void SetUpWriter ()
  26. {
  27. sw = new StringWriter ();
  28. xtw = new XmlTextWriter (sw);
  29. xtw.QuoteChar = '\'';
  30. xtw.Formatting = Formatting.None;
  31. }
  32. private string WriterText
  33. {
  34. get
  35. {
  36. string val = sw.GetStringBuilder ().ToString();
  37. int offset = val.IndexOf ('>') + 1;
  38. val = val.Substring (offset);
  39. return val;
  40. }
  41. }
  42. private void Serialize (object o, XmlAttributeOverrides ao)
  43. {
  44. SetUpWriter ();
  45. xs = new XmlSerializer (o.GetType (), ao);
  46. xs.Serialize (xtw, o);
  47. }
  48. private void Serialize (object o, XmlRootAttribute root)
  49. {
  50. SetUpWriter ();
  51. xs = new XmlSerializer (o.GetType(), root);
  52. xs.Serialize (xtw, o);
  53. }
  54. // Testcases.
  55. [Test]
  56. public void NewXmlAttributes ()
  57. {
  58. // seems not different from Type specified ctor().
  59. XmlAttributes atts = new XmlAttributes ();
  60. Assert.IsNull (atts.XmlAnyAttribute, "#1");
  61. Assert.IsNotNull (atts.XmlAnyElements, "#2");
  62. Assert.AreEqual (0, atts.XmlAnyElements.Count, "#3");
  63. Assert.IsNull (atts.XmlArray, "#4");
  64. Assert.IsNotNull (atts.XmlArrayItems, "#5");
  65. Assert.AreEqual (0, atts.XmlArrayItems.Count, "#6");
  66. Assert.IsNull (atts.XmlAttribute, "#7");
  67. Assert.IsNull (atts.XmlChoiceIdentifier, "#8");
  68. Assert.IsNull (atts.XmlDefaultValue, "#9");
  69. Assert.IsNotNull (atts.XmlElements, "#11");
  70. Assert.AreEqual (0, atts.XmlElements.Count, "#12");
  71. Assert.IsNull (atts.XmlEnum, "#13");
  72. Assert.IsNotNull (atts.XmlIgnore, "#14");
  73. Assert.AreEqual (TypeCode.Boolean, atts.XmlIgnore.GetTypeCode (), "#15");
  74. Assert.AreEqual (false, atts.Xmlns, "#16");
  75. Assert.IsNull (atts.XmlRoot, "#17");
  76. Assert.IsNull (atts.XmlText, "#18");
  77. Assert.IsNull (atts.XmlType, "#19");
  78. }
  79. [Test]
  80. public void XmlTextAttribute ()
  81. {
  82. // based on default ctor.
  83. XmlTextAttribute attr = new XmlTextAttribute ();
  84. Assert.AreEqual ("", attr.DataType, "#1");
  85. Assert.IsNull (attr.Type, "#2");
  86. // based on a type.
  87. XmlTextAttribute attr2 = new XmlTextAttribute (typeof (XmlNode));
  88. Assert.AreEqual ("", attr.DataType, "#3");
  89. Assert.IsNull (attr.Type, "#4");
  90. }
  91. [Test]
  92. public void XmlInvalidElementAttribute ()
  93. {
  94. XmlAttributeOverrides ao = new XmlAttributeOverrides ();
  95. XmlAttributes atts = new XmlAttributes ();
  96. atts.XmlElements.Add (new XmlElementAttribute ("xInt"));
  97. ao.Add (typeof (int), atts);
  98. try {
  99. Serialize (10, ao);
  100. Assert.Fail ("Should be invalid.");
  101. } catch (InvalidOperationException ex) {
  102. }
  103. }
  104. [Test]
  105. public void XmlIgnore ()
  106. {
  107. FieldInfo field = GetType ().GetField ("XmlIgnoreField");
  108. XmlAttributes atts = new XmlAttributes (field);
  109. Assert.AreEqual (true, atts.XmlIgnore, "#1");
  110. Assert.AreEqual (0, atts.XmlElements.Count, "#2");
  111. Assert.AreEqual (0, atts.XmlAnyElements.Count, "#3");
  112. }
  113. [XmlIgnore]
  114. [XmlElement (IsNullable = true)]
  115. [XmlAnyElement]
  116. public int XmlIgnoreField;
  117. }
  118. }