DataObjectTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // DataObjectTest.cs - NUnit Test Cases for DataObject
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. using System.Security.Cryptography;
  11. using System.Security.Cryptography.Xml;
  12. using System.Xml;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Security.Cryptography.Xml {
  15. [TestFixture]
  16. public class DataObjectTest : Assertion {
  17. [Test]
  18. public void NewDataObject ()
  19. {
  20. string test = "<Test>DataObject</Test>";
  21. XmlDocument doc = new XmlDocument ();
  22. doc.LoadXml (test);
  23. DataObject obj1 = new DataObject ();
  24. Assert ("Data.Count==0", (obj1.Data.Count == 0));
  25. obj1.Id = "id";
  26. obj1.MimeType = "mime";
  27. obj1.Encoding = "encoding";
  28. AssertEquals ("Only attributes", "<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml ().OuterXml));
  29. obj1.Data = doc.ChildNodes;
  30. Assert ("Data.Count==1", (obj1.Data.Count == 1));
  31. XmlElement xel = obj1.GetXml ();
  32. DataObject obj2 = new DataObject ();
  33. obj2.LoadXml (xel);
  34. AssertEquals ("obj1==obj2", (obj1.GetXml ().OuterXml), (obj2.GetXml ().OuterXml));
  35. DataObject obj3 = new DataObject (obj1.Id, obj1.MimeType, obj1.Encoding, doc.DocumentElement);
  36. AssertEquals ("obj2==obj3", (obj2.GetXml ().OuterXml), (obj3.GetXml ().OuterXml));
  37. }
  38. [Test]
  39. public void ImportDataObject ()
  40. {
  41. string value1 = "<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\">DataObject1</Test><Test xmlns=\"\">DataObject2</Test></Object>";
  42. XmlDocument doc = new XmlDocument ();
  43. doc.LoadXml (value1);
  44. DataObject obj1 = new DataObject ();
  45. obj1.LoadXml (doc.DocumentElement);
  46. Assert ("Data.Count==2", (obj1.Data.Count == 2));
  47. string s = (obj1.GetXml ().OuterXml);
  48. AssertEquals ("DataObject 1", value1, s);
  49. string value2 = "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  50. doc = new XmlDocument ();
  51. doc.LoadXml (value2);
  52. DataObject obj2 = new DataObject ();
  53. obj2.LoadXml (doc.DocumentElement);
  54. s = (obj2.GetXml ().OuterXml);
  55. AssertEquals ("DataObject 2", value2, s);
  56. string value3 = "<Object Id=\"id\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  57. doc = new XmlDocument ();
  58. doc.LoadXml (value3);
  59. DataObject obj3 = new DataObject ();
  60. obj3.LoadXml (doc.DocumentElement);
  61. s = (obj3.GetXml ().OuterXml);
  62. AssertEquals ("DataObject 3", value3, s);
  63. string value4 = "<Object MimeType=\"mime\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  64. doc = new XmlDocument ();
  65. doc.LoadXml (value4);
  66. DataObject obj4 = new DataObject ();
  67. obj4.LoadXml (doc.DocumentElement);
  68. s = (obj4.GetXml ().OuterXml);
  69. AssertEquals ("DataObject 4", value4, s);
  70. }
  71. [Test]
  72. [ExpectedException (typeof (ArgumentNullException))]
  73. public void InvalidDataObject1 ()
  74. {
  75. DataObject obj1 = new DataObject ();
  76. obj1.Data = null;
  77. }
  78. [Test]
  79. [ExpectedException (typeof (ArgumentNullException))]
  80. public void InvalidDataObject2 ()
  81. {
  82. DataObject obj1 = new DataObject ();
  83. obj1.LoadXml (null);
  84. }
  85. [Test]
  86. public void InvalidDataObject3 ()
  87. {
  88. DataObject obj1 = new DataObject ();
  89. // seems this isn't invalid !?!
  90. // but no exception is thrown
  91. string value = "<Test>Bad</Test>";
  92. XmlDocument doc = new XmlDocument ();
  93. doc.LoadXml (value);
  94. obj1.LoadXml (doc.DocumentElement);
  95. string s = (obj1.GetXml ().OuterXml);
  96. AssertEquals ("DataObject Bad", value, s);
  97. }
  98. }
  99. }