FileSystemInfoTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // FileSystemInfoTest.cs - NUnit Test Cases for System.IO.FileSystemInfo class
  2. //
  3. // Ville Palo ([email protected])
  4. //
  5. // (C) 2003 Ville Palo
  6. //
  7. using System;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Threading;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.IO
  13. {
  14. [TestFixture]
  15. public class FileSystemInfoTest : Assertion
  16. {
  17. CultureInfo old_culture;
  18. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  19. static readonly char DSC = Path.DirectorySeparatorChar;
  20. [SetUp]
  21. protected void SetUp()
  22. {
  23. if (Directory.Exists (TempFolder))
  24. Directory.Delete (TempFolder, true);
  25. Directory.CreateDirectory (TempFolder);
  26. old_culture = Thread.CurrentThread.CurrentCulture;
  27. Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
  28. }
  29. [TearDown]
  30. protected void TearDown()
  31. {
  32. if (Directory.Exists (TempFolder))
  33. Directory.Delete (TempFolder, true);
  34. Thread.CurrentThread.CurrentCulture = old_culture;
  35. }
  36. bool Windows
  37. {
  38. get {
  39. return Path.DirectorySeparatorChar == '\\';
  40. }
  41. }
  42. bool Unix
  43. {
  44. get {
  45. return Path.DirectorySeparatorChar == '/';
  46. }
  47. }
  48. bool Mac
  49. {
  50. get {
  51. return Path.DirectorySeparatorChar == ':';
  52. }
  53. }
  54. private void DeleteFile (string path)
  55. {
  56. if (File.Exists (path))
  57. File.Delete (path);
  58. }
  59. private void DeleteDir (string path)
  60. {
  61. if (Directory.Exists (path))
  62. Directory.Delete (path, true);
  63. }
  64. [Test]
  65. [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
  66. public void CreationTimeFile ()
  67. {
  68. string path = TempFolder + DSC + "FSIT.CreationTime.Test";
  69. DeleteFile (path);
  70. if (Unix) { // Unix doesn't support CreationTimes
  71. return;
  72. }
  73. try {
  74. File.Create (path).Close ();
  75. FileSystemInfo info = new FileInfo (path);
  76. info.CreationTime = new DateTime (1999, 12, 31, 11, 59, 59);
  77. DateTime time = info.CreationTime;
  78. AssertEquals ("test#01", 1999, time.Year);
  79. AssertEquals ("test#02", 12, time.Month);
  80. AssertEquals ("test#03", 31, time.Day);
  81. AssertEquals ("test#04", 11, time.Hour);
  82. AssertEquals ("test#05", 59, time.Minute);
  83. AssertEquals ("test#06", 59, time.Second);
  84. time = TimeZone.CurrentTimeZone.ToLocalTime (info.CreationTimeUtc);
  85. AssertEquals ("test#07", 1999, time.Year);
  86. AssertEquals ("test#08", 12, time.Month);
  87. AssertEquals ("test#09", 31, time.Day);
  88. AssertEquals ("test#10", 11, time.Hour);
  89. AssertEquals ("test#11", 59, time.Minute);
  90. AssertEquals ("test#12", 59, time.Second);
  91. info.CreationTimeUtc = new DateTime (1999, 12, 31, 11, 59, 59);
  92. time = TimeZone.CurrentTimeZone.ToUniversalTime (info.CreationTime);
  93. AssertEquals ("test#13", 1999, time.Year);
  94. AssertEquals ("test#14", 12, time.Month);
  95. AssertEquals ("test#15", 31, time.Day);
  96. AssertEquals ("test#16", 11, time.Hour);
  97. AssertEquals ("test#17", 59, time.Minute);
  98. AssertEquals ("test#18", 59, time.Second);
  99. time = info.CreationTimeUtc;
  100. AssertEquals ("test#19", 1999, time.Year);
  101. AssertEquals ("test#20", 12, time.Month);
  102. AssertEquals ("test#21", 31, time.Day);
  103. AssertEquals ("test#22", 11, time.Hour);
  104. AssertEquals ("test#23", 59, time.Minute);
  105. AssertEquals ("test#24", 59, time.Second);
  106. } finally {
  107. DeleteFile (path);
  108. }
  109. }
  110. [Test]
  111. [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
  112. public void CreationTimeDirectory ()
  113. {
  114. string path = TempFolder + DSC + "FSIT.CreationTimeDirectory.Test";
  115. DeleteDir (path);
  116. if (Unix) { // Unix doesn't support CreationTimes
  117. return;
  118. }
  119. try {
  120. FileSystemInfo info = Directory.CreateDirectory (path);
  121. info.CreationTime = new DateTime (1999, 12, 31, 11, 59, 59);
  122. DateTime time = info.CreationTime;
  123. AssertEquals ("test#01", 1999, time.Year);
  124. AssertEquals ("test#02", 12, time.Month);
  125. AssertEquals ("test#03", 31, time.Day);
  126. time = TimeZone.CurrentTimeZone.ToLocalTime (info.CreationTimeUtc);
  127. AssertEquals ("test#07", 1999, time.Year);
  128. AssertEquals ("test#08", 12, time.Month);
  129. AssertEquals ("test#09", 31, time.Day);
  130. AssertEquals ("test#10", 11, time.Hour);
  131. info.CreationTimeUtc = new DateTime (1999, 12, 31, 11, 59, 59);
  132. time = TimeZone.CurrentTimeZone.ToUniversalTime (info.CreationTime);
  133. AssertEquals ("test#13", 1999, time.Year);
  134. AssertEquals ("test#14", 12, time.Month);
  135. AssertEquals ("test#15", 31, time.Day);
  136. time = TimeZone.CurrentTimeZone.ToLocalTime (info.CreationTimeUtc);
  137. AssertEquals ("test#19", 1999, time.Year);
  138. AssertEquals ("test#20", 12, time.Month);
  139. AssertEquals ("test#21", 31, time.Day);
  140. } finally {
  141. DeleteDir (path);
  142. }
  143. }
  144. [Test]
  145. [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
  146. public void CreationTimeNoFileOrDirectory ()
  147. {
  148. string path = TempFolder + DSC + "FSIT.CreationTimeNoFile.Test";
  149. DeleteFile (path);
  150. DeleteDir (path);
  151. try {
  152. FileSystemInfo info = new FileInfo (path);
  153. DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime(info.CreationTime);
  154. AssertEquals ("test#01", 1601, time.Year);
  155. AssertEquals ("test#02", 1, time.Month);
  156. AssertEquals ("test#03", 1, time.Day);
  157. AssertEquals ("test#04", 0, time.Hour);
  158. AssertEquals ("test#05", 0, time.Minute);
  159. AssertEquals ("test#06", 0, time.Second);
  160. AssertEquals ("test#07", 0, time.Millisecond);
  161. info = new DirectoryInfo (path);
  162. time = TimeZone.CurrentTimeZone.ToUniversalTime(info.CreationTime);
  163. AssertEquals ("test#08", 1601, time.Year);
  164. AssertEquals ("test#09", 1, time.Month);
  165. AssertEquals ("test#10", 1, time.Day);
  166. AssertEquals ("test#11", 0, time.Hour);
  167. AssertEquals ("test#12", 0, time.Minute);
  168. AssertEquals ("test#13", 0, time.Second);
  169. AssertEquals ("test#14", 0, time.Millisecond);
  170. } finally {
  171. DeleteFile (path);
  172. DeleteDir (path);
  173. }
  174. }
  175. [Test]
  176. public void Extenssion ()
  177. {
  178. string path = TempFolder + DSC + "FSIT.Extenssion.Test";
  179. DeleteFile (path);
  180. try {
  181. FileSystemInfo info = new FileInfo (path);
  182. AssertEquals ("test#01", ".Test", info.Extension);
  183. } finally {
  184. DeleteFile (path);
  185. }
  186. }
  187. [Test]
  188. [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
  189. public void DefaultLastAccessTime ()
  190. {
  191. string path = TempFolder + DSC + "FSIT.DefaultLastAccessTime.Test";
  192. DeleteFile (path);
  193. try {
  194. FileSystemInfo info = new FileInfo (path);
  195. DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime(info.LastAccessTime);
  196. AssertEquals ("test#01", 1601, time.Year);
  197. AssertEquals ("test#02", 1, time.Month);
  198. AssertEquals ("test#03", 1, time.Day);
  199. AssertEquals ("test#04", 0, time.Hour);
  200. AssertEquals ("test#05", 0, time.Minute);
  201. AssertEquals ("test#06", 0, time.Second);
  202. AssertEquals ("test#07", 0, time.Millisecond);
  203. } finally {
  204. DeleteFile (path);
  205. }
  206. }
  207. [Test]
  208. [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
  209. public void LastAccessTime ()
  210. {
  211. string path = TempFolder + DSC + "FSIT.LastAccessTime.Test";
  212. DeleteFile (path);
  213. try {
  214. File.Create (path).Close ();
  215. FileSystemInfo info = new FileInfo (path);
  216. DateTime time;
  217. info = new FileInfo (path);
  218. info.LastAccessTime = new DateTime (2000, 1, 1, 1, 1, 1);
  219. time = info.LastAccessTime;
  220. AssertEquals ("test#01", 2000, time.Year);
  221. AssertEquals ("test#02", 1, time.Month);
  222. AssertEquals ("test#03", 1, time.Day);
  223. AssertEquals ("test#04", 1, time.Hour);
  224. time = TimeZone.CurrentTimeZone.ToLocalTime (info.LastAccessTimeUtc);
  225. AssertEquals ("test#05", 2000, time.Year);
  226. AssertEquals ("test#06", 1, time.Month);
  227. AssertEquals ("test#07", 1, time.Day);
  228. AssertEquals ("test#08", 1, time.Hour);
  229. info.LastAccessTimeUtc = new DateTime (2000, 1, 1, 1, 1, 1);
  230. time = TimeZone.CurrentTimeZone.ToUniversalTime (info.LastAccessTime);
  231. AssertEquals ("test#09", 2000, time.Year);
  232. AssertEquals ("test#10", 1, time.Month);
  233. AssertEquals ("test#11", 1, time.Day);
  234. AssertEquals ("test#12", 1, time.Hour);
  235. time = info.LastAccessTimeUtc;
  236. AssertEquals ("test#13", 2000, time.Year);
  237. AssertEquals ("test#14", 1, time.Month);
  238. AssertEquals ("test#15", 1, time.Day);
  239. AssertEquals ("test#16", 1, time.Hour);
  240. } finally {
  241. DeleteFile (path);
  242. }
  243. }
  244. [Test]
  245. [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
  246. public void DefaultLastWriteTime ()
  247. {
  248. string path = TempFolder + DSC + "FSIT.DefaultLastWriteTime.Test";
  249. DeleteDir (path);
  250. try {
  251. FileSystemInfo info = new DirectoryInfo (path);
  252. DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime(info.LastWriteTime);
  253. AssertEquals ("test#01", 1601, time.Year);
  254. AssertEquals ("test#02", 1, time.Month);
  255. AssertEquals ("test#03", 1, time.Day);
  256. AssertEquals ("test#04", 0, time.Hour);
  257. AssertEquals ("test#05", 0, time.Minute);
  258. AssertEquals ("test#06", 0, time.Second);
  259. AssertEquals ("test#07", 0, time.Millisecond);
  260. } finally {
  261. DeleteDir (path);
  262. }
  263. }
  264. [Test]
  265. public void LastWriteTime ()
  266. {
  267. string path = TempFolder + DSC + "FSIT.LastWriteTime.Test";
  268. DeleteDir (path);
  269. try {
  270. FileSystemInfo info = Directory.CreateDirectory (path);
  271. info.LastWriteTime = new DateTime (2000, 1, 1, 1, 1, 1);
  272. DateTime time = info.LastWriteTime;
  273. AssertEquals ("test#01", 2000, time.Year);
  274. AssertEquals ("test#02", 1, time.Month);
  275. AssertEquals ("test#03", 1, time.Day);
  276. AssertEquals ("test#04", 1, time.Hour);
  277. time = info.LastWriteTimeUtc.ToLocalTime ();
  278. AssertEquals ("test#05", 2000, time.Year);
  279. AssertEquals ("test#06", 1, time.Month);
  280. AssertEquals ("test#07", 1, time.Day);
  281. AssertEquals ("test#08", 1, time.Hour);
  282. info.LastWriteTimeUtc = new DateTime (2000, 1, 1, 1, 1, 1);
  283. time = info.LastWriteTimeUtc;
  284. AssertEquals ("test#09", 2000, time.Year);
  285. AssertEquals ("test#10", 1, time.Month);
  286. AssertEquals ("test#11", 1, time.Day);
  287. AssertEquals ("test#12", 1, time.Hour);
  288. time = info.LastWriteTime.ToUniversalTime ();
  289. AssertEquals ("test#13", 2000, time.Year);
  290. AssertEquals ("test#14", 1, time.Month);
  291. AssertEquals ("test#15", 1, time.Day);
  292. AssertEquals ("test#16", 1, time.Hour);
  293. } finally {
  294. DeleteDir (path);
  295. }
  296. }
  297. }
  298. }