| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- //
- // System.Xml.Serialization.XmlReflectionImporterTests
- //
- // Author:
- // Erik LeBel ([email protected])
- //
- // (C) 2003 Erik LeBel
- //
- // FIXME test some of these with Xml Attributes attached to some members:
- // do the names get carried over to Element for XmlAttributeAttribute and XmlElementAttribute?
- //
- using System;
- using System.Xml;
- using System.Xml.Serialization;
- using NUnit.Framework;
- using MonoTests.System.Xml.TestClasses;
- namespace MonoTests.System.Xml.Serialization
- {
- // debugging class
- internal class Debug
- {
- public static void Print(XmlTypeMapping tm)
- {
- Console.WriteLine("/XmlTypeMapping:");
- Console.WriteLine("ElementName: {0} ", tm.ElementName);
- Console.WriteLine("Namespace: {0} ", tm.Namespace);
- Console.WriteLine("TypeName: {0} ", tm.TypeName);
- Console.WriteLine("FullTypeName: {0} ", tm.TypeFullName);
- }
- public static void Print(XmlMemberMapping mm)
- {
- Console.WriteLine("/XmlMemberMapping:");
- Console.WriteLine("Any: {0} ", mm.Any);
- Console.WriteLine("ElementName: {0} ", mm.ElementName);
- Console.WriteLine("MemberName: {0} ", mm.MemberName);
- Console.WriteLine("Namespace: {0} ", mm.Namespace);
- Console.WriteLine("TypeFullName: {0} ", mm.TypeFullName);
- Console.WriteLine("TypeName: {0} ", mm.TypeName);
- Console.WriteLine("TypeNamespace: {0} ", mm.TypeNamespace);
- }
- }
- [TestFixture]
- public class XmlReflectionImporterTests : Assertion
- {
- private const string SomeNamespace = "some:urn";
- private const string AnotherNamespace = "another:urn";
- // these Map methods re-create the XmlReflectionImporter at every call.
- private XmlTypeMapping Map(Type t)
- {
- XmlReflectionImporter ri = new XmlReflectionImporter();
- XmlTypeMapping tm = ri.ImportTypeMapping(t);
- //Debug.Print(tm);
- return tm;
- }
- private XmlTypeMapping Map(Type t, XmlRootAttribute root)
- {
- XmlReflectionImporter ri = new XmlReflectionImporter();
- XmlTypeMapping tm = ri.ImportTypeMapping(t, root);
- return tm;
- }
- private XmlTypeMapping Map(Type t, string ns)
- {
- XmlReflectionImporter ri = new XmlReflectionImporter(ns);
- XmlTypeMapping tm = ri.ImportTypeMapping(t);
- //Debug.Print(tm);
- return tm;
- }
- private XmlTypeMapping Map(Type t, XmlAttributeOverrides overrides)
- {
- XmlReflectionImporter ri = new XmlReflectionImporter(overrides);
- XmlTypeMapping tm = ri.ImportTypeMapping(t);
- //Debug.Print(tm);
- return tm;
- }
- private XmlMembersMapping MembersMap(Type t, XmlAttributeOverrides overrides,
- XmlReflectionMember [] members, bool inContainer)
- {
- XmlReflectionImporter ri = new XmlReflectionImporter(overrides);
- XmlMembersMapping mm = ri.ImportMembersMapping(null, null, members, inContainer);
-
- return mm;
- }
-
- [Test]
- public void TestIntTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(int));
- AssertEquals(tm.ElementName, "int");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Int32");
- AssertEquals(tm.TypeFullName, "System.Int32");
- }
- [Test]
- public void TestIntArrayTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(int[]));
- AssertEquals(tm.ElementName, "ArrayOfInt");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Int32[]");
- AssertEquals(tm.TypeFullName, "System.Int32[]");
- }
- [Test]
- public void TestStringTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(string));
- AssertEquals(tm.ElementName, "string");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "String");
- AssertEquals(tm.TypeFullName, "System.String");
- }
- [Test]
- public void TestObjectTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(object));
- AssertEquals(tm.ElementName, "anyType");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Object");
- AssertEquals(tm.TypeFullName, "System.Object");
- }
- [Test]
- public void TestByteTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(byte));
- AssertEquals(tm.ElementName, "unsignedByte");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Byte");
- AssertEquals(tm.TypeFullName, "System.Byte");
- }
- [Test]
- public void TestByteArrayTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(byte[]));
- AssertEquals(tm.ElementName, "base64Binary");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Byte[]");
- AssertEquals(tm.TypeFullName, "System.Byte[]");
- }
- [Test]
- public void TestBoolTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(bool));
- AssertEquals(tm.ElementName, "boolean");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Boolean");
- AssertEquals(tm.TypeFullName, "System.Boolean");
- }
- [Test]
- public void TestShortTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(short));
- AssertEquals(tm.ElementName, "short");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Int16");
- AssertEquals(tm.TypeFullName, "System.Int16");
- }
- [Test]
- public void TestUnsignedShortTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(ushort));
- AssertEquals(tm.ElementName, "unsignedShort");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "UInt16");
- AssertEquals(tm.TypeFullName, "System.UInt16");
- }
-
- [Test]
- public void TestUIntTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(uint));
- AssertEquals(tm.ElementName, "unsignedInt");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "UInt32");
- AssertEquals(tm.TypeFullName, "System.UInt32");
- }
-
- [Test]
- public void TestLongTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(long));
- AssertEquals(tm.ElementName, "long");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Int64");
- AssertEquals(tm.TypeFullName, "System.Int64");
- }
-
- [Test]
- public void TestULongTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(ulong));
- AssertEquals(tm.ElementName, "unsignedLong");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "UInt64");
- AssertEquals(tm.TypeFullName, "System.UInt64");
- }
-
- [Test]
- public void TestFloatTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(float));
- AssertEquals(tm.ElementName, "float");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Single");
- AssertEquals(tm.TypeFullName, "System.Single");
- }
-
- [Test]
- public void TestDoubleTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(double));
- AssertEquals(tm.ElementName, "double");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Double");
- AssertEquals(tm.TypeFullName, "System.Double");
- }
-
- [Test]
- public void TestDateTimeTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(DateTime));
- AssertEquals(tm.ElementName, "dateTime");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "DateTime");
- AssertEquals(tm.TypeFullName, "System.DateTime");
- }
-
- [Test]
- public void TestGuidTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(Guid));
- AssertEquals(tm.ElementName, "guid");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Guid");
- AssertEquals(tm.TypeFullName, "System.Guid");
- }
-
- [Test]
- public void TestDecimalTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(decimal));
- AssertEquals(tm.ElementName, "decimal");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Decimal");
- AssertEquals(tm.TypeFullName, "System.Decimal");
- }
-
- [Test]
- public void TestXmlQualifiedNameTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(XmlQualifiedName));
- AssertEquals(tm.ElementName, "QName");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "XmlQualifiedName");
- AssertEquals(tm.TypeFullName, "System.Xml.XmlQualifiedName");
- }
-
- [Test]
- public void TestSByteTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(sbyte));
- AssertEquals(tm.ElementName, "byte");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "SByte");
- AssertEquals(tm.TypeFullName, "System.SByte");
- }
-
- [Test]
- public void TestCharTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof(char));
- AssertEquals(tm.ElementName, "char");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "Char");
- AssertEquals(tm.TypeFullName, "System.Char");
- }
- [Test]
- public void TestNullTypeMapping()
- {
- try
- {
- XmlTypeMapping tm = Map(null);
- Fail("Should not be able to map a null type");
- }
- catch (Exception)
- {
- }
- }
-
- [Test]
- public void TestInvalidClassTypeMapping()
- {
- try
- {
- // this can use any class
- XmlTypeMapping tm = Map(typeof(SimpleClass));
- Fail("Should not be able to this type");
- }
- catch (Exception)
- {
- }
- }
- /*
- [Test]
- public void TestTypeMapping()
- {
- XmlTypeMapping tm = Map(typeof());
- AssertEquals(tm.ElementName, "");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "");
- AssertEquals(tm.TypeFullName, "System.");
- }
- */
-
- [Test]
- public void TestIntTypeMappingWithDefaultNamespaces()
- {
- XmlTypeMapping tm = Map(typeof(int), SomeNamespace);
- AssertEquals(tm.ElementName, "int");
- AssertEquals(tm.Namespace, SomeNamespace);
- AssertEquals(tm.TypeName, "Int32");
- AssertEquals(tm.TypeFullName, "System.Int32");
- }
- [Test]
- public void TestValidClassTypeMapping()
- {
- Type type = typeof(SimpleClass);
- XmlAttributes attrs = new XmlAttributes();
- XmlAttributeOverrides overrides = new XmlAttributeOverrides();
- overrides.Add(typeof(SimpleClass), attrs);
-
- XmlTypeMapping tm = Map(type, overrides);
- AssertEquals(tm.ElementName, "SimpleClass");
- AssertEquals(tm.Namespace, "");
- AssertEquals(tm.TypeName, "SimpleClass");
- AssertEquals(tm.TypeFullName, "MonoTests.System.Xml.TestClasses.SimpleClass");
- }
-
- [Test]
- public void TestImportMembersMapping()
- {
- Type type = typeof(SimpleClass);
- XmlAttributes attrs = new XmlAttributes();
- XmlAttributeOverrides overrides = new XmlAttributeOverrides();
- overrides.Add(typeof(SimpleClass), attrs);
- XmlReflectionMember[] members = new XmlReflectionMember[0];
- XmlMembersMapping mm;
- try
- {
- mm = MembersMap(type, overrides, members, true);
- Fail("Should not be able to fetch an empty XmlMembersMapping");
- }
- catch (Exception)
- {
- }
-
- XmlReflectionMember rm = new XmlReflectionMember();
- rm.IsReturnValue = false;
- rm.MemberName = "something";
- rm.MemberType = typeof(string);
- members = new XmlReflectionMember[1];
- members[0] = rm;
- mm = MembersMap(type, overrides, members, false);
- Equals(mm.Count, 1);
- XmlMemberMapping smm = mm[0];
- AssertEquals(smm.Any, false);
- AssertEquals(smm.ElementName, "something");
- AssertEquals(smm.MemberName, "something");
- AssertEquals(smm.Namespace, null);
- AssertEquals(smm.TypeFullName, "System.String");
- AssertEquals(smm.TypeName, "string");
- AssertEquals(smm.TypeNamespace, null);
-
- rm = new XmlReflectionMember();
- rm.IsReturnValue = false;
- rm.MemberName = "nothing";
- rm.MemberType = typeof(string);
- members = new XmlReflectionMember[1];
- members[0] = rm;
- mm = MembersMap(type, overrides, members, false);
- Equals(mm.Count, 0);
- }
- [Test]
- public void TestIntTypeMappingWithXmlRootAttribute()
- {
- const string TheNamespace = "another:urn";
- XmlRootAttribute root = new XmlRootAttribute("price");
- root.Namespace = TheNamespace;
-
- XmlTypeMapping tm = Map(typeof(int), root);
- AssertEquals(tm.ElementName, "price");
- AssertEquals(tm.Namespace, TheNamespace);
- AssertEquals(tm.TypeName, "Int32");
- AssertEquals(tm.TypeFullName, "System.Int32");
- }
- }
- }
|