XmlSerializerTests.cs 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. //
  2. // System.Xml.XmlSerializerTests
  3. //
  4. // Author:
  5. // Erik LeBel <[email protected]>
  6. //
  7. // (C) 2003 Erik LeBel
  8. //
  9. //
  10. // NOTES:
  11. // Where possible, these tests avoid testing the order of
  12. // an object's members serialization. Mono and .NET do not
  13. // reflect members in the same order.
  14. //
  15. // Only serializations tests so far, no deserialization.
  16. //
  17. // FIXME
  18. // test XmlArrayAttribute
  19. // test XmlArrayItemAttribute
  20. // test serialization of decimal type
  21. // test serialization of Guid type
  22. // test XmlNode serialization with and without modifying attributes.
  23. // test deserialization
  24. // FIXMEs found in this file
  25. using System;
  26. using System.Collections;
  27. using System.Globalization;
  28. using System.IO;
  29. using System.Text;
  30. using System.Xml;
  31. using System.Xml.Schema;
  32. using System.Xml.Serialization;
  33. using NUnit.Framework;
  34. using MonoTests.System.Xml.TestClasses;
  35. namespace MonoTests.System.XmlSerialization
  36. {
  37. [TestFixture]
  38. public class XmlSerializerTests
  39. {
  40. const string XmlSchemaNamespace = "http://www.w3.org/2001/XMLSchema";
  41. const string XmlSchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
  42. const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  43. const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
  44. const string ANamespace = "some:urn";
  45. const string AnotherNamespace = "another:urn";
  46. StringWriter sw;
  47. XmlTextWriter xtw;
  48. XmlSerializer xs;
  49. private void SetUpWriter()
  50. {
  51. sw = new StringWriter ();
  52. xtw = new XmlTextWriter (sw);
  53. xtw.QuoteChar = '\'';
  54. xtw.Formatting = Formatting.None;
  55. }
  56. private string WriterText
  57. {
  58. get
  59. {
  60. string val = sw.GetStringBuilder().ToString();
  61. int offset = val.IndexOf('>') + 1;
  62. val = val.Substring(offset);
  63. return Infoset(val);
  64. }
  65. }
  66. private void Serialize(object o)
  67. {
  68. SetUpWriter();
  69. xs = new XmlSerializer(o.GetType());
  70. xs.Serialize(xtw, o);
  71. }
  72. private void Serialize(object o, Type type)
  73. {
  74. SetUpWriter();
  75. xs = new XmlSerializer(type);
  76. xs.Serialize(xtw, o);
  77. }
  78. private void Serialize(object o, XmlSerializerNamespaces ns)
  79. {
  80. SetUpWriter();
  81. xs = new XmlSerializer(o.GetType());
  82. xs.Serialize(xtw, o, ns);
  83. }
  84. private void Serialize(object o, XmlAttributeOverrides ao)
  85. {
  86. SetUpWriter();
  87. xs = new XmlSerializer(o.GetType(), ao);
  88. xs.Serialize(xtw, o);
  89. }
  90. private void Serialize(object o, XmlRootAttribute root)
  91. {
  92. SetUpWriter();
  93. xs = new XmlSerializer(o.GetType(), root);
  94. xs.Serialize(xtw, o);
  95. }
  96. private void Serialize (object o, XmlTypeMapping typeMapping)
  97. {
  98. SetUpWriter ();
  99. xs = new XmlSerializer (typeMapping);
  100. xs.Serialize (xtw, o);
  101. }
  102. private void SerializeEncoded (object o)
  103. {
  104. SerializeEncoded (o, o.GetType ());
  105. }
  106. private void SerializeEncoded (object o, Type type)
  107. {
  108. XmlTypeMapping mapping = CreateSoapMapping (type);
  109. SetUpWriter ();
  110. xs = new XmlSerializer (mapping);
  111. xs.Serialize (xtw, o);
  112. }
  113. // test constructors
  114. #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
  115. [Test]
  116. public void TestConstructor()
  117. {
  118. XmlSerializer ser = new XmlSerializer (null, "");
  119. }
  120. #else
  121. #endif
  122. // test basic types ////////////////////////////////////////////////////////
  123. [Test]
  124. public void TestSerializeInt()
  125. {
  126. Serialize(10);
  127. Assert.AreEqual (Infoset("<int>10</int>"), WriterText);
  128. }
  129. [Test]
  130. public void TestSerializeBool()
  131. {
  132. Serialize(true);
  133. Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
  134. Serialize(false);
  135. Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
  136. }
  137. [Test]
  138. public void TestSerializeString()
  139. {
  140. Serialize("hello");
  141. Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
  142. }
  143. [Test]
  144. public void TestSerializeEmptyString()
  145. {
  146. Serialize(String.Empty);
  147. Assert.AreEqual (Infoset ("<string />"), WriterText);
  148. }
  149. [Test]
  150. public void TestSerializeNullObject()
  151. {
  152. Serialize(null, typeof(object));
  153. Assert.AreEqual (Infoset ("<anyType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
  154. }
  155. [Test]
  156. [Ignore ("The generated XML is not exact but it is equivalent")]
  157. public void TestSerializeNullString()
  158. {
  159. Serialize(null, typeof(string));
  160. Assert.AreEqual (Infoset ("<string xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
  161. }
  162. [Test]
  163. public void TestSerializeIntArray()
  164. {
  165. Serialize(new int[] {1, 2, 3, 4});
  166. Assert.AreEqual (Infoset ("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>"), WriterText);
  167. }
  168. [Test]
  169. public void TestSerializeEmptyArray()
  170. {
  171. Serialize(new int[] {});
  172. Assert.AreEqual (Infoset ("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  173. }
  174. [Test]
  175. public void TestSerializeChar()
  176. {
  177. Serialize('A');
  178. Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
  179. Serialize('\0');
  180. Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
  181. Serialize('\n');
  182. Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
  183. Serialize('\uFF01');
  184. Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
  185. }
  186. [Test]
  187. public void TestSerializeFloat()
  188. {
  189. Serialize(10.78);
  190. Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
  191. Serialize(-1e8);
  192. Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
  193. // FIXME test INF and other boundary conditions that may exist with floats
  194. }
  195. [Test]
  196. [Category ("NotDotNet")] // bug #77501, will fail on .NET until we output name of constant
  197. public void TestSerializeEnumeration_FromValue ()
  198. {
  199. Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  200. Assert.AreEqual (
  201. "<?xml version='1.0' encoding='utf-16'?>" +
  202. "<SimpleEnumeration>1</SimpleEnumeration>",
  203. sw.ToString ());
  204. }
  205. [Test]
  206. [Category ("NotWorking")]
  207. public void TestSerializeEnumeration_FromValue_Encoded ()
  208. {
  209. SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  210. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  211. "<?xml version='1.0' encoding='utf-16'?>" +
  212. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  213. XmlSchemaInstanceNamespace), sw.ToString ());
  214. }
  215. [Test]
  216. public void TestSerializeEnumeration()
  217. {
  218. Serialize(SimpleEnumeration.FIRST);
  219. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
  220. Serialize(SimpleEnumeration.SECOND);
  221. Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
  222. }
  223. [Test]
  224. public void TestSerializeEnumeration_Encoded()
  225. {
  226. SerializeEncoded (SimpleEnumeration.FIRST);
  227. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  228. "<?xml version='1.0' encoding='utf-16'?>" +
  229. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  230. XmlSchemaInstanceNamespace), sw.ToString (), "#B1");
  231. SerializeEncoded (SimpleEnumeration.SECOND);
  232. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  233. "<?xml version='1.0' encoding='utf-16'?>" +
  234. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  235. XmlSchemaInstanceNamespace), sw.ToString (), "#B2");
  236. }
  237. [Test]
  238. [Category ("NotWorking")]
  239. public void TestSerializeEnumDefaultValue ()
  240. {
  241. Serialize (new EnumDefaultValue ());
  242. Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
  243. Serialize (new SimpleEnumeration ());
  244. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
  245. Serialize (3, typeof (EnumDefaultValue));
  246. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
  247. Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  248. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
  249. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  250. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
  251. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  252. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
  253. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  254. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
  255. Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  256. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
  257. Serialize (3, typeof (FlagEnum));
  258. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
  259. Serialize (5, typeof (FlagEnum));
  260. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
  261. Serialize (FlagEnum.e4, typeof (FlagEnum));
  262. Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
  263. Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  264. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
  265. Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  266. Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
  267. Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  268. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
  269. Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  270. Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
  271. Serialize (3, typeof (EnumDefaultValueNF));
  272. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
  273. Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  274. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
  275. Serialize (2, typeof (ZeroFlagEnum));
  276. Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t&lt;w&gt;o</ZeroFlagEnum>"), WriterText, "#18");
  277. Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
  278. Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
  279. }
  280. [Test]
  281. [Category ("NotWorking")]
  282. public void TestSerializeEnumDefaultValue_Encoded ()
  283. {
  284. SerializeEncoded (new EnumDefaultValue ());
  285. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  286. "<?xml version='1.0' encoding='utf-16'?>" +
  287. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
  288. XmlSchemaInstanceNamespace), sw.ToString (), "#1");
  289. SerializeEncoded (new SimpleEnumeration ());
  290. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  291. "<?xml version='1.0' encoding='utf-16'?>" +
  292. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  293. XmlSchemaInstanceNamespace), sw.ToString (), "#2");
  294. SerializeEncoded (3, typeof (EnumDefaultValue));
  295. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  296. "<?xml version='1.0' encoding='utf-16'?>" +
  297. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  298. XmlSchemaInstanceNamespace), sw.ToString (), "#3");
  299. SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  300. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  301. "<?xml version='1.0' encoding='utf-16'?>" +
  302. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  303. XmlSchemaInstanceNamespace), sw.ToString (), "#4");
  304. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  305. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  306. "<?xml version='1.0' encoding='utf-16'?>" +
  307. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  308. XmlSchemaInstanceNamespace), sw.ToString (), "#5");
  309. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  310. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  311. "<?xml version='1.0' encoding='utf-16'?>" +
  312. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  313. XmlSchemaInstanceNamespace), sw.ToString (), "#6");
  314. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  315. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  316. "<?xml version='1.0' encoding='utf-16'?>" +
  317. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  318. XmlSchemaInstanceNamespace), sw.ToString (), "#7");
  319. SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  320. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  321. "<?xml version='1.0' encoding='utf-16'?>" +
  322. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  323. XmlSchemaInstanceNamespace), sw.ToString (), "#8");
  324. SerializeEncoded (3, typeof (FlagEnum));
  325. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  326. "<?xml version='1.0' encoding='utf-16'?>" +
  327. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
  328. XmlSchemaInstanceNamespace), sw.ToString (), "#9");
  329. SerializeEncoded (5, typeof (FlagEnum));
  330. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  331. "<?xml version='1.0' encoding='utf-16'?>" +
  332. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
  333. XmlSchemaInstanceNamespace), sw.ToString (), "#10");
  334. SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
  335. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  336. "<?xml version='1.0' encoding='utf-16'?>" +
  337. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
  338. XmlSchemaInstanceNamespace), sw.ToString (), "#11");
  339. SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  340. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  341. "<?xml version='1.0' encoding='utf-16'?>" +
  342. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
  343. XmlSchemaInstanceNamespace), sw.ToString (), "#12");
  344. SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  345. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  346. "<?xml version='1.0' encoding='utf-16'?>" +
  347. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
  348. XmlSchemaInstanceNamespace), sw.ToString (), "#13");
  349. SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  350. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  351. "<?xml version='1.0' encoding='utf-16'?>" +
  352. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
  353. XmlSchemaInstanceNamespace), sw.ToString (), "#14");
  354. SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  355. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  356. "<?xml version='1.0' encoding='utf-16'?>" +
  357. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
  358. XmlSchemaInstanceNamespace), sw.ToString (), "#15");
  359. SerializeEncoded (3, typeof (EnumDefaultValueNF));
  360. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  361. "<?xml version='1.0' encoding='utf-16'?>" +
  362. "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
  363. XmlSchemaInstanceNamespace), sw.ToString (), "#16");
  364. SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  365. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  366. "<?xml version='1.0' encoding='utf-16'?>" +
  367. "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
  368. XmlSchemaInstanceNamespace), sw.ToString (), "#17");
  369. SerializeEncoded (2, typeof (ZeroFlagEnum));
  370. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  371. "<?xml version='1.0' encoding='utf-16'?>" +
  372. "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
  373. XmlSchemaInstanceNamespace), sw.ToString (), "#18");
  374. SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
  375. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  376. "<?xml version='1.0' encoding='utf-16'?>" +
  377. "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
  378. XmlSchemaInstanceNamespace), sw.ToString (), "#19");
  379. }
  380. [Test]
  381. [Category ("NotWorking")]
  382. public void TestSerializeEnumDefaultValue_InvalidValue1 ()
  383. {
  384. try {
  385. Serialize ("b", typeof (EnumDefaultValue));
  386. Assert.Fail ("#A1");
  387. } catch (InvalidOperationException ex) {
  388. Assert.IsNotNull (ex.InnerException, "#A2");
  389. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
  390. }
  391. try {
  392. Serialize ("e1", typeof (EnumDefaultValue));
  393. Assert.Fail ("#B1");
  394. } catch (InvalidOperationException ex) {
  395. Assert.IsNotNull (ex.InnerException, "#B2");
  396. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
  397. }
  398. try {
  399. Serialize ("e1,e2", typeof (EnumDefaultValue));
  400. Assert.Fail ("#C1");
  401. } catch (InvalidOperationException ex) {
  402. Assert.IsNotNull (ex.InnerException, "#C2");
  403. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
  404. }
  405. try {
  406. Serialize (string.Empty, typeof (EnumDefaultValue));
  407. Assert.Fail ("#D1");
  408. } catch (InvalidOperationException ex) {
  409. Assert.IsNotNull (ex.InnerException, "#D2");
  410. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
  411. }
  412. try {
  413. Serialize ("1", typeof (EnumDefaultValue));
  414. Assert.Fail ("#E1");
  415. } catch (InvalidOperationException ex) {
  416. Assert.IsNotNull (ex.InnerException, "#E2");
  417. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
  418. }
  419. try {
  420. Serialize ("0", typeof (EnumDefaultValue));
  421. Assert.Fail ("#F1");
  422. } catch (InvalidOperationException ex) {
  423. Assert.IsNotNull (ex.InnerException, "#F2");
  424. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
  425. }
  426. try {
  427. Serialize (new SimpleClass (), typeof (EnumDefaultValue));
  428. Assert.Fail ("#G1");
  429. } catch (InvalidOperationException ex) {
  430. Assert.IsNotNull (ex.InnerException, "#G2");
  431. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
  432. }
  433. }
  434. [Test]
  435. [Category ("NotWorking")]
  436. public void TestSerializeEnumDefaultValue_InvalidValue2 ()
  437. {
  438. #if NET_2_0
  439. try {
  440. Serialize (5, typeof (EnumDefaultValue));
  441. Assert.Fail ("#1");
  442. } catch (InvalidOperationException ex) {
  443. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  444. Assert.IsNotNull (ex.InnerException, "#3");
  445. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  446. Assert.IsNotNull (ex.InnerException.Message, "#5");
  447. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
  448. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
  449. }
  450. #else
  451. Serialize (5, typeof (EnumDefaultValue));
  452. Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
  453. #endif
  454. }
  455. [Test]
  456. [Category ("NotWorking")]
  457. public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
  458. {
  459. #if NET_2_0
  460. try {
  461. Serialize (new EnumDefaultValueNF ());
  462. Assert.Fail ("#1");
  463. } catch (InvalidOperationException ex) {
  464. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  465. Assert.IsNotNull (ex.InnerException, "#3");
  466. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  467. Assert.IsNotNull (ex.InnerException.Message, "#5");
  468. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
  469. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
  470. }
  471. #else
  472. Serialize (new EnumDefaultValueNF ());
  473. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
  474. #endif
  475. }
  476. [Test]
  477. [Category ("NotWorking")]
  478. public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
  479. {
  480. #if NET_2_0
  481. try {
  482. Serialize (15, typeof (EnumDefaultValueNF));
  483. Assert.Fail ("#1");
  484. } catch (InvalidOperationException ex) {
  485. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  486. Assert.IsNotNull (ex.InnerException, "#3");
  487. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  488. Assert.IsNotNull (ex.InnerException.Message, "#5");
  489. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
  490. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
  491. }
  492. #else
  493. Serialize (15, typeof (EnumDefaultValueNF));
  494. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
  495. #endif
  496. }
  497. [Test]
  498. [Category ("NotWorking")]
  499. public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
  500. {
  501. try {
  502. Serialize ("b", typeof (EnumDefaultValueNF));
  503. Assert.Fail ("#A1");
  504. } catch (InvalidOperationException ex) {
  505. Assert.IsNotNull (ex.InnerException, "#A2");
  506. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
  507. }
  508. try {
  509. Serialize ("e2", typeof (EnumDefaultValueNF));
  510. Assert.Fail ("#B1");
  511. } catch (InvalidOperationException ex) {
  512. Assert.IsNotNull (ex.InnerException, "#B2");
  513. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
  514. }
  515. try {
  516. Serialize (string.Empty, typeof (EnumDefaultValueNF));
  517. Assert.Fail ("#C1");
  518. } catch (InvalidOperationException ex) {
  519. Assert.IsNotNull (ex.InnerException, "#C2");
  520. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
  521. }
  522. try {
  523. Serialize ("1", typeof (EnumDefaultValueNF));
  524. Assert.Fail ("#D1");
  525. } catch (InvalidOperationException ex) {
  526. Assert.IsNotNull (ex.InnerException, "#D2");
  527. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
  528. }
  529. try {
  530. Serialize ("0", typeof (EnumDefaultValueNF));
  531. Assert.Fail ("#E1");
  532. } catch (InvalidOperationException ex) {
  533. Assert.IsNotNull (ex.InnerException, "#E2");
  534. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
  535. }
  536. }
  537. [Test]
  538. [Category ("NotWorking")]
  539. public void TestSerializeZeroFlagEnum_InvalidValue ()
  540. {
  541. #if NET_2_0
  542. try {
  543. Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
  544. Assert.Fail ("#1");
  545. } catch (InvalidOperationException ex) {
  546. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  547. Assert.IsNotNull (ex.InnerException, "#3");
  548. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  549. Assert.IsNotNull (ex.InnerException.Message, "#5");
  550. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
  551. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
  552. }
  553. #else
  554. Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
  555. Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
  556. #endif
  557. }
  558. [Test]
  559. public void TestSerializeQualifiedName()
  560. {
  561. Serialize(new XmlQualifiedName("me", "home.urn"));
  562. Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
  563. }
  564. [Test]
  565. public void TestSerializeBytes()
  566. {
  567. Serialize((byte)0xAB);
  568. Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
  569. Serialize((byte)15);
  570. Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
  571. }
  572. [Test]
  573. public void TestSerializeByteArrays()
  574. {
  575. Serialize(new byte[] {});
  576. Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
  577. Serialize(new byte[] {0xAB, 0xCD});
  578. Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
  579. }
  580. [Test]
  581. public void TestSerializeDateTime()
  582. {
  583. DateTime d = new DateTime();
  584. Serialize(d);
  585. TimeZone tz = TimeZone.CurrentTimeZone;
  586. TimeSpan off = tz.GetUtcOffset (d);
  587. string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
  588. Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
  589. }
  590. /*
  591. FIXME
  592. - decimal
  593. - Guid
  594. - XmlNode objects
  595. [Test]
  596. public void TestSerialize()
  597. {
  598. Serialize();
  599. Assert.AreEqual (WriterText, "");
  600. }
  601. */
  602. // test basic class serialization /////////////////////////////////////
  603. [Test]
  604. public void TestSerializeSimpleClass()
  605. {
  606. SimpleClass simple = new SimpleClass();
  607. Serialize(simple);
  608. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  609. simple.something = "hello";
  610. Serialize(simple);
  611. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
  612. }
  613. [Test]
  614. public void TestSerializeStringCollection()
  615. {
  616. StringCollection strings = new StringCollection();
  617. Serialize(strings);
  618. Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  619. strings.Add("hello");
  620. strings.Add("goodbye");
  621. Serialize(strings);
  622. Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);
  623. }
  624. [Test]
  625. public void TestSerializePlainContainer()
  626. {
  627. StringCollectionContainer container = new StringCollectionContainer();
  628. Serialize(container);
  629. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
  630. container.Messages.Add("hello");
  631. container.Messages.Add("goodbye");
  632. Serialize(container);
  633. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);
  634. }
  635. [Test]
  636. public void TestSerializeArrayContainer()
  637. {
  638. ArrayContainer container = new ArrayContainer();
  639. Serialize(container);
  640. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  641. container.items = new object[] {10, 20};
  642. Serialize(container);
  643. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"), WriterText);
  644. container.items = new object[] {10, "hello"};
  645. Serialize(container);
  646. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"), WriterText);
  647. }
  648. [Test]
  649. public void TestSerializeClassArrayContainer()
  650. {
  651. ClassArrayContainer container = new ClassArrayContainer();
  652. Serialize(container);
  653. Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  654. SimpleClass simple1 = new SimpleClass();
  655. simple1.something = "hello";
  656. SimpleClass simple2 = new SimpleClass();
  657. simple2.something = "hello";
  658. container.items = new SimpleClass[2];
  659. container.items[0] = simple1;
  660. container.items[1] = simple2;
  661. Serialize(container);
  662. Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"), WriterText);
  663. }
  664. // test basic attributes ///////////////////////////////////////////////
  665. [Test]
  666. public void TestSerializeSimpleClassWithXmlAttributes()
  667. {
  668. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  669. Serialize(simple);
  670. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  671. simple.something = "hello";
  672. Serialize(simple);
  673. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
  674. }
  675. // test overrides ///////////////////////////////////////////////////////
  676. [Test]
  677. public void TestSerializeSimpleClassWithOverrides()
  678. {
  679. // Also tests XmlIgnore
  680. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  681. XmlAttributes attr = new XmlAttributes();
  682. attr.XmlIgnore = true;
  683. overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);
  684. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  685. simple.something = "hello";
  686. Serialize(simple, overrides);
  687. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  688. }
  689. [Test]
  690. public void TestSerializeSchema ()
  691. {
  692. XmlSchema schema = new XmlSchema ();
  693. schema.Items.Add (new XmlSchemaAttribute ());
  694. schema.Items.Add (new XmlSchemaAttributeGroup ());
  695. schema.Items.Add (new XmlSchemaComplexType ());
  696. schema.Items.Add (new XmlSchemaNotation ());
  697. schema.Items.Add (new XmlSchemaSimpleType ());
  698. schema.Items.Add (new XmlSchemaGroup ());
  699. schema.Items.Add (new XmlSchemaElement ());
  700. StringWriter sw = new StringWriter ();
  701. XmlTextWriter xtw = new XmlTextWriter (sw);
  702. xtw.QuoteChar = '\'';
  703. xtw.Formatting = Formatting.Indented;
  704. XmlSerializer xs = new XmlSerializer (schema.GetType ());
  705. xs.Serialize (xtw, schema);
  706. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  707. "<?xml version='1.0' encoding='utf-16'?>{0}" +
  708. "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
  709. " <xsd:attribute />{0}" +
  710. " <xsd:attributeGroup />{0}" +
  711. " <xsd:complexType />{0}" +
  712. " <xsd:notation />{0}" +
  713. " <xsd:simpleType />{0}" +
  714. " <xsd:group />{0}" +
  715. " <xsd:element />{0}" +
  716. "</xsd:schema>", Environment.NewLine), sw.ToString ());
  717. }
  718. // test xmlText //////////////////////////////////////////////////////////
  719. [Test]
  720. public void TestSerializeXmlTextAttribute()
  721. {
  722. SimpleClass simple = new SimpleClass();
  723. simple.something = "hello";
  724. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  725. XmlAttributes attr = new XmlAttributes();
  726. overrides.Add(typeof(SimpleClass), "something", attr);
  727. attr.XmlText = new XmlTextAttribute();
  728. Serialize(simple, overrides);
  729. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1");
  730. attr.XmlText = new XmlTextAttribute(typeof(string));
  731. Serialize(simple, overrides);
  732. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#2");
  733. try {
  734. attr.XmlText = new XmlTextAttribute(typeof(byte[]));
  735. Serialize(simple, overrides);
  736. Assert.Fail("XmlText.Type does not match the type it serializes: this should have failed");
  737. } catch (InvalidOperationException ex) {
  738. // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
  739. Assert.IsNotNull (ex.Message, "#A1");
  740. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A2");
  741. // there was an error reflecting field 'something'.
  742. Assert.IsNotNull (ex.InnerException, "#A3");
  743. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A4");
  744. Assert.IsNotNull (ex.InnerException.Message, "#A5");
  745. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A6");
  746. // the type for XmlText may not be specified for primitive types.
  747. Assert.IsNotNull (ex.InnerException.InnerException, "#A7");
  748. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A8");
  749. Assert.IsNotNull (ex.InnerException.Message, "#A9");
  750. Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A10");
  751. } catch (InvalidCastException ex) {
  752. // FIXME: we should report InvalidOperationException
  753. }
  754. try {
  755. attr.XmlText = new XmlTextAttribute();
  756. attr.XmlText.DataType = "sometype";
  757. Serialize(simple, overrides);
  758. Assert.Fail("XmlText.DataType does not match the type it serializes: this should have failed");
  759. } catch (InvalidOperationException ex) {
  760. // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
  761. Assert.IsNotNull (ex.Message, "#B1");
  762. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B2");
  763. // there was an error reflecting field 'something'.
  764. Assert.IsNotNull (ex.InnerException, "#B3");
  765. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B4");
  766. Assert.IsNotNull (ex.InnerException.Message, "#B5");
  767. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B6");
  768. // there was an error reflecting type 'System.String'.
  769. Assert.IsNotNull (ex.InnerException.InnerException, "#B7");
  770. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B8");
  771. Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B9");
  772. Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B10");
  773. // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
  774. // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
  775. Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B11");
  776. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B12");
  777. Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B13");
  778. Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B14");
  779. } catch (NotSupportedException ex) {
  780. // FIXME: we should report InvalidOperationException
  781. }
  782. }
  783. // test xmlRoot //////////////////////////////////////////////////////////
  784. [Test]
  785. public void TestSerializeXmlRootAttribute()
  786. {
  787. // constructor override & element name
  788. XmlRootAttribute root = new XmlRootAttribute();
  789. root.ElementName = "renamed";
  790. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
  791. Serialize(simpleWithAttributes, root);
  792. Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  793. SimpleClass simple = null;
  794. root.IsNullable = false;
  795. try {
  796. Serialize(simple, root);
  797. Assert.Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
  798. } catch (NullReferenceException) {
  799. }
  800. root.IsNullable = true;
  801. try {
  802. Serialize(simple, root);
  803. Assert.Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
  804. } catch (NullReferenceException) {
  805. }
  806. simple = new SimpleClass();
  807. root.ElementName = null;
  808. root.Namespace = "some.urn";
  809. Serialize(simple, root);
  810. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
  811. }
  812. [Test]
  813. public void TestSerializeXmlRootAttributeOnMember()
  814. {
  815. // nested root
  816. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  817. XmlAttributes childAttr = new XmlAttributes();
  818. childAttr.XmlRoot = new XmlRootAttribute("simple");
  819. overrides.Add(typeof(SimpleClass), childAttr);
  820. XmlAttributes attr = new XmlAttributes();
  821. attr.XmlRoot = new XmlRootAttribute("simple");
  822. overrides.Add(typeof(ClassArrayContainer), attr);
  823. ClassArrayContainer container = new ClassArrayContainer();
  824. container.items = new SimpleClass[1];
  825. container.items[0] = new SimpleClass();
  826. Serialize(container, overrides);
  827. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText);
  828. // FIXME test data type
  829. }
  830. // test XmlAttribute /////////////////////////////////////////////////////
  831. [Test]
  832. public void TestSerializeXmlAttributeAttribute()
  833. {
  834. // null
  835. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  836. XmlAttributes attr = new XmlAttributes();
  837. attr.XmlAttribute = new XmlAttributeAttribute();
  838. overrides.Add(typeof(SimpleClass), "something", attr);
  839. SimpleClass simple = new SimpleClass();;
  840. Serialize(simple, overrides);
  841. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  842. // regular
  843. simple.something = "hello";
  844. Serialize(simple, overrides);
  845. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText, "#2");
  846. // AttributeName
  847. attr.XmlAttribute.AttributeName = "somethingelse";
  848. Serialize(simple, overrides);
  849. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText, "#3");
  850. // Type
  851. // FIXME this should work, shouldnt it?
  852. // attr.XmlAttribute.Type = typeof(string);
  853. // Serialize(simple, overrides);
  854. // Assert(WriterText.EndsWith(" something='hello' />"));
  855. // Namespace
  856. attr.XmlAttribute.Namespace = "some:urn";
  857. Serialize(simple, overrides);
  858. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText, "#4");
  859. // FIXME DataType
  860. // FIXME XmlSchemaForm Form
  861. // FIXME write XmlQualifiedName as attribute
  862. }
  863. // test XmlElement ///////////////////////////////////////////////////////
  864. [Test]
  865. public void TestSerializeXmlElementAttribute()
  866. {
  867. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  868. XmlAttributes attr = new XmlAttributes();
  869. XmlElementAttribute element = new XmlElementAttribute();
  870. attr.XmlElements.Add(element);
  871. overrides.Add(typeof(SimpleClass), "something", attr);
  872. // null
  873. SimpleClass simple = new SimpleClass();;
  874. Serialize(simple, overrides);
  875. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  876. // not null
  877. simple.something = "hello";
  878. Serialize(simple, overrides);
  879. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#2");
  880. //ElementName
  881. element.ElementName = "saying";
  882. Serialize(simple, overrides);
  883. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText, "#3");
  884. //IsNullable
  885. element.IsNullable = false;
  886. simple.something = null;
  887. Serialize(simple, overrides);
  888. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
  889. element.IsNullable = true;
  890. simple.something = null;
  891. Serialize(simple, overrides);
  892. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText, "#5");
  893. //Namespace
  894. element.ElementName = null;
  895. element.IsNullable = false;
  896. element.Namespace = "some:urn";
  897. simple.something = "hello";
  898. Serialize(simple, overrides);
  899. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6");
  900. //FIXME DataType
  901. //FIXME Form
  902. //FIXME Type
  903. }
  904. // test XmlElementAttribute with arrays and collections //////////////////
  905. [Test]
  906. public void TestSerializeCollectionWithXmlElementAttribute()
  907. {
  908. // the rule is:
  909. // if no type is specified or the specified type
  910. // matches the contents of the collection,
  911. // serialize each element in an element named after the member.
  912. // if the type does not match, or matches the collection itself,
  913. // create a base wrapping element for the member, and then
  914. // wrap each collection item in its own wrapping element based on type.
  915. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  916. XmlAttributes attr = new XmlAttributes();
  917. XmlElementAttribute element = new XmlElementAttribute();
  918. attr.XmlElements.Add(element);
  919. overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
  920. // empty collection & no type info in XmlElementAttribute
  921. StringCollectionContainer container = new StringCollectionContainer();
  922. Serialize(container, overrides);
  923. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  924. // non-empty collection & no type info in XmlElementAttribute
  925. container.Messages.Add("hello");
  926. Serialize(container, overrides);
  927. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
  928. // non-empty collection & only type info in XmlElementAttribute
  929. element.Type = typeof(StringCollection);
  930. Serialize(container, overrides);
  931. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText);
  932. // non-empty collection & only type info in XmlElementAttribute
  933. element.Type = typeof(string);
  934. Serialize(container, overrides);
  935. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
  936. // two elements
  937. container.Messages.Add("goodbye");
  938. element.Type = null;
  939. Serialize(container, overrides);
  940. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText);
  941. }
  942. // test DefaultValue /////////////////////////////////////////////////////
  943. [Test]
  944. public void TestSerializeDefaultValueAttribute()
  945. {
  946. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  947. XmlAttributes attr = new XmlAttributes();
  948. string defaultValueInstance = "nothing";
  949. attr.XmlDefaultValue = defaultValueInstance;
  950. overrides.Add(typeof(SimpleClass), "something", attr);
  951. // use the default
  952. SimpleClass simple = new SimpleClass();
  953. Serialize(simple, overrides);
  954. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  955. // same value as default
  956. simple.something = defaultValueInstance;
  957. Serialize(simple, overrides);
  958. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  959. // some other value
  960. simple.something = "hello";
  961. Serialize(simple, overrides);
  962. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
  963. }
  964. // test XmlEnum //////////////////////////////////////////////////////////
  965. [Test]
  966. public void TestSerializeXmlEnumAttribute()
  967. {
  968. Serialize(XmlSchemaForm.Qualified);
  969. Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
  970. Serialize(XmlSchemaForm.Unqualified);
  971. Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
  972. }
  973. [Test]
  974. #if NET_2_0
  975. [Category ("NotWorking")]
  976. #endif
  977. public void TestSerializeXmlEnumAttribute_IgnoredValue ()
  978. {
  979. // technically XmlSchemaForm.None has an XmlIgnore attribute,
  980. // but it is not being serialized as a member.
  981. #if NET_2_0
  982. try {
  983. Serialize (XmlSchemaForm.None);
  984. Assert.Fail ("#1");
  985. } catch (InvalidOperationException ex) {
  986. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  987. Assert.IsNotNull (ex.InnerException, "#3");
  988. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  989. Assert.IsNotNull (ex.InnerException.Message, "#5");
  990. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
  991. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
  992. }
  993. #else
  994. Serialize (XmlSchemaForm.None);
  995. Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
  996. #endif
  997. }
  998. [Test]
  999. public void TestSerializeXmlNodeArray ()
  1000. {
  1001. XmlDocument doc = new XmlDocument ();
  1002. Serialize (new XmlNode [] { doc.CreateAttribute("at"), doc.CreateElement("elem1"), doc.CreateElement("elem2") }, typeof(object));
  1003. Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  1004. }
  1005. [Test]
  1006. public void TestSerializeXmlElement ()
  1007. {
  1008. XmlDocument doc = new XmlDocument ();
  1009. Serialize (doc.CreateElement("elem"), typeof(XmlElement));
  1010. Assert.AreEqual (Infoset ("<elem/>"), WriterText);
  1011. }
  1012. [Test]
  1013. public void TestSerializeXmlElementSubclass ()
  1014. {
  1015. XmlDocument doc = new XmlDocument ();
  1016. Serialize (new MyElem (doc), typeof(XmlElement));
  1017. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText);
  1018. Serialize (new MyElem (doc), typeof(MyElem));
  1019. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText);
  1020. }
  1021. [Test]
  1022. public void TestSerializeXmlCDataSection ()
  1023. {
  1024. XmlDocument doc = new XmlDocument ();
  1025. CDataContainer c = new CDataContainer ();
  1026. c.cdata = doc.CreateCDataSection("data section contents");
  1027. Serialize (c);
  1028. Assert.AreEqual (Infoset ("<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>"), WriterText);
  1029. }
  1030. [Test]
  1031. public void TestSerializeXmlNode ()
  1032. {
  1033. XmlDocument doc = new XmlDocument ();
  1034. NodeContainer c = new NodeContainer ();
  1035. c.node = doc.CreateTextNode("text");
  1036. Serialize (c);
  1037. Assert.AreEqual (Infoset ("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
  1038. }
  1039. [Test]
  1040. public void TestSerializeChoice ()
  1041. {
  1042. Choices ch = new Choices ();
  1043. ch.MyChoice = "choice text";
  1044. ch.ItemType = ItemChoiceType.ChoiceZero;
  1045. Serialize (ch);
  1046. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceZero>choice text</ChoiceZero></Choices>"), WriterText);
  1047. ch.ItemType = ItemChoiceType.StrangeOne;
  1048. Serialize (ch);
  1049. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceOne>choice text</ChoiceOne></Choices>"), WriterText);
  1050. ch.ItemType = ItemChoiceType.ChoiceTwo;
  1051. Serialize (ch);
  1052. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceTwo>choice text</ChoiceTwo></Choices>"), WriterText);
  1053. }
  1054. [Test]
  1055. public void TestSerializeNamesWithSpaces ()
  1056. {
  1057. TestSpace ts = new TestSpace();
  1058. ts.elem = 4;
  1059. ts.attr = 5;
  1060. Serialize (ts);
  1061. Assert.AreEqual (Infoset ("<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>"), WriterText);
  1062. }
  1063. [Test]
  1064. public void TestSerializeReadOnlyProps ()
  1065. {
  1066. ReadOnlyProperties ts = new ReadOnlyProperties();
  1067. Serialize (ts);
  1068. Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1069. }
  1070. [Test]
  1071. public void TestSerializeIList()
  1072. {
  1073. clsPerson k = new clsPerson();
  1074. k.EmailAccounts = new ArrayList();
  1075. k.EmailAccounts.Add("a");
  1076. k.EmailAccounts.Add("b");
  1077. Serialize (k);
  1078. Assert.AreEqual (Infoset ("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText);
  1079. }
  1080. [Test]
  1081. public void TestSerializeArrayEnc ()
  1082. {
  1083. SoapReflectionImporter imp = new SoapReflectionImporter ();
  1084. XmlTypeMapping map = imp.ImportTypeMapping (typeof(ArrayClass));
  1085. XmlSerializer ser = new XmlSerializer (map);
  1086. StringWriter sw = new StringWriter ();
  1087. XmlTextWriter tw = new XmlTextWriter (sw);
  1088. tw.WriteStartElement ("aa");
  1089. ser.Serialize (tw, new ArrayClass ());
  1090. tw.WriteEndElement ();
  1091. }
  1092. [Test]
  1093. public void TestIncludeType()
  1094. {
  1095. // Test for bug #76049
  1096. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1097. XmlTypeMapping map = imp.ImportTypeMapping (typeof(object));
  1098. imp.IncludeType (typeof(TestSpace));
  1099. XmlSerializer ser = new XmlSerializer (map);
  1100. ser.Serialize (new StringWriter (), new TestSpace ());
  1101. }
  1102. [Test]
  1103. public void TestSerializeChoiceArray()
  1104. {
  1105. CompositeValueType v = new CompositeValueType ();
  1106. v.Init ();
  1107. Serialize (v);
  1108. Assert.AreEqual (Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>"), WriterText);
  1109. }
  1110. [Test]
  1111. public void TestArrayAttributeWithDataType ()
  1112. {
  1113. Serialize (new ArrayAttributeWithType ());
  1114. string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
  1115. res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
  1116. Assert.AreEqual (Infoset (res), WriterText);
  1117. }
  1118. [Test]
  1119. public void TestSubclassElementType ()
  1120. {
  1121. SubclassTestContainer c = new SubclassTestContainer ();
  1122. c.data = new SubclassTestSub ();
  1123. Serialize (c);
  1124. string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
  1125. res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
  1126. Assert.AreEqual (Infoset (res), WriterText);
  1127. }
  1128. [Test]
  1129. [ExpectedException (typeof(InvalidOperationException))]
  1130. public void TestArrayAttributeWithWrongDataType ()
  1131. {
  1132. Serialize (new ArrayAttributeWithWrongType ());
  1133. }
  1134. [Test]
  1135. [Category ("NotWorking")]
  1136. public void TestSerializePrimitiveTypesContainer ()
  1137. {
  1138. Serialize (new PrimitiveTypesContainer ());
  1139. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1140. "<?xml version='1.0' encoding='utf-16'?>" +
  1141. #if NET_2_0
  1142. "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
  1143. #else
  1144. "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
  1145. #endif
  1146. "<Number>2004</Number>" +
  1147. "<Name>some name</Name>" +
  1148. "<Index>56</Index>" +
  1149. "<Password>8w8=</Password>" +
  1150. "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
  1151. "</PrimitiveTypesContainer>", XmlSchemaNamespace,
  1152. XmlSchemaInstanceNamespace), sw.ToString (), "#1");
  1153. SerializeEncoded (new PrimitiveTypesContainer ());
  1154. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1155. "<?xml version='1.0' encoding='utf-16'?>" +
  1156. #if NET_2_0
  1157. "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
  1158. #else
  1159. "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
  1160. #endif
  1161. "<Number xsi:type='xsd:int'>2004</Number>" +
  1162. "<Name xsi:type='xsd:string'>some name</Name>" +
  1163. "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
  1164. "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
  1165. "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
  1166. "</q1:PrimitiveTypesContainer>", XmlSchemaNamespace,
  1167. XmlSchemaInstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
  1168. sw.ToString (), "#2");
  1169. }
  1170. // Helper methods
  1171. public static string Infoset (string sx)
  1172. {
  1173. XmlDocument doc = new XmlDocument ();
  1174. doc.LoadXml (sx);
  1175. StringBuilder sb = new StringBuilder ();
  1176. GetInfoset (doc.DocumentElement, sb);
  1177. return sb.ToString ();
  1178. }
  1179. public static string Infoset (XmlNode nod)
  1180. {
  1181. StringBuilder sb = new StringBuilder ();
  1182. GetInfoset (nod, sb);
  1183. return sb.ToString ();
  1184. }
  1185. static void GetInfoset (XmlNode nod, StringBuilder sb)
  1186. {
  1187. switch (nod.NodeType)
  1188. {
  1189. case XmlNodeType.Attribute:
  1190. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  1191. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  1192. break;
  1193. case XmlNodeType.Element:
  1194. XmlElement elem = (XmlElement) nod;
  1195. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  1196. ArrayList ats = new ArrayList ();
  1197. foreach (XmlAttribute at in elem.Attributes)
  1198. ats.Add (at.LocalName + " " + at.NamespaceURI);
  1199. ats.Sort ();
  1200. foreach (string name in ats)
  1201. {
  1202. string[] nn = name.Split (' ');
  1203. GetInfoset (elem.Attributes[nn[0],nn[1]], sb);
  1204. }
  1205. sb.Append (">");
  1206. foreach (XmlNode cn in elem.ChildNodes)
  1207. GetInfoset (cn, sb);
  1208. sb.Append ("</>");
  1209. break;
  1210. default:
  1211. sb.Append (nod.OuterXml);
  1212. break;
  1213. }
  1214. }
  1215. static XmlTypeMapping CreateSoapMapping (Type type)
  1216. {
  1217. SoapReflectionImporter importer = new SoapReflectionImporter ();
  1218. return importer.ImportTypeMapping (type);
  1219. }
  1220. }
  1221. }