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