XmlSerializerTests.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. //
  2. // System.Xml.XmlSerializerTests
  3. //
  4. // Author:
  5. // Erik LeBel <[email protected]>
  6. //
  7. // (C) 2003 Erik LeBel
  8. //
  9. //
  10. // NOTES:
  11. // Where possible, these tests avoid testing the order of
  12. // an object's members serialization. Mono and .NET do not
  13. // reflect members in the same order.
  14. //
  15. // Only serializations tests so far, no deserialization.
  16. //
  17. // FIXME
  18. // test XmlArrayAttribute
  19. // test XmlArrayItemAttribute
  20. // test serialization of decimal type
  21. // test serialization of Guid type
  22. // test XmlNode serialization with and without modifying attributes.
  23. // test deserialization
  24. // FIXMEs found in this file
  25. using System;
  26. using System.Collections;
  27. using System.Globalization;
  28. using System.IO;
  29. using System.Text;
  30. using System.Xml;
  31. using System.Xml.Schema;
  32. using System.Xml.Serialization;
  33. using NUnit.Framework;
  34. using MonoTests.System.Xml.TestClasses;
  35. namespace MonoTests.System.XmlSerialization
  36. {
  37. [TestFixture]
  38. public class XmlSerializerTests
  39. {
  40. StringWriter sw;
  41. XmlTextWriter xtw;
  42. XmlSerializer xs;
  43. private void SetUpWriter()
  44. {
  45. sw = new StringWriter ();
  46. xtw = new XmlTextWriter (sw);
  47. xtw.QuoteChar = '\'';
  48. xtw.Formatting = Formatting.None;
  49. }
  50. private string WriterText
  51. {
  52. get
  53. {
  54. string val = sw.GetStringBuilder().ToString();
  55. int offset = val.IndexOf('>') + 1;
  56. val = val.Substring(offset);
  57. return Infoset(val);
  58. }
  59. }
  60. private void Serialize(object o)
  61. {
  62. SetUpWriter();
  63. xs = new XmlSerializer(o.GetType());
  64. xs.Serialize(xtw, o);
  65. }
  66. private void Serialize(object o, Type type)
  67. {
  68. SetUpWriter();
  69. xs = new XmlSerializer(type);
  70. xs.Serialize(xtw, o);
  71. }
  72. private void Serialize(object o, XmlSerializerNamespaces ns)
  73. {
  74. SetUpWriter();
  75. xs = new XmlSerializer(o.GetType());
  76. xs.Serialize(xtw, o, ns);
  77. }
  78. private void Serialize(object o, XmlAttributeOverrides ao)
  79. {
  80. SetUpWriter();
  81. xs = new XmlSerializer(o.GetType(), ao);
  82. xs.Serialize(xtw, o);
  83. }
  84. private void Serialize(object o, XmlRootAttribute root)
  85. {
  86. SetUpWriter();
  87. xs = new XmlSerializer(o.GetType(), root);
  88. xs.Serialize(xtw, o);
  89. }
  90. // test constructors
  91. #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
  92. [Test]
  93. public void TestConstructor()
  94. {
  95. XmlSerializer ser = new XmlSerializer (null, "");
  96. }
  97. #else
  98. #endif
  99. // test basic types ////////////////////////////////////////////////////////
  100. [Test]
  101. public void TestSerializeInt()
  102. {
  103. Serialize(10);
  104. Assert.AreEqual (Infoset("<int>10</int>"), WriterText);
  105. }
  106. [Test]
  107. public void TestSerializeBool()
  108. {
  109. Serialize(true);
  110. Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
  111. Serialize(false);
  112. Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
  113. }
  114. [Test]
  115. public void TestSerializeString()
  116. {
  117. Serialize("hello");
  118. Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
  119. }
  120. [Test]
  121. public void TestSerializeEmptyString()
  122. {
  123. Serialize(String.Empty);
  124. Assert.AreEqual (Infoset ("<string />"), WriterText);
  125. }
  126. [Test]
  127. public void TestSerializeNullObject()
  128. {
  129. Serialize(null, typeof(object));
  130. Assert.AreEqual (Infoset ("<anyType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
  131. }
  132. [Test]
  133. [Ignore ("The generated XML is not exact but it is equivalent")]
  134. public void TestSerializeNullString()
  135. {
  136. Serialize(null, typeof(string));
  137. Assert.AreEqual (Infoset ("<string xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
  138. }
  139. [Test]
  140. public void TestSerializeIntArray()
  141. {
  142. Serialize(new int[] {1, 2, 3, 4});
  143. Assert.AreEqual (Infoset ("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>"), WriterText);
  144. }
  145. [Test]
  146. public void TestSerializeEmptyArray()
  147. {
  148. Serialize(new int[] {});
  149. Assert.AreEqual (Infoset ("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  150. }
  151. [Test]
  152. public void TestSerializeChar()
  153. {
  154. Serialize('A');
  155. Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
  156. Serialize('\0');
  157. Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
  158. Serialize('\n');
  159. Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
  160. Serialize('\uFF01');
  161. Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
  162. }
  163. [Test]
  164. public void TestSerializeFloat()
  165. {
  166. Serialize(10.78);
  167. Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
  168. Serialize(-1e8);
  169. Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
  170. // FIXME test INF and other boundary conditions that may exist with floats
  171. }
  172. [Test]
  173. public void TestSerializeEnumeration()
  174. {
  175. Serialize(SimpleEnumeration.FIRST);
  176. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
  177. Serialize(SimpleEnumeration.SECOND);
  178. Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText);
  179. }
  180. [Test]
  181. public void TestSerializeEnumDefaultValue() {
  182. Serialize(new EnumDefaultValue());
  183. Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText);
  184. Serialize(new EnumDefaultValueNF());
  185. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
  186. Serialize(new SimpleEnumeration());
  187. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
  188. }
  189. [Test]
  190. public void TestSerializeQualifiedName()
  191. {
  192. Serialize(new XmlQualifiedName("me", "home.urn"));
  193. Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
  194. }
  195. [Test]
  196. public void TestSerializeBytes()
  197. {
  198. Serialize((byte)0xAB);
  199. Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
  200. Serialize((byte)15);
  201. Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
  202. }
  203. [Test]
  204. public void TestSerializeByteArrays()
  205. {
  206. Serialize(new byte[] {});
  207. Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
  208. Serialize(new byte[] {0xAB, 0xCD});
  209. Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
  210. }
  211. [Test]
  212. public void TestSerializeDateTime()
  213. {
  214. DateTime d = new DateTime();
  215. Serialize(d);
  216. TimeZone tz = TimeZone.CurrentTimeZone;
  217. TimeSpan off = tz.GetUtcOffset (d);
  218. string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
  219. Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
  220. }
  221. /*
  222. FIXME
  223. - decimal
  224. - Guid
  225. - XmlNode objects
  226. [Test]
  227. public void TestSerialize()
  228. {
  229. Serialize();
  230. Assert.AreEqual (WriterText, "");
  231. }
  232. */
  233. // test basic class serialization /////////////////////////////////////
  234. [Test]
  235. public void TestSerializeSimpleClass()
  236. {
  237. SimpleClass simple = new SimpleClass();
  238. Serialize(simple);
  239. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  240. simple.something = "hello";
  241. Serialize(simple);
  242. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
  243. }
  244. [Test]
  245. public void TestSerializeStringCollection()
  246. {
  247. StringCollection strings = new StringCollection();
  248. Serialize(strings);
  249. Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  250. strings.Add("hello");
  251. strings.Add("goodbye");
  252. Serialize(strings);
  253. Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);
  254. }
  255. [Test]
  256. public void TestSerializePlainContainer()
  257. {
  258. StringCollectionContainer container = new StringCollectionContainer();
  259. Serialize(container);
  260. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
  261. container.Messages.Add("hello");
  262. container.Messages.Add("goodbye");
  263. Serialize(container);
  264. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);
  265. }
  266. [Test]
  267. public void TestSerializeArrayContainer()
  268. {
  269. ArrayContainer container = new ArrayContainer();
  270. Serialize(container);
  271. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  272. container.items = new object[] {10, 20};
  273. Serialize(container);
  274. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"), WriterText);
  275. container.items = new object[] {10, "hello"};
  276. Serialize(container);
  277. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"), WriterText);
  278. }
  279. [Test]
  280. public void TestSerializeClassArrayContainer()
  281. {
  282. ClassArrayContainer container = new ClassArrayContainer();
  283. Serialize(container);
  284. Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  285. SimpleClass simple1 = new SimpleClass();
  286. simple1.something = "hello";
  287. SimpleClass simple2 = new SimpleClass();
  288. simple2.something = "hello";
  289. container.items = new SimpleClass[2];
  290. container.items[0] = simple1;
  291. container.items[1] = simple2;
  292. Serialize(container);
  293. Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"), WriterText);
  294. }
  295. // test basic attributes ///////////////////////////////////////////////
  296. [Test]
  297. public void TestSerializeSimpleClassWithXmlAttributes()
  298. {
  299. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  300. Serialize(simple);
  301. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  302. simple.something = "hello";
  303. Serialize(simple);
  304. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
  305. }
  306. // test overrides ///////////////////////////////////////////////////////
  307. [Test]
  308. public void TestSerializeSimpleClassWithOverrides()
  309. {
  310. // Also tests XmlIgnore
  311. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  312. XmlAttributes attr = new XmlAttributes();
  313. attr.XmlIgnore = true;
  314. overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);
  315. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  316. simple.something = "hello";
  317. Serialize(simple, overrides);
  318. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  319. }
  320. [Test]
  321. public void TestSerializeSchema ()
  322. {
  323. XmlSchema schema = new XmlSchema ();
  324. schema.Items.Add (new XmlSchemaAttribute ());
  325. schema.Items.Add (new XmlSchemaAttributeGroup ());
  326. schema.Items.Add (new XmlSchemaComplexType ());
  327. schema.Items.Add (new XmlSchemaNotation ());
  328. schema.Items.Add (new XmlSchemaSimpleType ());
  329. schema.Items.Add (new XmlSchemaGroup ());
  330. schema.Items.Add (new XmlSchemaElement ());
  331. StringWriter sw = new StringWriter ();
  332. XmlTextWriter xtw = new XmlTextWriter (sw);
  333. xtw.QuoteChar = '\'';
  334. xtw.Formatting = Formatting.Indented;
  335. XmlSerializer xs = new XmlSerializer (schema.GetType ());
  336. xs.Serialize (xtw, schema);
  337. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  338. "<?xml version='1.0' encoding='utf-16'?>{0}" +
  339. "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
  340. " <xsd:attribute />{0}" +
  341. " <xsd:attributeGroup />{0}" +
  342. " <xsd:complexType />{0}" +
  343. " <xsd:notation />{0}" +
  344. " <xsd:simpleType />{0}" +
  345. " <xsd:group />{0}" +
  346. " <xsd:element />{0}" +
  347. "</xsd:schema>", Environment.NewLine), sw.ToString ());
  348. }
  349. // test xmlText //////////////////////////////////////////////////////////
  350. [Test]
  351. public void TestSerializeXmlTextAttribute()
  352. {
  353. SimpleClass simple = new SimpleClass();
  354. simple.something = "hello";
  355. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  356. XmlAttributes attr = new XmlAttributes();
  357. overrides.Add(typeof(SimpleClass), "something", attr);
  358. attr.XmlText = new XmlTextAttribute();
  359. Serialize(simple, overrides);
  360. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1");
  361. attr.XmlText = new XmlTextAttribute(typeof(string));
  362. Serialize(simple, overrides);
  363. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#2");
  364. try {
  365. attr.XmlText = new XmlTextAttribute(typeof(byte[]));
  366. Serialize(simple, overrides);
  367. Assert.Fail("XmlText.Type does not match the type it serializes: this should have failed");
  368. } catch (InvalidOperationException ex) {
  369. // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
  370. Assert.IsNotNull (ex.Message, "#A1");
  371. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A2");
  372. // there was an error reflecting field 'something'.
  373. Assert.IsNotNull (ex.InnerException, "#A3");
  374. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A4");
  375. Assert.IsNotNull (ex.InnerException.Message, "#A5");
  376. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A6");
  377. // the type for XmlText may not be specified for primitive types.
  378. Assert.IsNotNull (ex.InnerException.InnerException, "#A7");
  379. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A8");
  380. Assert.IsNotNull (ex.InnerException.Message, "#A9");
  381. Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A10");
  382. } catch (InvalidCastException ex) {
  383. // FIXME: we should report InvalidOperationException
  384. }
  385. try {
  386. attr.XmlText = new XmlTextAttribute();
  387. attr.XmlText.DataType = "sometype";
  388. Serialize(simple, overrides);
  389. Assert.Fail("XmlText.DataType does not match the type it serializes: this should have failed");
  390. } catch (InvalidOperationException ex) {
  391. // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
  392. Assert.IsNotNull (ex.Message, "#B1");
  393. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B2");
  394. // there was an error reflecting field 'something'.
  395. Assert.IsNotNull (ex.InnerException, "#B3");
  396. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B4");
  397. Assert.IsNotNull (ex.InnerException.Message, "#B5");
  398. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B6");
  399. // there was an error reflecting type 'System.String'.
  400. Assert.IsNotNull (ex.InnerException.InnerException, "#B7");
  401. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B8");
  402. Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B9");
  403. Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B10");
  404. // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
  405. // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
  406. Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B11");
  407. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B12");
  408. Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B13");
  409. Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B14");
  410. } catch (NotSupportedException ex) {
  411. // FIXME: we should report InvalidOperationException
  412. }
  413. }
  414. // test xmlRoot //////////////////////////////////////////////////////////
  415. [Test]
  416. public void TestSerializeXmlRootAttribute()
  417. {
  418. // constructor override & element name
  419. XmlRootAttribute root = new XmlRootAttribute();
  420. root.ElementName = "renamed";
  421. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
  422. Serialize(simpleWithAttributes, root);
  423. Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  424. SimpleClass simple = null;
  425. root.IsNullable = false;
  426. try {
  427. Serialize(simple, root);
  428. Assert.Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
  429. } catch (NullReferenceException) {
  430. }
  431. root.IsNullable = true;
  432. try {
  433. Serialize(simple, root);
  434. Assert.Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
  435. } catch (NullReferenceException) {
  436. }
  437. simple = new SimpleClass();
  438. root.ElementName = null;
  439. root.Namespace = "some.urn";
  440. Serialize(simple, root);
  441. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
  442. }
  443. [Test]
  444. public void TestSerializeXmlRootAttributeOnMember()
  445. {
  446. // nested root
  447. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  448. XmlAttributes childAttr = new XmlAttributes();
  449. childAttr.XmlRoot = new XmlRootAttribute("simple");
  450. overrides.Add(typeof(SimpleClass), childAttr);
  451. XmlAttributes attr = new XmlAttributes();
  452. attr.XmlRoot = new XmlRootAttribute("simple");
  453. overrides.Add(typeof(ClassArrayContainer), attr);
  454. ClassArrayContainer container = new ClassArrayContainer();
  455. container.items = new SimpleClass[1];
  456. container.items[0] = new SimpleClass();
  457. Serialize(container, overrides);
  458. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText);
  459. // FIXME test data type
  460. }
  461. // test XmlAttribute /////////////////////////////////////////////////////
  462. [Test]
  463. public void TestSerializeXmlAttributeAttribute()
  464. {
  465. // null
  466. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  467. XmlAttributes attr = new XmlAttributes();
  468. attr.XmlAttribute = new XmlAttributeAttribute();
  469. overrides.Add(typeof(SimpleClass), "something", attr);
  470. SimpleClass simple = new SimpleClass();;
  471. Serialize(simple, overrides);
  472. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  473. // regular
  474. simple.something = "hello";
  475. Serialize(simple, overrides);
  476. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText, "#2");
  477. // AttributeName
  478. attr.XmlAttribute.AttributeName = "somethingelse";
  479. Serialize(simple, overrides);
  480. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText, "#3");
  481. // Type
  482. // FIXME this should work, shouldnt it?
  483. // attr.XmlAttribute.Type = typeof(string);
  484. // Serialize(simple, overrides);
  485. // Assert(WriterText.EndsWith(" something='hello' />"));
  486. // Namespace
  487. attr.XmlAttribute.Namespace = "some:urn";
  488. Serialize(simple, overrides);
  489. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText, "#4");
  490. // FIXME DataType
  491. // FIXME XmlSchemaForm Form
  492. // FIXME write XmlQualifiedName as attribute
  493. }
  494. // test XmlElement ///////////////////////////////////////////////////////
  495. [Test]
  496. public void TestSerializeXmlElementAttribute()
  497. {
  498. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  499. XmlAttributes attr = new XmlAttributes();
  500. XmlElementAttribute element = new XmlElementAttribute();
  501. attr.XmlElements.Add(element);
  502. overrides.Add(typeof(SimpleClass), "something", attr);
  503. // null
  504. SimpleClass simple = new SimpleClass();;
  505. Serialize(simple, overrides);
  506. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  507. // not null
  508. simple.something = "hello";
  509. Serialize(simple, overrides);
  510. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#2");
  511. //ElementName
  512. element.ElementName = "saying";
  513. Serialize(simple, overrides);
  514. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText, "#3");
  515. //IsNullable
  516. element.IsNullable = false;
  517. simple.something = null;
  518. Serialize(simple, overrides);
  519. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
  520. element.IsNullable = true;
  521. simple.something = null;
  522. Serialize(simple, overrides);
  523. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText, "#5");
  524. //Namespace
  525. element.ElementName = null;
  526. element.IsNullable = false;
  527. element.Namespace = "some:urn";
  528. simple.something = "hello";
  529. Serialize(simple, overrides);
  530. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6");
  531. //FIXME DataType
  532. //FIXME Form
  533. //FIXME Type
  534. }
  535. // test XmlElementAttribute with arrays and collections //////////////////
  536. [Test]
  537. public void TestSerializeCollectionWithXmlElementAttribute()
  538. {
  539. // the rule is:
  540. // if no type is specified or the specified type
  541. // matches the contents of the collection,
  542. // serialize each element in an element named after the member.
  543. // if the type does not match, or matches the collection itself,
  544. // create a base wrapping element for the member, and then
  545. // wrap each collection item in its own wrapping element based on type.
  546. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  547. XmlAttributes attr = new XmlAttributes();
  548. XmlElementAttribute element = new XmlElementAttribute();
  549. attr.XmlElements.Add(element);
  550. overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
  551. // empty collection & no type info in XmlElementAttribute
  552. StringCollectionContainer container = new StringCollectionContainer();
  553. Serialize(container, overrides);
  554. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  555. // non-empty collection & no type info in XmlElementAttribute
  556. container.Messages.Add("hello");
  557. Serialize(container, overrides);
  558. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
  559. // non-empty collection & only type info in XmlElementAttribute
  560. element.Type = typeof(StringCollection);
  561. Serialize(container, overrides);
  562. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText);
  563. // non-empty collection & only type info in XmlElementAttribute
  564. element.Type = typeof(string);
  565. Serialize(container, overrides);
  566. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
  567. // two elements
  568. container.Messages.Add("goodbye");
  569. element.Type = null;
  570. Serialize(container, overrides);
  571. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText);
  572. }
  573. // test DefaultValue /////////////////////////////////////////////////////
  574. [Test]
  575. public void TestSerializeDefaultValueAttribute()
  576. {
  577. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  578. XmlAttributes attr = new XmlAttributes();
  579. string defaultValueInstance = "nothing";
  580. attr.XmlDefaultValue = defaultValueInstance;
  581. overrides.Add(typeof(SimpleClass), "something", attr);
  582. // use the default
  583. SimpleClass simple = new SimpleClass();
  584. Serialize(simple, overrides);
  585. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  586. // same value as default
  587. simple.something = defaultValueInstance;
  588. Serialize(simple, overrides);
  589. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  590. // some other value
  591. simple.something = "hello";
  592. Serialize(simple, overrides);
  593. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
  594. }
  595. // test XmlEnum //////////////////////////////////////////////////////////
  596. [Test]
  597. public void TestSerializeXmlEnumAttribute()
  598. {
  599. // technically this has an XmlIgnore attribute,
  600. // but it is not being serialized as a member.
  601. Serialize(XmlSchemaForm.None);
  602. Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
  603. Serialize(XmlSchemaForm.Qualified);
  604. Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText);
  605. Serialize(XmlSchemaForm.Unqualified);
  606. Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText);
  607. }
  608. [Test]
  609. public void TestSerializeXmlNodeArray ()
  610. {
  611. XmlDocument doc = new XmlDocument ();
  612. Serialize (new XmlNode [] { doc.CreateAttribute("at"), doc.CreateElement("elem1"), doc.CreateElement("elem2") }, typeof(object));
  613. Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  614. }
  615. [Test]
  616. public void TestSerializeXmlElement ()
  617. {
  618. XmlDocument doc = new XmlDocument ();
  619. Serialize (doc.CreateElement("elem"), typeof(XmlElement));
  620. Assert.AreEqual (Infoset ("<elem/>"), WriterText);
  621. }
  622. [Test]
  623. public void TestSerializeXmlElementSubclass ()
  624. {
  625. XmlDocument doc = new XmlDocument ();
  626. Serialize (new MyElem (doc), typeof(XmlElement));
  627. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText);
  628. Serialize (new MyElem (doc), typeof(MyElem));
  629. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText);
  630. }
  631. [Test]
  632. public void TestSerializeXmlCDataSection ()
  633. {
  634. XmlDocument doc = new XmlDocument ();
  635. CDataContainer c = new CDataContainer ();
  636. c.cdata = doc.CreateCDataSection("data section contents");
  637. Serialize (c);
  638. Assert.AreEqual (Infoset ("<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>"), WriterText);
  639. }
  640. [Test]
  641. public void TestSerializeXmlNode ()
  642. {
  643. XmlDocument doc = new XmlDocument ();
  644. NodeContainer c = new NodeContainer ();
  645. c.node = doc.CreateTextNode("text");
  646. Serialize (c);
  647. Assert.AreEqual (Infoset ("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
  648. }
  649. [Test]
  650. public void TestSerializeChoice ()
  651. {
  652. Choices ch = new Choices ();
  653. ch.MyChoice = "choice text";
  654. ch.ItemType = ItemChoiceType.ChoiceZero;
  655. Serialize (ch);
  656. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceZero>choice text</ChoiceZero></Choices>"), WriterText);
  657. ch.ItemType = ItemChoiceType.StrangeOne;
  658. Serialize (ch);
  659. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceOne>choice text</ChoiceOne></Choices>"), WriterText);
  660. ch.ItemType = ItemChoiceType.ChoiceTwo;
  661. Serialize (ch);
  662. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceTwo>choice text</ChoiceTwo></Choices>"), WriterText);
  663. }
  664. [Test]
  665. public void TestSerializeNamesWithSpaces ()
  666. {
  667. TestSpace ts = new TestSpace();
  668. ts.elem = 4;
  669. ts.attr = 5;
  670. Serialize (ts);
  671. Assert.AreEqual (Infoset ("<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>"), WriterText);
  672. }
  673. [Test]
  674. public void TestSerializeReadOnlyProps ()
  675. {
  676. ReadOnlyProperties ts = new ReadOnlyProperties();
  677. Serialize (ts);
  678. Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  679. }
  680. [Test]
  681. public void TestSerializeIList()
  682. {
  683. clsPerson k = new clsPerson();
  684. k.EmailAccounts = new ArrayList();
  685. k.EmailAccounts.Add("a");
  686. k.EmailAccounts.Add("b");
  687. Serialize (k);
  688. Assert.AreEqual (Infoset ("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText);
  689. }
  690. [Test]
  691. public void TestSerializeArrayEnc ()
  692. {
  693. SoapReflectionImporter imp = new SoapReflectionImporter ();
  694. XmlTypeMapping map = imp.ImportTypeMapping (typeof(ArrayClass));
  695. XmlSerializer ser = new XmlSerializer (map);
  696. StringWriter sw = new StringWriter ();
  697. XmlTextWriter tw = new XmlTextWriter (sw);
  698. tw.WriteStartElement ("aa");
  699. ser.Serialize (tw, new ArrayClass ());
  700. tw.WriteEndElement ();
  701. }
  702. [Test]
  703. public void TestIncludeType()
  704. {
  705. // Test for bug #76049
  706. XmlReflectionImporter imp = new XmlReflectionImporter ();
  707. XmlTypeMapping map = imp.ImportTypeMapping (typeof(object));
  708. imp.IncludeType (typeof(TestSpace));
  709. XmlSerializer ser = new XmlSerializer (map);
  710. ser.Serialize (new StringWriter (), new TestSpace ());
  711. }
  712. [Test]
  713. public void TestSerializeChoiceArray()
  714. {
  715. CompositeValueType v = new CompositeValueType ();
  716. v.Init ();
  717. Serialize (v);
  718. Assert.AreEqual (Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>"), WriterText);
  719. }
  720. [Test]
  721. public void TestArrayAttributeWithDataType ()
  722. {
  723. Serialize (new ArrayAttributeWithType ());
  724. string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
  725. res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
  726. Assert.AreEqual (Infoset (res), WriterText);
  727. }
  728. [Test]
  729. [ExpectedException (typeof(InvalidOperationException))]
  730. public void TestArrayAttributeWithWrongDataType ()
  731. {
  732. Serialize (new ArrayAttributeWithWrongType ());
  733. }
  734. // Helper methods
  735. public static string Infoset (string sx)
  736. {
  737. XmlDocument doc = new XmlDocument ();
  738. doc.LoadXml (sx);
  739. StringBuilder sb = new StringBuilder ();
  740. GetInfoset (doc.DocumentElement, sb);
  741. return sb.ToString ();
  742. }
  743. public static string Infoset (XmlNode nod)
  744. {
  745. StringBuilder sb = new StringBuilder ();
  746. GetInfoset (nod, sb);
  747. return sb.ToString ();
  748. }
  749. static void GetInfoset (XmlNode nod, StringBuilder sb)
  750. {
  751. switch (nod.NodeType)
  752. {
  753. case XmlNodeType.Attribute:
  754. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  755. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  756. break;
  757. case XmlNodeType.Element:
  758. XmlElement elem = (XmlElement) nod;
  759. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  760. ArrayList ats = new ArrayList ();
  761. foreach (XmlAttribute at in elem.Attributes)
  762. ats.Add (at.LocalName + " " + at.NamespaceURI);
  763. ats.Sort ();
  764. foreach (string name in ats)
  765. {
  766. string[] nn = name.Split (' ');
  767. GetInfoset (elem.Attributes[nn[0],nn[1]], sb);
  768. }
  769. sb.Append (">");
  770. foreach (XmlNode cn in elem.ChildNodes)
  771. GetInfoset (cn, sb);
  772. sb.Append ("</>");
  773. break;
  774. default:
  775. sb.Append (nod.OuterXml);
  776. break;
  777. }
  778. }
  779. }
  780. }