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