XmlSerializerTests.cs 30 KB

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