XmlReflectionImporterTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //
  2. // System.Xml.Serialization.XmlReflectionImporterTests
  3. //
  4. // Author:
  5. // Erik LeBel ([email protected])
  6. //
  7. // (C) 2003 Erik LeBel
  8. //
  9. // FIXME test some of these with Xml Attributes attached to some members:
  10. // do the names get carried over to Element for XmlAttributeAttribute and XmlElementAttribute?
  11. //
  12. using System;
  13. using System.Xml;
  14. using System.Xml.Serialization;
  15. using NUnit.Framework;
  16. using MonoTests.System.Xml.TestClasses;
  17. namespace MonoTests.System.XmlSerialization
  18. {
  19. // debugging class
  20. internal class Debug
  21. {
  22. public static void Print(XmlTypeMapping tm)
  23. {
  24. Console.WriteLine("/XmlTypeMapping:");
  25. Console.WriteLine("ElementName: {0} ", tm.ElementName);
  26. Console.WriteLine("Namespace: {0} ", tm.Namespace);
  27. Console.WriteLine("TypeName: {0} ", tm.TypeName);
  28. Console.WriteLine("FullTypeName: {0} ", tm.TypeFullName);
  29. }
  30. public static void Print(XmlMemberMapping mm)
  31. {
  32. Console.WriteLine("/XmlMemberMapping:");
  33. Console.WriteLine("Any: {0} ", mm.Any);
  34. Console.WriteLine("ElementName: {0} ", mm.ElementName);
  35. Console.WriteLine("MemberName: {0} ", mm.MemberName);
  36. Console.WriteLine("Namespace: {0} ", mm.Namespace);
  37. Console.WriteLine("TypeFullName: {0} ", mm.TypeFullName);
  38. Console.WriteLine("TypeName: {0} ", mm.TypeName);
  39. Console.WriteLine("TypeNamespace: {0} ", mm.TypeNamespace);
  40. }
  41. }
  42. [TestFixture]
  43. public class XmlReflectionImporterTests : Assertion
  44. {
  45. private const string SomeNamespace = "some:urn";
  46. private const string AnotherNamespace = "another:urn";
  47. // these Map methods re-create the XmlReflectionImporter at every call.
  48. private XmlTypeMapping Map(Type t)
  49. {
  50. XmlReflectionImporter ri = new XmlReflectionImporter();
  51. XmlTypeMapping tm = ri.ImportTypeMapping(t);
  52. //Debug.Print(tm);
  53. return tm;
  54. }
  55. private XmlTypeMapping Map(Type t, XmlRootAttribute root)
  56. {
  57. XmlReflectionImporter ri = new XmlReflectionImporter();
  58. XmlTypeMapping tm = ri.ImportTypeMapping(t, root);
  59. return tm;
  60. }
  61. private XmlTypeMapping Map(Type t, string ns)
  62. {
  63. XmlReflectionImporter ri = new XmlReflectionImporter(ns);
  64. XmlTypeMapping tm = ri.ImportTypeMapping(t);
  65. //Debug.Print(tm);
  66. return tm;
  67. }
  68. private XmlTypeMapping Map(Type t, XmlAttributeOverrides overrides)
  69. {
  70. XmlReflectionImporter ri = new XmlReflectionImporter(overrides);
  71. XmlTypeMapping tm = ri.ImportTypeMapping(t);
  72. //Debug.Print(tm);
  73. return tm;
  74. }
  75. private XmlMembersMapping MembersMap(Type t, XmlAttributeOverrides overrides,
  76. XmlReflectionMember [] members, bool inContainer)
  77. {
  78. XmlReflectionImporter ri = new XmlReflectionImporter(overrides);
  79. XmlMembersMapping mm = ri.ImportMembersMapping(null, null, members, inContainer);
  80. return mm;
  81. }
  82. [Test]
  83. public void TestIntTypeMapping()
  84. {
  85. XmlTypeMapping tm = Map(typeof(int));
  86. AssertEquals("int", tm.ElementName);
  87. AssertEquals("", tm.Namespace);
  88. AssertEquals("Int32", tm.TypeName);
  89. AssertEquals("System.Int32", tm.TypeFullName);
  90. }
  91. [Test]
  92. public void TestIntArrayTypeMapping()
  93. {
  94. XmlTypeMapping tm = Map(typeof(int[]));
  95. AssertEquals("ArrayOfInt", tm.ElementName);
  96. AssertEquals("", tm.Namespace);
  97. AssertEquals("Int32[]", tm.TypeName);
  98. AssertEquals("System.Int32[]", tm.TypeFullName);
  99. }
  100. [Test]
  101. public void TestStringTypeMapping()
  102. {
  103. XmlTypeMapping tm = Map(typeof(string));
  104. AssertEquals("string", tm.ElementName);
  105. AssertEquals("", tm.Namespace);
  106. AssertEquals("String", tm.TypeName);
  107. AssertEquals("System.String", tm.TypeFullName);
  108. }
  109. [Test]
  110. public void TestObjectTypeMapping()
  111. {
  112. XmlTypeMapping tm = Map(typeof(object));
  113. AssertEquals("anyType", tm.ElementName);
  114. AssertEquals("", tm.Namespace);
  115. AssertEquals("Object", tm.TypeName);
  116. AssertEquals("System.Object", tm.TypeFullName);
  117. }
  118. [Test]
  119. public void TestByteTypeMapping()
  120. {
  121. XmlTypeMapping tm = Map(typeof(byte));
  122. AssertEquals("unsignedByte", tm.ElementName);
  123. AssertEquals("", tm.Namespace);
  124. AssertEquals("Byte", tm.TypeName);
  125. AssertEquals("System.Byte", tm.TypeFullName);
  126. }
  127. [Test]
  128. public void TestByteArrayTypeMapping()
  129. {
  130. XmlTypeMapping tm = Map(typeof(byte[]));
  131. AssertEquals("base64Binary", tm.ElementName);
  132. AssertEquals("", tm.Namespace);
  133. AssertEquals("Byte[]", tm.TypeName);
  134. AssertEquals("System.Byte[]", tm.TypeFullName);
  135. }
  136. [Test]
  137. public void TestBoolTypeMapping()
  138. {
  139. XmlTypeMapping tm = Map(typeof(bool));
  140. AssertEquals("boolean", tm.ElementName);
  141. AssertEquals("", tm.Namespace);
  142. AssertEquals("Boolean", tm.TypeName);
  143. AssertEquals("System.Boolean", tm.TypeFullName);
  144. }
  145. [Test]
  146. public void TestShortTypeMapping()
  147. {
  148. XmlTypeMapping tm = Map(typeof(short));
  149. AssertEquals("short", tm.ElementName);
  150. AssertEquals("", tm.Namespace);
  151. AssertEquals("Int16", tm.TypeName);
  152. AssertEquals("System.Int16", tm.TypeFullName);
  153. }
  154. [Test]
  155. public void TestUnsignedShortTypeMapping()
  156. {
  157. XmlTypeMapping tm = Map(typeof(ushort));
  158. AssertEquals("unsignedShort", tm.ElementName);
  159. AssertEquals("", tm.Namespace);
  160. AssertEquals("UInt16", tm.TypeName);
  161. AssertEquals("System.UInt16", tm.TypeFullName);
  162. }
  163. [Test]
  164. public void TestUIntTypeMapping()
  165. {
  166. XmlTypeMapping tm = Map(typeof(uint));
  167. AssertEquals("unsignedInt", tm.ElementName);
  168. AssertEquals("", tm.Namespace);
  169. AssertEquals("UInt32", tm.TypeName);
  170. AssertEquals("System.UInt32", tm.TypeFullName);
  171. }
  172. [Test]
  173. public void TestLongTypeMapping()
  174. {
  175. XmlTypeMapping tm = Map(typeof(long));
  176. AssertEquals("long", tm.ElementName);
  177. AssertEquals("", tm.Namespace);
  178. AssertEquals("Int64", tm.TypeName);
  179. AssertEquals("System.Int64", tm.TypeFullName);
  180. }
  181. [Test]
  182. public void TestULongTypeMapping()
  183. {
  184. XmlTypeMapping tm = Map(typeof(ulong));
  185. AssertEquals("unsignedLong", tm.ElementName);
  186. AssertEquals("", tm.Namespace);
  187. AssertEquals("UInt64", tm.TypeName);
  188. AssertEquals("System.UInt64", tm.TypeFullName);
  189. }
  190. [Test]
  191. public void TestFloatTypeMapping()
  192. {
  193. XmlTypeMapping tm = Map(typeof(float));
  194. AssertEquals("float", tm.ElementName);
  195. AssertEquals("", tm.Namespace);
  196. AssertEquals("Single", tm.TypeName);
  197. AssertEquals("System.Single", tm.TypeFullName);
  198. }
  199. [Test]
  200. public void TestDoubleTypeMapping()
  201. {
  202. XmlTypeMapping tm = Map(typeof(double));
  203. AssertEquals("double", tm.ElementName);
  204. AssertEquals("", tm.Namespace);
  205. AssertEquals("Double", tm.TypeName);
  206. AssertEquals("System.Double", tm.TypeFullName);
  207. }
  208. [Test]
  209. public void TestDateTimeTypeMapping()
  210. {
  211. XmlTypeMapping tm = Map(typeof(DateTime));
  212. AssertEquals("dateTime", tm.ElementName);
  213. AssertEquals("", tm.Namespace);
  214. AssertEquals("DateTime", tm.TypeName);
  215. AssertEquals("System.DateTime", tm.TypeFullName);
  216. }
  217. [Test]
  218. public void TestGuidTypeMapping()
  219. {
  220. XmlTypeMapping tm = Map(typeof(Guid));
  221. AssertEquals("guid", tm.ElementName);
  222. AssertEquals("", tm.Namespace);
  223. AssertEquals("Guid", tm.TypeName);
  224. AssertEquals("System.Guid", tm.TypeFullName);
  225. }
  226. [Test]
  227. public void TestDecimalTypeMapping()
  228. {
  229. XmlTypeMapping tm = Map(typeof(decimal));
  230. AssertEquals("decimal", tm.ElementName);
  231. AssertEquals("", tm.Namespace);
  232. AssertEquals("Decimal", tm.TypeName);
  233. AssertEquals("System.Decimal", tm.TypeFullName);
  234. }
  235. [Test]
  236. public void TestXmlQualifiedNameTypeMapping()
  237. {
  238. XmlTypeMapping tm = Map(typeof(XmlQualifiedName));
  239. AssertEquals("QName", tm.ElementName);
  240. AssertEquals("", tm.Namespace);
  241. AssertEquals("XmlQualifiedName", tm.TypeName);
  242. AssertEquals("System.Xml.XmlQualifiedName", tm.TypeFullName);
  243. }
  244. [Test]
  245. public void TestSByteTypeMapping()
  246. {
  247. XmlTypeMapping tm = Map(typeof(sbyte));
  248. AssertEquals("byte", tm.ElementName);
  249. AssertEquals("", tm.Namespace);
  250. AssertEquals("SByte", tm.TypeName);
  251. AssertEquals("System.SByte", tm.TypeFullName);
  252. }
  253. [Test]
  254. public void TestCharTypeMapping()
  255. {
  256. XmlTypeMapping tm = Map(typeof(char));
  257. AssertEquals("char", tm.ElementName);
  258. AssertEquals("", tm.Namespace);
  259. AssertEquals("Char", tm.TypeName);
  260. AssertEquals("System.Char", tm.TypeFullName);
  261. }
  262. [Test]
  263. public void TestNullTypeMapping()
  264. {
  265. try
  266. {
  267. XmlTypeMapping tm = Map(null);
  268. Fail("Should not be able to map a null type");
  269. }
  270. catch (Exception)
  271. {
  272. }
  273. }
  274. [Test]
  275. public void TestInvalidClassTypeMapping()
  276. {
  277. try
  278. {
  279. // this can use any class
  280. XmlTypeMapping tm = Map(typeof(SimpleClass));
  281. Fail("Should not be able to this type");
  282. }
  283. catch (Exception)
  284. {
  285. }
  286. }
  287. /*
  288. [Test]
  289. public void TestTypeMapping()
  290. {
  291. XmlTypeMapping tm = Map(typeof());
  292. AssertEquals(tm.ElementName, "");
  293. AssertEquals(tm.Namespace, "");
  294. AssertEquals(tm.TypeName, "");
  295. AssertEquals(tm.TypeFullName, "System.");
  296. }
  297. */
  298. [Test]
  299. public void TestIntTypeMappingWithDefaultNamespaces()
  300. {
  301. XmlTypeMapping tm = Map(typeof(int), SomeNamespace);
  302. AssertEquals("int", tm.ElementName);
  303. AssertEquals(SomeNamespace, tm.Namespace);
  304. AssertEquals("Int32", tm.TypeName);
  305. AssertEquals("System.Int32", tm.TypeFullName);
  306. }
  307. [Test]
  308. public void TestValidClassTypeMapping()
  309. {
  310. Type type = typeof(SimpleClass);
  311. XmlAttributes attrs = new XmlAttributes();
  312. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  313. overrides.Add(typeof(SimpleClass), attrs);
  314. XmlTypeMapping tm = Map(type, overrides);
  315. AssertEquals("SimpleClass", tm.ElementName);
  316. AssertEquals("", tm.Namespace);
  317. AssertEquals("SimpleClass", tm.TypeName);
  318. AssertEquals("MonoTests.System.Xml.TestClasses.SimpleClass", tm.TypeFullName);
  319. }
  320. [Test]
  321. [Category ("NotWorking")]
  322. public void TestImportMembersMapping()
  323. {
  324. Type type = typeof(SimpleClass);
  325. XmlAttributes attrs = new XmlAttributes();
  326. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  327. overrides.Add(typeof(SimpleClass), attrs);
  328. XmlReflectionMember[] members = new XmlReflectionMember[0];
  329. XmlMembersMapping mm;
  330. try
  331. {
  332. mm = MembersMap(type, overrides, members, true);
  333. Fail("Should not be able to fetch an empty XmlMembersMapping");
  334. }
  335. catch (Exception)
  336. {
  337. }
  338. XmlReflectionMember rm = new XmlReflectionMember();
  339. rm.IsReturnValue = false;
  340. rm.MemberName = "something";
  341. rm.MemberType = typeof(string);
  342. members = new XmlReflectionMember[1];
  343. members[0] = rm;
  344. mm = MembersMap(type, overrides, members, false);
  345. Equals(mm.Count, 1);
  346. XmlMemberMapping smm = mm[0];
  347. AssertEquals(false, smm.Any);
  348. AssertEquals("something", smm.ElementName);
  349. AssertEquals("something", smm.MemberName);
  350. AssertEquals(null, smm.Namespace);
  351. AssertEquals("System.String", smm.TypeFullName);
  352. AssertEquals("string", smm.TypeName);
  353. AssertEquals(null, smm.TypeNamespace);
  354. rm = new XmlReflectionMember();
  355. rm.IsReturnValue = false;
  356. rm.MemberName = "nothing";
  357. rm.MemberType = typeof(string);
  358. members = new XmlReflectionMember[1];
  359. members[0] = rm;
  360. mm = MembersMap(type, overrides, members, false);
  361. Equals(mm.Count, 0);
  362. }
  363. [Test]
  364. public void TestIntTypeMappingWithXmlRootAttribute()
  365. {
  366. const string TheNamespace = "another:urn";
  367. XmlRootAttribute root = new XmlRootAttribute("price");
  368. root.Namespace = TheNamespace;
  369. XmlTypeMapping tm = Map(typeof(int), root);
  370. AssertEquals("price", tm.ElementName);
  371. AssertEquals(TheNamespace, tm.Namespace);
  372. AssertEquals("Int32", tm.TypeName);
  373. AssertEquals("System.Int32", tm.TypeFullName);
  374. }
  375. [Test]
  376. [ExpectedException (typeof (InvalidOperationException))]
  377. public void TestSerializeWrongChoice ()
  378. {
  379. new XmlSerializer (typeof(WrongChoices));
  380. }
  381. }
  382. }