FileSystemInfoTest.cs 10 KB

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