Merge pull request #1991 from esdrubal/seq_test_fix
[mono.git] / mcs / class / corlib / Test / System / TimeZoneInfoTest.cs
index 73bed53d10f0686626a00ea00d3e2d7de1edbcda..8648e22df7fb5c482166a760f4c31af2c97134bb 100644 (file)
 
 using System;
 using System.IO;
+using System.Runtime.InteropServices;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Collections;
+using System.Reflection;
+using System.Globalization;
 
 using NUnit.Framework;
 namespace MonoTests.System
@@ -48,6 +51,26 @@ namespace MonoTests.System
                                Assert.IsNotNull (local);
                                Assert.IsTrue (true);
                        }
+
+                       [DllImport ("libc")]
+                       private static extern int readlink (string path, byte[] buffer, int buflen);
+
+                       [Test] // Covers #24958
+                       public void LocalId ()
+                       {
+                               byte[] buf = new byte [512];
+
+                               var path = "/etc/localtime";
+                               try {
+                                       var ret = readlink (path, buf, buf.Length);
+                                       if (ret == -1)
+                                               return; // path is not a symbolic link, nothing to test
+                               } catch (DllNotFoundException e) {
+                                       return;
+                               }
+
+                               Assert.IsTrue (TimeZoneInfo.Local.Id != "Local", "Local timezone id should not be \"Local\"");
+                       }
                }
 
                [TestFixture]
@@ -449,11 +472,7 @@ namespace MonoTests.System
                                DateTime utc = DateTime.UtcNow;
                                Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
                                DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
-                       #if NET_4_0
                                Assert.AreEqual(DateTimeKind.Local, converted.Kind);
-                       #else
-                               Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
-                       #endif
                                DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
                                Assert.AreEqual(back.Kind, DateTimeKind.Utc);
                                Assert.AreEqual(utc, back);
@@ -664,6 +683,12 @@ namespace MonoTests.System
                [TestFixture]
                public class GetSystemTimeZonesTests
                {
+                       [Test]
+                       public void Identity ()
+                       {
+                               Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
+                       }
+
                        [Test]
                        public void NotEmpty ()
                        {
@@ -686,6 +711,15 @@ namespace MonoTests.System
                                }
                                Assert.Fail ("Europe/Brussels not found in SystemTZ");
                        }
+
+                       [Test]
+                       public void ReflectionReturnsTheCorrectMethod ()
+                       {
+                               var method = (MethodInfo) typeof (TimeZoneInfo).GetMember ("GetSystemTimeZones", MemberTypes.Method, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)[0];
+
+                               var timeZones = (global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo>) method.Invoke (null, null);
+                               Assert.IsTrue (timeZones.Count > 0, "GetSystemTimeZones should not return an empty collection.");
+                       }
                }
                
                [TestFixture]
@@ -804,14 +838,30 @@ namespace MonoTests.System
                #endif
 
                        [Test]
-                       public void Dublin ()
+                       public void SubminuteDSTOffsets ()
                        {
                                if (Environment.OSVersion.Platform != PlatformID.Unix)
                                        Assert.Ignore ();
-                               // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
-                               TimeZoneInfo.FindSystemTimeZoneById ("Europe/Dublin");
-                       }
 
+                               var subMinuteDSTs = new string [] {
+                                       "Europe/Dublin", // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
+                                       "Europe/Amsterdam",
+                                       "America/St_Johns",
+                                       "Canada/Newfoundland",
+                                       "Europe/Moscow",
+                                       "Europe/Riga",
+                                       "N/A", // testing that the test doesn't fail with inexistent TZs
+                               };
+                               foreach (var tz in subMinuteDSTs) {
+                                       try {
+                                               TimeZoneInfo.FindSystemTimeZoneById (tz);
+                                       } catch (TimeZoneNotFoundException) {
+                                               // ok;
+                                       } catch (Exception ex) {
+                                               Assert.Fail (string.Format ("Failed to load TZ {0}: {1}", tz, ex.ToString ()));
+                                       }
+                               }
+                       }
                }
                
                [TestFixture]
@@ -1010,5 +1060,34 @@ namespace MonoTests.System
                                Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
                        }
                }
+
+               [TestFixture]
+               public class GetDaylightChanges
+               {
+                       MethodInfo getChanges;
+
+                       [SetUp]
+                       public void Setup ()
+                       {
+                               var flags = BindingFlags.Instance | BindingFlags.NonPublic;
+                               getChanges = typeof (TimeZoneInfo).GetMethod ("GetDaylightChanges", flags);
+                       }
+
+                       [Test]
+                       public void TestSydneyDaylightChanges ()
+                       {
+                               TimeZoneInfo tz;
+                               if (Environment.OSVersion.Platform == PlatformID.Unix)
+                                       tz = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Sydney");
+                               else
+                                       tz = TimeZoneInfo.FindSystemTimeZoneById ("W. Australia Standard Time");
+
+                               var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
+
+                               Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
+                               Assert.AreEqual (new DateTime (2014, 10, 5, 2, 0, 0), changes.Start);
+                               Assert.AreEqual (new DateTime (2014, 4, 6, 3, 0, 0), changes.End);
+                       }
+               }
        }
 }