XmlSerializerTests.cs 132 KB

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