fixed tests
[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 #if TARGET_JVM
73             [Category("NotWorking")]
74 #endif
75                 public void CreationTimeFile ()
76                 {
77                         string path = TempFolder + DSC + "FSIT.CreationTime.Test";
78                         DeleteFile (path);
79                         if (Unix) {  // Unix doesn't support CreationTimes
80                           return;
81                         }
82                         try {
83                                 File.Create (path).Close ();
84                                 FileSystemInfo info = new FileInfo (path);
85                                 info.CreationTime = new DateTime (1999, 12, 31, 11, 59, 59);
86
87                                 DateTime time = info.CreationTime;                              
88                                 AssertEquals ("test#01", 1999, time.Year);
89                                 AssertEquals ("test#02", 12, time.Month);
90                                 AssertEquals ("test#03", 31, time.Day);
91                                 AssertEquals ("test#04", 11, time.Hour);
92                                 AssertEquals ("test#05", 59, time.Minute);
93                                 AssertEquals ("test#06", 59, time.Second);
94                                 
95                                 time = TimeZone.CurrentTimeZone.ToLocalTime (info.CreationTimeUtc);     
96                                 AssertEquals ("test#07", 1999, time.Year);
97                                 AssertEquals ("test#08", 12, time.Month);
98                                 AssertEquals ("test#09", 31, time.Day);
99                                 AssertEquals ("test#10", 11, time.Hour);
100                                 AssertEquals ("test#11", 59, time.Minute);
101                                 AssertEquals ("test#12", 59, time.Second);
102                                 
103                                 info.CreationTimeUtc = new DateTime (1999, 12, 31, 11, 59, 59);
104
105                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (info.CreationTime);                            
106                                 AssertEquals ("test#13", 1999, time.Year);
107                                 AssertEquals ("test#14", 12, time.Month);
108                                 AssertEquals ("test#15", 31, time.Day);
109                                 AssertEquals ("test#16", 11, time.Hour);
110                                 AssertEquals ("test#17", 59, time.Minute);
111                                 AssertEquals ("test#18", 59, time.Second);
112
113                                 time = info.CreationTimeUtc;    
114                                 AssertEquals ("test#19", 1999, time.Year);
115                                 AssertEquals ("test#20", 12, time.Month);
116                                 AssertEquals ("test#21", 31, time.Day);
117                                 AssertEquals ("test#22", 11, time.Hour);
118                                 AssertEquals ("test#23", 59, time.Minute);
119                                 AssertEquals ("test#24", 59, time.Second);
120
121                         } finally {
122                                 DeleteFile (path);
123                         }
124                 }
125
126                 [Test]
127 #if TARGET_JVM
128             [Category("NotWorking")]
129 #endif
130                 public void CreationTimeDirectory ()
131                 {
132                         string path = TempFolder + DSC + "FSIT.CreationTimeDirectory.Test";
133                         DeleteDir (path);
134                         if (Unix) {  // Unix doesn't support CreationTimes
135                           return;
136                         }
137                         
138                         try {                           
139                                 FileSystemInfo info = Directory.CreateDirectory (path);
140                                 info.CreationTime = new DateTime (1999, 12, 31, 11, 59, 59);
141                                 DateTime time = info.CreationTime;      
142                                 
143                                 AssertEquals ("test#01", 1999, time.Year);
144                                 AssertEquals ("test#02", 12, time.Month);
145                                 AssertEquals ("test#03", 31, time.Day);
146                                 
147                                 time = TimeZone.CurrentTimeZone.ToLocalTime (info.CreationTimeUtc);     
148                                 AssertEquals ("test#07", 1999, time.Year);
149                                 AssertEquals ("test#08", 12, time.Month);
150                                 AssertEquals ("test#09", 31, time.Day);
151                                 AssertEquals ("test#10", 11, time.Hour);
152                                 
153                                 info.CreationTimeUtc = new DateTime (1999, 12, 31, 11, 59, 59);
154                                 
155                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (info.CreationTime);                            
156                                 AssertEquals ("test#13", 1999, time.Year);
157                                 AssertEquals ("test#14", 12, time.Month);
158                                 AssertEquals ("test#15", 31, time.Day);
159                                 
160                                 time = TimeZone.CurrentTimeZone.ToLocalTime (info.CreationTimeUtc);     
161                                 AssertEquals ("test#19", 1999, time.Year);
162                                 AssertEquals ("test#20", 12, time.Month);
163                                 AssertEquals ("test#21", 31, time.Day);
164                                 
165                         } finally {
166                                 DeleteDir (path);
167                         }
168                 }
169                 
170                 [Test]
171 #if TARGET_JVM
172             [Category("NotWorking")]
173 #endif
174                 public void CreationTimeNoFileOrDirectory ()
175                 {
176                         string path = TempFolder + DSC + "FSIT.CreationTimeNoFile.Test";
177                         DeleteFile (path);
178                         DeleteDir (path);
179                         
180                         try {
181                                 FileSystemInfo info = new FileInfo (path);
182                                 
183                                 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime(info.CreationTime);
184                                 AssertEquals ("test#01", 1601, time.Year);
185                                 AssertEquals ("test#02", 1, time.Month);
186                                 AssertEquals ("test#03", 1, time.Day);
187                                 AssertEquals ("test#04", 0, time.Hour);
188                                 AssertEquals ("test#05", 0, time.Minute);
189                                 AssertEquals ("test#06", 0, time.Second);
190                                 AssertEquals ("test#07", 0, time.Millisecond);
191                                 
192                                 info = new DirectoryInfo (path);
193                                 
194                                 time = TimeZone.CurrentTimeZone.ToUniversalTime(info.CreationTime);
195                                 AssertEquals ("test#08", 1601, time.Year);
196                                 AssertEquals ("test#09", 1, time.Month);
197                                 AssertEquals ("test#10", 1, time.Day);
198                                 AssertEquals ("test#11", 0, time.Hour);
199                                 AssertEquals ("test#12", 0, time.Minute);
200                                 AssertEquals ("test#13", 0, time.Second);
201                                 AssertEquals ("test#14", 0, time.Millisecond);
202                         } finally {
203                                 DeleteFile (path);
204                                 DeleteDir (path);
205                         }
206                 }
207                 
208                 [Test]
209                 public void Extenssion ()
210                 {
211                         string path = TempFolder + DSC + "FSIT.Extenssion.Test";
212                         DeleteFile (path);
213                         
214                         try {
215                                 FileSystemInfo info = new FileInfo (path);
216                                 AssertEquals ("test#01", ".Test", info.Extension);
217                         } finally {
218                                 DeleteFile (path);
219                         }
220                 }
221                 
222                 [Test]
223 #if TARGET_JVM
224             [Category("NotWorking")]
225 #endif
226                 public void DefaultLastAccessTime ()
227                 {
228                         string path = TempFolder + DSC + "FSIT.DefaultLastAccessTime.Test";
229                         DeleteFile (path);
230                         
231                         try {
232                                 
233                                 FileSystemInfo info = new FileInfo (path);
234                                 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime(info.LastAccessTime);
235
236                                 AssertEquals ("test#01", 1601, time.Year);
237                                 AssertEquals ("test#02", 1, time.Month);
238                                 AssertEquals ("test#03", 1, time.Day);
239                                 AssertEquals ("test#04", 0, time.Hour);
240                                 AssertEquals ("test#05", 0, time.Minute);
241                                 AssertEquals ("test#06", 0, time.Second);
242                                 AssertEquals ("test#07", 0, time.Millisecond);
243                         } finally {
244                                 DeleteFile (path);
245                         }
246                 }
247
248                 [Test]
249 #if TARGET_JVM
250             [Category("NotWorking")]
251 #endif
252                 public void LastAccessTime ()
253                 {
254                         string path = TempFolder + DSC + "FSIT.LastAccessTime.Test";
255                         DeleteFile (path);
256
257                         try {
258                                 File.Create (path).Close ();
259                                 FileSystemInfo info = new FileInfo (path);
260                                 DateTime time;
261                                 info = new FileInfo (path);
262                                 
263                                 info.LastAccessTime = new DateTime (2000, 1, 1, 1, 1, 1);
264                                 time = info.LastAccessTime;
265                                 AssertEquals ("test#01", 2000, time.Year);
266                                 AssertEquals ("test#02", 1, time.Month);
267                                 AssertEquals ("test#03", 1, time.Day);
268                                 AssertEquals ("test#04", 1, time.Hour);
269                                 
270                                 time = TimeZone.CurrentTimeZone.ToLocalTime (info.LastAccessTimeUtc);
271                                 AssertEquals ("test#05", 2000, time.Year);
272                                 AssertEquals ("test#06", 1, time.Month);
273                                 AssertEquals ("test#07", 1, time.Day);
274                                 AssertEquals ("test#08", 1, time.Hour);
275                                 
276                                 info.LastAccessTimeUtc = new DateTime (2000, 1, 1, 1, 1, 1);
277                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (info.LastAccessTime);
278                                 AssertEquals ("test#09", 2000, time.Year);
279                                 AssertEquals ("test#10", 1, time.Month);
280                                 AssertEquals ("test#11", 1, time.Day);
281                                 AssertEquals ("test#12", 1, time.Hour);
282
283                                 time = info.LastAccessTimeUtc;
284                                 AssertEquals ("test#13", 2000, time.Year);
285                                 AssertEquals ("test#14", 1, time.Month);
286                                 AssertEquals ("test#15", 1, time.Day);
287                                 AssertEquals ("test#16", 1, time.Hour);                         
288                                 
289                         } finally {
290                                 DeleteFile (path);
291                         }
292                 }
293                 
294                 [Test]
295 #if TARGET_JVM
296         [Category("NotWorking")]
297 #endif
298                 public void DefaultLastWriteTime ()
299                 {
300                         string path = TempFolder + DSC + "FSIT.DefaultLastWriteTime.Test";
301                         DeleteDir (path);
302
303                         try {
304
305                                 FileSystemInfo info = new DirectoryInfo (path);
306                                 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime(info.LastWriteTime);
307                                 
308                                 AssertEquals ("test#01", 1601, time.Year);
309                                 AssertEquals ("test#02", 1, time.Month);
310                                 AssertEquals ("test#03", 1, time.Day);
311                                 AssertEquals ("test#04", 0, time.Hour);
312                                 AssertEquals ("test#05", 0, time.Minute);
313                                 AssertEquals ("test#06", 0, time.Second);
314                                 AssertEquals ("test#07", 0, time.Millisecond);
315                         } finally {
316                                 DeleteDir (path);
317                         }
318                 }
319                 
320                 [Test]
321                 public void LastWriteTime ()
322                 {
323                         string path = TempFolder + DSC + "FSIT.LastWriteTime.Test";
324                         DeleteDir (path);
325                         
326                         try {
327                                 FileSystemInfo info = Directory.CreateDirectory (path);
328                                 
329                                 info.LastWriteTime = new DateTime (2000, 1, 1, 1, 1, 1);
330                                 DateTime time = info.LastWriteTime;
331                                 
332                                 AssertEquals ("test#01", 2000, time.Year);
333                                 AssertEquals ("test#02", 1, time.Month);
334                                 AssertEquals ("test#03", 1, time.Day);
335                                 AssertEquals ("test#04", 1, time.Hour);
336                                 
337                                 time = info.LastWriteTimeUtc.ToLocalTime ();
338                                 AssertEquals ("test#05", 2000, time.Year);
339                                 AssertEquals ("test#06", 1, time.Month);
340                                 AssertEquals ("test#07", 1, time.Day);
341                                 AssertEquals ("test#08", 1, time.Hour);
342
343                                 info.LastWriteTimeUtc = new DateTime (2000, 1, 1, 1, 1, 1);
344                                 time = info.LastWriteTimeUtc;
345                                 AssertEquals ("test#09", 2000, time.Year);
346                                 AssertEquals ("test#10", 1, time.Month);
347                                 AssertEquals ("test#11", 1, time.Day);
348                                 AssertEquals ("test#12", 1, time.Hour);
349
350                                 time = info.LastWriteTime.ToUniversalTime ();
351                                 AssertEquals ("test#13", 2000, time.Year);
352                                 AssertEquals ("test#14", 1, time.Month);
353                                 AssertEquals ("test#15", 1, time.Day);
354                                 AssertEquals ("test#16", 1, time.Hour);
355
356                         } finally {
357                                 DeleteDir (path);
358                         }
359                 }
360         }
361 }