XmlSerializerTests.cs 111 KB

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