TimeZoneInfo.TransitionTimeTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using NUnit.Framework;
  5. namespace MonoTests.System
  6. {
  7. public class TimeZoneInfo_TransitionTimeTest
  8. {
  9. [TestFixture]
  10. public class CreateFixedDateRuleExceptions
  11. {
  12. [Test]
  13. [ExpectedException (typeof (ArgumentException))]
  14. public void DateHasNonDefaultComponent ()
  15. {
  16. TimeZoneInfo.TransitionTime.CreateFixedDateRule (new DateTime (1, 1, 10, 2, 0, 0), 3, 15);
  17. }
  18. [Test]
  19. [ExpectedException (typeof (ArgumentException))]
  20. public void KindNotUnspecified()
  21. {
  22. TimeZoneInfo.TransitionTime.CreateFixedDateRule (new DateTime (1, 1, 1, 2, 0, 0, DateTimeKind.Utc), 3, 15);
  23. }
  24. [Test]
  25. [ExpectedException (typeof (ArgumentException))]
  26. public void DateNotInMilliSeconds ()
  27. {
  28. TimeZoneInfo.TransitionTime.CreateFixedDateRule (new DateTime (50), 3, 15);
  29. }
  30. [Test]
  31. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  32. public void MonthOutOfRange ()
  33. {
  34. TimeZoneInfo.TransitionTime.CreateFixedDateRule (new DateTime (1, 1, 1, 2, 0, 0), 13, 15);
  35. }
  36. [Test]
  37. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  38. public void DayOutOfRange ()
  39. {
  40. TimeZoneInfo.TransitionTime.CreateFixedDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, -2);
  41. }
  42. }
  43. [TestFixture]
  44. public class CreateFloatingDateRuleExceptions
  45. {
  46. [Test]
  47. [ExpectedException (typeof (ArgumentException))]
  48. public void DateHasNonDefaultComponent ()
  49. {
  50. TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 10, 2, 0, 0), 3, 4, DayOfWeek.Sunday);
  51. }
  52. [Test]
  53. [ExpectedException (typeof (ArgumentException))]
  54. public void KindNotUnspecified()
  55. {
  56. TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0, DateTimeKind.Utc), 3, 4, DayOfWeek.Sunday);
  57. }
  58. [Test]
  59. [ExpectedException (typeof (ArgumentException))]
  60. public void DateNotInSeconds ()
  61. {
  62. TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (50), 3, 4, DayOfWeek.Sunday);
  63. }
  64. [Test]
  65. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  66. public void MonthOutOfRange ()
  67. {
  68. TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 13, 4, DayOfWeek.Sunday);
  69. }
  70. [Test]
  71. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  72. public void WeekOutOfRange ()
  73. {
  74. TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, -2, DayOfWeek.Sunday);
  75. }
  76. [Test]
  77. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  78. public void DayOfWeekOutOfRange ()
  79. {
  80. TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 4, (DayOfWeek)12);
  81. }
  82. }
  83. [TestFixture]
  84. public class NonExceptional {
  85. [Test]
  86. public void EqualsObject ()
  87. {
  88. DateTime dt = new DateTime (1, 1, 1, 2, 0, 0, DateTimeKind.Unspecified);
  89. TimeZoneInfo.TransitionTime tt1 = TimeZoneInfo.TransitionTime.CreateFixedDateRule (dt, 1, 21);
  90. Assert.IsFalse (tt1.Equals (null), "null"); // found using Gendarme :)
  91. Assert.IsTrue (tt1.Equals (tt1), "self");
  92. TimeZoneInfo.TransitionTime tt2 = TimeZoneInfo.TransitionTime.CreateFixedDateRule (dt, 2, 12);
  93. Assert.IsFalse (tt2.Equals (tt1), "1!=2");
  94. Assert.IsFalse (tt1.Equals (tt2), "2!=1");
  95. }
  96. [Test]
  97. public void Serialize_Deserialize_FloatingDateRule ()
  98. {
  99. TimeZoneInfo.TransitionTime floatingDateRule = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 1, 0, 0), 3, 5, DayOfWeek.Sunday);
  100. MemoryStream stream = new MemoryStream ();
  101. BinaryFormatter formatter = new BinaryFormatter ();
  102. formatter.Serialize (stream, floatingDateRule);
  103. stream.Position = 0;
  104. TimeZoneInfo.TransitionTime deserialized = (TimeZoneInfo.TransitionTime) formatter.Deserialize (stream);
  105. stream.Close ();
  106. stream.Dispose ();
  107. Assert.AreEqual (floatingDateRule, deserialized);
  108. }
  109. [Test]
  110. public void Serialize_Deserialize_FixedDateRule ()
  111. {
  112. TimeZoneInfo.TransitionTime fixedDateRule = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 1, 0, 0), 3, 12);
  113. MemoryStream stream = new MemoryStream ();
  114. BinaryFormatter formatter = new BinaryFormatter ();
  115. formatter.Serialize (stream, fixedDateRule);
  116. stream.Position = 0;
  117. TimeZoneInfo.TransitionTime deserialized = (TimeZoneInfo.TransitionTime) formatter.Deserialize (stream);
  118. stream.Close ();
  119. stream.Dispose ();
  120. Assert.AreEqual (fixedDateRule, deserialized);
  121. }
  122. }
  123. }
  124. }