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