XmlReflectionImporterTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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.Xml.Serialization
  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(tm.ElementName, "int");
  87. AssertEquals(tm.Namespace, "");
  88. AssertEquals(tm.TypeName, "Int32");
  89. AssertEquals(tm.TypeFullName, "System.Int32");
  90. }
  91. [Test]
  92. public void TestIntArrayTypeMapping()
  93. {
  94. XmlTypeMapping tm = Map(typeof(int[]));
  95. AssertEquals(tm.ElementName, "ArrayOfInt");
  96. AssertEquals(tm.Namespace, "");
  97. AssertEquals(tm.TypeName, "Int32[]");
  98. AssertEquals(tm.TypeFullName, "System.Int32[]");
  99. }
  100. [Test]
  101. public void TestStringTypeMapping()
  102. {
  103. XmlTypeMapping tm = Map(typeof(string));
  104. AssertEquals(tm.ElementName, "string");
  105. AssertEquals(tm.Namespace, "");
  106. AssertEquals(tm.TypeName, "String");
  107. AssertEquals(tm.TypeFullName, "System.String");
  108. }
  109. [Test]
  110. public void TestObjectTypeMapping()
  111. {
  112. XmlTypeMapping tm = Map(typeof(object));
  113. AssertEquals(tm.ElementName, "anyType");
  114. AssertEquals(tm.Namespace, "");
  115. AssertEquals(tm.TypeName, "Object");
  116. AssertEquals(tm.TypeFullName, "System.Object");
  117. }
  118. [Test]
  119. public void TestByteTypeMapping()
  120. {
  121. XmlTypeMapping tm = Map(typeof(byte));
  122. AssertEquals(tm.ElementName, "unsignedByte");
  123. AssertEquals(tm.Namespace, "");
  124. AssertEquals(tm.TypeName, "Byte");
  125. AssertEquals(tm.TypeFullName, "System.Byte");
  126. }
  127. [Test]
  128. public void TestByteArrayTypeMapping()
  129. {
  130. XmlTypeMapping tm = Map(typeof(byte[]));
  131. AssertEquals(tm.ElementName, "base64Binary");
  132. AssertEquals(tm.Namespace, "");
  133. AssertEquals(tm.TypeName, "Byte[]");
  134. AssertEquals(tm.TypeFullName, "System.Byte[]");
  135. }
  136. [Test]
  137. public void TestBoolTypeMapping()
  138. {
  139. XmlTypeMapping tm = Map(typeof(bool));
  140. AssertEquals(tm.ElementName, "boolean");
  141. AssertEquals(tm.Namespace, "");
  142. AssertEquals(tm.TypeName, "Boolean");
  143. AssertEquals(tm.TypeFullName, "System.Boolean");
  144. }
  145. [Test]
  146. public void TestShortTypeMapping()
  147. {
  148. XmlTypeMapping tm = Map(typeof(short));
  149. AssertEquals(tm.ElementName, "short");
  150. AssertEquals(tm.Namespace, "");
  151. AssertEquals(tm.TypeName, "Int16");
  152. AssertEquals(tm.TypeFullName, "System.Int16");
  153. }
  154. [Test]
  155. public void TestUnsignedShortTypeMapping()
  156. {
  157. XmlTypeMapping tm = Map(typeof(ushort));
  158. AssertEquals(tm.ElementName, "unsignedShort");
  159. AssertEquals(tm.Namespace, "");
  160. AssertEquals(tm.TypeName, "UInt16");
  161. AssertEquals(tm.TypeFullName, "System.UInt16");
  162. }
  163. [Test]
  164. public void TestUIntTypeMapping()
  165. {
  166. XmlTypeMapping tm = Map(typeof(uint));
  167. AssertEquals(tm.ElementName, "unsignedInt");
  168. AssertEquals(tm.Namespace, "");
  169. AssertEquals(tm.TypeName, "UInt32");
  170. AssertEquals(tm.TypeFullName, "System.UInt32");
  171. }
  172. [Test]
  173. public void TestLongTypeMapping()
  174. {
  175. XmlTypeMapping tm = Map(typeof(long));
  176. AssertEquals(tm.ElementName, "long");
  177. AssertEquals(tm.Namespace, "");
  178. AssertEquals(tm.TypeName, "Int64");
  179. AssertEquals(tm.TypeFullName, "System.Int64");
  180. }
  181. [Test]
  182. public void TestULongTypeMapping()
  183. {
  184. XmlTypeMapping tm = Map(typeof(ulong));
  185. AssertEquals(tm.ElementName, "unsignedLong");
  186. AssertEquals(tm.Namespace, "");
  187. AssertEquals(tm.TypeName, "UInt64");
  188. AssertEquals(tm.TypeFullName, "System.UInt64");
  189. }
  190. [Test]
  191. public void TestFloatTypeMapping()
  192. {
  193. XmlTypeMapping tm = Map(typeof(float));
  194. AssertEquals(tm.ElementName, "float");
  195. AssertEquals(tm.Namespace, "");
  196. AssertEquals(tm.TypeName, "Single");
  197. AssertEquals(tm.TypeFullName, "System.Single");
  198. }
  199. [Test]
  200. public void TestDoubleTypeMapping()
  201. {
  202. XmlTypeMapping tm = Map(typeof(double));
  203. AssertEquals(tm.ElementName, "double");
  204. AssertEquals(tm.Namespace, "");
  205. AssertEquals(tm.TypeName, "Double");
  206. AssertEquals(tm.TypeFullName, "System.Double");
  207. }
  208. [Test]
  209. public void TestDateTimeTypeMapping()
  210. {
  211. XmlTypeMapping tm = Map(typeof(DateTime));
  212. AssertEquals(tm.ElementName, "dateTime");
  213. AssertEquals(tm.Namespace, "");
  214. AssertEquals(tm.TypeName, "DateTime");
  215. AssertEquals(tm.TypeFullName, "System.DateTime");
  216. }
  217. [Test]
  218. public void TestGuidTypeMapping()
  219. {
  220. XmlTypeMapping tm = Map(typeof(Guid));
  221. AssertEquals(tm.ElementName, "guid");
  222. AssertEquals(tm.Namespace, "");
  223. AssertEquals(tm.TypeName, "Guid");
  224. AssertEquals(tm.TypeFullName, "System.Guid");
  225. }
  226. [Test]
  227. public void TestDecimalTypeMapping()
  228. {
  229. XmlTypeMapping tm = Map(typeof(decimal));
  230. AssertEquals(tm.ElementName, "decimal");
  231. AssertEquals(tm.Namespace, "");
  232. AssertEquals(tm.TypeName, "Decimal");
  233. AssertEquals(tm.TypeFullName, "System.Decimal");
  234. }
  235. [Test]
  236. public void TestXmlQualifiedNameTypeMapping()
  237. {
  238. XmlTypeMapping tm = Map(typeof(XmlQualifiedName));
  239. AssertEquals(tm.ElementName, "QName");
  240. AssertEquals(tm.Namespace, "");
  241. AssertEquals(tm.TypeName, "XmlQualifiedName");
  242. AssertEquals(tm.TypeFullName, "System.Xml.XmlQualifiedName");
  243. }
  244. [Test]
  245. public void TestSByteTypeMapping()
  246. {
  247. XmlTypeMapping tm = Map(typeof(sbyte));
  248. AssertEquals(tm.ElementName, "byte");
  249. AssertEquals(tm.Namespace, "");
  250. AssertEquals(tm.TypeName, "SByte");
  251. AssertEquals(tm.TypeFullName, "System.SByte");
  252. }
  253. [Test]
  254. public void TestCharTypeMapping()
  255. {
  256. XmlTypeMapping tm = Map(typeof(char));
  257. AssertEquals(tm.ElementName, "char");
  258. AssertEquals(tm.Namespace, "");
  259. AssertEquals(tm.TypeName, "Char");
  260. AssertEquals(tm.TypeFullName, "System.Char");
  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(tm.ElementName, "int");
  303. AssertEquals(tm.Namespace, SomeNamespace);
  304. AssertEquals(tm.TypeName, "Int32");
  305. AssertEquals(tm.TypeFullName, "System.Int32");
  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(tm.ElementName, "SimpleClass");
  316. AssertEquals(tm.Namespace, "");
  317. AssertEquals(tm.TypeName, "SimpleClass");
  318. AssertEquals(tm.TypeFullName, "MonoTests.System.Xml.TestClasses.SimpleClass");
  319. }
  320. [Test]
  321. public void TestImportMembersMapping()
  322. {
  323. Type type = typeof(SimpleClass);
  324. XmlAttributes attrs = new XmlAttributes();
  325. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  326. overrides.Add(typeof(SimpleClass), attrs);
  327. XmlReflectionMember[] members = new XmlReflectionMember[0];
  328. XmlMembersMapping mm;
  329. try
  330. {
  331. mm = MembersMap(type, overrides, members, true);
  332. Fail("Should not be able to fetch an empty XmlMembersMapping");
  333. }
  334. catch (Exception)
  335. {
  336. }
  337. XmlReflectionMember rm = new XmlReflectionMember();
  338. rm.IsReturnValue = false;
  339. rm.MemberName = "something";
  340. rm.MemberType = typeof(string);
  341. members = new XmlReflectionMember[1];
  342. members[0] = rm;
  343. mm = MembersMap(type, overrides, members, false);
  344. Equals(mm.Count, 1);
  345. XmlMemberMapping smm = mm[0];
  346. AssertEquals(smm.Any, false);
  347. AssertEquals(smm.ElementName, "something");
  348. AssertEquals(smm.MemberName, "something");
  349. AssertEquals(smm.Namespace, null);
  350. AssertEquals(smm.TypeFullName, "System.String");
  351. AssertEquals(smm.TypeName, "string");
  352. AssertEquals(smm.TypeNamespace, null);
  353. rm = new XmlReflectionMember();
  354. rm.IsReturnValue = false;
  355. rm.MemberName = "nothing";
  356. rm.MemberType = typeof(string);
  357. members = new XmlReflectionMember[1];
  358. members[0] = rm;
  359. mm = MembersMap(type, overrides, members, false);
  360. Equals(mm.Count, 0);
  361. }
  362. [Test]
  363. public void TestIntTypeMappingWithXmlRootAttribute()
  364. {
  365. const string TheNamespace = "another:urn";
  366. XmlRootAttribute root = new XmlRootAttribute("price");
  367. root.Namespace = TheNamespace;
  368. XmlTypeMapping tm = Map(typeof(int), root);
  369. AssertEquals(tm.ElementName, "price");
  370. AssertEquals(tm.Namespace, TheNamespace);
  371. AssertEquals(tm.TypeName, "Int32");
  372. AssertEquals(tm.TypeFullName, "System.Int32");
  373. }
  374. }
  375. }