XmlSerializerTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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.IO;
  27. using System.Text;
  28. using System.Xml;
  29. using System.Xml.Schema;
  30. using System.Xml.Serialization;
  31. using NUnit.Framework;
  32. using MonoTests.System.Xml.TestClasses;
  33. namespace MonoTests.System.Xml
  34. {
  35. [TestFixture]
  36. public class XmlSerializerTests : Assertion
  37. {
  38. StringWriter sw;
  39. XmlTextWriter xtw;
  40. XmlSerializer xs;
  41. private void SetUpWriter()
  42. {
  43. sw = new StringWriter ();
  44. xtw = new XmlTextWriter (sw);
  45. xtw.QuoteChar = '\'';
  46. xtw.Formatting = Formatting.None;
  47. }
  48. private string WriterText
  49. {
  50. get
  51. {
  52. string val = sw.GetStringBuilder().ToString();
  53. int offset = val.IndexOf('>') + 1;
  54. val = val.Substring(offset);
  55. return val;
  56. }
  57. }
  58. private void Serialize(object o)
  59. {
  60. SetUpWriter();
  61. xs = new XmlSerializer(o.GetType());
  62. xs.Serialize(xtw, o);
  63. }
  64. private void Serialize(object o, Type type)
  65. {
  66. SetUpWriter();
  67. xs = new XmlSerializer(type);
  68. xs.Serialize(xtw, o);
  69. }
  70. private void Serialize(object o, XmlSerializerNamespaces ns)
  71. {
  72. SetUpWriter();
  73. xs = new XmlSerializer(o.GetType());
  74. xs.Serialize(xtw, o, ns);
  75. }
  76. private void Serialize(object o, XmlAttributeOverrides ao)
  77. {
  78. SetUpWriter();
  79. xs = new XmlSerializer(o.GetType(), ao);
  80. xs.Serialize(xtw, o);
  81. }
  82. private void Serialize(object o, XmlRootAttribute root)
  83. {
  84. SetUpWriter();
  85. xs = new XmlSerializer(o.GetType(), root);
  86. xs.Serialize(xtw, o);
  87. }
  88. // test constructors
  89. #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
  90. [Test]
  91. public void TestConstructor()
  92. {
  93. XmlSerializer ser = new XmlSerializer (null, "");
  94. }
  95. #else
  96. #endif
  97. // test basic types ////////////////////////////////////////////////////////
  98. [Test]
  99. public void TestSerializeInt()
  100. {
  101. Serialize(10);
  102. AssertEquals("<int>10</int>", WriterText);
  103. }
  104. [Test]
  105. public void TestSerializeBool()
  106. {
  107. Serialize(true);
  108. AssertEquals("<boolean>true</boolean>", WriterText);
  109. Serialize(false);
  110. AssertEquals("<boolean>false</boolean>", WriterText);
  111. }
  112. [Test]
  113. public void TestSerializeString()
  114. {
  115. Serialize("hello");
  116. AssertEquals("<string>hello</string>", WriterText);
  117. }
  118. [Test]
  119. public void TestSerializeEmptyString()
  120. {
  121. Serialize(String.Empty);
  122. AssertEquals("<string />", WriterText);
  123. }
  124. [Test]
  125. public void TestSerializeNullObject()
  126. {
  127. Serialize(null, typeof(object));
  128. AssertEquals("<anyType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />", WriterText);
  129. }
  130. [Test]
  131. public void TestSerializeNullString()
  132. {
  133. Serialize(null, typeof(string));
  134. AssertEquals ("<string xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />", WriterText);
  135. }
  136. [Test]
  137. public void TestSerializeIntArray()
  138. {
  139. Serialize(new int[] {1, 2, 3, 4});
  140. AssertEquals ("<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);
  141. }
  142. [Test]
  143. public void TestSerializeEmptyArray()
  144. {
  145. Serialize(new int[] {});
  146. AssertEquals("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  147. }
  148. [Test]
  149. public void TestSerializeChar()
  150. {
  151. Serialize('A');
  152. AssertEquals("<char>65</char>", WriterText);
  153. Serialize('\0');
  154. AssertEquals("<char>0</char>", WriterText);
  155. Serialize('\n');
  156. AssertEquals("<char>10</char>", WriterText);
  157. Serialize('\uFF01');
  158. AssertEquals("<char>65281</char>", WriterText);
  159. }
  160. [Test]
  161. public void TestSerializeFloat()
  162. {
  163. Serialize(10.78);
  164. AssertEquals("<double>10.78</double>", WriterText);
  165. Serialize(-1e8);
  166. AssertEquals("<double>-100000000</double>", WriterText);
  167. // FIXME test INF and other boundary conditions that may exist with floats
  168. }
  169. [Test]
  170. public void TestSerializeEnumeration()
  171. {
  172. Serialize(SimpleEnumeration.FIRST);
  173. AssertEquals("<SimpleEnumeration>FIRST</SimpleEnumeration>", WriterText);
  174. Serialize(SimpleEnumeration.SECOND);
  175. AssertEquals("<SimpleEnumeration>SECOND</SimpleEnumeration>", WriterText);
  176. }
  177. [Test]
  178. public void TestSerializeQualifiedName()
  179. {
  180. Serialize(new XmlQualifiedName("me", "home.urn"));
  181. AssertEquals("<QName xmlns:q1='home.urn'>q1:me</QName>", WriterText);
  182. }
  183. [Test]
  184. public void TestSerializeBytes()
  185. {
  186. Serialize((byte)0xAB);
  187. AssertEquals("<unsignedByte>171</unsignedByte>", WriterText);
  188. Serialize((byte)15);
  189. AssertEquals("<unsignedByte>15</unsignedByte>", WriterText);
  190. }
  191. [Test]
  192. public void TestSerializeByteArrays()
  193. {
  194. Serialize(new byte[] {});
  195. AssertEquals("<base64Binary />", WriterText);
  196. Serialize(new byte[] {0xAB, 0xCD});
  197. AssertEquals("<base64Binary>q80=</base64Binary>", WriterText);
  198. }
  199. [Test]
  200. public void TestSerializeDateTime()
  201. {
  202. DateTime d = new DateTime();
  203. Serialize(d);
  204. AssertEquals ("<dateTime>0001-01-01T00:00:00.0000000", WriterText.Substring (0, 37));
  205. }
  206. /*
  207. FIXME
  208. - decimal
  209. - Guid
  210. - XmlNode objects
  211. [Test]
  212. public void TestSerialize()
  213. {
  214. Serialize();
  215. AssertEquals(WriterText, "");
  216. }
  217. */
  218. // test basic class serialization /////////////////////////////////////
  219. [Test]
  220. public void TestSerializeSimpleClass()
  221. {
  222. SimpleClass simple = new SimpleClass();
  223. Serialize(simple);
  224. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  225. simple.something = "hello";
  226. Serialize(simple);
  227. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>", WriterText);
  228. }
  229. [Test]
  230. public void TestSerializeStringCollection()
  231. {
  232. StringCollection strings = new StringCollection();
  233. Serialize(strings);
  234. AssertEquals(WriterText, "<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />");
  235. strings.Add("hello");
  236. strings.Add("goodbye");
  237. Serialize(strings);
  238. Assert(WriterText.EndsWith("><string>hello</string><string>goodbye</string></ArrayOfString>"));
  239. }
  240. [Test]
  241. public void TestSerializePlainContainer()
  242. {
  243. StringCollectionContainer container = new StringCollectionContainer();
  244. Serialize(container);
  245. Assert(WriterText.EndsWith("><Messages /></StringCollectionContainer>"));
  246. container.Messages.Add("hello");
  247. container.Messages.Add("goodbye");
  248. Serialize(container);
  249. Assert(WriterText.EndsWith("><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"));
  250. }
  251. [Test]
  252. public void TestSerializeArrayContainer()
  253. {
  254. ArrayContainer container = new ArrayContainer();
  255. Serialize(container);
  256. AssertEquals(WriterText, "<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />");
  257. container.items = new object[] {10, 20};
  258. Serialize(container);
  259. Assert(WriterText.EndsWith("><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"));
  260. container.items = new object[] {10, "hello"};
  261. Serialize(container);
  262. Assert(WriterText.EndsWith("><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"));
  263. }
  264. [Test]
  265. public void TestSerializeClassArrayContainer()
  266. {
  267. ClassArrayContainer container = new ClassArrayContainer();
  268. Serialize(container);
  269. AssertEquals(WriterText, "<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />");
  270. SimpleClass simple1 = new SimpleClass();
  271. simple1.something = "hello";
  272. SimpleClass simple2 = new SimpleClass();
  273. simple2.something = "hello";
  274. container.items = new SimpleClass[2];
  275. container.items[0] = simple1;
  276. container.items[1] = simple2;
  277. Serialize(container);
  278. Assert(WriterText.EndsWith("><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"));
  279. }
  280. // test basic attributes ///////////////////////////////////////////////
  281. [Test]
  282. public void TestSerializeSimpleClassWithXmlAttributes()
  283. {
  284. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  285. Serialize(simple);
  286. AssertEquals("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  287. simple.something = "hello";
  288. Serialize(simple);
  289. AssertEquals ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />", WriterText);
  290. }
  291. // test overrides ///////////////////////////////////////////////////////
  292. [Test]
  293. public void TestSerializeSimpleClassWithOverrides()
  294. {
  295. // Also tests XmlIgnore
  296. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  297. XmlAttributes attr = new XmlAttributes();
  298. attr.XmlIgnore = true;
  299. overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);
  300. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  301. simple.something = "hello";
  302. Serialize(simple, overrides);
  303. AssertEquals("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  304. }
  305. // test xmlText //////////////////////////////////////////////////////////
  306. [Test]
  307. public void TestSerializeXmlTextAttribute()
  308. {
  309. SimpleClass simple = new SimpleClass();
  310. simple.something = "hello";
  311. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  312. XmlAttributes attr = new XmlAttributes();
  313. overrides.Add(typeof(SimpleClass), "something", attr);
  314. attr.XmlText = new XmlTextAttribute();
  315. Serialize(simple, overrides);
  316. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>", WriterText);
  317. attr.XmlText = new XmlTextAttribute(typeof(string));
  318. Serialize(simple, overrides);
  319. Assert(WriterText.EndsWith(">hello</SimpleClass>"));
  320. try
  321. {
  322. attr.XmlText = new XmlTextAttribute(typeof(byte[]));
  323. Serialize(simple, overrides);
  324. Fail("XmlText.Type does not match the type it serializes: this should have failed");
  325. }
  326. catch (Exception)
  327. {
  328. }
  329. try
  330. {
  331. attr.XmlText = new XmlTextAttribute();
  332. attr.XmlText.DataType = "sometype";
  333. Serialize(simple, overrides);
  334. Fail("XmlText.DataType does not match the type it serializes: this should have failed");
  335. }
  336. catch (Exception)
  337. {
  338. }
  339. }
  340. // test xmlRoot //////////////////////////////////////////////////////////
  341. [Test]
  342. public void TestSerializeXmlRootAttribute()
  343. {
  344. // constructor override & element name
  345. XmlRootAttribute root = new XmlRootAttribute();
  346. root.ElementName = "renamed";
  347. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
  348. Serialize(simpleWithAttributes, root);
  349. Assert(WriterText.StartsWith("<renamed "));
  350. SimpleClass simple = null;
  351. root.IsNullable = false;
  352. try
  353. {
  354. Serialize(simple, root);
  355. Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
  356. }
  357. catch (Exception)
  358. {
  359. }
  360. root.IsNullable = true;
  361. try
  362. {
  363. Serialize(simple, root);
  364. Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
  365. }
  366. catch (Exception)
  367. {
  368. }
  369. simple = new SimpleClass();
  370. root.ElementName = null;
  371. root.Namespace = "some.urn";
  372. Serialize(simple, root);
  373. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />", WriterText);
  374. }
  375. [Test]
  376. public void TestSerializeXmlRootAttributeOnMember()
  377. {
  378. // nested root
  379. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  380. XmlAttributes childAttr = new XmlAttributes();
  381. childAttr.XmlRoot = new XmlRootAttribute("simple");
  382. overrides.Add(typeof(SimpleClass), childAttr);
  383. XmlAttributes attr = new XmlAttributes();
  384. attr.XmlRoot = new XmlRootAttribute("simple");
  385. overrides.Add(typeof(ClassArrayContainer), attr);
  386. ClassArrayContainer container = new ClassArrayContainer();
  387. container.items = new SimpleClass[1];
  388. container.items[0] = new SimpleClass();;
  389. Serialize(container, overrides);
  390. Assert(WriterText.EndsWith("><items><SimpleClass /></items></simple>"));
  391. // FIXME test data type
  392. }
  393. // test XmlAttribute /////////////////////////////////////////////////////
  394. [Test]
  395. public void TestSerializeXmlAttributeAttribute()
  396. {
  397. // null
  398. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  399. XmlAttributes attr = new XmlAttributes();
  400. attr.XmlAttribute = new XmlAttributeAttribute();
  401. overrides.Add(typeof(SimpleClass), "something", attr);
  402. SimpleClass simple = new SimpleClass();;
  403. Serialize(simple, overrides);
  404. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  405. // regular
  406. simple.something = "hello";
  407. Serialize(simple, overrides);
  408. AssertEquals ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />", WriterText);
  409. // AttributeName
  410. attr.XmlAttribute.AttributeName = "somethingelse";
  411. Serialize(simple, overrides);
  412. AssertEquals ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />", WriterText);
  413. // Type
  414. // FIXME this should work, shouldnt it?
  415. // attr.XmlAttribute.Type = typeof(string);
  416. // Serialize(simple, overrides);
  417. // Assert(WriterText.EndsWith(" something='hello' />"));
  418. // Namespace
  419. attr.XmlAttribute.Namespace = "some:urn";
  420. Serialize(simple, overrides);
  421. Assert(WriterText.EndsWith(" d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"));
  422. // FIXME DataType
  423. // FIXME XmlSchemaForm Form
  424. // FIXME write XmlQualifiedName as attribute
  425. }
  426. // test XmlElement ///////////////////////////////////////////////////////
  427. [Test]
  428. public void TestSerializeXmlElementAttribute()
  429. {
  430. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  431. XmlAttributes attr = new XmlAttributes();
  432. XmlElementAttribute element = new XmlElementAttribute();
  433. attr.XmlElements.Add(element);
  434. overrides.Add(typeof(SimpleClass), "something", attr);
  435. // null
  436. SimpleClass simple = new SimpleClass();;
  437. Serialize(simple, overrides);
  438. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  439. // not null
  440. simple.something = "hello";
  441. Serialize(simple, overrides);
  442. AssertEquals ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>", WriterText);
  443. //ElementName
  444. element.ElementName = "saying";
  445. Serialize(simple, overrides);
  446. Assert(WriterText.EndsWith("><saying>hello</saying></SimpleClass>"));
  447. //IsNullable
  448. element.IsNullable = false;
  449. simple.something = null;
  450. Serialize(simple, overrides);
  451. AssertEquals(WriterText, "<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />");
  452. element.IsNullable = true;
  453. simple.something = null;
  454. Serialize(simple, overrides);
  455. Assert(WriterText.EndsWith("><saying xsi:nil='true' /></SimpleClass>"));
  456. //Namespace
  457. element.ElementName = null;
  458. element.IsNullable = false;
  459. element.Namespace = "some:urn";
  460. simple.something = "hello";
  461. Serialize(simple, overrides);
  462. Assert(WriterText.EndsWith("><something xmlns='some:urn'>hello</something></SimpleClass>"));
  463. //FIXME DataType
  464. //FIXME Form
  465. //FIXME Type
  466. }
  467. // test XmlElementAttribute with arrays and collections //////////////////
  468. [Test]
  469. public void TestSerializeCollectionWithXmlElementAttribute()
  470. {
  471. // the rule is:
  472. // if no type is specified or the specified type
  473. // matches the contents of the collection,
  474. // serialize each element in an element named after the member.
  475. // if the type does not match, or matches the collection itself,
  476. // create a base wrapping element for the member, and then
  477. // wrap each collection item in its own wrapping element based on type.
  478. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  479. XmlAttributes attr = new XmlAttributes();
  480. XmlElementAttribute element = new XmlElementAttribute();
  481. attr.XmlElements.Add(element);
  482. overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
  483. // empty collection & no type info in XmlElementAttribute
  484. StringCollectionContainer container = new StringCollectionContainer();
  485. Serialize(container, overrides);
  486. AssertEquals("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  487. // non-empty collection & no type info in XmlElementAttribute
  488. container.Messages.Add("hello");
  489. Serialize(container, overrides);
  490. AssertEquals ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>", WriterText);
  491. // non-empty collection & only type info in XmlElementAttribute
  492. element.Type = typeof(StringCollection);
  493. Serialize(container, overrides);
  494. AssertEquals ("<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);
  495. // non-empty collection & only type info in XmlElementAttribute
  496. element.Type = typeof(string);
  497. Serialize(container, overrides);
  498. Assert(WriterText.EndsWith("><Messages>hello</Messages></StringCollectionContainer>"));
  499. // two elements
  500. container.Messages.Add("goodbye");
  501. element.Type = null;
  502. Serialize(container, overrides);
  503. Assert(WriterText.EndsWith("><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"));
  504. }
  505. // test DefaultValue /////////////////////////////////////////////////////
  506. [Test]
  507. public void TestSerializeDefaultValueAttribute()
  508. {
  509. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  510. XmlAttributes attr = new XmlAttributes();
  511. string defaultValueInstance = "nothing";
  512. attr.XmlDefaultValue = defaultValueInstance;
  513. overrides.Add(typeof(SimpleClass), "something", attr);
  514. // use the default
  515. SimpleClass simple = new SimpleClass();
  516. Serialize(simple, overrides);
  517. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  518. // same value as default
  519. simple.something = defaultValueInstance;
  520. Serialize(simple, overrides);
  521. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />", WriterText);
  522. // some other value
  523. simple.something = "hello";
  524. Serialize(simple, overrides);
  525. AssertEquals("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>", WriterText);
  526. }
  527. // test XmlEnum //////////////////////////////////////////////////////////
  528. [Test]
  529. public void TestSerializeXmlEnumAttribute()
  530. {
  531. // technically this has an XmlIgnore attribute,
  532. // but it is not being serialized as a member.
  533. Serialize(XmlSchemaForm.None);
  534. AssertEquals(WriterText, "<XmlSchemaForm>0</XmlSchemaForm>");
  535. Serialize(XmlSchemaForm.Qualified);
  536. AssertEquals(WriterText, "<XmlSchemaForm>qualified</XmlSchemaForm>");
  537. Serialize(XmlSchemaForm.Unqualified);
  538. AssertEquals(WriterText, "<XmlSchemaForm>unqualified</XmlSchemaForm>");
  539. }
  540. }
  541. }