XmlAttributesTests.cs 3.0 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
  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. Assert.IsNull (atts.XmlAnyAttribute, "#1");
  60. Assert.IsNotNull (atts.XmlAnyElements, "#2");
  61. Assert.AreEqual (0, atts.XmlAnyElements.Count, "#3");
  62. Assert.IsNull (atts.XmlArray, "#4");
  63. Assert.IsNotNull (atts.XmlArrayItems, "#5");
  64. Assert.AreEqual (0, atts.XmlArrayItems.Count, "#6");
  65. Assert.IsNull (atts.XmlAttribute, "#7");
  66. Assert.IsNull (atts.XmlChoiceIdentifier, "#8");
  67. Assert.IsNotNull (atts.XmlDefaultValue, "#9");
  68. // DBNull??
  69. Assert.AreEqual (DBNull.Value, atts.XmlDefaultValue, "#10");
  70. Assert.IsNotNull (atts.XmlElements, "#11");
  71. Assert.AreEqual (0, atts.XmlElements.Count, "#12");
  72. Assert.IsNull (atts.XmlEnum, "#13");
  73. Assert.IsNotNull (atts.XmlIgnore, "#14");
  74. Assert.AreEqual (TypeCode.Boolean, atts.XmlIgnore.GetTypeCode (), "#15");
  75. Assert.AreEqual (false, atts.Xmlns, "#16");
  76. Assert.IsNull (atts.XmlRoot, "#17");
  77. Assert.IsNull (atts.XmlText, "#18");
  78. Assert.IsNull (atts.XmlType, "#19");
  79. }
  80. [Test]
  81. public void XmlTextAttribute ()
  82. {
  83. // based on default ctor.
  84. XmlTextAttribute attr = new XmlTextAttribute ();
  85. Assert.AreEqual ("", attr.DataType, "#1");
  86. Assert.IsNull (attr.Type, "#2");
  87. // based on a type.
  88. XmlTextAttribute attr2 = new XmlTextAttribute (typeof (XmlNode));
  89. Assert.AreEqual ("", attr.DataType, "#3");
  90. Assert.IsNull (attr.Type, "#4");
  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. Assert.Fail ("Should be invalid.");
  102. } catch (InvalidOperationException ex) {
  103. }
  104. }
  105. }
  106. }