TimeZoneInfo.TransitionTimeTest.cs 4.5 KB

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