XmlSerializerTests.cs 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700
  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. [Category ("NotWorking")] // hmm, why?
  1924. public void XmlArrayAttributeUnqualifiedWithNamespace ()
  1925. {
  1926. new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
  1927. }
  1928. [Test]
  1929. [ExpectedException (typeof (InvalidOperationException))]
  1930. public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
  1931. {
  1932. new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
  1933. }
  1934. [Test] // bug #78042
  1935. public void XmlArrayAttributeOnArray ()
  1936. {
  1937. XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
  1938. sw = new StringWriter ();
  1939. XmlTextWriter xtw = new XmlTextWriter (sw);
  1940. ser.Serialize (xtw, new XmlArrayOnArray ());
  1941. xtw.Close ();
  1942. 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>";
  1943. Assert.AreEqual (Infoset (expected), WriterText);
  1944. }
  1945. [Test]
  1946. public void XmlArrayAttributeOnCollection ()
  1947. {
  1948. XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
  1949. XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
  1950. inst.Sane.Add ("abc");
  1951. inst.Sane.Add (1);
  1952. sw = new StringWriter ();
  1953. XmlTextWriter xtw = new XmlTextWriter (sw);
  1954. ser.Serialize (xtw, inst);
  1955. xtw.Close ();
  1956. 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>";
  1957. Assert.AreEqual (Infoset (expected), WriterText);
  1958. }
  1959. #region GenericsSeralizationTests
  1960. #if NET_2_0
  1961. [Test]
  1962. [Category ("NotWorking")]
  1963. public void TestSerializeGenSimpleClassString ()
  1964. {
  1965. GenSimpleClass<string> simple = new GenSimpleClass<string> ();
  1966. Serialize (simple);
  1967. Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1968. simple.something = "hello";
  1969. Serialize (simple);
  1970. 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);
  1971. }
  1972. [Test]
  1973. [Category ("NotWorking")]
  1974. public void TestSerializeGenSimpleClassBool ()
  1975. {
  1976. GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
  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>false</something></GenSimpleClassOfBoolean>"), WriterText);
  1979. simple.something = true;
  1980. Serialize (simple);
  1981. 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);
  1982. }
  1983. [Test]
  1984. [Category ("NotWorking")]
  1985. public void TestSerializeGenSimpleStructInt ()
  1986. {
  1987. GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
  1988. Serialize (simple);
  1989. 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);
  1990. simple.something = 123;
  1991. Serialize (simple);
  1992. 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);
  1993. }
  1994. [Test]
  1995. [Category ("NotWorking")]
  1996. public void TestSerializeGenListClassString ()
  1997. {
  1998. GenListClass<string> genlist = new GenListClass<string> ();
  1999. Serialize (genlist);
  2000. 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);
  2001. genlist.somelist.Add ("Value1");
  2002. genlist.somelist.Add ("Value2");
  2003. Serialize (genlist);
  2004. 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);
  2005. }
  2006. [Test]
  2007. [Category ("NotWorking")]
  2008. public void TestSerializeGenListClassFloat ()
  2009. {
  2010. GenListClass<float> genlist = new GenListClass<float> ();
  2011. Serialize (genlist);
  2012. 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);
  2013. genlist.somelist.Add (1);
  2014. genlist.somelist.Add (2.2F);
  2015. Serialize (genlist);
  2016. 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);
  2017. }
  2018. [Test]
  2019. [Category ("NotWorking")]
  2020. public void TestSerializeGenListClassList ()
  2021. {
  2022. GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
  2023. Serialize (genlist);
  2024. 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);
  2025. GenListClass<int> inlist1 = new GenListClass<int> ();
  2026. inlist1.somelist.Add (1);
  2027. inlist1.somelist.Add (2);
  2028. GenListClass<int> inlist2 = new GenListClass<int> ();
  2029. inlist2.somelist.Add (10);
  2030. inlist2.somelist.Add (20);
  2031. genlist.somelist.Add (inlist1);
  2032. genlist.somelist.Add (inlist2);
  2033. Serialize (genlist);
  2034. 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);
  2035. }
  2036. [Test]
  2037. [Category ("NotWorking")]
  2038. public void TestSerializeGenListClassArray ()
  2039. {
  2040. GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
  2041. Serialize (genlist);
  2042. 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);
  2043. GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
  2044. genarr1.arr[0] = 'a';
  2045. genarr1.arr[1] = 'b';
  2046. genlist.somelist.Add (genarr1);
  2047. GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
  2048. genarr2.arr[0] = 'd';
  2049. genarr2.arr[1] = 'e';
  2050. genarr2.arr[2] = 'f';
  2051. genlist.somelist.Add (genarr2);
  2052. Serialize (genlist);
  2053. 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);
  2054. }
  2055. [Test]
  2056. [Category ("NotWorking")]
  2057. public void TestSerializeGenTwoClassCharDouble ()
  2058. {
  2059. GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
  2060. Serialize (gentwo);
  2061. 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);
  2062. gentwo.something1 = 'a';
  2063. gentwo.something2 = 2.2;
  2064. Serialize (gentwo);
  2065. 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);
  2066. }
  2067. [Test]
  2068. [Category ("NotWorking")]
  2069. public void TestSerializeGenDerivedClassDecimalShort ()
  2070. {
  2071. GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
  2072. Serialize (derived);
  2073. 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);
  2074. derived.something1 = "Value1";
  2075. derived.something2 = 1;
  2076. derived.another1 = 1.1M;
  2077. derived.another2 = -22;
  2078. Serialize (derived);
  2079. 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);
  2080. }
  2081. [Test]
  2082. [Category ("NotWorking")]
  2083. public void TestSerializeGenDerivedSecondClassByteUlong ()
  2084. {
  2085. GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
  2086. Serialize (derived2);
  2087. 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);
  2088. derived2.something1 = 1;
  2089. derived2.something2 = 222;
  2090. derived2.another1 = 111;
  2091. derived2.another2 = 222222;
  2092. Serialize (derived2);
  2093. 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);
  2094. }
  2095. [Test]
  2096. [Category ("NotWorking")]
  2097. public void TestSerializeGenNestedClass ()
  2098. {
  2099. GenNestedClass<string, int>.InnerClass<bool> nested =
  2100. new GenNestedClass<string, int>.InnerClass<bool> ();
  2101. Serialize (nested);
  2102. 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);
  2103. nested.inner = 5;
  2104. nested.something = true;
  2105. Serialize (nested);
  2106. 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);
  2107. }
  2108. [Test]
  2109. [Category ("NotWorking")]
  2110. public void TestSerializeGenListClassListNested ()
  2111. {
  2112. GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
  2113. new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
  2114. Serialize (genlist);
  2115. 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);
  2116. GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
  2117. new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
  2118. GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
  2119. inval1.inner = 1;
  2120. inval1.something = "ONE";
  2121. inlist1.somelist.Add (inval1);
  2122. GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
  2123. inval2.inner = 2;
  2124. inval2.something = "TWO";
  2125. inlist1.somelist.Add (inval2);
  2126. GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
  2127. new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
  2128. GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
  2129. inval3.inner = 30;
  2130. inval3.something = "THIRTY";
  2131. inlist2.somelist.Add (inval3);
  2132. genlist.somelist.Add (inlist1);
  2133. genlist.somelist.Add (inlist2);
  2134. Serialize (genlist);
  2135. 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);
  2136. }
  2137. public enum Myenum { one, two, three, four, five, six };
  2138. [Test]
  2139. [Category ("NotWorking")]
  2140. public void TestSerializeGenArrayClassEnum ()
  2141. {
  2142. GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
  2143. Serialize (genarr);
  2144. 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);
  2145. genarr.arr[0] = Myenum.one;
  2146. genarr.arr[1] = Myenum.three;
  2147. genarr.arr[2] = Myenum.five;
  2148. Serialize (genarr);
  2149. 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);
  2150. }
  2151. [Test]
  2152. [Category ("NotWorking")]
  2153. public void TestSerializeGenArrayStruct ()
  2154. {
  2155. GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
  2156. Serialize (genarr);
  2157. 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);
  2158. GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
  2159. genstruct.something = 111;
  2160. genarr.arr[0] = genstruct;
  2161. genstruct.something = 222;
  2162. genarr.arr[1] = genstruct;
  2163. genstruct.something = 333;
  2164. genarr.arr[2] = genstruct;
  2165. Serialize (genarr);
  2166. 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);
  2167. }
  2168. [Test]
  2169. [Category ("NotDotNet")]
  2170. // There is a bug in DotNet for this scenario, see comment below.
  2171. [Category ("NotWorking")]
  2172. public void TestSerializeGenArrayList ()
  2173. {
  2174. GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
  2175. Serialize (genarr);
  2176. 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);
  2177. GenListClass<string> genlist1 = new GenListClass<string> ();
  2178. genlist1.somelist.Add ("list1-val1");
  2179. genlist1.somelist.Add ("list1-val2");
  2180. genarr.arr[0] = genlist1;
  2181. GenListClass<string> genlist2 = new GenListClass<string> ();
  2182. genlist2.somelist.Add ("list2-val1");
  2183. genlist2.somelist.Add ("list2-val2");
  2184. genlist2.somelist.Add ("list2-val3");
  2185. genlist2.somelist.Add ("list2-val4");
  2186. genarr.arr[1] = genlist2;
  2187. GenListClass<string> genlist3 = new GenListClass<string> ();
  2188. genlist1.somelist.Add ("list3val");
  2189. genarr.arr[2] = genlist3;
  2190. Serialize (genarr);
  2191. 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);
  2192. // Following is the DotNet result which is a bug, where member
  2193. // of the third list is wrongly added to the first list.
  2194. //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</><:string>list3val</></></><:GenListClassOfString><:somelist><:string>list2-val1</><:string>list2-val2</><:string>list2-val3</><:string>list2-val4</></></><:GenListClassOfString><:somelist></></></></>", WriterText);
  2195. }
  2196. [Test]
  2197. [Category ("NotWorking")]
  2198. public void TestSerializeGenComplexStruct ()
  2199. {
  2200. GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
  2201. Serialize (complex);
  2202. 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);
  2203. complex.something = 123;
  2204. complex.simpleclass.something = 456;
  2205. complex.simplestruct.something = 789;
  2206. GenListClass<int> genlist = new GenListClass<int> ();
  2207. genlist.somelist.Add (100);
  2208. genlist.somelist.Add (200);
  2209. complex.listclass = genlist;
  2210. GenArrayClass<int> genarr = new GenArrayClass<int> ();
  2211. genarr.arr[0] = 11;
  2212. genarr.arr[1] = 22;
  2213. genarr.arr[2] = 33;
  2214. complex.arrayclass = genarr;
  2215. complex.twoclass.something1 = 10;
  2216. complex.twoclass.something2 = "Ten";
  2217. complex.derivedclass.another1 = 1;
  2218. complex.derivedclass.another2 = "one";
  2219. complex.derivedclass.something1 = "two";
  2220. complex.derivedclass.something2 = 2;
  2221. complex.derived2.another1 = 3;
  2222. complex.derived2.another2 = "three";
  2223. complex.derived2.something1 = 4;
  2224. complex.derived2.something2 = "four";
  2225. complex.nestedouter.outer = 5;
  2226. complex.nestedinner.inner = "six";
  2227. complex.nestedinner.something = 6;
  2228. Serialize (complex);
  2229. 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);
  2230. }
  2231. #endif
  2232. #endregion //GenericsSeralizationTests
  2233. public class XmlArrayOnInt
  2234. {
  2235. [XmlArray]
  2236. public int Bogus;
  2237. }
  2238. public class XmlArrayUnqualifiedWithNamespace
  2239. {
  2240. [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
  2241. public ArrayList Sane = new ArrayList ();
  2242. }
  2243. public class XmlArrayItemUnqualifiedWithNamespace
  2244. {
  2245. [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
  2246. public ArrayList Sane = new ArrayList ();
  2247. }
  2248. [XmlRoot (Namespace = "urn:foo")]
  2249. public class XmlArrayOnArrayList
  2250. {
  2251. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2252. public ArrayList Sane = new ArrayList ();
  2253. }
  2254. [XmlRoot (Namespace = "urn:foo")]
  2255. public class XmlArrayOnArray
  2256. {
  2257. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2258. public string[] Sane = new string[] { "foo", "bar" };
  2259. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2260. public ArrayItemInXmlArray[] Mids =
  2261. new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
  2262. }
  2263. [XmlType (Namespace = "urn:gyabo")]
  2264. public class ArrayItemInXmlArray
  2265. {
  2266. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2267. public string[] Whee = new string[] { "foo", "bar" };
  2268. }
  2269. [XmlRoot ("Base64Binary")]
  2270. public class Base64Binary
  2271. {
  2272. [XmlAttribute (DataType = "base64Binary")]
  2273. public byte [] Data = new byte [] {1, 2, 3};
  2274. }
  2275. [XmlRoot ("HexBinary")]
  2276. public class HexBinary
  2277. {
  2278. [XmlAttribute (DataType = "hexBinary")]
  2279. public byte[] Data = new byte[] { 1, 2, 3 };
  2280. }
  2281. public class CDataTextNodesType
  2282. {
  2283. public CDataTextNodesInternal foo;
  2284. }
  2285. public class CDataTextNodesInternal
  2286. {
  2287. [XmlText]
  2288. public string Value;
  2289. }
  2290. void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
  2291. {
  2292. Assert.Fail ();
  2293. }
  2294. // Helper methods
  2295. public static string Infoset (string sx)
  2296. {
  2297. XmlDocument doc = new XmlDocument ();
  2298. doc.LoadXml (sx);
  2299. StringBuilder sb = new StringBuilder ();
  2300. GetInfoset (doc.DocumentElement, sb);
  2301. return sb.ToString ();
  2302. }
  2303. public static string Infoset (XmlNode nod)
  2304. {
  2305. StringBuilder sb = new StringBuilder ();
  2306. GetInfoset (nod, sb);
  2307. return sb.ToString ();
  2308. }
  2309. static void GetInfoset (XmlNode nod, StringBuilder sb)
  2310. {
  2311. switch (nod.NodeType) {
  2312. case XmlNodeType.Attribute:
  2313. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  2314. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  2315. break;
  2316. case XmlNodeType.Element:
  2317. XmlElement elem = (XmlElement) nod;
  2318. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  2319. ArrayList ats = new ArrayList ();
  2320. foreach (XmlAttribute at in elem.Attributes)
  2321. ats.Add (at.LocalName + " " + at.NamespaceURI);
  2322. ats.Sort ();
  2323. foreach (string name in ats) {
  2324. string[] nn = name.Split (' ');
  2325. GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
  2326. }
  2327. sb.Append (">");
  2328. foreach (XmlNode cn in elem.ChildNodes)
  2329. GetInfoset (cn, sb);
  2330. sb.Append ("</>");
  2331. break;
  2332. default:
  2333. sb.Append (nod.OuterXml);
  2334. break;
  2335. }
  2336. }
  2337. static XmlTypeMapping CreateSoapMapping (Type type)
  2338. {
  2339. SoapReflectionImporter importer = new SoapReflectionImporter ();
  2340. return importer.ImportTypeMapping (type);
  2341. }
  2342. static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
  2343. {
  2344. SoapReflectionImporter importer = new SoapReflectionImporter (ao);
  2345. return importer.ImportTypeMapping (type);
  2346. }
  2347. static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
  2348. {
  2349. SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
  2350. return importer.ImportTypeMapping (type);
  2351. }
  2352. }
  2353. }