XmlSerializerTests.cs 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768
  1. //
  2. // System.Xml.XmlSerializerTests
  3. //
  4. // Author:
  5. // Erik LeBel <[email protected]>
  6. // Hagit Yidov <[email protected]>
  7. //
  8. // (C) 2003 Erik LeBel
  9. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  10. //
  11. //
  12. // NOTES:
  13. // Where possible, these tests avoid testing the order of
  14. // an object's members serialization. Mono and .NET do not
  15. // reflect members in the same order.
  16. //
  17. // Only serializations tests so far, no deserialization.
  18. //
  19. // FIXME
  20. // test XmlArrayAttribute
  21. // test XmlArrayItemAttribute
  22. // test serialization of decimal type
  23. // test serialization of Guid type
  24. // test XmlNode serialization with and without modifying attributes.
  25. // test deserialization
  26. // FIXMEs found in this file
  27. using System;
  28. using System.Collections;
  29. using System.Globalization;
  30. using System.IO;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using System.Xml.Serialization;
  35. #if NET_2_0
  36. using System.Collections.Generic;
  37. #endif
  38. using NUnit.Framework;
  39. using MonoTests.System.Xml.TestClasses;
  40. namespace MonoTests.System.XmlSerialization
  41. {
  42. [TestFixture]
  43. public class XmlSerializerTests
  44. {
  45. const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  46. const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
  47. const string ANamespace = "some:urn";
  48. const string AnotherNamespace = "another:urn";
  49. StringWriter sw;
  50. XmlTextWriter xtw;
  51. XmlSerializer xs;
  52. private void SetUpWriter ()
  53. {
  54. sw = new StringWriter ();
  55. xtw = new XmlTextWriter (sw);
  56. xtw.QuoteChar = '\'';
  57. xtw.Formatting = Formatting.None;
  58. }
  59. private string WriterText
  60. {
  61. get
  62. {
  63. string val = sw.GetStringBuilder ().ToString ();
  64. int offset = val.IndexOf ('>') + 1;
  65. val = val.Substring (offset);
  66. return Infoset (val);
  67. }
  68. }
  69. private void Serialize (object o)
  70. {
  71. SetUpWriter ();
  72. xs = new XmlSerializer (o.GetType ());
  73. xs.Serialize (xtw, o);
  74. }
  75. private void Serialize (object o, Type type)
  76. {
  77. SetUpWriter ();
  78. xs = new XmlSerializer (type);
  79. xs.Serialize (xtw, o);
  80. }
  81. private void Serialize (object o, XmlSerializerNamespaces ns)
  82. {
  83. SetUpWriter ();
  84. xs = new XmlSerializer (o.GetType ());
  85. xs.Serialize (xtw, o, ns);
  86. }
  87. private void Serialize (object o, XmlAttributeOverrides ao)
  88. {
  89. SetUpWriter ();
  90. xs = new XmlSerializer (o.GetType (), ao);
  91. xs.Serialize (xtw, o);
  92. }
  93. private void Serialize (object o, XmlAttributeOverrides ao, string defaultNamespace)
  94. {
  95. SetUpWriter ();
  96. xs = new XmlSerializer (o.GetType (), ao, Type.EmptyTypes,
  97. (XmlRootAttribute) null, defaultNamespace);
  98. xs.Serialize (xtw, o);
  99. }
  100. private void Serialize (object o, XmlRootAttribute root)
  101. {
  102. SetUpWriter ();
  103. xs = new XmlSerializer (o.GetType (), root);
  104. xs.Serialize (xtw, o);
  105. }
  106. private void Serialize (object o, XmlTypeMapping typeMapping)
  107. {
  108. SetUpWriter ();
  109. xs = new XmlSerializer (typeMapping);
  110. xs.Serialize (xtw, o);
  111. }
  112. private void SerializeEncoded (object o)
  113. {
  114. SerializeEncoded (o, o.GetType ());
  115. }
  116. private void SerializeEncoded (object o, SoapAttributeOverrides ao)
  117. {
  118. XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao);
  119. SetUpWriter ();
  120. xs = new XmlSerializer (mapping);
  121. xs.Serialize (xtw, o);
  122. }
  123. private void SerializeEncoded (object o, SoapAttributeOverrides ao, string defaultNamespace)
  124. {
  125. XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao, defaultNamespace);
  126. SetUpWriter ();
  127. xs = new XmlSerializer (mapping);
  128. xs.Serialize (xtw, o);
  129. }
  130. private void SerializeEncoded (object o, Type type)
  131. {
  132. XmlTypeMapping mapping = CreateSoapMapping (type);
  133. SetUpWriter ();
  134. xs = new XmlSerializer (mapping);
  135. xs.Serialize (xtw, o);
  136. }
  137. private void SerializeEncoded (XmlTextWriter xtw, object o, Type type)
  138. {
  139. XmlTypeMapping mapping = CreateSoapMapping (type);
  140. xs = new XmlSerializer (mapping);
  141. xs.Serialize (xtw, o);
  142. }
  143. // test constructors
  144. #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
  145. [Test]
  146. public void TestConstructor()
  147. {
  148. XmlSerializer ser = new XmlSerializer (null, "");
  149. }
  150. #else
  151. #endif
  152. // test basic types ////////////////////////////////////////////////////////
  153. [Test]
  154. public void TestSerializeInt ()
  155. {
  156. Serialize (10);
  157. Assert.AreEqual (Infoset ("<int>10</int>"), WriterText);
  158. }
  159. [Test]
  160. public void TestSerializeBool ()
  161. {
  162. Serialize (true);
  163. Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
  164. Serialize (false);
  165. Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
  166. }
  167. [Test]
  168. public void TestSerializeString ()
  169. {
  170. Serialize ("hello");
  171. Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
  172. }
  173. [Test]
  174. public void TestSerializeEmptyString ()
  175. {
  176. Serialize (String.Empty);
  177. Assert.AreEqual (Infoset ("<string />"), WriterText);
  178. }
  179. [Test]
  180. public void TestSerializeNullObject ()
  181. {
  182. Serialize (null, typeof (object));
  183. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  184. "<anyType xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
  185. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  186. }
  187. [Test]
  188. [Ignore ("The generated XML is not exact but it is equivalent")]
  189. public void TestSerializeNullString ()
  190. {
  191. Serialize (null, typeof (string));
  192. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  193. "<string xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
  194. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  195. }
  196. [Test]
  197. public void TestSerializeIntArray ()
  198. {
  199. Serialize (new int[] { 1, 2, 3, 4 });
  200. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  201. "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>",
  202. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  203. }
  204. [Test]
  205. public void TestSerializeEmptyArray ()
  206. {
  207. Serialize (new int[] { });
  208. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  209. "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}' />",
  210. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  211. }
  212. [Test]
  213. #if TARGET_JVM
  214. [Ignore ("TD #7464")]
  215. #endif
  216. public void TestSerializeChar ()
  217. {
  218. Serialize ('A');
  219. Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
  220. Serialize ('\0');
  221. Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
  222. Serialize ('\n');
  223. Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
  224. Serialize ('\uFF01');
  225. Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
  226. }
  227. [Test]
  228. public void TestSerializeFloat ()
  229. {
  230. Serialize (10.78);
  231. Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
  232. Serialize (-1e8);
  233. Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
  234. // FIXME test INF and other boundary conditions that may exist with floats
  235. }
  236. [Test]
  237. public void TestSerializeEnumeration_FromValue ()
  238. {
  239. Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  240. Assert.AreEqual (
  241. "<?xml version='1.0' encoding='utf-16'?>" +
  242. "<SimpleEnumeration>SECOND</SimpleEnumeration>",
  243. sw.ToString ());
  244. }
  245. [Test]
  246. [Category ("NotWorking")]
  247. public void TestSerializeEnumeration_FromValue_Encoded ()
  248. {
  249. SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  250. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  251. "<?xml version='1.0' encoding='utf-16'?>" +
  252. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  253. XmlSchema.InstanceNamespace), sw.ToString ());
  254. }
  255. [Test]
  256. public void TestSerializeEnumeration ()
  257. {
  258. Serialize (SimpleEnumeration.FIRST);
  259. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
  260. Serialize (SimpleEnumeration.SECOND);
  261. Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
  262. }
  263. [Test]
  264. public void TestSerializeEnumeration_Encoded ()
  265. {
  266. SerializeEncoded (SimpleEnumeration.FIRST);
  267. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  268. "<?xml version='1.0' encoding='utf-16'?>" +
  269. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  270. XmlSchema.InstanceNamespace), sw.ToString (), "#B1");
  271. SerializeEncoded (SimpleEnumeration.SECOND);
  272. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  273. "<?xml version='1.0' encoding='utf-16'?>" +
  274. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  275. XmlSchema.InstanceNamespace), sw.ToString (), "#B2");
  276. }
  277. [Test]
  278. public void TestSerializeEnumDefaultValue ()
  279. {
  280. Serialize (new EnumDefaultValue ());
  281. Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
  282. Serialize (new SimpleEnumeration ());
  283. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
  284. Serialize (3, typeof (EnumDefaultValue));
  285. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
  286. Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  287. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
  288. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  289. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
  290. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  291. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
  292. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  293. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
  294. Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  295. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
  296. Serialize (3, typeof (FlagEnum));
  297. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
  298. Serialize (5, typeof (FlagEnum));
  299. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
  300. Serialize (FlagEnum.e4, typeof (FlagEnum));
  301. Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
  302. Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  303. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
  304. Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  305. Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
  306. Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  307. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
  308. Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  309. Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
  310. Serialize (3, typeof (EnumDefaultValueNF));
  311. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
  312. Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  313. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
  314. Serialize (2, typeof (ZeroFlagEnum));
  315. Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t&lt;w&gt;o</ZeroFlagEnum>"), WriterText, "#18");
  316. Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
  317. Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
  318. }
  319. [Test]
  320. [Category ("NotWorking")]
  321. public void TestSerializeEnumDefaultValue_Encoded ()
  322. {
  323. SerializeEncoded (new EnumDefaultValue ());
  324. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  325. "<?xml version='1.0' encoding='utf-16'?>" +
  326. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
  327. XmlSchema.InstanceNamespace), sw.ToString (), "#1");
  328. SerializeEncoded (new SimpleEnumeration ());
  329. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  330. "<?xml version='1.0' encoding='utf-16'?>" +
  331. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  332. XmlSchema.InstanceNamespace), sw.ToString (), "#2");
  333. SerializeEncoded (3, typeof (EnumDefaultValue));
  334. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  335. "<?xml version='1.0' encoding='utf-16'?>" +
  336. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  337. XmlSchema.InstanceNamespace), sw.ToString (), "#3");
  338. SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  339. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  340. "<?xml version='1.0' encoding='utf-16'?>" +
  341. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  342. XmlSchema.InstanceNamespace), sw.ToString (), "#4");
  343. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  344. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  345. "<?xml version='1.0' encoding='utf-16'?>" +
  346. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  347. XmlSchema.InstanceNamespace), sw.ToString (), "#5");
  348. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  349. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  350. "<?xml version='1.0' encoding='utf-16'?>" +
  351. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  352. XmlSchema.InstanceNamespace), sw.ToString (), "#6");
  353. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  354. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  355. "<?xml version='1.0' encoding='utf-16'?>" +
  356. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  357. XmlSchema.InstanceNamespace), sw.ToString (), "#7");
  358. SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  359. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  360. "<?xml version='1.0' encoding='utf-16'?>" +
  361. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  362. XmlSchema.InstanceNamespace), sw.ToString (), "#8");
  363. SerializeEncoded (3, typeof (FlagEnum));
  364. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  365. "<?xml version='1.0' encoding='utf-16'?>" +
  366. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
  367. XmlSchema.InstanceNamespace), sw.ToString (), "#9");
  368. SerializeEncoded (5, typeof (FlagEnum));
  369. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  370. "<?xml version='1.0' encoding='utf-16'?>" +
  371. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
  372. XmlSchema.InstanceNamespace), sw.ToString (), "#10");
  373. SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
  374. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  375. "<?xml version='1.0' encoding='utf-16'?>" +
  376. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
  377. XmlSchema.InstanceNamespace), sw.ToString (), "#11");
  378. SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  379. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  380. "<?xml version='1.0' encoding='utf-16'?>" +
  381. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
  382. XmlSchema.InstanceNamespace), sw.ToString (), "#12");
  383. SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  384. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  385. "<?xml version='1.0' encoding='utf-16'?>" +
  386. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
  387. XmlSchema.InstanceNamespace), sw.ToString (), "#13");
  388. SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  389. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  390. "<?xml version='1.0' encoding='utf-16'?>" +
  391. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
  392. XmlSchema.InstanceNamespace), sw.ToString (), "#14");
  393. SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  394. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  395. "<?xml version='1.0' encoding='utf-16'?>" +
  396. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
  397. XmlSchema.InstanceNamespace), sw.ToString (), "#15");
  398. SerializeEncoded (3, typeof (EnumDefaultValueNF));
  399. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  400. "<?xml version='1.0' encoding='utf-16'?>" +
  401. "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
  402. XmlSchema.InstanceNamespace), sw.ToString (), "#16");
  403. SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  404. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  405. "<?xml version='1.0' encoding='utf-16'?>" +
  406. "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
  407. XmlSchema.InstanceNamespace), sw.ToString (), "#17");
  408. SerializeEncoded (2, typeof (ZeroFlagEnum));
  409. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  410. "<?xml version='1.0' encoding='utf-16'?>" +
  411. "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
  412. XmlSchema.InstanceNamespace), sw.ToString (), "#18");
  413. SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
  414. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  415. "<?xml version='1.0' encoding='utf-16'?>" +
  416. "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
  417. XmlSchema.InstanceNamespace), sw.ToString (), "#19");
  418. }
  419. [Test]
  420. public void TestSerializeEnumDefaultValue_InvalidValue1 ()
  421. {
  422. try {
  423. Serialize ("b", typeof (EnumDefaultValue));
  424. Assert.Fail ("#A1");
  425. } catch (InvalidOperationException ex) {
  426. Assert.IsNotNull (ex.InnerException, "#A2");
  427. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
  428. }
  429. try {
  430. Serialize ("e1", typeof (EnumDefaultValue));
  431. Assert.Fail ("#B1");
  432. } catch (InvalidOperationException ex) {
  433. Assert.IsNotNull (ex.InnerException, "#B2");
  434. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
  435. }
  436. try {
  437. Serialize ("e1,e2", typeof (EnumDefaultValue));
  438. Assert.Fail ("#C1");
  439. } catch (InvalidOperationException ex) {
  440. Assert.IsNotNull (ex.InnerException, "#C2");
  441. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
  442. }
  443. try {
  444. Serialize (string.Empty, typeof (EnumDefaultValue));
  445. Assert.Fail ("#D1");
  446. } catch (InvalidOperationException ex) {
  447. Assert.IsNotNull (ex.InnerException, "#D2");
  448. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
  449. }
  450. try {
  451. Serialize ("1", typeof (EnumDefaultValue));
  452. Assert.Fail ("#E1");
  453. } catch (InvalidOperationException ex) {
  454. Assert.IsNotNull (ex.InnerException, "#E2");
  455. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
  456. }
  457. try {
  458. Serialize ("0", typeof (EnumDefaultValue));
  459. Assert.Fail ("#F1");
  460. } catch (InvalidOperationException ex) {
  461. Assert.IsNotNull (ex.InnerException, "#F2");
  462. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
  463. }
  464. try {
  465. Serialize (new SimpleClass (), typeof (EnumDefaultValue));
  466. Assert.Fail ("#G1");
  467. } catch (InvalidOperationException ex) {
  468. Assert.IsNotNull (ex.InnerException, "#G2");
  469. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
  470. }
  471. }
  472. [Test]
  473. public void TestSerializeEnumDefaultValue_InvalidValue2 ()
  474. {
  475. #if NET_2_0
  476. try {
  477. Serialize (5, typeof (EnumDefaultValue));
  478. Assert.Fail ("#1");
  479. } catch (InvalidOperationException ex) {
  480. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  481. Assert.IsNotNull (ex.InnerException, "#3");
  482. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  483. Assert.IsNotNull (ex.InnerException.Message, "#5");
  484. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
  485. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
  486. }
  487. #else
  488. Serialize (5, typeof (EnumDefaultValue));
  489. Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
  490. #endif
  491. }
  492. [Test]
  493. public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
  494. {
  495. #if NET_2_0
  496. try {
  497. Serialize (new EnumDefaultValueNF ());
  498. Assert.Fail ("#1");
  499. } catch (InvalidOperationException ex) {
  500. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  501. Assert.IsNotNull (ex.InnerException, "#3");
  502. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  503. Assert.IsNotNull (ex.InnerException.Message, "#5");
  504. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
  505. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
  506. }
  507. #else
  508. Serialize (new EnumDefaultValueNF ());
  509. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
  510. #endif
  511. }
  512. [Test]
  513. public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
  514. {
  515. #if NET_2_0
  516. try {
  517. Serialize (15, typeof (EnumDefaultValueNF));
  518. Assert.Fail ("#1");
  519. } catch (InvalidOperationException ex) {
  520. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  521. Assert.IsNotNull (ex.InnerException, "#3");
  522. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  523. Assert.IsNotNull (ex.InnerException.Message, "#5");
  524. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
  525. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
  526. }
  527. #else
  528. Serialize (15, typeof (EnumDefaultValueNF));
  529. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
  530. #endif
  531. }
  532. [Test]
  533. public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
  534. {
  535. try {
  536. Serialize ("b", typeof (EnumDefaultValueNF));
  537. Assert.Fail ("#A1");
  538. } catch (InvalidOperationException ex) {
  539. Assert.IsNotNull (ex.InnerException, "#A2");
  540. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
  541. }
  542. try {
  543. Serialize ("e2", typeof (EnumDefaultValueNF));
  544. Assert.Fail ("#B1");
  545. } catch (InvalidOperationException ex) {
  546. Assert.IsNotNull (ex.InnerException, "#B2");
  547. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
  548. }
  549. try {
  550. Serialize (string.Empty, typeof (EnumDefaultValueNF));
  551. Assert.Fail ("#C1");
  552. } catch (InvalidOperationException ex) {
  553. Assert.IsNotNull (ex.InnerException, "#C2");
  554. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
  555. }
  556. try {
  557. Serialize ("1", typeof (EnumDefaultValueNF));
  558. Assert.Fail ("#D1");
  559. } catch (InvalidOperationException ex) {
  560. Assert.IsNotNull (ex.InnerException, "#D2");
  561. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
  562. }
  563. try {
  564. Serialize ("0", typeof (EnumDefaultValueNF));
  565. Assert.Fail ("#E1");
  566. } catch (InvalidOperationException ex) {
  567. Assert.IsNotNull (ex.InnerException, "#E2");
  568. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
  569. }
  570. }
  571. [Test]
  572. public void TestSerializeField ()
  573. {
  574. Field f = new Field ();
  575. Serialize (f, typeof (Field));
  576. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  577. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag3=''" +
  578. " flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
  579. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#A");
  580. f.Flags1 = FlagEnum.e1;
  581. f.Flags2 = FlagEnum.e1;
  582. f.Flags3 = FlagEnum.e2;
  583. f.Modifiers = MapModifiers.Protected;
  584. f.Modifiers2 = MapModifiers.Public;
  585. f.Modifiers3 = MapModifiers.Public;
  586. f.Modifiers4 = MapModifiers.Protected;
  587. f.Modifiers5 = MapModifiers.Public;
  588. Serialize (f, typeof (Field));
  589. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  590. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4=''" +
  591. " modifiers='protected' modifiers2='public' />",
  592. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#B");
  593. f.Flags1 = (FlagEnum) 1;
  594. f.Flags1 = FlagEnum.e2;
  595. f.Flags2 = FlagEnum.e2;
  596. f.Flags3 = FlagEnum.e1 | FlagEnum.e2;
  597. f.Modifiers = MapModifiers.Public;
  598. f.Modifiers2 = MapModifiers.Protected;
  599. f.Modifiers3 = MapModifiers.Protected;
  600. f.Modifiers4 = MapModifiers.Public;
  601. f.Modifiers5 = MapModifiers.Protected;
  602. Serialize (f, typeof (Field));
  603. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  604. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two'" +
  605. " flag4='' modifiers='public' modifiers2='protected'" +
  606. " modifiers3='protected' modifiers4='public'" +
  607. " modifiers5='protected' />",
  608. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C");
  609. f.Flags1 = FlagEnum.e1 | FlagEnum.e2;
  610. f.Flags2 = FlagEnum.e2;
  611. f.Flags3 = FlagEnum.e4;
  612. f.Flags4 = FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4;
  613. f.Modifiers3 = MapModifiers.Public;
  614. f.Modifiers4 = MapModifiers.Protected;
  615. f.Modifiers5 = MapModifiers.Public;
  616. f.Names = new string[] { "a", "b" };
  617. Serialize (f, typeof (Field));
  618. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  619. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='two'" +
  620. " flag3='four' flag4='one two four' modifiers='public'" +
  621. " modifiers2='protected' names='a b' />",
  622. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#D");
  623. f.Flags2 = (FlagEnum) 444;
  624. f.Flags3 = (FlagEnum) 555;
  625. f.Modifiers = (MapModifiers) 666;
  626. f.Modifiers2 = (MapModifiers) 777;
  627. f.Modifiers3 = (MapModifiers) 0;
  628. f.Modifiers4 = (MapModifiers) 888;
  629. f.Modifiers5 = (MapModifiers) 999;
  630. #if NET_2_0
  631. try {
  632. Serialize (f, typeof (Field));
  633. Assert.Fail ("#E1");
  634. } catch (InvalidOperationException ex) {
  635. // There was an error generating the XML document
  636. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
  637. Assert.IsNotNull (ex.Message, "#E3");
  638. Assert.IsNotNull (ex.InnerException, "#E4");
  639. // Instance validation error: '444' is not a valid value for
  640. // MonoTests.System.Xml.TestClasses.FlagEnum
  641. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
  642. Assert.IsNotNull (ex.InnerException.Message, "#E6");
  643. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#E7");
  644. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
  645. Assert.IsNull (ex.InnerException.InnerException, "#E9");
  646. }
  647. #else
  648. Serialize (f, typeof (Field));
  649. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  650. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='444'" +
  651. " flag3='555' flag4='one two four' modifiers='666' modifiers2='777'" +
  652. " modifiers4='888' modifiers5='999' names='a b' />",
  653. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#E");
  654. #endif
  655. }
  656. [Test]
  657. [Category ("NotDotNet")] // MS bug
  658. #if TARGET_JVM
  659. [Ignore ("TD #7458")]
  660. #endif
  661. public void TestSerializeField_Encoded ()
  662. {
  663. Field_Encoded f = new Field_Encoded ();
  664. SerializeEncoded (f, typeof (Field_Encoded));
  665. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  666. "<?xml version='1.0' encoding='utf-16'?>" +
  667. #if NET_2_0
  668. "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1=''" +
  669. " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
  670. " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
  671. #else
  672. "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
  673. " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
  674. " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
  675. #endif
  676. XmlSchema.Namespace, XmlSchema.InstanceNamespace),
  677. sw.GetStringBuilder ().ToString (), "#A");
  678. f.Flags1 = FlagEnum_Encoded.e1;
  679. f.Flags2 = FlagEnum_Encoded.e1;
  680. f.Flags3 = FlagEnum_Encoded.e2;
  681. f.Modifiers = MapModifiers.Protected;
  682. f.Modifiers2 = MapModifiers.Public;
  683. f.Modifiers3 = MapModifiers.Public;
  684. f.Modifiers4 = MapModifiers.Protected;
  685. f.Modifiers5 = MapModifiers.Public;
  686. SerializeEncoded (f, typeof (Field_Encoded));
  687. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  688. "<?xml version='1.0' encoding='utf-16'?>" +
  689. #if NET_2_0
  690. "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag3='two'" +
  691. " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
  692. " xmlns:q1='some:urn' />",
  693. #else
  694. "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
  695. " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
  696. " xmlns:q1='some:urn' />",
  697. #endif
  698. XmlSchema.Namespace, XmlSchema.InstanceNamespace),
  699. sw.GetStringBuilder ().ToString (), "#B");
  700. f.Flags1 = FlagEnum_Encoded.e2;
  701. f.Flags2 = FlagEnum_Encoded.e2;
  702. f.Flags3 = FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2;
  703. f.Modifiers = MapModifiers.Public;
  704. f.Modifiers2 = MapModifiers.Protected;
  705. f.Modifiers3 = MapModifiers.Protected;
  706. f.Modifiers4 = MapModifiers.Public;
  707. f.Modifiers5 = MapModifiers.Protected;
  708. SerializeEncoded (f, typeof (Field_Encoded));
  709. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  710. "<?xml version='1.0' encoding='utf-16'?>" +
  711. #if NET_2_0
  712. "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1='two'" +
  713. " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
  714. " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
  715. " xmlns:q1='some:urn' />",
  716. #else
  717. "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
  718. " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
  719. " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
  720. " xmlns:q1='some:urn' />",
  721. #endif
  722. XmlSchema.Namespace, XmlSchema.InstanceNamespace),
  723. sw.GetStringBuilder ().ToString (), "#C");
  724. f.Flags1 = (FlagEnum_Encoded) 1;
  725. f.Flags2 = (FlagEnum_Encoded) 444;
  726. f.Flags3 = (FlagEnum_Encoded) 555;
  727. f.Modifiers = (MapModifiers) 666;
  728. f.Modifiers2 = (MapModifiers) 777;
  729. f.Modifiers3 = (MapModifiers) 0;
  730. f.Modifiers4 = (MapModifiers) 888;
  731. f.Modifiers5 = (MapModifiers) 999;
  732. #if NET_2_0
  733. try {
  734. #endif
  735. SerializeEncoded (f, typeof (Field_Encoded));
  736. #if NET_2_0
  737. Assert.Fail ("#D1");
  738. } catch (InvalidOperationException ex) {
  739. // There was an error generating the XML document
  740. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
  741. Assert.IsNotNull (ex.Message, "#D3");
  742. Assert.IsNotNull (ex.InnerException, "#D4");
  743. // Instance validation error: '444' is not a valid value for
  744. // MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
  745. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
  746. Assert.IsNotNull (ex.InnerException.Message, "#D6");
  747. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
  748. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
  749. Assert.IsNull (ex.InnerException.InnerException, "#D9");
  750. }
  751. #else
  752. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  753. "<?xml version='1.0' encoding='utf-16'?>" +
  754. "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag2='444'" +
  755. " flag3='555' flag4='' modifiers='666' modifiers2='777'" +
  756. " modifiers4='888' modifiers5='999' xmlns:q1='some:urn' />",
  757. XmlSchema.Namespace, XmlSchema.InstanceNamespace),
  758. sw.GetStringBuilder ().ToString (), "#D");
  759. #endif
  760. }
  761. [Test]
  762. #if TARGET_JVM
  763. [Ignore ("JVM returns fields in different order")]
  764. #endif
  765. public void TestSerializeGroup ()
  766. {
  767. Group myGroup = new Group ();
  768. myGroup.GroupName = ".NET";
  769. Byte[] hexByte = new Byte[] { 0x64, 0x32 };
  770. myGroup.GroupNumber = hexByte;
  771. DateTime myDate = new DateTime (2002, 5, 2);
  772. myGroup.Today = myDate;
  773. myGroup.PostitiveInt = "10000";
  774. myGroup.IgnoreThis = true;
  775. Car thisCar = (Car) myGroup.myCar ("1234566");
  776. myGroup.MyVehicle = thisCar;
  777. SetUpWriter ();
  778. xtw.WriteStartDocument (true);
  779. xtw.WriteStartElement ("Wrapper");
  780. SerializeEncoded (xtw, myGroup, typeof (Group));
  781. xtw.WriteEndElement ();
  782. xtw.Close ();
  783. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  784. "<Wrapper>" +
  785. "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
  786. "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
  787. "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
  788. "<MyVehicle href='#id2' />" +
  789. "</Group>" +
  790. "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
  791. "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
  792. "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
  793. "</Car>" +
  794. "</Wrapper>",
  795. XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  796. WriterText, "#1");
  797. myGroup.GroupName = null;
  798. myGroup.Grouptype = GroupType.B;
  799. myGroup.MyVehicle.licenseNumber = null;
  800. myGroup.MyVehicle.weight = "450";
  801. SetUpWriter ();
  802. xtw.WriteStartDocument (true);
  803. xtw.WriteStartElement ("Wrapper");
  804. SerializeEncoded (xtw, myGroup, typeof (Group));
  805. xtw.WriteEndElement ();
  806. xtw.Close ();
  807. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  808. "<Wrapper>" +
  809. "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
  810. "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
  811. "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
  812. "<MyVehicle href='#id2' />" +
  813. "</Group>" +
  814. "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
  815. "<makeDate xmlns:q1='{0}' d2p1:type='q1:date'>0001-01-01</makeDate>" +
  816. "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
  817. "</Car>" +
  818. "</Wrapper>",
  819. XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  820. WriterText, "#2");
  821. }
  822. [Test]
  823. public void TestSerializeZeroFlagEnum_InvalidValue ()
  824. {
  825. #if NET_2_0
  826. try {
  827. Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
  828. Assert.Fail ("#1");
  829. } catch (InvalidOperationException ex) {
  830. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  831. Assert.IsNotNull (ex.InnerException, "#3");
  832. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  833. Assert.IsNotNull (ex.InnerException.Message, "#5");
  834. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
  835. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
  836. }
  837. #else
  838. Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
  839. Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
  840. #endif
  841. }
  842. [Test]
  843. public void TestSerializeQualifiedName ()
  844. {
  845. Serialize (new XmlQualifiedName ("me", "home.urn"));
  846. Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
  847. }
  848. [Test]
  849. public void TestSerializeBytes ()
  850. {
  851. Serialize ((byte) 0xAB);
  852. Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
  853. Serialize ((byte) 15);
  854. Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
  855. }
  856. [Test]
  857. public void TestSerializeByteArrays ()
  858. {
  859. Serialize (new byte[] { });
  860. Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
  861. Serialize (new byte[] { 0xAB, 0xCD });
  862. Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
  863. }
  864. [Test]
  865. public void TestSerializeDateTime ()
  866. {
  867. DateTime d = new DateTime ();
  868. Serialize (d);
  869. TimeZone tz = TimeZone.CurrentTimeZone;
  870. TimeSpan off = tz.GetUtcOffset (d);
  871. string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
  872. Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
  873. }
  874. /*
  875. FIXME
  876. - decimal
  877. - Guid
  878. - XmlNode objects
  879. [Test]
  880. public void TestSerialize()
  881. {
  882. Serialize();
  883. Assert.AreEqual (WriterText, "");
  884. }
  885. */
  886. // test basic class serialization /////////////////////////////////////
  887. [Test]
  888. public void TestSerializeSimpleClass ()
  889. {
  890. SimpleClass simple = new SimpleClass ();
  891. Serialize (simple);
  892. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  893. simple.something = "hello";
  894. Serialize (simple);
  895. 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);
  896. }
  897. [Test]
  898. public void TestSerializeStringCollection ()
  899. {
  900. StringCollection strings = new StringCollection ();
  901. Serialize (strings);
  902. Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  903. strings.Add ("hello");
  904. strings.Add ("goodbye");
  905. Serialize (strings);
  906. 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);
  907. }
  908. [Test]
  909. public void TestSerializeOptionalValueTypeContainer ()
  910. {
  911. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  912. XmlAttributes attr;
  913. OptionalValueTypeContainer optionalValue = new OptionalValueTypeContainer ();
  914. Serialize (optionalValue);
  915. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  916. "<?xml version='1.0' encoding='utf-16'?>" +
  917. #if NET_2_0
  918. "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}' />",
  919. #else
  920. "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />",
  921. #endif
  922. XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace),
  923. sw.ToString (), "#1");
  924. attr = new XmlAttributes ();
  925. // remove the DefaultValue attribute on the Flags member
  926. overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
  927. // remove the DefaultValue attribute on the Attributes member
  928. overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
  929. Serialize (optionalValue, overrides);
  930. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  931. "<?xml version='1.0' encoding='utf-16'?>" +
  932. #if NET_2_0
  933. "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
  934. #else
  935. "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
  936. #endif
  937. "<Attributes xmlns='{3}'>one four</Attributes>" +
  938. "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  939. AnotherNamespace, ANamespace), sw.ToString (), "#2");
  940. optionalValue.FlagsSpecified = true;
  941. Serialize (optionalValue, overrides);
  942. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  943. "<?xml version='1.0' encoding='utf-16'?>" +
  944. #if NET_2_0
  945. "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
  946. #else
  947. "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
  948. #endif
  949. "<Attributes xmlns='{3}'>one four</Attributes>" +
  950. "<Flags xmlns='{3}'>one</Flags>" +
  951. "</optionalValue>",
  952. XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace,
  953. ANamespace), sw.ToString (), "#3");
  954. }
  955. [Test]
  956. public void TestSerializePlainContainer ()
  957. {
  958. StringCollectionContainer container = new StringCollectionContainer ();
  959. Serialize (container);
  960. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
  961. container.Messages.Add ("hello");
  962. container.Messages.Add ("goodbye");
  963. Serialize (container);
  964. 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);
  965. }
  966. [Test]
  967. public void TestSerializeArrayContainer ()
  968. {
  969. ArrayContainer container = new ArrayContainer ();
  970. Serialize (container);
  971. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  972. container.items = new object[] { 10, 20 };
  973. Serialize (container);
  974. 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);
  975. container.items = new object[] { 10, "hello" };
  976. Serialize (container);
  977. 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);
  978. }
  979. [Test]
  980. public void TestSerializeClassArrayContainer ()
  981. {
  982. ClassArrayContainer container = new ClassArrayContainer ();
  983. Serialize (container);
  984. Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  985. SimpleClass simple1 = new SimpleClass ();
  986. simple1.something = "hello";
  987. SimpleClass simple2 = new SimpleClass ();
  988. simple2.something = "hello";
  989. container.items = new SimpleClass[2];
  990. container.items[0] = simple1;
  991. container.items[1] = simple2;
  992. Serialize (container);
  993. 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);
  994. }
  995. // test basic attributes ///////////////////////////////////////////////
  996. [Test]
  997. public void TestSerializeSimpleClassWithXmlAttributes ()
  998. {
  999. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
  1000. Serialize (simple);
  1001. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1002. simple.something = "hello";
  1003. Serialize (simple);
  1004. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
  1005. }
  1006. // test overrides ///////////////////////////////////////////////////////
  1007. [Test]
  1008. public void TestSerializeSimpleClassWithOverrides ()
  1009. {
  1010. // Also tests XmlIgnore
  1011. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1012. XmlAttributes attr = new XmlAttributes ();
  1013. attr.XmlIgnore = true;
  1014. overrides.Add (typeof (SimpleClassWithXmlAttributes), "something", attr);
  1015. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
  1016. simple.something = "hello";
  1017. Serialize (simple, overrides);
  1018. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1019. }
  1020. [Test]
  1021. public void TestSerializeSchema ()
  1022. {
  1023. XmlSchema schema = new XmlSchema ();
  1024. schema.Items.Add (new XmlSchemaAttribute ());
  1025. schema.Items.Add (new XmlSchemaAttributeGroup ());
  1026. schema.Items.Add (new XmlSchemaComplexType ());
  1027. schema.Items.Add (new XmlSchemaNotation ());
  1028. schema.Items.Add (new XmlSchemaSimpleType ());
  1029. schema.Items.Add (new XmlSchemaGroup ());
  1030. schema.Items.Add (new XmlSchemaElement ());
  1031. StringWriter sw = new StringWriter ();
  1032. XmlTextWriter xtw = new XmlTextWriter (sw);
  1033. xtw.QuoteChar = '\'';
  1034. xtw.Formatting = Formatting.Indented;
  1035. XmlSerializer xs = new XmlSerializer (schema.GetType ());
  1036. xs.Serialize (xtw, schema);
  1037. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1038. "<?xml version='1.0' encoding='utf-16'?>{0}" +
  1039. "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
  1040. " <xsd:attribute />{0}" +
  1041. " <xsd:attributeGroup />{0}" +
  1042. " <xsd:complexType />{0}" +
  1043. " <xsd:notation />{0}" +
  1044. " <xsd:simpleType />{0}" +
  1045. " <xsd:group />{0}" +
  1046. " <xsd:element />{0}" +
  1047. "</xsd:schema>", Environment.NewLine), sw.ToString ());
  1048. }
  1049. // test xmlText //////////////////////////////////////////////////////////
  1050. [Test]
  1051. public void TestSerializeXmlTextAttribute ()
  1052. {
  1053. SimpleClass simple = new SimpleClass ();
  1054. simple.something = "hello";
  1055. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1056. XmlAttributes attr = new XmlAttributes ();
  1057. overrides.Add (typeof (SimpleClass), "something", attr);
  1058. attr.XmlText = new XmlTextAttribute ();
  1059. Serialize (simple, overrides);
  1060. 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");
  1061. attr.XmlText = new XmlTextAttribute (typeof (string));
  1062. Serialize (simple, overrides);
  1063. 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");
  1064. try {
  1065. attr.XmlText = new XmlTextAttribute (typeof (byte[]));
  1066. Serialize (simple, overrides);
  1067. Assert.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed");
  1068. } catch (InvalidOperationException ex) {
  1069. // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'
  1070. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  1071. Assert.IsNotNull (ex.Message, "#A3");
  1072. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A4");
  1073. Assert.IsNotNull (ex.InnerException, "#A5");
  1074. // there was an error reflecting field 'something'.
  1075. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A6");
  1076. Assert.IsNotNull (ex.InnerException.Message, "#A7");
  1077. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A8");
  1078. Assert.IsNotNull (ex.InnerException.InnerException, "#A9");
  1079. // the type for XmlText may not be specified for primitive types.
  1080. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#A10");
  1081. Assert.IsNotNull (ex.InnerException.InnerException.Message, "#A11");
  1082. Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A12");
  1083. }
  1084. try {
  1085. attr.XmlText = new XmlTextAttribute ();
  1086. attr.XmlText.DataType = "sometype";
  1087. Serialize (simple, overrides);
  1088. Assert.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed");
  1089. } catch (InvalidOperationException ex) {
  1090. // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
  1091. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  1092. Assert.IsNotNull (ex.Message, "#B3");
  1093. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B4");
  1094. Assert.IsNotNull (ex.InnerException, "#B5");
  1095. // There was an error reflecting field 'something'.
  1096. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B6");
  1097. Assert.IsNotNull (ex.InnerException.Message, "#B7");
  1098. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B8");
  1099. Assert.IsNotNull (ex.InnerException.InnerException, "#B9");
  1100. //FIXME
  1101. /*
  1102. // There was an error reflecting type 'System.String'.
  1103. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10");
  1104. Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11");
  1105. Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12");
  1106. Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13");
  1107. // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
  1108. // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
  1109. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14");
  1110. Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15");
  1111. Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16");
  1112. Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17");
  1113. */
  1114. }
  1115. }
  1116. // test xmlRoot //////////////////////////////////////////////////////////
  1117. [Test]
  1118. public void TestSerializeXmlRootAttribute ()
  1119. {
  1120. // constructor override & element name
  1121. XmlRootAttribute root = new XmlRootAttribute ();
  1122. root.ElementName = "renamed";
  1123. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes ();
  1124. Serialize (simpleWithAttributes, root);
  1125. Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1126. SimpleClass simple = null;
  1127. root.IsNullable = false;
  1128. try {
  1129. Serialize (simple, root);
  1130. Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == false");
  1131. } catch (NullReferenceException) {
  1132. }
  1133. root.IsNullable = true;
  1134. try {
  1135. Serialize (simple, root);
  1136. Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == true");
  1137. } catch (NullReferenceException) {
  1138. }
  1139. simple = new SimpleClass ();
  1140. root.ElementName = null;
  1141. root.Namespace = "some.urn";
  1142. Serialize (simple, root);
  1143. 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);
  1144. }
  1145. [Test]
  1146. public void TestSerializeXmlRootAttributeOnMember ()
  1147. {
  1148. // nested root
  1149. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1150. XmlAttributes childAttr = new XmlAttributes ();
  1151. childAttr.XmlRoot = new XmlRootAttribute ("simple");
  1152. overrides.Add (typeof (SimpleClass), childAttr);
  1153. XmlAttributes attr = new XmlAttributes ();
  1154. attr.XmlRoot = new XmlRootAttribute ("simple");
  1155. overrides.Add (typeof (ClassArrayContainer), attr);
  1156. ClassArrayContainer container = new ClassArrayContainer ();
  1157. container.items = new SimpleClass[1];
  1158. container.items[0] = new SimpleClass ();
  1159. Serialize (container, overrides);
  1160. 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);
  1161. // FIXME test data type
  1162. }
  1163. // test XmlAttribute /////////////////////////////////////////////////////
  1164. [Test]
  1165. public void TestSerializeXmlAttributeAttribute ()
  1166. {
  1167. // null
  1168. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1169. XmlAttributes attr = new XmlAttributes ();
  1170. attr.XmlAttribute = new XmlAttributeAttribute ();
  1171. overrides.Add (typeof (SimpleClass), "something", attr);
  1172. SimpleClass simple = new SimpleClass (); ;
  1173. Serialize (simple, overrides);
  1174. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  1175. // regular
  1176. simple.something = "hello";
  1177. Serialize (simple, overrides);
  1178. 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");
  1179. // AttributeName
  1180. attr.XmlAttribute.AttributeName = "somethingelse";
  1181. Serialize (simple, overrides);
  1182. 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");
  1183. // Type
  1184. // FIXME this should work, shouldnt it?
  1185. // attr.XmlAttribute.Type = typeof(string);
  1186. // Serialize(simple, overrides);
  1187. // Assert(WriterText.EndsWith(" something='hello' />"));
  1188. // Namespace
  1189. attr.XmlAttribute.Namespace = "some:urn";
  1190. Serialize (simple, overrides);
  1191. 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");
  1192. // FIXME DataType
  1193. // FIXME XmlSchemaForm Form
  1194. // FIXME write XmlQualifiedName as attribute
  1195. }
  1196. // test XmlElement ///////////////////////////////////////////////////////
  1197. [Test]
  1198. public void TestSerializeXmlElementAttribute ()
  1199. {
  1200. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1201. XmlAttributes attr = new XmlAttributes ();
  1202. XmlElementAttribute element = new XmlElementAttribute ();
  1203. attr.XmlElements.Add (element);
  1204. overrides.Add (typeof (SimpleClass), "something", attr);
  1205. // null
  1206. SimpleClass simple = new SimpleClass (); ;
  1207. Serialize (simple, overrides);
  1208. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  1209. // not null
  1210. simple.something = "hello";
  1211. Serialize (simple, overrides);
  1212. 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");
  1213. //ElementName
  1214. element.ElementName = "saying";
  1215. Serialize (simple, overrides);
  1216. 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");
  1217. //IsNullable
  1218. element.IsNullable = false;
  1219. simple.something = null;
  1220. Serialize (simple, overrides);
  1221. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
  1222. element.IsNullable = true;
  1223. simple.something = null;
  1224. Serialize (simple, overrides);
  1225. 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");
  1226. //Namespace
  1227. element.ElementName = null;
  1228. element.IsNullable = false;
  1229. element.Namespace = "some:urn";
  1230. simple.something = "hello";
  1231. Serialize (simple, overrides);
  1232. 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");
  1233. //FIXME DataType
  1234. //FIXME Form
  1235. //FIXME Type
  1236. }
  1237. // test XmlElementAttribute with arrays and collections //////////////////
  1238. [Test]
  1239. public void TestSerializeCollectionWithXmlElementAttribute ()
  1240. {
  1241. // the rule is:
  1242. // if no type is specified or the specified type
  1243. // matches the contents of the collection,
  1244. // serialize each element in an element named after the member.
  1245. // if the type does not match, or matches the collection itself,
  1246. // create a base wrapping element for the member, and then
  1247. // wrap each collection item in its own wrapping element based on type.
  1248. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1249. XmlAttributes attr = new XmlAttributes ();
  1250. XmlElementAttribute element = new XmlElementAttribute ();
  1251. attr.XmlElements.Add (element);
  1252. overrides.Add (typeof (StringCollectionContainer), "Messages", attr);
  1253. // empty collection & no type info in XmlElementAttribute
  1254. StringCollectionContainer container = new StringCollectionContainer ();
  1255. Serialize (container, overrides);
  1256. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  1257. // non-empty collection & no type info in XmlElementAttribute
  1258. container.Messages.Add ("hello");
  1259. Serialize (container, overrides);
  1260. 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");
  1261. // non-empty collection & only type info in XmlElementAttribute
  1262. element.Type = typeof (StringCollection);
  1263. Serialize (container, overrides);
  1264. 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");
  1265. // non-empty collection & only type info in XmlElementAttribute
  1266. element.Type = typeof (string);
  1267. Serialize (container, overrides);
  1268. 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");
  1269. // two elements
  1270. container.Messages.Add ("goodbye");
  1271. element.Type = null;
  1272. Serialize (container, overrides);
  1273. 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");
  1274. }
  1275. // test DefaultValue /////////////////////////////////////////////////////
  1276. [Test]
  1277. #if TARGET_JVM
  1278. [Ignore ("TD #7458")]
  1279. #endif
  1280. public void TestSerializeDefaultValueAttribute ()
  1281. {
  1282. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1283. XmlAttributes attr = new XmlAttributes ();
  1284. string defaultValueInstance = "nothing";
  1285. attr.XmlDefaultValue = defaultValueInstance;
  1286. overrides.Add (typeof (SimpleClass), "something", attr);
  1287. // use the default
  1288. SimpleClass simple = new SimpleClass ();
  1289. Serialize (simple, overrides);
  1290. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
  1291. // same value as default
  1292. simple.something = defaultValueInstance;
  1293. Serialize (simple, overrides);
  1294. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
  1295. // some other value
  1296. simple.something = "hello";
  1297. Serialize (simple, overrides);
  1298. 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, "#A3");
  1299. overrides = new XmlAttributeOverrides ();
  1300. attr = new XmlAttributes ();
  1301. attr.XmlAttribute = new XmlAttributeAttribute ();
  1302. attr.XmlDefaultValue = defaultValueInstance;
  1303. overrides.Add (typeof (SimpleClass), "something", attr);
  1304. // use the default
  1305. simple = new SimpleClass ();
  1306. Serialize (simple, overrides);
  1307. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
  1308. // same value as default
  1309. simple.something = defaultValueInstance;
  1310. Serialize (simple, overrides);
  1311. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B2");
  1312. // some other value
  1313. simple.something = "hello";
  1314. Serialize (simple, overrides);
  1315. Assert.AreEqual (Infoset ("<SimpleClass something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B3");
  1316. overrides = new XmlAttributeOverrides ();
  1317. attr = new XmlAttributes ();
  1318. attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
  1319. overrides.Add (typeof (TestDefault), "flagencoded", attr);
  1320. // use the default
  1321. TestDefault testDefault = new TestDefault ();
  1322. Serialize (testDefault);
  1323. Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C1");
  1324. // use the default with overrides
  1325. Serialize (testDefault, overrides);
  1326. Assert.AreEqual (Infoset ("<testDefault flagenc='e1 e4' xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C2");
  1327. overrides = new XmlAttributeOverrides ();
  1328. attr = new XmlAttributes ();
  1329. attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
  1330. attr.XmlDefaultValue = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4); // add default again
  1331. overrides.Add (typeof (TestDefault), "flagencoded", attr);
  1332. // use the default with overrides
  1333. Serialize (testDefault, overrides);
  1334. Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C3");
  1335. // use the default with overrides and default namspace
  1336. Serialize (testDefault, overrides, AnotherNamespace);
  1337. Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C4");
  1338. // non-default values
  1339. testDefault.strDefault = "Some Text";
  1340. testDefault.boolT = false;
  1341. testDefault.boolF = true;
  1342. testDefault.decimalval = 20m;
  1343. testDefault.flag = FlagEnum.e2;
  1344. testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
  1345. Serialize (testDefault);
  1346. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1347. "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1348. " <strDefault>Some Text</strDefault>" +
  1349. " <boolT>false</boolT>" +
  1350. " <boolF>true</boolF>" +
  1351. " <decimalval>20</decimalval>" +
  1352. " <flag>two</flag>" +
  1353. " <flagencoded>e1 e2</flagencoded>" +
  1354. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1355. WriterText, "#C5");
  1356. Serialize (testDefault, overrides);
  1357. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1358. "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1359. " <strDefault>Some Text</strDefault>" +
  1360. " <boolT>false</boolT>" +
  1361. " <boolF>true</boolF>" +
  1362. " <decimalval>20</decimalval>" +
  1363. " <flag>two</flag>" +
  1364. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1365. WriterText, "#C6");
  1366. Serialize (testDefault, overrides, AnotherNamespace);
  1367. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1368. "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1369. " <strDefault>Some Text</strDefault>" +
  1370. " <boolT>false</boolT>" +
  1371. " <boolF>true</boolF>" +
  1372. " <decimalval>20</decimalval>" +
  1373. " <flag>two</flag>" +
  1374. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1375. WriterText, "#C7");
  1376. attr = new XmlAttributes ();
  1377. XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum");
  1378. xmlType.Namespace = "yetanother:urn";
  1379. attr.XmlType = xmlType;
  1380. overrides.Add (typeof (FlagEnum_Encoded), attr);
  1381. Serialize (testDefault, overrides, AnotherNamespace);
  1382. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1383. "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1384. " <strDefault>Some Text</strDefault>" +
  1385. " <boolT>false</boolT>" +
  1386. " <boolF>true</boolF>" +
  1387. " <decimalval>20</decimalval>" +
  1388. " <flag>two</flag>" +
  1389. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1390. WriterText, "#C8");
  1391. attr = new XmlAttributes ();
  1392. attr.XmlType = new XmlTypeAttribute ("testDefault");
  1393. overrides.Add (typeof (TestDefault), attr);
  1394. Serialize (testDefault, overrides, AnotherNamespace);
  1395. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1396. "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1397. " <strDefault>Some Text</strDefault>" +
  1398. " <boolT>false</boolT>" +
  1399. " <boolF>true</boolF>" +
  1400. " <decimalval>20</decimalval>" +
  1401. " <flag>two</flag>" +
  1402. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  1403. AnotherNamespace)), WriterText, "#C9");
  1404. }
  1405. [Test]
  1406. [Category ("NotWorking")] // SerializationCodeGenerator outputs wrong xsi:type for flagencoded in #C1
  1407. public void TestSerializeDefaultValueAttribute_Encoded ()
  1408. {
  1409. SoapAttributeOverrides overrides = new SoapAttributeOverrides ();
  1410. SoapAttributes attr = new SoapAttributes ();
  1411. attr.SoapAttribute = new SoapAttributeAttribute ();
  1412. string defaultValueInstance = "nothing";
  1413. attr.SoapDefaultValue = defaultValueInstance;
  1414. overrides.Add (typeof (SimpleClass), "something", attr);
  1415. // use the default
  1416. SimpleClass simple = new SimpleClass ();
  1417. SerializeEncoded (simple, overrides);
  1418. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
  1419. // same value as default
  1420. simple.something = defaultValueInstance;
  1421. SerializeEncoded (simple, overrides);
  1422. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
  1423. // some other value
  1424. simple.something = "hello";
  1425. SerializeEncoded (simple, overrides);
  1426. Assert.AreEqual (Infoset ("<SimpleClass id='id1' something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A3");
  1427. attr.SoapAttribute = null;
  1428. attr.SoapElement = new SoapElementAttribute ();
  1429. // use the default
  1430. simple = new SimpleClass ();
  1431. SerializeEncoded (simple, overrides);
  1432. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
  1433. // same value as default
  1434. simple.something = defaultValueInstance;
  1435. SerializeEncoded (simple, overrides);
  1436. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>nothing</something></SimpleClass>"), WriterText, "#B2");
  1437. // some other value
  1438. simple.something = "hello";
  1439. SerializeEncoded (simple, overrides);
  1440. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>hello</something></SimpleClass>"), WriterText, "#B3");
  1441. overrides = new SoapAttributeOverrides ();
  1442. attr = new SoapAttributes ();
  1443. attr.SoapElement = new SoapElementAttribute ("flagenc");
  1444. overrides.Add (typeof (TestDefault), "flagencoded", attr);
  1445. // use the default (from MS KB325691)
  1446. TestDefault testDefault = new TestDefault ();
  1447. SerializeEncoded (testDefault);
  1448. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1449. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1450. " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
  1451. " <boolT xsi:type='xsd:boolean'>true</boolT>" +
  1452. " <boolF xsi:type='xsd:boolean'>false</boolF>" +
  1453. " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
  1454. " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
  1455. " <flagencoded xsi:type='flagenum'>one four</flagencoded>" +
  1456. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1457. WriterText, "#C1");
  1458. SerializeEncoded (testDefault, overrides);
  1459. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1460. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1461. " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
  1462. " <boolT xsi:type='xsd:boolean'>true</boolT>" +
  1463. " <boolF xsi:type='xsd:boolean'>false</boolF>" +
  1464. " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
  1465. " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
  1466. " <flagenc xsi:type='flagenum'>one four</flagenc>" +
  1467. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1468. WriterText, "#C2");
  1469. SerializeEncoded (testDefault, overrides, AnotherNamespace);
  1470. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1471. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1472. " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
  1473. " <boolT xsi:type='xsd:boolean'>true</boolT>" +
  1474. " <boolF xsi:type='xsd:boolean'>false</boolF>" +
  1475. " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
  1476. " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" +
  1477. " <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" +
  1478. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  1479. AnotherNamespace)), WriterText, "#C3");
  1480. // non-default values
  1481. testDefault.strDefault = "Some Text";
  1482. testDefault.boolT = false;
  1483. testDefault.boolF = true;
  1484. testDefault.decimalval = 20m;
  1485. testDefault.flag = FlagEnum.e2;
  1486. testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
  1487. SerializeEncoded (testDefault);
  1488. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1489. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1490. " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
  1491. " <boolT xsi:type='xsd:boolean'>false</boolT>" +
  1492. " <boolF xsi:type='xsd:boolean'>true</boolF>" +
  1493. " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
  1494. " <flag xsi:type='FlagEnum'>e2</flag>" +
  1495. " <flagencoded xsi:type='flagenum'>one two</flagencoded>" +
  1496. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1497. WriterText, "#C4");
  1498. SerializeEncoded (testDefault, overrides);
  1499. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1500. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1501. " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
  1502. " <boolT xsi:type='xsd:boolean'>false</boolT>" +
  1503. " <boolF xsi:type='xsd:boolean'>true</boolF>" +
  1504. " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
  1505. " <flag xsi:type='FlagEnum'>e2</flag>" +
  1506. " <flagenc xsi:type='flagenum'>one two</flagenc>" +
  1507. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1508. WriterText, "#C5");
  1509. attr = new SoapAttributes ();
  1510. attr.SoapType = new SoapTypeAttribute ("flagenum", "yetanother:urn");
  1511. overrides.Add (typeof (FlagEnum_Encoded), attr);
  1512. SerializeEncoded (testDefault, overrides, AnotherNamespace);
  1513. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1514. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1515. " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
  1516. " <boolT xsi:type='xsd:boolean'>false</boolT>" +
  1517. " <boolF xsi:type='xsd:boolean'>true</boolF>" +
  1518. " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
  1519. " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" +
  1520. " <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" +
  1521. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  1522. AnotherNamespace)), WriterText, "#C6");
  1523. attr = new SoapAttributes ();
  1524. attr.SoapType = new SoapTypeAttribute ("testDefault");
  1525. overrides.Add (typeof (TestDefault), attr);
  1526. SerializeEncoded (testDefault, overrides, AnotherNamespace);
  1527. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1528. "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1529. " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
  1530. " <boolT xsi:type='xsd:boolean'>false</boolT>" +
  1531. " <boolF xsi:type='xsd:boolean'>true</boolF>" +
  1532. " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
  1533. " <flag xsi:type='q1:FlagEnum'>e2</flag>" +
  1534. " <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" +
  1535. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  1536. AnotherNamespace)), WriterText, "#C7");
  1537. }
  1538. // test XmlEnum //////////////////////////////////////////////////////////
  1539. [Test]
  1540. public void TestSerializeXmlEnumAttribute ()
  1541. {
  1542. Serialize (XmlSchemaForm.Qualified);
  1543. Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
  1544. Serialize (XmlSchemaForm.Unqualified);
  1545. Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
  1546. }
  1547. [Test]
  1548. public void TestSerializeXmlEnumAttribute_IgnoredValue ()
  1549. {
  1550. // technically XmlSchemaForm.None has an XmlIgnore attribute,
  1551. // but it is not being serialized as a member.
  1552. #if NET_2_0
  1553. try {
  1554. Serialize (XmlSchemaForm.None);
  1555. Assert.Fail ("#1");
  1556. } catch (InvalidOperationException ex) {
  1557. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  1558. Assert.IsNotNull (ex.InnerException, "#3");
  1559. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  1560. Assert.IsNotNull (ex.InnerException.Message, "#5");
  1561. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
  1562. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
  1563. }
  1564. #else
  1565. Serialize (XmlSchemaForm.None);
  1566. Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
  1567. #endif
  1568. }
  1569. [Test]
  1570. public void TestSerializeXmlNodeArray ()
  1571. {
  1572. XmlDocument doc = new XmlDocument ();
  1573. Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
  1574. Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  1575. }
  1576. [Test]
  1577. public void TestSerializeXmlElementArray ()
  1578. {
  1579. XmlDocument doc = new XmlDocument ();
  1580. Serialize (new XmlElement[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
  1581. Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
  1582. }
  1583. #if NET_2_0
  1584. [Test]
  1585. [ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
  1586. public void TestSerializeGenericListOfNode ()
  1587. {
  1588. XmlDocument doc = new XmlDocument ();
  1589. Serialize (new List<XmlNode> (new XmlNode [] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
  1590. Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  1591. }
  1592. [Test]
  1593. [ExpectedException (typeof (InvalidOperationException))] // List<XmlElement> is not supported
  1594. public void TestSerializeGenericListOfElement ()
  1595. {
  1596. XmlDocument doc = new XmlDocument ();
  1597. Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
  1598. Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
  1599. }
  1600. #endif
  1601. [Test]
  1602. public void TestSerializeXmlElement ()
  1603. {
  1604. XmlDocument doc = new XmlDocument ();
  1605. Serialize (doc.CreateElement ("elem"), typeof (XmlElement));
  1606. Assert.AreEqual (Infoset ("<elem/>"), WriterText);
  1607. }
  1608. [Test]
  1609. public void TestSerializeXmlElementSubclass ()
  1610. {
  1611. XmlDocument doc = new XmlDocument ();
  1612. Serialize (new MyElem (doc), typeof (XmlElement));
  1613. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
  1614. Serialize (new MyElem (doc), typeof (MyElem));
  1615. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
  1616. }
  1617. [Test]
  1618. public void TestSerializeXmlCDataSection ()
  1619. {
  1620. XmlDocument doc = new XmlDocument ();
  1621. CDataContainer c = new CDataContainer ();
  1622. c.cdata = doc.CreateCDataSection ("data section contents");
  1623. Serialize (c);
  1624. 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);
  1625. }
  1626. [Test]
  1627. public void TestSerializeXmlNode ()
  1628. {
  1629. XmlDocument doc = new XmlDocument ();
  1630. NodeContainer c = new NodeContainer ();
  1631. c.node = doc.CreateTextNode ("text");
  1632. Serialize (c);
  1633. 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);
  1634. }
  1635. [Test]
  1636. public void TestSerializeChoice ()
  1637. {
  1638. Choices ch = new Choices ();
  1639. ch.MyChoice = "choice text";
  1640. ch.ItemType = ItemChoiceType.ChoiceZero;
  1641. Serialize (ch);
  1642. 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");
  1643. ch.ItemType = ItemChoiceType.StrangeOne;
  1644. Serialize (ch);
  1645. 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");
  1646. ch.ItemType = ItemChoiceType.ChoiceTwo;
  1647. Serialize (ch);
  1648. 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");
  1649. }
  1650. [Test]
  1651. public void TestSerializeNamesWithSpaces ()
  1652. {
  1653. TestSpace ts = new TestSpace ();
  1654. ts.elem = 4;
  1655. ts.attr = 5;
  1656. Serialize (ts);
  1657. 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);
  1658. }
  1659. [Test]
  1660. public void TestSerializeReadOnlyProps ()
  1661. {
  1662. ReadOnlyProperties ts = new ReadOnlyProperties ();
  1663. Serialize (ts);
  1664. Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1665. }
  1666. [Test]
  1667. public void TestSerializeIList ()
  1668. {
  1669. clsPerson k = new clsPerson ();
  1670. k.EmailAccounts = new ArrayList ();
  1671. k.EmailAccounts.Add ("a");
  1672. k.EmailAccounts.Add ("b");
  1673. Serialize (k);
  1674. 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);
  1675. }
  1676. [Test]
  1677. public void TestSerializeArrayEnc ()
  1678. {
  1679. SoapReflectionImporter imp = new SoapReflectionImporter ();
  1680. XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
  1681. XmlSerializer ser = new XmlSerializer (map);
  1682. StringWriter sw = new StringWriter ();
  1683. XmlTextWriter tw = new XmlTextWriter (sw);
  1684. tw.WriteStartElement ("aa");
  1685. ser.Serialize (tw, new ArrayClass ());
  1686. tw.WriteEndElement ();
  1687. }
  1688. [Test] // bug #76049
  1689. public void TestIncludeType ()
  1690. {
  1691. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1692. XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
  1693. imp.IncludeType (typeof (TestSpace));
  1694. XmlSerializer ser = new XmlSerializer (map);
  1695. ser.Serialize (new StringWriter (), new TestSpace ());
  1696. }
  1697. [Test]
  1698. public void TestSerializeChoiceArray ()
  1699. {
  1700. CompositeValueType v = new CompositeValueType ();
  1701. v.Init ();
  1702. Serialize (v);
  1703. 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);
  1704. }
  1705. [Test]
  1706. public void TestArrayAttributeWithDataType ()
  1707. {
  1708. Serialize (new ArrayAttributeWithType ());
  1709. string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
  1710. res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
  1711. Assert.AreEqual (Infoset (res), WriterText);
  1712. }
  1713. [Test]
  1714. public void TestSubclassElementType ()
  1715. {
  1716. SubclassTestContainer c = new SubclassTestContainer ();
  1717. c.data = new SubclassTestSub ();
  1718. Serialize (c);
  1719. string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
  1720. res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
  1721. Assert.AreEqual (Infoset (res), WriterText);
  1722. }
  1723. [Test]
  1724. [ExpectedException (typeof (InvalidOperationException))]
  1725. public void TestArrayAttributeWithWrongDataType ()
  1726. {
  1727. Serialize (new ArrayAttributeWithWrongType ());
  1728. }
  1729. [Test]
  1730. [Category ("NotWorking")]
  1731. public void TestSerializePrimitiveTypesContainer ()
  1732. {
  1733. Serialize (new PrimitiveTypesContainer ());
  1734. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1735. "<?xml version='1.0' encoding='utf-16'?>" +
  1736. #if NET_2_0
  1737. "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
  1738. #else
  1739. "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
  1740. #endif
  1741. "<Number>2004</Number>" +
  1742. "<Name>some name</Name>" +
  1743. "<Index>56</Index>" +
  1744. "<Password>8w8=</Password>" +
  1745. "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
  1746. "</PrimitiveTypesContainer>", XmlSchema.Namespace,
  1747. XmlSchema.InstanceNamespace), sw.ToString (), "#1");
  1748. SerializeEncoded (new PrimitiveTypesContainer ());
  1749. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1750. "<?xml version='1.0' encoding='utf-16'?>" +
  1751. #if NET_2_0
  1752. "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
  1753. #else
  1754. "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
  1755. #endif
  1756. "<Number xsi:type='xsd:int'>2004</Number>" +
  1757. "<Name xsi:type='xsd:string'>some name</Name>" +
  1758. "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
  1759. "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
  1760. "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
  1761. "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
  1762. XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
  1763. sw.ToString (), "#2");
  1764. }
  1765. [Test]
  1766. public void TestSchemaForm ()
  1767. {
  1768. TestSchemaForm1 t1 = new TestSchemaForm1 ();
  1769. t1.p1 = new PrintTypeResponse ();
  1770. t1.p1.Init ();
  1771. t1.p2 = new PrintTypeResponse ();
  1772. t1.p2.Init ();
  1773. TestSchemaForm2 t2 = new TestSchemaForm2 ();
  1774. t2.p1 = new PrintTypeResponse ();
  1775. t2.p1.Init ();
  1776. t2.p2 = new PrintTypeResponse ();
  1777. t2.p2.Init ();
  1778. Serialize (t1);
  1779. string res = "";
  1780. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1781. res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  1782. res += " <p1>";
  1783. res += " <result>";
  1784. res += " <data>data1</data>";
  1785. res += " </result>";
  1786. res += " <intern xmlns=\"urn:responseTypes\">";
  1787. res += " <result xmlns=\"\">";
  1788. res += " <data>data2</data>";
  1789. res += " </result>";
  1790. res += " </intern>";
  1791. res += " </p1>";
  1792. res += " <p2 xmlns=\"urn:oo\">";
  1793. res += " <result xmlns=\"\">";
  1794. res += " <data>data1</data>";
  1795. res += " </result>";
  1796. res += " <intern xmlns=\"urn:responseTypes\">";
  1797. res += " <result xmlns=\"\">";
  1798. res += " <data>data2</data>";
  1799. res += " </result>";
  1800. res += " </intern>";
  1801. res += " </p2>";
  1802. res += "</TestSchemaForm1>";
  1803. Assert.AreEqual (Infoset (res), WriterText);
  1804. Serialize (t2);
  1805. res = "";
  1806. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1807. res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  1808. res += " <p1 xmlns=\"urn:testForm\">";
  1809. res += " <result xmlns=\"\">";
  1810. res += " <data>data1</data>";
  1811. res += " </result>";
  1812. res += " <intern xmlns=\"urn:responseTypes\">";
  1813. res += " <result xmlns=\"\">";
  1814. res += " <data>data2</data>";
  1815. res += " </result>";
  1816. res += " </intern>";
  1817. res += " </p1>";
  1818. res += " <p2 xmlns=\"urn:oo\">";
  1819. res += " <result xmlns=\"\">";
  1820. res += " <data>data1</data>";
  1821. res += " </result>";
  1822. res += " <intern xmlns=\"urn:responseTypes\">";
  1823. res += " <result xmlns=\"\">";
  1824. res += " <data>data2</data>";
  1825. res += " </result>";
  1826. res += " </intern>";
  1827. res += " </p2>";
  1828. res += "</TestSchemaForm2>";
  1829. Assert.AreEqual (Infoset (res), WriterText);
  1830. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1831. XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
  1832. Serialize (t1, map);
  1833. res = "";
  1834. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1835. res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
  1836. res += " <p1>";
  1837. res += " <result xmlns=\"\">";
  1838. res += " <data>data1</data>";
  1839. res += " </result>";
  1840. res += " <intern xmlns=\"urn:responseTypes\">";
  1841. res += " <result xmlns=\"\">";
  1842. res += " <data>data2</data>";
  1843. res += " </result>";
  1844. res += " </intern>";
  1845. res += " </p1>";
  1846. res += " <p2 xmlns=\"urn:oo\">";
  1847. res += " <result xmlns=\"\">";
  1848. res += " <data>data1</data>";
  1849. res += " </result>";
  1850. res += " <intern xmlns=\"urn:responseTypes\">";
  1851. res += " <result xmlns=\"\">";
  1852. res += " <data>data2</data>";
  1853. res += " </result>";
  1854. res += " </intern>";
  1855. res += " </p2>";
  1856. res += "</TestSchemaForm1>";
  1857. Assert.AreEqual (Infoset (res), WriterText);
  1858. imp = new XmlReflectionImporter ();
  1859. map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
  1860. Serialize (t2, map);
  1861. res = "";
  1862. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1863. res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
  1864. res += " <p1 xmlns=\"urn:testForm\">";
  1865. res += " <result xmlns=\"\">";
  1866. res += " <data>data1</data>";
  1867. res += " </result>";
  1868. res += " <intern xmlns=\"urn:responseTypes\">";
  1869. res += " <result xmlns=\"\">";
  1870. res += " <data>data2</data>";
  1871. res += " </result>";
  1872. res += " </intern>";
  1873. res += " </p1>";
  1874. res += " <p2 xmlns=\"urn:oo\">";
  1875. res += " <result xmlns=\"\">";
  1876. res += " <data>data1</data>";
  1877. res += " </result>";
  1878. res += " <intern xmlns=\"urn:responseTypes\">";
  1879. res += " <result xmlns=\"\">";
  1880. res += " <data>data2</data>";
  1881. res += " </result>";
  1882. res += " </intern>";
  1883. res += " </p2>";
  1884. res += "</TestSchemaForm2>";
  1885. Assert.AreEqual (Infoset (res), WriterText);
  1886. }
  1887. [Test] // bug #78536
  1888. public void CDataTextNodes ()
  1889. {
  1890. XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
  1891. ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
  1892. string xml = @"<CDataTextNodesType>
  1893. <foo><![CDATA[
  1894. (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
  1895. \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
  1896. (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
  1897. ]]></foo>
  1898. </CDataTextNodesType>";
  1899. ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
  1900. }
  1901. #if NET_2_0
  1902. #if !TARGET_JVM
  1903. [Test]
  1904. public void GenerateSerializerGenerics ()
  1905. {
  1906. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1907. Type type = typeof (List<int>);
  1908. XmlSerializer.GenerateSerializer (
  1909. new Type [] {type},
  1910. new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
  1911. }
  1912. #endif
  1913. [Test]
  1914. public void Nullable ()
  1915. {
  1916. XmlSerializer ser = new XmlSerializer (typeof (int?));
  1917. int? nullableType = 5;
  1918. sw = new StringWriter ();
  1919. xtw = new XmlTextWriter (sw);
  1920. ser.Serialize (xtw, nullableType);
  1921. xtw.Close ();
  1922. string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
  1923. Assert.AreEqual (Infoset (expected), WriterText);
  1924. int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
  1925. Assert.AreEqual (5, i);
  1926. }
  1927. #endif
  1928. [Test]
  1929. public void SerializeBase64Binary()
  1930. {
  1931. XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
  1932. sw = new StringWriter ();
  1933. XmlTextWriter xtw = new XmlTextWriter (sw);
  1934. ser.Serialize (xtw, new Base64Binary ());
  1935. xtw.Close ();
  1936. string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><Base64Binary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""AQID"" />";
  1937. Assert.AreEqual (Infoset (expected), WriterText);
  1938. Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
  1939. Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
  1940. }
  1941. [Test] // bug #79989, #79990
  1942. public void SerializeHexBinary ()
  1943. {
  1944. XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
  1945. sw = new StringWriter ();
  1946. XmlTextWriter xtw = new XmlTextWriter (sw);
  1947. ser.Serialize (xtw, new HexBinary ());
  1948. xtw.Close ();
  1949. string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><HexBinary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""010203"" />";
  1950. Assert.AreEqual (Infoset (expected), WriterText);
  1951. HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
  1952. Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
  1953. }
  1954. [Test]
  1955. [ExpectedException (typeof (InvalidOperationException))]
  1956. public void XmlArrayAttributeOnInt ()
  1957. {
  1958. new XmlSerializer (typeof (XmlArrayOnInt));
  1959. }
  1960. [Test]
  1961. [ExpectedException (typeof (InvalidOperationException))]
  1962. public void XmlArrayAttributeUnqualifiedWithNamespace ()
  1963. {
  1964. new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
  1965. }
  1966. [Test]
  1967. [ExpectedException (typeof (InvalidOperationException))]
  1968. public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
  1969. {
  1970. new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
  1971. }
  1972. [Test] // bug #78042
  1973. public void XmlArrayAttributeOnArray ()
  1974. {
  1975. XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
  1976. sw = new StringWriter ();
  1977. XmlTextWriter xtw = new XmlTextWriter (sw);
  1978. ser.Serialize (xtw, new XmlArrayOnArray ());
  1979. xtw.Close ();
  1980. string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArray xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><string xmlns=""urn:foo"">foo</string><string xmlns=""urn:foo"">bar</string></Sane><Mids xmlns=""""><ArrayItemInXmlArray xmlns=""urn:foo""><Whee xmlns=""""><string xmlns=""urn:gyabo"">foo</string><string xmlns=""urn:gyabo"">bar</string></Whee></ArrayItemInXmlArray></Mids></XmlArrayOnArray>";
  1981. Assert.AreEqual (Infoset (expected), WriterText);
  1982. }
  1983. [Test]
  1984. public void XmlArrayAttributeOnCollection ()
  1985. {
  1986. XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
  1987. XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
  1988. inst.Sane.Add ("abc");
  1989. inst.Sane.Add (1);
  1990. sw = new StringWriter ();
  1991. XmlTextWriter xtw = new XmlTextWriter (sw);
  1992. ser.Serialize (xtw, inst);
  1993. xtw.Close ();
  1994. string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArrayList xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><anyType xsi:type=""xsd:string"" xmlns=""urn:foo"">abc</anyType><anyType xsi:type=""xsd:int"" xmlns=""urn:foo"">1</anyType></Sane></XmlArrayOnArrayList>";
  1995. Assert.AreEqual (Infoset (expected), WriterText);
  1996. }
  1997. #region GenericsSeralizationTests
  1998. #if NET_2_0
  1999. [Test]
  2000. public void TestSerializeGenSimpleClassString ()
  2001. {
  2002. GenSimpleClass<string> simple = new GenSimpleClass<string> ();
  2003. Serialize (simple);
  2004. Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  2005. simple.something = "hello";
  2006. Serialize (simple);
  2007. Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></GenSimpleClassOfString>"), WriterText);
  2008. }
  2009. [Test]
  2010. public void TestSerializeGenSimpleClassBool ()
  2011. {
  2012. GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
  2013. Serialize (simple);
  2014. Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>false</something></GenSimpleClassOfBoolean>"), WriterText);
  2015. simple.something = true;
  2016. Serialize (simple);
  2017. Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>true</something></GenSimpleClassOfBoolean>"), WriterText);
  2018. }
  2019. [Test]
  2020. public void TestSerializeGenSimpleStructInt ()
  2021. {
  2022. GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
  2023. Serialize (simple);
  2024. Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>0</something></GenSimpleStructOfInt32>"), WriterText);
  2025. simple.something = 123;
  2026. Serialize (simple);
  2027. Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something></GenSimpleStructOfInt32>"), WriterText);
  2028. }
  2029. [Test]
  2030. public void TestSerializeGenListClassString ()
  2031. {
  2032. GenListClass<string> genlist = new GenListClass<string> ();
  2033. Serialize (genlist);
  2034. Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfString>"), WriterText);
  2035. genlist.somelist.Add ("Value1");
  2036. genlist.somelist.Add ("Value2");
  2037. Serialize (genlist);
  2038. Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><string>Value1</string><string>Value2</string></somelist></GenListClassOfString>"), WriterText);
  2039. }
  2040. [Test]
  2041. public void TestSerializeGenListClassFloat ()
  2042. {
  2043. GenListClass<float> genlist = new GenListClass<float> ();
  2044. Serialize (genlist);
  2045. Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfSingle>"), WriterText);
  2046. genlist.somelist.Add (1);
  2047. genlist.somelist.Add (2.2F);
  2048. Serialize (genlist);
  2049. Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><float>1</float><float>2.2</float></somelist></GenListClassOfSingle>"), WriterText);
  2050. }
  2051. [Test]
  2052. public void TestSerializeGenListClassList ()
  2053. {
  2054. GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
  2055. Serialize (genlist);
  2056. Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
  2057. GenListClass<int> inlist1 = new GenListClass<int> ();
  2058. inlist1.somelist.Add (1);
  2059. inlist1.somelist.Add (2);
  2060. GenListClass<int> inlist2 = new GenListClass<int> ();
  2061. inlist2.somelist.Add (10);
  2062. inlist2.somelist.Add (20);
  2063. genlist.somelist.Add (inlist1);
  2064. genlist.somelist.Add (inlist2);
  2065. Serialize (genlist);
  2066. Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInt32><somelist><int>1</int><int>2</int></somelist></GenListClassOfInt32><GenListClassOfInt32><somelist><int>10</int><int>20</int></somelist></GenListClassOfInt32></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
  2067. }
  2068. [Test]
  2069. public void TestSerializeGenListClassArray ()
  2070. {
  2071. GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
  2072. Serialize (genlist);
  2073. Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
  2074. GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
  2075. genarr1.arr[0] = 'a';
  2076. genarr1.arr[1] = 'b';
  2077. genlist.somelist.Add (genarr1);
  2078. GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
  2079. genarr2.arr[0] = 'd';
  2080. genarr2.arr[1] = 'e';
  2081. genarr2.arr[2] = 'f';
  2082. genlist.somelist.Add (genarr2);
  2083. Serialize (genlist);
  2084. Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenArrayClassOfChar><arr><char>97</char><char>98</char><char>0</char></arr></GenArrayClassOfChar><GenArrayClassOfChar><arr><char>100</char><char>101</char><char>102</char></arr></GenArrayClassOfChar></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
  2085. }
  2086. [Test]
  2087. public void TestSerializeGenTwoClassCharDouble ()
  2088. {
  2089. GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
  2090. Serialize (gentwo);
  2091. Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2></GenTwoClassOfCharDouble>"), WriterText);
  2092. gentwo.something1 = 'a';
  2093. gentwo.something2 = 2.2;
  2094. Serialize (gentwo);
  2095. Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>97</something1><something2>2.2</something2></GenTwoClassOfCharDouble>"), WriterText);
  2096. }
  2097. [Test]
  2098. public void TestSerializeGenDerivedClassDecimalShort ()
  2099. {
  2100. GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
  2101. Serialize (derived);
  2102. Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something2>0</something2><another1>0</another1><another2>0</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
  2103. derived.something1 = "Value1";
  2104. derived.something2 = 1;
  2105. derived.another1 = 1.1M;
  2106. derived.another2 = -22;
  2107. Serialize (derived);
  2108. Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>Value1</something1><something2>1</something2><another1>1.1</another1><another2>-22</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
  2109. }
  2110. [Test]
  2111. public void TestSerializeGenDerivedSecondClassByteUlong ()
  2112. {
  2113. GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
  2114. Serialize (derived2);
  2115. Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2><another1>0</another1><another2>0</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
  2116. derived2.something1 = 1;
  2117. derived2.something2 = 222;
  2118. derived2.another1 = 111;
  2119. derived2.another2 = 222222;
  2120. Serialize (derived2);
  2121. Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>1</something1><something2>222</something2><another1>111</another1><another2>222222</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
  2122. }
  2123. [Test]
  2124. public void TestSerializeGenNestedClass ()
  2125. {
  2126. GenNestedClass<string, int>.InnerClass<bool> nested =
  2127. new GenNestedClass<string, int>.InnerClass<bool> ();
  2128. Serialize (nested);
  2129. Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>0</inner><something>false</something></InnerClassOfStringInt32Boolean>"), WriterText);
  2130. nested.inner = 5;
  2131. nested.something = true;
  2132. Serialize (nested);
  2133. Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>5</inner><something>true</something></InnerClassOfStringInt32Boolean>"), WriterText);
  2134. }
  2135. [Test]
  2136. public void TestSerializeGenListClassListNested ()
  2137. {
  2138. GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
  2139. new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
  2140. Serialize (genlist);
  2141. Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
  2142. GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
  2143. new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
  2144. GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
  2145. inval1.inner = 1;
  2146. inval1.something = "ONE";
  2147. inlist1.somelist.Add (inval1);
  2148. GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
  2149. inval2.inner = 2;
  2150. inval2.something = "TWO";
  2151. inlist1.somelist.Add (inval2);
  2152. GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
  2153. new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
  2154. GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
  2155. inval3.inner = 30;
  2156. inval3.something = "THIRTY";
  2157. inlist2.somelist.Add (inval3);
  2158. genlist.somelist.Add (inlist1);
  2159. genlist.somelist.Add (inlist2);
  2160. Serialize (genlist);
  2161. Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>1</inner><something>ONE</something></InnerClassOfInt32Int32String><InnerClassOfInt32Int32String><inner>2</inner><something>TWO</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>30</inner><something>THIRTY</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
  2162. }
  2163. public enum Myenum { one, two, three, four, five, six };
  2164. [Test]
  2165. public void TestSerializeGenArrayClassEnum ()
  2166. {
  2167. GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
  2168. Serialize (genarr);
  2169. Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>one</Myenum><Myenum>one</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
  2170. genarr.arr[0] = Myenum.one;
  2171. genarr.arr[1] = Myenum.three;
  2172. genarr.arr[2] = Myenum.five;
  2173. Serialize (genarr);
  2174. Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
  2175. }
  2176. [Test]
  2177. public void TestSerializeGenArrayStruct ()
  2178. {
  2179. GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
  2180. Serialize (genarr);
  2181. Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></></></>", WriterText);
  2182. GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
  2183. genstruct.something = 111;
  2184. genarr.arr[0] = genstruct;
  2185. genstruct.something = 222;
  2186. genarr.arr[1] = genstruct;
  2187. genstruct.something = 333;
  2188. genarr.arr[2] = genstruct;
  2189. Serialize (genarr);
  2190. Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>111</></><:GenSimpleStructOfUInt32><:something>222</></><:GenSimpleStructOfUInt32><:something>333</></></></>", WriterText);
  2191. }
  2192. [Test]
  2193. public void TestSerializeGenArrayList ()
  2194. {
  2195. GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
  2196. Serialize (genarr);
  2197. Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></></></>", WriterText);
  2198. GenListClass<string> genlist1 = new GenListClass<string> ();
  2199. genlist1.somelist.Add ("list1-val1");
  2200. genlist1.somelist.Add ("list1-val2");
  2201. genarr.arr[0] = genlist1;
  2202. GenListClass<string> genlist2 = new GenListClass<string> ();
  2203. genlist2.somelist.Add ("list2-val1");
  2204. genlist2.somelist.Add ("list2-val2");
  2205. genlist2.somelist.Add ("list2-val3");
  2206. genlist2.somelist.Add ("list2-val4");
  2207. genarr.arr[1] = genlist2;
  2208. GenListClass<string> genlist3 = new GenListClass<string> ();
  2209. genlist3.somelist.Add ("list3val");
  2210. genarr.arr[2] = genlist3;
  2211. Serialize (genarr);
  2212. Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString><:somelist><:string>list1-val1</><:string>list1-val2</></></><:GenListClassOfString><:somelist><:string>list2-val1</><:string>list2-val2</><:string>list2-val3</><:string>list2-val4</></></><:GenListClassOfString><:somelist><:string>list3val</></></></></>", WriterText);
  2213. }
  2214. [Test]
  2215. public void TestSerializeGenComplexStruct ()
  2216. {
  2217. GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
  2218. Serialize (complex);
  2219. Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>0</><:simpleclass><:something>0</></><:simplestruct><:something>0</></><:listclass><:somelist></></><:arrayclass><:arr><:int>0</><:int>0</><:int>0</></></><:twoclass><:something1>0</></><:derivedclass><:something2>0</><:another1>0</></><:derived2><:something1>0</><:another1>0</></><:nestedouter><:outer>0</></><:nestedinner><:something>0</></></>", WriterText);
  2220. complex.something = 123;
  2221. complex.simpleclass.something = 456;
  2222. complex.simplestruct.something = 789;
  2223. GenListClass<int> genlist = new GenListClass<int> ();
  2224. genlist.somelist.Add (100);
  2225. genlist.somelist.Add (200);
  2226. complex.listclass = genlist;
  2227. GenArrayClass<int> genarr = new GenArrayClass<int> ();
  2228. genarr.arr[0] = 11;
  2229. genarr.arr[1] = 22;
  2230. genarr.arr[2] = 33;
  2231. complex.arrayclass = genarr;
  2232. complex.twoclass.something1 = 10;
  2233. complex.twoclass.something2 = "Ten";
  2234. complex.derivedclass.another1 = 1;
  2235. complex.derivedclass.another2 = "one";
  2236. complex.derivedclass.something1 = "two";
  2237. complex.derivedclass.something2 = 2;
  2238. complex.derived2.another1 = 3;
  2239. complex.derived2.another2 = "three";
  2240. complex.derived2.something1 = 4;
  2241. complex.derived2.something2 = "four";
  2242. complex.nestedouter.outer = 5;
  2243. complex.nestedinner.inner = "six";
  2244. complex.nestedinner.something = 6;
  2245. Serialize (complex);
  2246. Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>123</><:simpleclass><:something>456</></><:simplestruct><:something>789</></><:listclass><:somelist><:int>100</><:int>200</></></><:arrayclass><:arr><:int>11</><:int>22</><:int>33</></></><:twoclass><:something1>10</><:something2>Ten</></><:derivedclass><:something1>two</><:something2>2</><:another1>1</><:another2>one</></><:derived2><:something1>4</><:something2>four</><:another1>3</><:another2>three</></><:nestedouter><:outer>5</></><:nestedinner><:inner>six</><:something>6</></></>", WriterText);
  2247. }
  2248. [Test] // bug #80759
  2249. public void HasFieldSpecifiedButIrrelevant ()
  2250. {
  2251. Bug80759 foo = new Bug80759 ();
  2252. foo.Test = "BAR";
  2253. foo.NullableInt = 10;
  2254. XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
  2255. MemoryStream stream = new MemoryStream ();
  2256. serializer.Serialize (stream, foo);
  2257. stream.Position = 0;
  2258. foo = (Bug80759) serializer.Deserialize (stream);
  2259. }
  2260. [Test]
  2261. public void SupportPrivateCtorOnly ()
  2262. {
  2263. XmlSerializer xs =
  2264. new XmlSerializer (typeof (PrivateCtorOnly));
  2265. StringWriter sw = new StringWriter ();
  2266. xs.Serialize (sw, PrivateCtorOnly.Instance);
  2267. xs.Deserialize (new StringReader (sw.ToString ()));
  2268. }
  2269. #endif
  2270. #endregion //GenericsSeralizationTests
  2271. public class XmlArrayOnInt
  2272. {
  2273. [XmlArray]
  2274. public int Bogus;
  2275. }
  2276. public class XmlArrayUnqualifiedWithNamespace
  2277. {
  2278. [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
  2279. public ArrayList Sane = new ArrayList ();
  2280. }
  2281. public class XmlArrayItemUnqualifiedWithNamespace
  2282. {
  2283. [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
  2284. public ArrayList Sane = new ArrayList ();
  2285. }
  2286. [XmlRoot (Namespace = "urn:foo")]
  2287. public class XmlArrayOnArrayList
  2288. {
  2289. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2290. public ArrayList Sane = new ArrayList ();
  2291. }
  2292. [XmlRoot (Namespace = "urn:foo")]
  2293. public class XmlArrayOnArray
  2294. {
  2295. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2296. public string[] Sane = new string[] { "foo", "bar" };
  2297. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2298. public ArrayItemInXmlArray[] Mids =
  2299. new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
  2300. }
  2301. [XmlType (Namespace = "urn:gyabo")]
  2302. public class ArrayItemInXmlArray
  2303. {
  2304. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2305. public string[] Whee = new string[] { "foo", "bar" };
  2306. }
  2307. [XmlRoot ("Base64Binary")]
  2308. public class Base64Binary
  2309. {
  2310. [XmlAttribute (DataType = "base64Binary")]
  2311. public byte [] Data = new byte [] {1, 2, 3};
  2312. }
  2313. [XmlRoot ("HexBinary")]
  2314. public class HexBinary
  2315. {
  2316. [XmlAttribute (DataType = "hexBinary")]
  2317. public byte[] Data = new byte[] { 1, 2, 3 };
  2318. }
  2319. [XmlRoot ("PrivateCtorOnly")]
  2320. public class PrivateCtorOnly
  2321. {
  2322. public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
  2323. private PrivateCtorOnly ()
  2324. {
  2325. }
  2326. }
  2327. public class CDataTextNodesType
  2328. {
  2329. public CDataTextNodesInternal foo;
  2330. }
  2331. public class CDataTextNodesInternal
  2332. {
  2333. [XmlText]
  2334. public string Value;
  2335. }
  2336. #if NET_2_0
  2337. public class Bug80759
  2338. {
  2339. public string Test;
  2340. public int? NullableInt;
  2341. [XmlIgnore]
  2342. public bool NullableIntSpecified {
  2343. get { return NullableInt.HasValue; }
  2344. }
  2345. }
  2346. #endif
  2347. void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
  2348. {
  2349. Assert.Fail ();
  2350. }
  2351. // Helper methods
  2352. public static string Infoset (string sx)
  2353. {
  2354. XmlDocument doc = new XmlDocument ();
  2355. doc.LoadXml (sx);
  2356. StringBuilder sb = new StringBuilder ();
  2357. GetInfoset (doc.DocumentElement, sb);
  2358. return sb.ToString ();
  2359. }
  2360. public static string Infoset (XmlNode nod)
  2361. {
  2362. StringBuilder sb = new StringBuilder ();
  2363. GetInfoset (nod, sb);
  2364. return sb.ToString ();
  2365. }
  2366. static void GetInfoset (XmlNode nod, StringBuilder sb)
  2367. {
  2368. switch (nod.NodeType) {
  2369. case XmlNodeType.Attribute:
  2370. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  2371. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  2372. break;
  2373. case XmlNodeType.Element:
  2374. XmlElement elem = (XmlElement) nod;
  2375. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  2376. ArrayList ats = new ArrayList ();
  2377. foreach (XmlAttribute at in elem.Attributes)
  2378. ats.Add (at.LocalName + " " + at.NamespaceURI);
  2379. ats.Sort ();
  2380. foreach (string name in ats) {
  2381. string[] nn = name.Split (' ');
  2382. GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
  2383. }
  2384. sb.Append (">");
  2385. foreach (XmlNode cn in elem.ChildNodes)
  2386. GetInfoset (cn, sb);
  2387. sb.Append ("</>");
  2388. break;
  2389. default:
  2390. sb.Append (nod.OuterXml);
  2391. break;
  2392. }
  2393. }
  2394. static XmlTypeMapping CreateSoapMapping (Type type)
  2395. {
  2396. SoapReflectionImporter importer = new SoapReflectionImporter ();
  2397. return importer.ImportTypeMapping (type);
  2398. }
  2399. static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
  2400. {
  2401. SoapReflectionImporter importer = new SoapReflectionImporter (ao);
  2402. return importer.ImportTypeMapping (type);
  2403. }
  2404. static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
  2405. {
  2406. SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
  2407. return importer.ImportTypeMapping (type);
  2408. }
  2409. }
  2410. }