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