XmlAttributesTests.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.Serialization;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.XmlSerialization
  17. {
  18. [TestFixture]
  19. public class XmlAttributesTests : Assertion
  20. {
  21. StringWriter sw;
  22. XmlTextWriter xtw;
  23. XmlSerializer xs;
  24. private void SetUpWriter ()
  25. {
  26. sw = new StringWriter ();
  27. xtw = new XmlTextWriter (sw);
  28. xtw.QuoteChar = '\'';
  29. xtw.Formatting = Formatting.None;
  30. }
  31. private string WriterText
  32. {
  33. get
  34. {
  35. string val = sw.GetStringBuilder ().ToString();
  36. int offset = val.IndexOf ('>') + 1;
  37. val = val.Substring (offset);
  38. return val;
  39. }
  40. }
  41. private void Serialize (object o, XmlAttributeOverrides ao)
  42. {
  43. SetUpWriter ();
  44. xs = new XmlSerializer (o.GetType (), ao);
  45. xs.Serialize (xtw, o);
  46. }
  47. private void Serialize (object o, XmlRootAttribute root)
  48. {
  49. SetUpWriter ();
  50. xs = new XmlSerializer (o.GetType(), root);
  51. xs.Serialize (xtw, o);
  52. }
  53. // Testcases.
  54. [Test]
  55. public void NewXmlAttributes ()
  56. {
  57. // seems not different from Type specified ctor().
  58. XmlAttributes atts = new XmlAttributes ();
  59. AssertNull (atts.XmlAnyAttribute);
  60. AssertNotNull (atts.XmlAnyElements);
  61. AssertEquals (0, atts.XmlAnyElements.Count);
  62. AssertNull (atts.XmlArray);
  63. AssertNotNull (atts.XmlArrayItems);
  64. AssertEquals (0, atts.XmlArrayItems.Count);
  65. AssertNull (atts.XmlAttribute);
  66. AssertNull (atts.XmlChoiceIdentifier);
  67. AssertNotNull (atts.XmlDefaultValue);
  68. // DBNull??
  69. AssertEquals (DBNull.Value, atts.XmlDefaultValue);
  70. AssertNotNull (atts.XmlElements);
  71. AssertEquals (0, atts.XmlElements.Count);
  72. AssertNull (atts.XmlEnum);
  73. AssertNotNull (atts.XmlIgnore);
  74. AssertEquals (TypeCode.Boolean, atts.XmlIgnore.GetTypeCode ());
  75. AssertEquals (false, atts.Xmlns);
  76. AssertNull (atts.XmlRoot);
  77. AssertNull (atts.XmlText);
  78. AssertNull (atts.XmlType);
  79. }
  80. [Test]
  81. public void XmlTextAttribute ()
  82. {
  83. // based on default ctor.
  84. XmlTextAttribute attr = new XmlTextAttribute ();
  85. AssertEquals ("", attr.DataType);
  86. AssertNull (attr.Type);
  87. // based on a type.
  88. XmlTextAttribute attr2 = new XmlTextAttribute (typeof (XmlNode));
  89. AssertEquals ("", attr.DataType);
  90. AssertNull (attr.Type);
  91. }
  92. [Test]
  93. public void XmlInvalidElementAttribute ()
  94. {
  95. XmlAttributeOverrides ao = new XmlAttributeOverrides ();
  96. XmlAttributes atts = new XmlAttributes ();
  97. atts.XmlElements.Add (new XmlElementAttribute ("xInt"));
  98. ao.Add (typeof (int), atts);
  99. try {
  100. Serialize (10, ao);
  101. Fail ("Should be invalid.");
  102. } catch (InvalidOperationException ex) {
  103. }
  104. }
  105. }
  106. }