XmlSerializerTests.cs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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. public void TestSerializeEnumeration_FromValue ()
  197. {
  198. Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  199. Assert.AreEqual (
  200. "<?xml version='1.0' encoding='utf-16'?>" +
  201. "<SimpleEnumeration>SECOND</SimpleEnumeration>",
  202. sw.ToString ());
  203. }
  204. [Test]
  205. [Category ("NotWorking")]
  206. public void TestSerializeEnumeration_FromValue_Encoded ()
  207. {
  208. SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  209. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  210. "<?xml version='1.0' encoding='utf-16'?>" +
  211. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  212. XmlSchemaInstanceNamespace), sw.ToString ());
  213. }
  214. [Test]
  215. public void TestSerializeEnumeration()
  216. {
  217. Serialize(SimpleEnumeration.FIRST);
  218. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
  219. Serialize(SimpleEnumeration.SECOND);
  220. Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
  221. }
  222. [Test]
  223. public void TestSerializeEnumeration_Encoded()
  224. {
  225. SerializeEncoded (SimpleEnumeration.FIRST);
  226. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  227. "<?xml version='1.0' encoding='utf-16'?>" +
  228. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  229. XmlSchemaInstanceNamespace), sw.ToString (), "#B1");
  230. SerializeEncoded (SimpleEnumeration.SECOND);
  231. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  232. "<?xml version='1.0' encoding='utf-16'?>" +
  233. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  234. XmlSchemaInstanceNamespace), sw.ToString (), "#B2");
  235. }
  236. [Test]
  237. public void TestSerializeEnumDefaultValue ()
  238. {
  239. Serialize (new EnumDefaultValue ());
  240. Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
  241. Serialize (new SimpleEnumeration ());
  242. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
  243. Serialize (3, typeof (EnumDefaultValue));
  244. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
  245. Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  246. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
  247. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  248. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
  249. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  250. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
  251. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  252. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
  253. Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  254. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
  255. Serialize (3, typeof (FlagEnum));
  256. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
  257. Serialize (5, typeof (FlagEnum));
  258. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
  259. Serialize (FlagEnum.e4, typeof (FlagEnum));
  260. Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
  261. Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  262. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
  263. Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  264. Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
  265. Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  266. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
  267. Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  268. Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
  269. Serialize (3, typeof (EnumDefaultValueNF));
  270. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
  271. Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  272. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
  273. Serialize (2, typeof (ZeroFlagEnum));
  274. Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t&lt;w&gt;o</ZeroFlagEnum>"), WriterText, "#18");
  275. Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
  276. Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
  277. }
  278. [Test]
  279. [Category ("NotWorking")]
  280. public void TestSerializeEnumDefaultValue_Encoded ()
  281. {
  282. SerializeEncoded (new EnumDefaultValue ());
  283. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  284. "<?xml version='1.0' encoding='utf-16'?>" +
  285. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
  286. XmlSchemaInstanceNamespace), sw.ToString (), "#1");
  287. SerializeEncoded (new SimpleEnumeration ());
  288. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  289. "<?xml version='1.0' encoding='utf-16'?>" +
  290. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  291. XmlSchemaInstanceNamespace), sw.ToString (), "#2");
  292. SerializeEncoded (3, typeof (EnumDefaultValue));
  293. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  294. "<?xml version='1.0' encoding='utf-16'?>" +
  295. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  296. XmlSchemaInstanceNamespace), sw.ToString (), "#3");
  297. SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  298. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  299. "<?xml version='1.0' encoding='utf-16'?>" +
  300. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  301. XmlSchemaInstanceNamespace), sw.ToString (), "#4");
  302. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  303. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  304. "<?xml version='1.0' encoding='utf-16'?>" +
  305. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  306. XmlSchemaInstanceNamespace), sw.ToString (), "#5");
  307. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  308. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  309. "<?xml version='1.0' encoding='utf-16'?>" +
  310. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  311. XmlSchemaInstanceNamespace), sw.ToString (), "#6");
  312. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  313. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  314. "<?xml version='1.0' encoding='utf-16'?>" +
  315. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  316. XmlSchemaInstanceNamespace), sw.ToString (), "#7");
  317. SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  318. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  319. "<?xml version='1.0' encoding='utf-16'?>" +
  320. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  321. XmlSchemaInstanceNamespace), sw.ToString (), "#8");
  322. SerializeEncoded (3, typeof (FlagEnum));
  323. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  324. "<?xml version='1.0' encoding='utf-16'?>" +
  325. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
  326. XmlSchemaInstanceNamespace), sw.ToString (), "#9");
  327. SerializeEncoded (5, typeof (FlagEnum));
  328. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  329. "<?xml version='1.0' encoding='utf-16'?>" +
  330. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
  331. XmlSchemaInstanceNamespace), sw.ToString (), "#10");
  332. SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
  333. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  334. "<?xml version='1.0' encoding='utf-16'?>" +
  335. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
  336. XmlSchemaInstanceNamespace), sw.ToString (), "#11");
  337. SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  338. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  339. "<?xml version='1.0' encoding='utf-16'?>" +
  340. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
  341. XmlSchemaInstanceNamespace), sw.ToString (), "#12");
  342. SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  343. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  344. "<?xml version='1.0' encoding='utf-16'?>" +
  345. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
  346. XmlSchemaInstanceNamespace), sw.ToString (), "#13");
  347. SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  348. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  349. "<?xml version='1.0' encoding='utf-16'?>" +
  350. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
  351. XmlSchemaInstanceNamespace), sw.ToString (), "#14");
  352. SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  353. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  354. "<?xml version='1.0' encoding='utf-16'?>" +
  355. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
  356. XmlSchemaInstanceNamespace), sw.ToString (), "#15");
  357. SerializeEncoded (3, typeof (EnumDefaultValueNF));
  358. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  359. "<?xml version='1.0' encoding='utf-16'?>" +
  360. "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
  361. XmlSchemaInstanceNamespace), sw.ToString (), "#16");
  362. SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  363. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  364. "<?xml version='1.0' encoding='utf-16'?>" +
  365. "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
  366. XmlSchemaInstanceNamespace), sw.ToString (), "#17");
  367. SerializeEncoded (2, typeof (ZeroFlagEnum));
  368. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  369. "<?xml version='1.0' encoding='utf-16'?>" +
  370. "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
  371. XmlSchemaInstanceNamespace), sw.ToString (), "#18");
  372. SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
  373. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  374. "<?xml version='1.0' encoding='utf-16'?>" +
  375. "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
  376. XmlSchemaInstanceNamespace), sw.ToString (), "#19");
  377. }
  378. [Test]
  379. public void TestSerializeEnumDefaultValue_InvalidValue1 ()
  380. {
  381. try {
  382. Serialize ("b", typeof (EnumDefaultValue));
  383. Assert.Fail ("#A1");
  384. } catch (InvalidOperationException ex) {
  385. Assert.IsNotNull (ex.InnerException, "#A2");
  386. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
  387. }
  388. try {
  389. Serialize ("e1", typeof (EnumDefaultValue));
  390. Assert.Fail ("#B1");
  391. } catch (InvalidOperationException ex) {
  392. Assert.IsNotNull (ex.InnerException, "#B2");
  393. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
  394. }
  395. try {
  396. Serialize ("e1,e2", typeof (EnumDefaultValue));
  397. Assert.Fail ("#C1");
  398. } catch (InvalidOperationException ex) {
  399. Assert.IsNotNull (ex.InnerException, "#C2");
  400. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
  401. }
  402. try {
  403. Serialize (string.Empty, typeof (EnumDefaultValue));
  404. Assert.Fail ("#D1");
  405. } catch (InvalidOperationException ex) {
  406. Assert.IsNotNull (ex.InnerException, "#D2");
  407. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
  408. }
  409. try {
  410. Serialize ("1", typeof (EnumDefaultValue));
  411. Assert.Fail ("#E1");
  412. } catch (InvalidOperationException ex) {
  413. Assert.IsNotNull (ex.InnerException, "#E2");
  414. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
  415. }
  416. try {
  417. Serialize ("0", typeof (EnumDefaultValue));
  418. Assert.Fail ("#F1");
  419. } catch (InvalidOperationException ex) {
  420. Assert.IsNotNull (ex.InnerException, "#F2");
  421. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
  422. }
  423. try {
  424. Serialize (new SimpleClass (), typeof (EnumDefaultValue));
  425. Assert.Fail ("#G1");
  426. } catch (InvalidOperationException ex) {
  427. Assert.IsNotNull (ex.InnerException, "#G2");
  428. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
  429. }
  430. }
  431. [Test]
  432. public void TestSerializeEnumDefaultValue_InvalidValue2 ()
  433. {
  434. #if NET_2_0
  435. try {
  436. Serialize (5, typeof (EnumDefaultValue));
  437. Assert.Fail ("#1");
  438. } catch (InvalidOperationException ex) {
  439. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  440. Assert.IsNotNull (ex.InnerException, "#3");
  441. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  442. Assert.IsNotNull (ex.InnerException.Message, "#5");
  443. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
  444. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
  445. }
  446. #else
  447. Serialize (5, typeof (EnumDefaultValue));
  448. Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
  449. #endif
  450. }
  451. [Test]
  452. public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
  453. {
  454. #if NET_2_0
  455. try {
  456. Serialize (new EnumDefaultValueNF ());
  457. Assert.Fail ("#1");
  458. } catch (InvalidOperationException ex) {
  459. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  460. Assert.IsNotNull (ex.InnerException, "#3");
  461. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  462. Assert.IsNotNull (ex.InnerException.Message, "#5");
  463. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
  464. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
  465. }
  466. #else
  467. Serialize (new EnumDefaultValueNF ());
  468. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
  469. #endif
  470. }
  471. [Test]
  472. public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
  473. {
  474. #if NET_2_0
  475. try {
  476. Serialize (15, typeof (EnumDefaultValueNF));
  477. Assert.Fail ("#1");
  478. } catch (InvalidOperationException ex) {
  479. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  480. Assert.IsNotNull (ex.InnerException, "#3");
  481. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  482. Assert.IsNotNull (ex.InnerException.Message, "#5");
  483. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
  484. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
  485. }
  486. #else
  487. Serialize (15, typeof (EnumDefaultValueNF));
  488. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
  489. #endif
  490. }
  491. [Test]
  492. public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
  493. {
  494. try {
  495. Serialize ("b", typeof (EnumDefaultValueNF));
  496. Assert.Fail ("#A1");
  497. } catch (InvalidOperationException ex) {
  498. Assert.IsNotNull (ex.InnerException, "#A2");
  499. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
  500. }
  501. try {
  502. Serialize ("e2", typeof (EnumDefaultValueNF));
  503. Assert.Fail ("#B1");
  504. } catch (InvalidOperationException ex) {
  505. Assert.IsNotNull (ex.InnerException, "#B2");
  506. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
  507. }
  508. try {
  509. Serialize (string.Empty, typeof (EnumDefaultValueNF));
  510. Assert.Fail ("#C1");
  511. } catch (InvalidOperationException ex) {
  512. Assert.IsNotNull (ex.InnerException, "#C2");
  513. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
  514. }
  515. try {
  516. Serialize ("1", typeof (EnumDefaultValueNF));
  517. Assert.Fail ("#D1");
  518. } catch (InvalidOperationException ex) {
  519. Assert.IsNotNull (ex.InnerException, "#D2");
  520. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
  521. }
  522. try {
  523. Serialize ("0", typeof (EnumDefaultValueNF));
  524. Assert.Fail ("#E1");
  525. } catch (InvalidOperationException ex) {
  526. Assert.IsNotNull (ex.InnerException, "#E2");
  527. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
  528. }
  529. }
  530. [Test]
  531. public void TestSerializeZeroFlagEnum_InvalidValue ()
  532. {
  533. #if NET_2_0
  534. try {
  535. Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
  536. Assert.Fail ("#1");
  537. } catch (InvalidOperationException ex) {
  538. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  539. Assert.IsNotNull (ex.InnerException, "#3");
  540. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  541. Assert.IsNotNull (ex.InnerException.Message, "#5");
  542. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
  543. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
  544. }
  545. #else
  546. Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
  547. Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
  548. #endif
  549. }
  550. [Test]
  551. public void TestSerializeQualifiedName()
  552. {
  553. Serialize(new XmlQualifiedName("me", "home.urn"));
  554. Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
  555. }
  556. [Test]
  557. public void TestSerializeBytes()
  558. {
  559. Serialize((byte)0xAB);
  560. Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
  561. Serialize((byte)15);
  562. Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
  563. }
  564. [Test]
  565. public void TestSerializeByteArrays()
  566. {
  567. Serialize(new byte[] {});
  568. Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
  569. Serialize(new byte[] {0xAB, 0xCD});
  570. Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
  571. }
  572. [Test]
  573. public void TestSerializeDateTime()
  574. {
  575. DateTime d = new DateTime();
  576. Serialize(d);
  577. TimeZone tz = TimeZone.CurrentTimeZone;
  578. TimeSpan off = tz.GetUtcOffset (d);
  579. string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
  580. Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
  581. }
  582. /*
  583. FIXME
  584. - decimal
  585. - Guid
  586. - XmlNode objects
  587. [Test]
  588. public void TestSerialize()
  589. {
  590. Serialize();
  591. Assert.AreEqual (WriterText, "");
  592. }
  593. */
  594. // test basic class serialization /////////////////////////////////////
  595. [Test]
  596. public void TestSerializeSimpleClass()
  597. {
  598. SimpleClass simple = new SimpleClass();
  599. Serialize(simple);
  600. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  601. simple.something = "hello";
  602. Serialize(simple);
  603. 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);
  604. }
  605. [Test]
  606. public void TestSerializeStringCollection()
  607. {
  608. StringCollection strings = new StringCollection();
  609. Serialize(strings);
  610. Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  611. strings.Add("hello");
  612. strings.Add("goodbye");
  613. Serialize(strings);
  614. 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);
  615. }
  616. [Test]
  617. public void TestSerializePlainContainer()
  618. {
  619. StringCollectionContainer container = new StringCollectionContainer();
  620. Serialize(container);
  621. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
  622. container.Messages.Add("hello");
  623. container.Messages.Add("goodbye");
  624. Serialize(container);
  625. 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);
  626. }
  627. [Test]
  628. public void TestSerializeArrayContainer()
  629. {
  630. ArrayContainer container = new ArrayContainer();
  631. Serialize(container);
  632. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  633. container.items = new object[] {10, 20};
  634. Serialize(container);
  635. 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);
  636. container.items = new object[] {10, "hello"};
  637. Serialize(container);
  638. 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);
  639. }
  640. [Test]
  641. public void TestSerializeClassArrayContainer()
  642. {
  643. ClassArrayContainer container = new ClassArrayContainer();
  644. Serialize(container);
  645. Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  646. SimpleClass simple1 = new SimpleClass();
  647. simple1.something = "hello";
  648. SimpleClass simple2 = new SimpleClass();
  649. simple2.something = "hello";
  650. container.items = new SimpleClass[2];
  651. container.items[0] = simple1;
  652. container.items[1] = simple2;
  653. Serialize(container);
  654. 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);
  655. }
  656. // test basic attributes ///////////////////////////////////////////////
  657. [Test]
  658. public void TestSerializeSimpleClassWithXmlAttributes()
  659. {
  660. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  661. Serialize(simple);
  662. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  663. simple.something = "hello";
  664. Serialize(simple);
  665. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
  666. }
  667. // test overrides ///////////////////////////////////////////////////////
  668. [Test]
  669. public void TestSerializeSimpleClassWithOverrides()
  670. {
  671. // Also tests XmlIgnore
  672. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  673. XmlAttributes attr = new XmlAttributes();
  674. attr.XmlIgnore = true;
  675. overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);
  676. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  677. simple.something = "hello";
  678. Serialize(simple, overrides);
  679. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  680. }
  681. [Test]
  682. public void TestSerializeSchema ()
  683. {
  684. XmlSchema schema = new XmlSchema ();
  685. schema.Items.Add (new XmlSchemaAttribute ());
  686. schema.Items.Add (new XmlSchemaAttributeGroup ());
  687. schema.Items.Add (new XmlSchemaComplexType ());
  688. schema.Items.Add (new XmlSchemaNotation ());
  689. schema.Items.Add (new XmlSchemaSimpleType ());
  690. schema.Items.Add (new XmlSchemaGroup ());
  691. schema.Items.Add (new XmlSchemaElement ());
  692. StringWriter sw = new StringWriter ();
  693. XmlTextWriter xtw = new XmlTextWriter (sw);
  694. xtw.QuoteChar = '\'';
  695. xtw.Formatting = Formatting.Indented;
  696. XmlSerializer xs = new XmlSerializer (schema.GetType ());
  697. xs.Serialize (xtw, schema);
  698. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  699. "<?xml version='1.0' encoding='utf-16'?>{0}" +
  700. "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
  701. " <xsd:attribute />{0}" +
  702. " <xsd:attributeGroup />{0}" +
  703. " <xsd:complexType />{0}" +
  704. " <xsd:notation />{0}" +
  705. " <xsd:simpleType />{0}" +
  706. " <xsd:group />{0}" +
  707. " <xsd:element />{0}" +
  708. "</xsd:schema>", Environment.NewLine), sw.ToString ());
  709. }
  710. // test xmlText //////////////////////////////////////////////////////////
  711. [Test]
  712. public void TestSerializeXmlTextAttribute()
  713. {
  714. SimpleClass simple = new SimpleClass();
  715. simple.something = "hello";
  716. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  717. XmlAttributes attr = new XmlAttributes();
  718. overrides.Add(typeof(SimpleClass), "something", attr);
  719. attr.XmlText = new XmlTextAttribute();
  720. Serialize(simple, overrides);
  721. 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");
  722. attr.XmlText = new XmlTextAttribute(typeof(string));
  723. Serialize(simple, overrides);
  724. 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");
  725. try {
  726. attr.XmlText = new XmlTextAttribute(typeof(byte[]));
  727. Serialize(simple, overrides);
  728. Assert.Fail("XmlText.Type does not match the type it serializes: this should have failed");
  729. } catch (InvalidOperationException ex) {
  730. // FIXME
  731. /*
  732. // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
  733. Assert.IsNotNull (ex.Message, "#A1");
  734. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A2");
  735. // there was an error reflecting field 'something'.
  736. Assert.IsNotNull (ex.InnerException, "#A3");
  737. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A4");
  738. Assert.IsNotNull (ex.InnerException.Message, "#A5");
  739. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A6");
  740. // the type for XmlText may not be specified for primitive types.
  741. Assert.IsNotNull (ex.InnerException.InnerException, "#A7");
  742. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A8");
  743. Assert.IsNotNull (ex.InnerException.Message, "#A9");
  744. Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A10");
  745. */
  746. }
  747. try {
  748. attr.XmlText = new XmlTextAttribute();
  749. attr.XmlText.DataType = "sometype";
  750. Serialize(simple, overrides);
  751. Assert.Fail("XmlText.DataType does not match the type it serializes: this should have failed");
  752. } catch (InvalidOperationException ex) {
  753. // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
  754. Assert.IsNotNull (ex.Message, "#B1");
  755. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B2");
  756. // there was an error reflecting field 'something'.
  757. Assert.IsNotNull (ex.InnerException, "#B3");
  758. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B4");
  759. Assert.IsNotNull (ex.InnerException.Message, "#B5");
  760. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B6");
  761. // there was an error reflecting type 'System.String'.
  762. Assert.IsNotNull (ex.InnerException.InnerException, "#B7");
  763. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B8");
  764. Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B9");
  765. Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B10");
  766. // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
  767. // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
  768. Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B11");
  769. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B12");
  770. Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B13");
  771. Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B14");
  772. } catch (NotSupportedException ex) {
  773. // FIXME: we should report InvalidOperationException
  774. }
  775. }
  776. // test xmlRoot //////////////////////////////////////////////////////////
  777. [Test]
  778. public void TestSerializeXmlRootAttribute()
  779. {
  780. // constructor override & element name
  781. XmlRootAttribute root = new XmlRootAttribute();
  782. root.ElementName = "renamed";
  783. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
  784. Serialize(simpleWithAttributes, root);
  785. Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  786. SimpleClass simple = null;
  787. root.IsNullable = false;
  788. try {
  789. Serialize(simple, root);
  790. Assert.Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
  791. } catch (NullReferenceException) {
  792. }
  793. root.IsNullable = true;
  794. try {
  795. Serialize(simple, root);
  796. Assert.Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
  797. } catch (NullReferenceException) {
  798. }
  799. simple = new SimpleClass();
  800. root.ElementName = null;
  801. root.Namespace = "some.urn";
  802. Serialize(simple, root);
  803. 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);
  804. }
  805. [Test]
  806. public void TestSerializeXmlRootAttributeOnMember()
  807. {
  808. // nested root
  809. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  810. XmlAttributes childAttr = new XmlAttributes();
  811. childAttr.XmlRoot = new XmlRootAttribute("simple");
  812. overrides.Add(typeof(SimpleClass), childAttr);
  813. XmlAttributes attr = new XmlAttributes();
  814. attr.XmlRoot = new XmlRootAttribute("simple");
  815. overrides.Add(typeof(ClassArrayContainer), attr);
  816. ClassArrayContainer container = new ClassArrayContainer();
  817. container.items = new SimpleClass[1];
  818. container.items[0] = new SimpleClass();
  819. Serialize(container, overrides);
  820. 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);
  821. // FIXME test data type
  822. }
  823. // test XmlAttribute /////////////////////////////////////////////////////
  824. [Test]
  825. public void TestSerializeXmlAttributeAttribute()
  826. {
  827. // null
  828. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  829. XmlAttributes attr = new XmlAttributes();
  830. attr.XmlAttribute = new XmlAttributeAttribute();
  831. overrides.Add(typeof(SimpleClass), "something", attr);
  832. SimpleClass simple = new SimpleClass();;
  833. Serialize(simple, overrides);
  834. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  835. // regular
  836. simple.something = "hello";
  837. Serialize(simple, overrides);
  838. 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");
  839. // AttributeName
  840. attr.XmlAttribute.AttributeName = "somethingelse";
  841. Serialize(simple, overrides);
  842. 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");
  843. // Type
  844. // FIXME this should work, shouldnt it?
  845. // attr.XmlAttribute.Type = typeof(string);
  846. // Serialize(simple, overrides);
  847. // Assert(WriterText.EndsWith(" something='hello' />"));
  848. // Namespace
  849. attr.XmlAttribute.Namespace = "some:urn";
  850. Serialize(simple, overrides);
  851. 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");
  852. // FIXME DataType
  853. // FIXME XmlSchemaForm Form
  854. // FIXME write XmlQualifiedName as attribute
  855. }
  856. // test XmlElement ///////////////////////////////////////////////////////
  857. [Test]
  858. public void TestSerializeXmlElementAttribute()
  859. {
  860. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  861. XmlAttributes attr = new XmlAttributes();
  862. XmlElementAttribute element = new XmlElementAttribute();
  863. attr.XmlElements.Add(element);
  864. overrides.Add(typeof(SimpleClass), "something", attr);
  865. // null
  866. SimpleClass simple = new SimpleClass();;
  867. Serialize(simple, overrides);
  868. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  869. // not null
  870. simple.something = "hello";
  871. Serialize(simple, overrides);
  872. 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");
  873. //ElementName
  874. element.ElementName = "saying";
  875. Serialize(simple, overrides);
  876. 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");
  877. //IsNullable
  878. element.IsNullable = false;
  879. simple.something = null;
  880. Serialize(simple, overrides);
  881. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
  882. element.IsNullable = true;
  883. simple.something = null;
  884. Serialize(simple, overrides);
  885. 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");
  886. //Namespace
  887. element.ElementName = null;
  888. element.IsNullable = false;
  889. element.Namespace = "some:urn";
  890. simple.something = "hello";
  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'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6");
  893. //FIXME DataType
  894. //FIXME Form
  895. //FIXME Type
  896. }
  897. // test XmlElementAttribute with arrays and collections //////////////////
  898. [Test]
  899. public void TestSerializeCollectionWithXmlElementAttribute()
  900. {
  901. // the rule is:
  902. // if no type is specified or the specified type
  903. // matches the contents of the collection,
  904. // serialize each element in an element named after the member.
  905. // if the type does not match, or matches the collection itself,
  906. // create a base wrapping element for the member, and then
  907. // wrap each collection item in its own wrapping element based on type.
  908. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  909. XmlAttributes attr = new XmlAttributes();
  910. XmlElementAttribute element = new XmlElementAttribute();
  911. attr.XmlElements.Add(element);
  912. overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
  913. // empty collection & no type info in XmlElementAttribute
  914. StringCollectionContainer container = new StringCollectionContainer();
  915. Serialize(container, overrides);
  916. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  917. // non-empty collection & no type info in XmlElementAttribute
  918. container.Messages.Add("hello");
  919. Serialize(container, overrides);
  920. 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, "#2");
  921. // non-empty collection & only type info in XmlElementAttribute
  922. element.Type = typeof(StringCollection);
  923. Serialize(container, overrides);
  924. 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, "#3");
  925. // non-empty collection & only type info in XmlElementAttribute
  926. element.Type = typeof(string);
  927. Serialize(container, overrides);
  928. 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, "#4");
  929. // two elements
  930. container.Messages.Add("goodbye");
  931. element.Type = null;
  932. Serialize(container, overrides);
  933. 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, "#5");
  934. }
  935. // test DefaultValue /////////////////////////////////////////////////////
  936. [Test]
  937. public void TestSerializeDefaultValueAttribute()
  938. {
  939. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  940. XmlAttributes attr = new XmlAttributes();
  941. string defaultValueInstance = "nothing";
  942. attr.XmlDefaultValue = defaultValueInstance;
  943. overrides.Add(typeof(SimpleClass), "something", attr);
  944. // use the default
  945. SimpleClass simple = new SimpleClass();
  946. Serialize(simple, overrides);
  947. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  948. // same value as default
  949. simple.something = defaultValueInstance;
  950. Serialize(simple, overrides);
  951. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#2");
  952. // some other value
  953. simple.something = "hello";
  954. Serialize(simple, overrides);
  955. 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, "#3");
  956. }
  957. // test XmlEnum //////////////////////////////////////////////////////////
  958. [Test]
  959. public void TestSerializeXmlEnumAttribute()
  960. {
  961. Serialize(XmlSchemaForm.Qualified);
  962. Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
  963. Serialize(XmlSchemaForm.Unqualified);
  964. Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
  965. }
  966. [Test]
  967. public void TestSerializeXmlEnumAttribute_IgnoredValue ()
  968. {
  969. // technically XmlSchemaForm.None has an XmlIgnore attribute,
  970. // but it is not being serialized as a member.
  971. #if NET_2_0
  972. try {
  973. Serialize (XmlSchemaForm.None);
  974. Assert.Fail ("#1");
  975. } catch (InvalidOperationException ex) {
  976. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  977. Assert.IsNotNull (ex.InnerException, "#3");
  978. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  979. Assert.IsNotNull (ex.InnerException.Message, "#5");
  980. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
  981. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
  982. }
  983. #else
  984. Serialize (XmlSchemaForm.None);
  985. Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
  986. #endif
  987. }
  988. [Test]
  989. public void TestSerializeXmlNodeArray ()
  990. {
  991. XmlDocument doc = new XmlDocument ();
  992. Serialize (new XmlNode [] { doc.CreateAttribute("at"), doc.CreateElement("elem1"), doc.CreateElement("elem2") }, typeof(object));
  993. Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  994. }
  995. [Test]
  996. public void TestSerializeXmlElement ()
  997. {
  998. XmlDocument doc = new XmlDocument ();
  999. Serialize (doc.CreateElement("elem"), typeof(XmlElement));
  1000. Assert.AreEqual (Infoset ("<elem/>"), WriterText);
  1001. }
  1002. [Test]
  1003. public void TestSerializeXmlElementSubclass ()
  1004. {
  1005. XmlDocument doc = new XmlDocument ();
  1006. Serialize (new MyElem (doc), typeof(XmlElement));
  1007. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
  1008. Serialize (new MyElem (doc), typeof(MyElem));
  1009. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
  1010. }
  1011. [Test]
  1012. public void TestSerializeXmlCDataSection ()
  1013. {
  1014. XmlDocument doc = new XmlDocument ();
  1015. CDataContainer c = new CDataContainer ();
  1016. c.cdata = doc.CreateCDataSection("data section contents");
  1017. Serialize (c);
  1018. 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);
  1019. }
  1020. [Test]
  1021. public void TestSerializeXmlNode ()
  1022. {
  1023. XmlDocument doc = new XmlDocument ();
  1024. NodeContainer c = new NodeContainer ();
  1025. c.node = doc.CreateTextNode("text");
  1026. Serialize (c);
  1027. 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);
  1028. }
  1029. [Test]
  1030. public void TestSerializeChoice ()
  1031. {
  1032. Choices ch = new Choices ();
  1033. ch.MyChoice = "choice text";
  1034. ch.ItemType = ItemChoiceType.ChoiceZero;
  1035. Serialize (ch);
  1036. 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, "#1");
  1037. ch.ItemType = ItemChoiceType.StrangeOne;
  1038. Serialize (ch);
  1039. 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, "#2");
  1040. ch.ItemType = ItemChoiceType.ChoiceTwo;
  1041. Serialize (ch);
  1042. 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, "#3");
  1043. }
  1044. [Test]
  1045. public void TestSerializeNamesWithSpaces ()
  1046. {
  1047. TestSpace ts = new TestSpace();
  1048. ts.elem = 4;
  1049. ts.attr = 5;
  1050. Serialize (ts);
  1051. 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);
  1052. }
  1053. [Test]
  1054. public void TestSerializeReadOnlyProps ()
  1055. {
  1056. ReadOnlyProperties ts = new ReadOnlyProperties();
  1057. Serialize (ts);
  1058. Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1059. }
  1060. [Test]
  1061. public void TestSerializeIList()
  1062. {
  1063. clsPerson k = new clsPerson();
  1064. k.EmailAccounts = new ArrayList();
  1065. k.EmailAccounts.Add("a");
  1066. k.EmailAccounts.Add("b");
  1067. Serialize (k);
  1068. 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);
  1069. }
  1070. [Test]
  1071. public void TestSerializeArrayEnc ()
  1072. {
  1073. SoapReflectionImporter imp = new SoapReflectionImporter ();
  1074. XmlTypeMapping map = imp.ImportTypeMapping (typeof(ArrayClass));
  1075. XmlSerializer ser = new XmlSerializer (map);
  1076. StringWriter sw = new StringWriter ();
  1077. XmlTextWriter tw = new XmlTextWriter (sw);
  1078. tw.WriteStartElement ("aa");
  1079. ser.Serialize (tw, new ArrayClass ());
  1080. tw.WriteEndElement ();
  1081. }
  1082. [Test]
  1083. public void TestIncludeType()
  1084. {
  1085. // Test for bug #76049
  1086. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1087. XmlTypeMapping map = imp.ImportTypeMapping (typeof(object));
  1088. imp.IncludeType (typeof(TestSpace));
  1089. XmlSerializer ser = new XmlSerializer (map);
  1090. ser.Serialize (new StringWriter (), new TestSpace ());
  1091. }
  1092. [Test]
  1093. public void TestSerializeChoiceArray()
  1094. {
  1095. CompositeValueType v = new CompositeValueType ();
  1096. v.Init ();
  1097. Serialize (v);
  1098. 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);
  1099. }
  1100. [Test]
  1101. public void TestArrayAttributeWithDataType ()
  1102. {
  1103. Serialize (new ArrayAttributeWithType ());
  1104. string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
  1105. res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
  1106. Assert.AreEqual (Infoset (res), WriterText);
  1107. }
  1108. [Test]
  1109. public void TestSubclassElementType ()
  1110. {
  1111. SubclassTestContainer c = new SubclassTestContainer ();
  1112. c.data = new SubclassTestSub ();
  1113. Serialize (c);
  1114. string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
  1115. res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
  1116. Assert.AreEqual (Infoset (res), WriterText);
  1117. }
  1118. [Test]
  1119. [ExpectedException (typeof(InvalidOperationException))]
  1120. public void TestArrayAttributeWithWrongDataType ()
  1121. {
  1122. Serialize (new ArrayAttributeWithWrongType ());
  1123. }
  1124. [Test]
  1125. [Category ("NotWorking")]
  1126. public void TestSerializePrimitiveTypesContainer ()
  1127. {
  1128. Serialize (new PrimitiveTypesContainer ());
  1129. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1130. "<?xml version='1.0' encoding='utf-16'?>" +
  1131. #if NET_2_0
  1132. "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
  1133. #else
  1134. "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
  1135. #endif
  1136. "<Number>2004</Number>" +
  1137. "<Name>some name</Name>" +
  1138. "<Index>56</Index>" +
  1139. "<Password>8w8=</Password>" +
  1140. "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
  1141. "</PrimitiveTypesContainer>", XmlSchemaNamespace,
  1142. XmlSchemaInstanceNamespace), sw.ToString (), "#1");
  1143. SerializeEncoded (new PrimitiveTypesContainer ());
  1144. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1145. "<?xml version='1.0' encoding='utf-16'?>" +
  1146. #if NET_2_0
  1147. "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
  1148. #else
  1149. "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
  1150. #endif
  1151. "<Number xsi:type='xsd:int'>2004</Number>" +
  1152. "<Name xsi:type='xsd:string'>some name</Name>" +
  1153. "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
  1154. "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
  1155. "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
  1156. "</q1:PrimitiveTypesContainer>", XmlSchemaNamespace,
  1157. XmlSchemaInstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
  1158. sw.ToString (), "#2");
  1159. }
  1160. [Test]
  1161. public void TestSchemaForm ()
  1162. {
  1163. TestSchemaForm1 t1 = new TestSchemaForm1 ();
  1164. t1.p1 = new PrintTypeResponse ();
  1165. t1.p1.Init ();
  1166. t1.p2 = new PrintTypeResponse ();
  1167. t1.p2.Init ();
  1168. TestSchemaForm2 t2 = new TestSchemaForm2 ();
  1169. t2.p1 = new PrintTypeResponse ();
  1170. t2.p1.Init ();
  1171. t2.p2 = new PrintTypeResponse ();
  1172. t2.p2.Init ();
  1173. Serialize (t1);
  1174. string res = "";
  1175. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1176. res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  1177. res += " <p1>";
  1178. res += " <result>";
  1179. res += " <data>data1</data>";
  1180. res += " </result>";
  1181. res += " <intern xmlns=\"urn:responseTypes\">";
  1182. res += " <result xmlns=\"\">";
  1183. res += " <data>data2</data>";
  1184. res += " </result>";
  1185. res += " </intern>";
  1186. res += " </p1>";
  1187. res += " <p2 xmlns=\"urn:oo\">";
  1188. res += " <result xmlns=\"\">";
  1189. res += " <data>data1</data>";
  1190. res += " </result>";
  1191. res += " <intern xmlns=\"urn:responseTypes\">";
  1192. res += " <result xmlns=\"\">";
  1193. res += " <data>data2</data>";
  1194. res += " </result>";
  1195. res += " </intern>";
  1196. res += " </p2>";
  1197. res += "</TestSchemaForm1>";
  1198. Assert.AreEqual (Infoset (res), WriterText);
  1199. Serialize (t2);
  1200. res = "";
  1201. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1202. res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  1203. res += " <p1 xmlns=\"urn:testForm\">";
  1204. res += " <result xmlns=\"\">";
  1205. res += " <data>data1</data>";
  1206. res += " </result>";
  1207. res += " <intern xmlns=\"urn:responseTypes\">";
  1208. res += " <result xmlns=\"\">";
  1209. res += " <data>data2</data>";
  1210. res += " </result>";
  1211. res += " </intern>";
  1212. res += " </p1>";
  1213. res += " <p2 xmlns=\"urn:oo\">";
  1214. res += " <result xmlns=\"\">";
  1215. res += " <data>data1</data>";
  1216. res += " </result>";
  1217. res += " <intern xmlns=\"urn:responseTypes\">";
  1218. res += " <result xmlns=\"\">";
  1219. res += " <data>data2</data>";
  1220. res += " </result>";
  1221. res += " </intern>";
  1222. res += " </p2>";
  1223. res += "</TestSchemaForm2>";
  1224. Assert.AreEqual (Infoset (res), WriterText);
  1225. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1226. XmlTypeMapping map = imp.ImportTypeMapping (typeof(TestSchemaForm1), "urn:extra");
  1227. Serialize (t1, map);
  1228. res = "";
  1229. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1230. res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
  1231. res += " <p1>";
  1232. res += " <result xmlns=\"\">";
  1233. res += " <data>data1</data>";
  1234. res += " </result>";
  1235. res += " <intern xmlns=\"urn:responseTypes\">";
  1236. res += " <result xmlns=\"\">";
  1237. res += " <data>data2</data>";
  1238. res += " </result>";
  1239. res += " </intern>";
  1240. res += " </p1>";
  1241. res += " <p2 xmlns=\"urn:oo\">";
  1242. res += " <result xmlns=\"\">";
  1243. res += " <data>data1</data>";
  1244. res += " </result>";
  1245. res += " <intern xmlns=\"urn:responseTypes\">";
  1246. res += " <result xmlns=\"\">";
  1247. res += " <data>data2</data>";
  1248. res += " </result>";
  1249. res += " </intern>";
  1250. res += " </p2>";
  1251. res += "</TestSchemaForm1>";
  1252. Assert.AreEqual (Infoset (res), WriterText);
  1253. imp = new XmlReflectionImporter ();
  1254. map = imp.ImportTypeMapping (typeof(TestSchemaForm2), "urn:extra");
  1255. Serialize (t2, map);
  1256. res = "";
  1257. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1258. res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
  1259. res += " <p1 xmlns=\"urn:testForm\">";
  1260. res += " <result xmlns=\"\">";
  1261. res += " <data>data1</data>";
  1262. res += " </result>";
  1263. res += " <intern xmlns=\"urn:responseTypes\">";
  1264. res += " <result xmlns=\"\">";
  1265. res += " <data>data2</data>";
  1266. res += " </result>";
  1267. res += " </intern>";
  1268. res += " </p1>";
  1269. res += " <p2 xmlns=\"urn:oo\">";
  1270. res += " <result xmlns=\"\">";
  1271. res += " <data>data1</data>";
  1272. res += " </result>";
  1273. res += " <intern xmlns=\"urn:responseTypes\">";
  1274. res += " <result xmlns=\"\">";
  1275. res += " <data>data2</data>";
  1276. res += " </result>";
  1277. res += " </intern>";
  1278. res += " </p2>";
  1279. res += "</TestSchemaForm2>";
  1280. Assert.AreEqual (Infoset (res), WriterText);
  1281. }
  1282. // Helper methods
  1283. public static string Infoset (string sx)
  1284. {
  1285. XmlDocument doc = new XmlDocument ();
  1286. doc.LoadXml (sx);
  1287. StringBuilder sb = new StringBuilder ();
  1288. GetInfoset (doc.DocumentElement, sb);
  1289. return sb.ToString ();
  1290. }
  1291. public static string Infoset (XmlNode nod)
  1292. {
  1293. StringBuilder sb = new StringBuilder ();
  1294. GetInfoset (nod, sb);
  1295. return sb.ToString ();
  1296. }
  1297. static void GetInfoset (XmlNode nod, StringBuilder sb)
  1298. {
  1299. switch (nod.NodeType)
  1300. {
  1301. case XmlNodeType.Attribute:
  1302. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  1303. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  1304. break;
  1305. case XmlNodeType.Element:
  1306. XmlElement elem = (XmlElement) nod;
  1307. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  1308. ArrayList ats = new ArrayList ();
  1309. foreach (XmlAttribute at in elem.Attributes)
  1310. ats.Add (at.LocalName + " " + at.NamespaceURI);
  1311. ats.Sort ();
  1312. foreach (string name in ats)
  1313. {
  1314. string[] nn = name.Split (' ');
  1315. GetInfoset (elem.Attributes[nn[0],nn[1]], sb);
  1316. }
  1317. sb.Append (">");
  1318. foreach (XmlNode cn in elem.ChildNodes)
  1319. GetInfoset (cn, sb);
  1320. sb.Append ("</>");
  1321. break;
  1322. default:
  1323. sb.Append (nod.OuterXml);
  1324. break;
  1325. }
  1326. }
  1327. static XmlTypeMapping CreateSoapMapping (Type type)
  1328. {
  1329. SoapReflectionImporter importer = new SoapReflectionImporter ();
  1330. return importer.ImportTypeMapping (type);
  1331. }
  1332. }
  1333. }