XmlSerializerTests.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 TestSerializeEnumDefaultValue() {
  182. Serialize(new EnumDefaultValue());
  183. AssertEquals(Infoset("<EnumDefaultValue />"), WriterText);
  184. Serialize(new EnumDefaultValueNF());
  185. AssertEquals(Infoset("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
  186. Serialize(new SimpleEnumeration());
  187. AssertEquals(Infoset("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
  188. }
  189. [Test]
  190. public void TestSerializeQualifiedName()
  191. {
  192. Serialize(new XmlQualifiedName("me", "home.urn"));
  193. AssertEquals(Infoset("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
  194. }
  195. [Test]
  196. public void TestSerializeBytes()
  197. {
  198. Serialize((byte)0xAB);
  199. AssertEquals(Infoset("<unsignedByte>171</unsignedByte>"), WriterText);
  200. Serialize((byte)15);
  201. AssertEquals(Infoset("<unsignedByte>15</unsignedByte>"), WriterText);
  202. }
  203. [Test]
  204. public void TestSerializeByteArrays()
  205. {
  206. Serialize(new byte[] {});
  207. AssertEquals(Infoset("<base64Binary />"), WriterText);
  208. Serialize(new byte[] {0xAB, 0xCD});
  209. AssertEquals(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. AssertEquals (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. AssertEquals(WriterText, "");
  231. }
  232. */
  233. // test basic class serialization /////////////////////////////////////
  234. [Test]
  235. public void TestSerializeSimpleClass()
  236. {
  237. SimpleClass simple = new SimpleClass();
  238. Serialize(simple);
  239. AssertEquals(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. 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);
  243. }
  244. [Test]
  245. public void TestSerializeStringCollection()
  246. {
  247. StringCollection strings = new StringCollection();
  248. Serialize(strings);
  249. AssertEquals(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. 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);
  254. }
  255. [Test]
  256. public void TestSerializePlainContainer()
  257. {
  258. StringCollectionContainer container = new StringCollectionContainer();
  259. Serialize(container);
  260. AssertEquals(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. 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);
  265. }
  266. [Test]
  267. public void TestSerializeArrayContainer()
  268. {
  269. ArrayContainer container = new ArrayContainer();
  270. Serialize(container);
  271. AssertEquals(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. 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);
  275. container.items = new object[] {10, "hello"};
  276. Serialize(container);
  277. 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);
  278. }
  279. [Test]
  280. public void TestSerializeClassArrayContainer()
  281. {
  282. ClassArrayContainer container = new ClassArrayContainer();
  283. Serialize(container);
  284. AssertEquals(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. 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);
  294. }
  295. // test basic attributes ///////////////////////////////////////////////
  296. [Test]
  297. public void TestSerializeSimpleClassWithXmlAttributes()
  298. {
  299. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  300. Serialize(simple);
  301. AssertEquals(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. AssertEquals (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. AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  319. }
  320. // test xmlText //////////////////////////////////////////////////////////
  321. [Test]
  322. public void TestSerializeXmlTextAttribute()
  323. {
  324. SimpleClass simple = new SimpleClass();
  325. simple.something = "hello";
  326. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  327. XmlAttributes attr = new XmlAttributes();
  328. overrides.Add(typeof(SimpleClass), "something", attr);
  329. attr.XmlText = new XmlTextAttribute();
  330. Serialize(simple, overrides);
  331. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
  332. attr.XmlText = new XmlTextAttribute(typeof(string));
  333. Serialize(simple, overrides);
  334. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
  335. try
  336. {
  337. attr.XmlText = new XmlTextAttribute(typeof(byte[]));
  338. Serialize(simple, overrides);
  339. Fail("XmlText.Type does not match the type it serializes: this should have failed");
  340. }
  341. catch (Exception)
  342. {
  343. }
  344. try
  345. {
  346. attr.XmlText = new XmlTextAttribute();
  347. attr.XmlText.DataType = "sometype";
  348. Serialize(simple, overrides);
  349. Fail("XmlText.DataType does not match the type it serializes: this should have failed");
  350. }
  351. catch (Exception)
  352. {
  353. }
  354. }
  355. // test xmlRoot //////////////////////////////////////////////////////////
  356. [Test]
  357. public void TestSerializeXmlRootAttribute()
  358. {
  359. // constructor override & element name
  360. XmlRootAttribute root = new XmlRootAttribute();
  361. root.ElementName = "renamed";
  362. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
  363. Serialize(simpleWithAttributes, root);
  364. AssertEquals(Infoset("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  365. SimpleClass simple = null;
  366. root.IsNullable = false;
  367. try
  368. {
  369. Serialize(simple, root);
  370. Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
  371. }
  372. catch (Exception)
  373. {
  374. }
  375. root.IsNullable = true;
  376. try
  377. {
  378. Serialize(simple, root);
  379. Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
  380. }
  381. catch (Exception)
  382. {
  383. }
  384. simple = new SimpleClass();
  385. root.ElementName = null;
  386. root.Namespace = "some.urn";
  387. Serialize(simple, root);
  388. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
  389. }
  390. [Test]
  391. public void TestSerializeXmlRootAttributeOnMember()
  392. {
  393. // nested root
  394. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  395. XmlAttributes childAttr = new XmlAttributes();
  396. childAttr.XmlRoot = new XmlRootAttribute("simple");
  397. overrides.Add(typeof(SimpleClass), childAttr);
  398. XmlAttributes attr = new XmlAttributes();
  399. attr.XmlRoot = new XmlRootAttribute("simple");
  400. overrides.Add(typeof(ClassArrayContainer), attr);
  401. ClassArrayContainer container = new ClassArrayContainer();
  402. container.items = new SimpleClass[1];
  403. container.items[0] = new SimpleClass();
  404. Serialize(container, overrides);
  405. 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);
  406. // FIXME test data type
  407. }
  408. // test XmlAttribute /////////////////////////////////////////////////////
  409. [Test]
  410. public void TestSerializeXmlAttributeAttribute()
  411. {
  412. // null
  413. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  414. XmlAttributes attr = new XmlAttributes();
  415. attr.XmlAttribute = new XmlAttributeAttribute();
  416. overrides.Add(typeof(SimpleClass), "something", attr);
  417. SimpleClass simple = new SimpleClass();;
  418. Serialize(simple, overrides);
  419. AssertEquals("#1", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  420. // regular
  421. simple.something = "hello";
  422. Serialize(simple, overrides);
  423. AssertEquals ("#2", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText);
  424. // AttributeName
  425. attr.XmlAttribute.AttributeName = "somethingelse";
  426. Serialize(simple, overrides);
  427. AssertEquals ("#3", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText);
  428. // Type
  429. // FIXME this should work, shouldnt it?
  430. // attr.XmlAttribute.Type = typeof(string);
  431. // Serialize(simple, overrides);
  432. // Assert(WriterText.EndsWith(" something='hello' />"));
  433. // Namespace
  434. attr.XmlAttribute.Namespace = "some:urn";
  435. Serialize(simple, overrides);
  436. 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);
  437. // FIXME DataType
  438. // FIXME XmlSchemaForm Form
  439. // FIXME write XmlQualifiedName as attribute
  440. }
  441. // test XmlElement ///////////////////////////////////////////////////////
  442. [Test]
  443. public void TestSerializeXmlElementAttribute()
  444. {
  445. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  446. XmlAttributes attr = new XmlAttributes();
  447. XmlElementAttribute element = new XmlElementAttribute();
  448. attr.XmlElements.Add(element);
  449. overrides.Add(typeof(SimpleClass), "something", attr);
  450. // null
  451. SimpleClass simple = new SimpleClass();;
  452. Serialize(simple, overrides);
  453. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  454. // not null
  455. simple.something = "hello";
  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'><something>hello</something></SimpleClass>"), WriterText);
  458. //ElementName
  459. element.ElementName = "saying";
  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>hello</saying></SimpleClass>"), WriterText);
  462. //IsNullable
  463. element.IsNullable = false;
  464. simple.something = null;
  465. Serialize(simple, overrides);
  466. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
  467. element.IsNullable = true;
  468. simple.something = null;
  469. Serialize(simple, overrides);
  470. 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);
  471. //Namespace
  472. element.ElementName = null;
  473. element.IsNullable = false;
  474. element.Namespace = "some:urn";
  475. simple.something = "hello";
  476. Serialize(simple, overrides);
  477. 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);
  478. //FIXME DataType
  479. //FIXME Form
  480. //FIXME Type
  481. }
  482. // test XmlElementAttribute with arrays and collections //////////////////
  483. [Test]
  484. public void TestSerializeCollectionWithXmlElementAttribute()
  485. {
  486. // the rule is:
  487. // if no type is specified or the specified type
  488. // matches the contents of the collection,
  489. // serialize each element in an element named after the member.
  490. // if the type does not match, or matches the collection itself,
  491. // create a base wrapping element for the member, and then
  492. // wrap each collection item in its own wrapping element based on type.
  493. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  494. XmlAttributes attr = new XmlAttributes();
  495. XmlElementAttribute element = new XmlElementAttribute();
  496. attr.XmlElements.Add(element);
  497. overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
  498. // empty collection & no type info in XmlElementAttribute
  499. StringCollectionContainer container = new StringCollectionContainer();
  500. Serialize(container, overrides);
  501. AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  502. // non-empty collection & no type info in XmlElementAttribute
  503. container.Messages.Add("hello");
  504. Serialize(container, overrides);
  505. 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);
  506. // non-empty collection & only type info in XmlElementAttribute
  507. element.Type = typeof(StringCollection);
  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><string>hello</string></Messages></StringCollectionContainer>"), WriterText);
  510. // non-empty collection & only type info in XmlElementAttribute
  511. element.Type = typeof(string);
  512. Serialize(container, overrides);
  513. 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);
  514. // two elements
  515. container.Messages.Add("goodbye");
  516. element.Type = null;
  517. Serialize(container, overrides);
  518. 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);
  519. }
  520. // test DefaultValue /////////////////////////////////////////////////////
  521. [Test]
  522. public void TestSerializeDefaultValueAttribute()
  523. {
  524. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  525. XmlAttributes attr = new XmlAttributes();
  526. string defaultValueInstance = "nothing";
  527. attr.XmlDefaultValue = defaultValueInstance;
  528. overrides.Add(typeof(SimpleClass), "something", attr);
  529. // use the default
  530. SimpleClass simple = new SimpleClass();
  531. Serialize(simple, overrides);
  532. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  533. // same value as default
  534. simple.something = defaultValueInstance;
  535. Serialize(simple, overrides);
  536. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  537. // some other value
  538. simple.something = "hello";
  539. Serialize(simple, overrides);
  540. 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);
  541. }
  542. // test XmlEnum //////////////////////////////////////////////////////////
  543. [Test]
  544. public void TestSerializeXmlEnumAttribute()
  545. {
  546. // technically this has an XmlIgnore attribute,
  547. // but it is not being serialized as a member.
  548. Serialize(XmlSchemaForm.None);
  549. AssertEquals(Infoset("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
  550. Serialize(XmlSchemaForm.Qualified);
  551. AssertEquals(Infoset("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText);
  552. Serialize(XmlSchemaForm.Unqualified);
  553. AssertEquals(Infoset("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText);
  554. }
  555. [Test]
  556. public void TestSerializeXmlNodeArray ()
  557. {
  558. XmlDocument doc = new XmlDocument ();
  559. Serialize (new XmlNode [] { doc.CreateAttribute("at"), doc.CreateElement("elem1"), doc.CreateElement("elem2") }, typeof(object));
  560. AssertEquals(Infoset("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  561. }
  562. [Test]
  563. public void TestSerializeXmlElement ()
  564. {
  565. XmlDocument doc = new XmlDocument ();
  566. Serialize (doc.CreateElement("elem"), typeof(XmlElement));
  567. AssertEquals(Infoset("<elem/>"), WriterText);
  568. }
  569. [Test]
  570. public void TestSerializeXmlElementSubclass ()
  571. {
  572. XmlDocument doc = new XmlDocument ();
  573. Serialize (new MyElem (doc), typeof(XmlElement));
  574. AssertEquals(Infoset("<myelem aa=\"1\"/>"), WriterText);
  575. Serialize (new MyElem (doc), typeof(MyElem));
  576. AssertEquals(Infoset("<myelem aa=\"1\"/>"), WriterText);
  577. }
  578. [Test]
  579. public void TestSerializeXmlCDataSection ()
  580. {
  581. XmlDocument doc = new XmlDocument ();
  582. CDataContainer c = new CDataContainer ();
  583. c.cdata = doc.CreateCDataSection("data section contents");
  584. Serialize (c);
  585. 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);
  586. }
  587. [Test]
  588. public void TestSerializeXmlNode ()
  589. {
  590. XmlDocument doc = new XmlDocument ();
  591. NodeContainer c = new NodeContainer ();
  592. c.node = doc.CreateTextNode("text");
  593. Serialize (c);
  594. 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);
  595. }
  596. [Test]
  597. public void TestSerializeChoice ()
  598. {
  599. Choices ch = new Choices ();
  600. ch.MyChoice = "choice text";
  601. ch.ItemType = ItemChoiceType.ChoiceZero;
  602. Serialize (ch);
  603. 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);
  604. ch.ItemType = ItemChoiceType.StrangeOne;
  605. Serialize (ch);
  606. 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);
  607. ch.ItemType = ItemChoiceType.ChoiceTwo;
  608. Serialize (ch);
  609. 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);
  610. }
  611. [Test]
  612. public void TestSerializeNamesWithSpaces ()
  613. {
  614. TestSpace ts = new TestSpace();
  615. ts.elem = 4;
  616. ts.attr = 5;
  617. Serialize (ts);
  618. 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);
  619. }
  620. [Test]
  621. public void TestSerializeReadOnlyProps ()
  622. {
  623. ReadOnlyProperties ts = new ReadOnlyProperties();
  624. Serialize (ts);
  625. AssertEquals(Infoset("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  626. }
  627. [Test]
  628. public void TestSerializeIList()
  629. {
  630. clsPerson k = new clsPerson();
  631. k.EmailAccounts = new ArrayList();
  632. k.EmailAccounts.Add("a");
  633. k.EmailAccounts.Add("b");
  634. Serialize (k);
  635. 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);
  636. }
  637. [Test]
  638. public void TestSerializeArrayEnc ()
  639. {
  640. SoapReflectionImporter imp = new SoapReflectionImporter ();
  641. XmlTypeMapping map = imp.ImportTypeMapping (typeof(ArrayClass));
  642. XmlSerializer ser = new XmlSerializer (map);
  643. StringWriter sw = new StringWriter ();
  644. XmlTextWriter tw = new XmlTextWriter (sw);
  645. tw.WriteStartElement ("aa");
  646. ser.Serialize (tw, new ArrayClass ());
  647. tw.WriteEndElement ();
  648. }
  649. [Test]
  650. public void TestIncludeType()
  651. {
  652. // Test for bug #76049
  653. XmlReflectionImporter imp = new XmlReflectionImporter ();
  654. XmlTypeMapping map = imp.ImportTypeMapping (typeof(object));
  655. imp.IncludeType (typeof(TestSpace));
  656. XmlSerializer ser = new XmlSerializer (map);
  657. ser.Serialize (new StringWriter (), new TestSpace ());
  658. }
  659. [Test]
  660. public void TestSerializeChoiceArray()
  661. {
  662. CompositeValueType v = new CompositeValueType ();
  663. v.Init ();
  664. Serialize (v);
  665. 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);
  666. }
  667. [Test]
  668. public void TestArrayAttributeWithDataType ()
  669. {
  670. Serialize (new ArrayAttributeWithType ());
  671. string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
  672. res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
  673. AssertEquals (Infoset(res), WriterText);
  674. }
  675. [Test]
  676. [ExpectedException (typeof(InvalidOperationException))]
  677. public void TestArrayAttributeWithWrongDataType ()
  678. {
  679. Serialize (new ArrayAttributeWithWrongType ());
  680. }
  681. // Helper methods
  682. public static string Infoset (string sx)
  683. {
  684. XmlDocument doc = new XmlDocument ();
  685. doc.LoadXml (sx);
  686. StringBuilder sb = new StringBuilder ();
  687. GetInfoset (doc.DocumentElement, sb);
  688. return sb.ToString ();
  689. }
  690. public static string Infoset (XmlNode nod)
  691. {
  692. StringBuilder sb = new StringBuilder ();
  693. GetInfoset (nod, sb);
  694. return sb.ToString ();
  695. }
  696. static void GetInfoset (XmlNode nod, StringBuilder sb)
  697. {
  698. switch (nod.NodeType)
  699. {
  700. case XmlNodeType.Attribute:
  701. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  702. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  703. break;
  704. case XmlNodeType.Element:
  705. XmlElement elem = (XmlElement) nod;
  706. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  707. ArrayList ats = new ArrayList ();
  708. foreach (XmlAttribute at in elem.Attributes)
  709. ats.Add (at.LocalName + " " + at.NamespaceURI);
  710. ats.Sort ();
  711. foreach (string name in ats)
  712. {
  713. string[] nn = name.Split (' ');
  714. GetInfoset (elem.Attributes[nn[0],nn[1]], sb);
  715. }
  716. sb.Append (">");
  717. foreach (XmlNode cn in elem.ChildNodes)
  718. GetInfoset (cn, sb);
  719. sb.Append ("</>");
  720. break;
  721. default:
  722. sb.Append (nod.OuterXml);
  723. break;
  724. }
  725. }
  726. }
  727. }