XmlSerializerTests.cs 111 KB

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