XmlSerializerTests.cs 89 KB

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