Merge pull request #1909 from esdrubal/reflection
[mono.git] / mcs / class / corlib / Test / System.IO / DirectoryInfoTest.cs
1 // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class
2 //
3 // Authors
4 //      Ville Palo (vi64pa@koti.soon.fi)
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 // 
7 // (C) 2003 Ville Palo
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // 
10
11 using System;
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.IO;
15 using System.Runtime.Serialization;
16 using System.Runtime.Serialization.Formatters.Binary;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.System.IO
21 {
22         [TestFixture]
23         public class DirectoryInfoTest
24         {
25                 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
26
27                 static readonly char DSC = Path.DirectorySeparatorChar;
28                 string current;
29
30                 [SetUp]
31                 protected void SetUp ()
32                 {
33                         current = Directory.GetCurrentDirectory ();
34                         if (Directory.Exists (TempFolder))
35                                 Directory.Delete (TempFolder, true);
36                         Directory.CreateDirectory (TempFolder);
37                 }
38
39                 [TearDown]
40                 protected void TearDown ()
41                 {
42                         if (Directory.Exists (TempFolder))
43                                 Directory.Delete (TempFolder, true);
44                         Directory.SetCurrentDirectory (current);
45                 }
46
47                 [Test] // ctor (String)
48                 public void Constructor1 ()
49                 {
50                         string path = TempFolder + DSC + "DIT.Ctr.Test";
51                         DeleteDir (path);
52
53                         DirectoryInfo info = new DirectoryInfo (path);
54                         Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#1");
55                         Assert.IsFalse (info.Exists, "#2");
56                         Assert.AreEqual (".Test", info.Extension, "#3");
57                         Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#4");
58                 }
59
60                 [Test] // ctor (String)
61                 public void Constructor1_Path_Null ()
62                 {
63                         try {
64                                 new DirectoryInfo (null);
65                                 Assert.Fail ("#1");
66                         } catch (ArgumentNullException ex) {
67                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
68                                 Assert.IsNull (ex.InnerException, "#3");
69                                 Assert.IsNotNull (ex.Message, "#4");
70                                 Assert.AreEqual ("path", ex.ParamName, "#5");
71                         }
72                 }
73
74                 [Test] // ctor (String)
75                 public void Constructor1_Path_Empty ()
76                 {
77                         try {
78                                 new DirectoryInfo (string.Empty);
79                                 Assert.Fail ("#1");
80                         } catch (ArgumentException ex) {
81                                 // Empty file name is not legal
82                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
83                                 Assert.IsNull (ex.InnerException, "#3");
84                                 Assert.IsNotNull (ex.Message, "#4");
85                                 Assert.IsNull (ex.ParamName, "#5");
86                         }
87                 }
88
89                 [Test] // ctor (String)
90                 public void Constructor1_Path_Whitespace ()
91                 {
92                         try {
93                                 new DirectoryInfo ("   ");
94                                 Assert.Fail ("#1");
95                         } catch (ArgumentException ex) {
96                                 // The path is not of a legal form
97                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
98                                 Assert.IsNull (ex.InnerException, "#3");
99                                 Assert.IsNotNull (ex.Message, "#4");
100                                 Assert.IsNull (ex.ParamName, "#5");
101                         }
102                 }
103
104                 [Test] // ctor (String)
105                 public void Constructor1_Path_InvalidPathChars ()
106                 {
107                         string path = string.Empty;
108                         foreach (char c in Path.InvalidPathChars)
109                                 path += c;
110                         try {
111                                 new DirectoryInfo (path);
112                                 Assert.Fail ("#1");
113                         } catch (ArgumentException ex) {
114                                 // The path contains illegal characters
115                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
116                                 Assert.IsNull (ex.InnerException, "#3");
117                                 Assert.IsNotNull (ex.Message, "#4");
118                                 Assert.IsNull (ex.ParamName, "#5");
119                         }
120                 }
121
122                 [Test]
123                 public void Exists ()
124                 {
125                         string path = TempFolder + DSC + "DIT.Exists.Test";
126                         DeleteDir (path);
127
128                         try {
129                                 DirectoryInfo info = new DirectoryInfo (path);
130                                 Assert.IsFalse (info.Exists, "#1");
131
132                                 Directory.CreateDirectory (path);
133                                 Assert.IsFalse (info.Exists, "#2");
134                                 info = new DirectoryInfo (path);
135                                 Assert.IsTrue (info.Exists, "#3");
136                         } finally {
137                                 DeleteDir (path);
138                         }
139                 }
140
141                 [Test]
142                 [Category ("MobileNotWorking")]
143                 public void Name ()
144                 {
145                         string path = TempFolder + DSC + "DIT.Name.Test";
146                         DeleteDir (path);
147
148                         try {
149                                 DirectoryInfo info = new DirectoryInfo (path);
150                                 Assert.AreEqual ("DIT.Name.Test", info.Name, "#1");
151
152                                 info = Directory.CreateDirectory (path);
153                                 Assert.AreEqual ("DIT.Name.Test", info.Name, "#2");
154
155                                 info = Directory.CreateDirectory ("whatever");
156                                 Assert.AreEqual ("whatever", info.Name, "#3");
157
158                                 if (RunningOnUnix) {
159                                         info = new DirectoryInfo ("/");
160                                         Assert.AreEqual ("/", info.Name, "#4");
161
162                                         info = new DirectoryInfo ("test/");
163                                         Assert.AreEqual ("test", info.Name, "#5");
164
165                                         info = new DirectoryInfo ("/test");
166                                         Assert.AreEqual ("test", info.Name, "#4");
167
168                                         info = new DirectoryInfo ("/test/");
169                                         Assert.AreEqual ("test", info.Name, "#4");
170                                 } else {
171                                         info = new DirectoryInfo (@"c:");
172                                         Assert.AreEqual (@"C:\", info.Name, "#4");
173
174                                         info = new DirectoryInfo (@"c:\");
175                                         Assert.AreEqual (@"c:\", info.Name, "#5");
176
177                                         info = new DirectoryInfo (@"c:\test");
178                                         Assert.AreEqual ("test", info.Name, "#6");
179
180                                         info = new DirectoryInfo (@"c:\test\");
181                                         Assert.AreEqual ("test", info.Name, "#7");
182                                 }
183                         } finally {
184                                 DeleteDir (path);
185                         }
186                 }
187
188                 [Test]
189                 public void Parent ()
190                 {
191                         string path = TempFolder + DSC + "DIT.Parent.Test";
192                         DeleteDir (path);
193
194                         try {
195                                 DirectoryInfo info = new DirectoryInfo (path);
196                                 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#1");
197
198                                 info = Directory.CreateDirectory (path);
199                                 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#2");
200
201                                 info = new DirectoryInfo ("test");
202                                 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#3");
203
204                                 if (RunningOnUnix) {
205                                         info = new DirectoryInfo ("/");
206                                         Assert.IsNull (info.Parent, "#4");
207
208                                         info = new DirectoryInfo ("test/");
209                                         Assert.IsNotNull (info.Parent, "#5a");
210                                         Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#5b");
211
212                                         info = new DirectoryInfo ("/test");
213                                         Assert.IsNotNull (info.Parent, "#6a");
214                                         Assert.AreEqual ("/", info.Parent.FullName, "#6b");
215
216                                         info = new DirectoryInfo ("/test/");
217                                         Assert.IsNotNull (info.Parent, "#7a");
218                                         Assert.AreEqual ("/", info.Parent.FullName, "#7b");
219                                 } else {
220                                         info = new DirectoryInfo (@"c:");
221                                         Assert.IsNull (info.Parent, "#4");
222
223                                         info = new DirectoryInfo (@"c:\");
224                                         Assert.IsNull (info.Parent, "#5");
225
226                                         info = new DirectoryInfo (@"c:\test");
227                                         Assert.IsNotNull (info.Parent, "#6a");
228                                         Assert.AreEqual (@"c:\", info.Parent.FullName, "#6b");
229
230                                         info = new DirectoryInfo (@"c:\test\");
231                                         Assert.IsNotNull (info.Parent, "#7a");
232                                         Assert.AreEqual (@"c:\", info.Parent.FullName, "#7b");
233                                 }
234                         } finally {
235                                 DeleteDir (path);
236                         }
237                 }
238
239                 [Test]
240                 public void Create ()
241                 {
242                         string path = TempFolder + DSC + "DIT.Create.Test";
243                         DeleteDir (path);
244
245                         try {
246                                 DirectoryInfo info = new DirectoryInfo (path);
247                                 Assert.IsFalse (info.Exists, "#1");
248                                 info.Create ();
249                                 Assert.IsFalse (info.Exists, "#2");
250                                 info = new DirectoryInfo (path);
251                                 Assert.IsTrue (info.Exists, "#3");
252                         } finally {
253                                 DeleteDir (path);
254                         }
255                 }
256
257                 [Test]
258                 public void CreateSubdirectory ()
259                 {
260                         string sub_path = Path.Combine ("test01", "test02");
261                         try {
262                                 DirectoryInfo info = new DirectoryInfo (TempFolder);
263                                 DirectoryInfo sub = info.CreateSubdirectory (sub_path);
264                                 Assert.IsNotNull (sub, "#1");
265                                 Assert.AreEqual (Path.Combine (TempFolder, sub_path), sub.FullName, "#2");
266                                 Assert.IsTrue (Directory.Exists (sub.FullName), "#3");
267                         } finally {
268                                 DeleteDir (Path.Combine (TempFolder, sub_path));
269                         }
270                 }
271
272                 [Test]
273                 public void CreateSubdirectory_Path_Null ()
274                 {
275                         DirectoryInfo info = new DirectoryInfo (TempFolder);
276                         try {
277                                 info.CreateSubdirectory (null);
278                                 Assert.Fail ("#1");
279                         } catch (ArgumentNullException ex) {
280                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
281                                 Assert.IsNull (ex.InnerException, "#3");
282                                 Assert.IsNotNull (ex.Message, "#4");
283                                 Assert.AreEqual ("path", ex.ParamName, "#5");
284                         }
285                 }
286                 
287                 [Test]
288                 public void CreateSubdirectory_Path_Empty ()
289                 {
290                         DirectoryInfo info = new DirectoryInfo (TempFolder);
291                         try {
292                                 info.CreateSubdirectory (string.Empty);
293                                 Assert.Fail ("#1");
294                         } catch (ArgumentException ex) {
295                                 // Empty file name is not legal
296                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
297                                 Assert.IsNull (ex.InnerException, "#3");
298                                 Assert.IsNotNull (ex.Message, "#4");
299                         }
300                 }
301
302                 [Test] // Delete ()
303                 public void Delete1 ()
304                 {
305                         string path = TempFolder + DSC + "DIT.Delete1.Test";
306                         DeleteDir (path);
307                         
308                         try {
309                                 Directory.CreateDirectory (path);
310                                 DirectoryInfo info = new DirectoryInfo (path);
311                                 Assert.IsTrue (info.Exists, "#1");
312                                 
313                                 info.Delete ();
314                                 Assert.IsTrue (info.Exists, "#2");
315                                 
316                                 info = new DirectoryInfo (path);
317                                 Assert.IsFalse (info.Exists, "#3");
318                         } finally {
319                                 DeleteDir (path);
320                         }
321                 }
322
323                 [Test] // Delete ()
324                 public void Delete1_DirectoryNotEmpty ()
325                 {
326                         string path = TempFolder + DSC + "DIT.DeleteIOException1.Test";
327                         DeleteDir (path);
328                         
329                         try {
330                                 Directory.CreateDirectory (path);
331                                 File.Create (path + DSC + "test").Close ();
332                                 DirectoryInfo info = new DirectoryInfo (path);
333                                 try {
334                                         info.Delete ();
335                                         Assert.Fail ("#1");
336                                 } catch (IOException ex) {
337                                         // The directory is not empty
338                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
339                                         Assert.IsNull (ex.InnerException, "#3");
340                                         Assert.IsNotNull (ex.Message, "#4");
341                                 }
342                         } finally {
343                                 DeleteDir (path);
344                         }
345                 }
346
347                 [Test] // Delete (Boolean)
348                 public void Delete2 ()
349                 {
350                         string path = TempFolder + DSC + "DIT.Delete2.Test";
351                         DeleteDir (path);
352
353                         try {
354                                 Directory.CreateDirectory (path);
355                                 File.Create (path + DSC + "test").Close ();
356                                 DirectoryInfo info = new DirectoryInfo (path);
357                                 Assert.IsTrue (info.Exists, "#1");
358
359                                 info.Delete (true);
360                                 Assert.IsTrue (info.Exists, "#2");
361
362                                 info = new DirectoryInfo (path);
363                                 Assert.IsFalse (info.Exists, "#3");
364                         } finally {
365                                 DeleteDir (path);
366                         }
367                 }
368
369                 [Test] // Delete (Boolean)
370                 public void Delete2_DirectoryNotEmpty ()
371                 {
372                         string path = TempFolder + DSC + "DIT.DeleteIOException2.Test";
373                         DeleteDir (path);
374                         
375                         try {
376                                 Directory.CreateDirectory (path);
377                                 File.Create (path + DSC + "test").Close ();
378                                 DirectoryInfo info = new DirectoryInfo (path);
379                                 try {
380                                         info.Delete (false);
381                                         Assert.Fail ("#1");
382                                 } catch (IOException ex) {
383                                         // The directory is not empty
384                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
385                                         Assert.IsNull (ex.InnerException, "#3");
386                                         Assert.IsNotNull (ex.Message, "#4");
387                                 }
388                         } finally {
389                                 DeleteDir (path);
390                         }
391                 }
392
393                 [Test] // bug #75443
394                 public void FullName ()
395                 {
396                         DirectoryInfo di = new DirectoryInfo ("something");
397                         Assert.IsFalse (di.Exists, "#A1");
398                         Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "something"), di.FullName, "#A2");
399
400                         di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar);
401                         Assert.IsFalse (di.Exists, "#B1");
402                         Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#B2");
403
404                         di = new DirectoryInfo ("something" + Path.AltDirectorySeparatorChar);
405                         Assert.IsFalse (di.Exists, "#C1");
406                         Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#C2");
407
408                         if (RunningOnUnix) {
409                                 di = new DirectoryInfo ("/");
410                                 Assert.AreEqual ("/", di.FullName, "#D1");
411
412                                 di = new DirectoryInfo ("test/");
413                                 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "test/"), di.FullName, "#D2");
414
415                                 di = new DirectoryInfo ("/test");
416                                 Assert.AreEqual ("/test", di.FullName, "#D3");
417
418                                 di = new DirectoryInfo ("/test/");
419                                 Assert.AreEqual ("/test/", di.FullName, "#D4");
420                         } else {
421                                 di = new DirectoryInfo (@"c:");
422                                 Assert.AreEqual (@"C:\", di.FullName, "#D1");
423
424                                 di = new DirectoryInfo (@"c:\");
425                                 Assert.AreEqual (@"c:\", di.FullName, "#D2");
426
427                                 di = new DirectoryInfo (@"c:\test");
428                                 Assert.AreEqual (@"c:\test", di.FullName, "#D3");
429
430                                 di = new DirectoryInfo (@"c:\test\");
431                                 Assert.AreEqual (@"c:\test\", di.FullName, "#D4");
432                         }
433                 }
434
435                 [Test]
436                 public void FullName_RootDirectory ()
437                 {
438                         DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar);
439                         if (RunningOnUnix) {
440                                 // can't be sure of the root drive under windows
441                                 Assert.AreEqual ("/", di.FullName, "#1");
442                         }
443                         Assert.IsNull (di.Parent, "#2");
444
445                         di = new DirectoryInfo (String.Empty + Path.AltDirectorySeparatorChar);
446                         if (RunningOnUnix) {
447                                 // can't be sure of the root drive under windows
448                                 Assert.AreEqual ("/", di.FullName, "#3");
449                         }
450                         Assert.IsNull (di.Parent, "#4");
451                 }
452
453                 [Test] // GetDirectories ()
454                 public void GetDirectories1 ()
455                 {
456                         string path = TempFolder + DSC + "DIT.GetDirectories1.Test";
457                         
458                         try {
459                                 DirectoryInfo info = Directory.CreateDirectory (path);
460                                 Assert.AreEqual (0, info.GetDirectories ().Length, "#1");
461                                 
462                                 Directory.CreateDirectory (path + DSC + "1");
463                                 Directory.CreateDirectory (path + DSC + "2");
464                                 File.Create (path + DSC + "filetest").Close ();
465                                 Assert.AreEqual (2, info.GetDirectories ().Length, "#2");
466                                 
467                                 Directory.Delete (path + DSC + 2);
468                                 Assert.AreEqual (1, info.GetDirectories ().Length, "#3");
469                         } finally {
470                                 DeleteDir (path);
471                         }
472                 }
473
474                 [Test] // GetDirectories ()
475                 public void GetDirectories1_DirectoryDoesNotExist ()
476                 {
477                         string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
478                         DeleteDir (path);
479
480                         try {
481                                 DirectoryInfo info = new DirectoryInfo (path);
482                                 info.GetDirectories ();
483                                 Assert.Fail ("#1");
484                         } catch (DirectoryNotFoundException ex) {
485                                 // Could not find a part of '...'
486                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
487                                 Assert.IsNull (ex.InnerException, "#3");
488                                 Assert.IsNotNull (ex.Message, "#4");
489                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
490                         } finally {
491                                 DeleteDir (path);
492                         }
493                 }
494
495                 [Test] // GetDirectories (String)
496                 public void GetDirectories2 ()
497                 {
498                         string path = TempFolder + DSC + "DIT.GetDirectories2.Test";
499                         
500                         try {
501                                 DirectoryInfo info = Directory.CreateDirectory (path);
502                                 Assert.AreEqual (0, info.GetDirectories ("*").Length, "#1");
503                                 
504                                 Directory.CreateDirectory (path + DSC + "test120");
505                                 Directory.CreateDirectory (path + DSC + "test210");
506                                 Directory.CreateDirectory (path + DSC + "atest330");
507                                 Directory.CreateDirectory (path + DSC + "test220");
508                                 Directory.CreateDirectory (path + DSC + "rest");
509                                 Directory.CreateDirectory (path + DSC + "rest" + DSC + "subdir");
510                                 File.Create (path + DSC + "filetest").Close ();
511
512                                 Assert.AreEqual (5, info.GetDirectories ("*").Length, "#2");
513                                 Assert.AreEqual (3, info.GetDirectories ("test*").Length, "#3");
514                                 Assert.AreEqual (2, info.GetDirectories ("test?20").Length, "#4");
515                                 Assert.AreEqual (0, info.GetDirectories ("test?").Length, "#5");
516                                 Assert.AreEqual (0, info.GetDirectories ("test[12]*").Length, "#6");
517                                 Assert.AreEqual (2, info.GetDirectories ("test2*0").Length, "#7");
518                                 Assert.AreEqual (4, info.GetDirectories ("*test*").Length, "#8");
519                                 Assert.AreEqual (6, info.GetDirectories ("*", SearchOption.AllDirectories).Length, "#9");
520                         } finally {
521                                 DeleteDir (path);
522                         }
523                 }
524
525                 [Test] // GetDirectories (String)
526                 public void GetDirectories2_DirectoryDoesNotExist ()
527                 {
528                         string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException2.Test";
529                         DeleteDir (path);
530
531                         try {
532                                 DirectoryInfo info = new DirectoryInfo (path);
533                                 info.GetDirectories ("*");
534                                 Assert.Fail ("#1");
535                         } catch (DirectoryNotFoundException ex) {
536                                 // Could not find a part of '...'
537                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
538                                 Assert.IsNull (ex.InnerException, "#3");
539                                 Assert.IsNotNull (ex.Message, "#4");
540                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
541                         } finally {
542                                 DeleteDir (path);
543                         }
544                 }
545
546                 [Test] // GetDirectories (String)
547                 public void GetDirectories2_SearchPattern_Null ()
548                 {
549                         DirectoryInfo info = new DirectoryInfo (TempFolder);
550                         try {
551                                 info.GetDirectories (null);
552                                 Assert.Fail ("#1");
553                         } catch (ArgumentNullException ex) {
554                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
555                                 Assert.IsNull (ex.InnerException, "#3");
556                                 Assert.IsNotNull (ex.Message, "#4");
557                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
558                         }
559                 }
560
561                 [Test] //GetDirectories (String, SearchOptions)
562                 public void GetDirectiories_SearchOptionAllDirectories ()
563                 {
564                         string directoryToBeLookedFor = "lookforme";
565                         DirectoryInfo baseDir = Directory.CreateDirectory(Path.Combine(TempFolder, "GetDirectiories_SearchOptionAllDirectories"));
566                         DirectoryInfo subdir = baseDir.CreateSubdirectory("subdir");
567                         DirectoryInfo subsubdir = subdir.CreateSubdirectory(directoryToBeLookedFor);
568                         DirectoryInfo[] directoriesFound = baseDir.GetDirectories(directoryToBeLookedFor, SearchOption.AllDirectories);
569                         Assert.AreEqual(1, directoriesFound.Length, "There should be exactly one directory with the specified name.");
570                         Assert.AreEqual(directoryToBeLookedFor, directoriesFound[0].Name, "The name of the directory found should match the expected one.");
571                 }
572
573                 [Test] // GetDirectories (String, SearchOption)
574                 public void GetDirectories3_SearchPattern_Null ()
575                 {
576                         DirectoryInfo info = new DirectoryInfo (TempFolder);
577                         try {
578                                 info.GetDirectories (null, SearchOption.AllDirectories);
579                                 Assert.Fail ("#1");
580                         } catch (ArgumentNullException ex) {
581                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
582                                 Assert.IsNull (ex.InnerException, "#3");
583                                 Assert.IsNotNull (ex.Message, "#4");
584                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
585                         }
586                 }
587
588                 [Test] // GetFiles ()
589                 public void GetFiles1 ()
590                 {
591                         string path = TempFolder + DSC + "DIT.GetFiles1.Test";
592                         DeleteDir (path);
593
594                         try {
595                                 DirectoryInfo info = Directory.CreateDirectory (path);
596                                 Assert.AreEqual (0, info.GetFiles ().Length, "#1");
597                                 File.Create (path + DSC + "file1").Close ();
598                                 File.Create (path + DSC + "file2").Close ();
599                                 Directory.CreateDirectory (path + DSC + "directory1");
600                                 Assert.AreEqual (2, info.GetFiles ().Length, "#2");
601                         } finally {
602                                 DeleteDir (path);
603                         }
604                 }
605
606                 [Test] // GetFiles ()
607                 public void GetFiles1_DirectoryDoesNotExist ()
608                 {
609                         string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
610                         DeleteDir (path);
611
612                         try {
613                                 DirectoryInfo info = new DirectoryInfo (path);
614                                 info.GetFiles ();
615                                 Assert.Fail ("#1");
616                         } catch (DirectoryNotFoundException ex) {
617                                 // Could not find a part of '...'
618                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
619                                 Assert.IsNull (ex.InnerException, "#3");
620                                 Assert.IsNotNull (ex.Message, "#4");
621                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
622                         } finally {
623                                 DeleteDir (path);
624                         }
625                 }
626
627                 [Test] // GetFiles (String)
628                 public void GetFiles2 ()
629                 {
630                         string path = TempFolder + DSC + "DIT.GetFiles2.Test";
631                         DeleteDir (path);
632
633                         try {
634                                 DirectoryInfo info = Directory.CreateDirectory (path);
635                                 Assert.AreEqual (0, info.GetFiles ("*").Length, "#1");
636                                 File.Create (path + DSC + "file120file").Close ();
637                                 File.Create (path + DSC + "file220file").Close ();
638                                 File.Create (path + DSC + "afile330file").Close ();
639                                 File.Create (path + DSC + "test.abc").Close ();
640                                 File.Create (path + DSC + "test.abcd").Close ();
641                                 File.Create (path + DSC + "test.abcdef").Close ();
642                                 Directory.CreateDirectory (path + DSC + "dir");
643
644                                 Assert.AreEqual (6, info.GetFiles ("*").Length, "#2");
645                                 Assert.AreEqual (2, info.GetFiles ("file*file").Length, "#3");
646                                 Assert.AreEqual (3, info.GetFiles ("*file*").Length, "#4");
647                                 Assert.AreEqual (2, info.GetFiles ("file?20file").Length, "#5");
648                                 Assert.AreEqual (1, info.GetFiles ("*.abcd").Length, "#6");
649                                 Assert.AreEqual (2, info.GetFiles ("*.abcd*").Length, "#7");
650                         } finally {
651                                 DeleteDir (path);
652                         }
653                 }
654
655                 [Test] // GetFiles (String)
656                 public void GetFiles2_DirectoryDoesNotExist ()
657                 {
658                         string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
659                         DeleteDir (path);
660
661                         try {
662                                 DirectoryInfo info = new DirectoryInfo (path);
663                                 info.GetFiles ("*");
664                                 Assert.Fail ("#1");
665                         } catch (DirectoryNotFoundException ex) {
666                                 // Could not find a part of '...'
667                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
668                                 Assert.IsNull (ex.InnerException, "#3");
669                                 Assert.IsNotNull (ex.Message, "#4");
670                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
671                         } finally {
672                                 DeleteDir (path);
673                         }
674                 }
675
676                 [Test] // GetFiles (String)
677                 public void GetFiles2_SearchPattern_Null ()
678                 {
679                         DirectoryInfo info = new DirectoryInfo (TempFolder);
680                         try {
681                                 info.GetFiles (null);
682                                 Assert.Fail ("#1");
683                         } catch (ArgumentNullException ex) {
684                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
685                                 Assert.IsNull (ex.InnerException, "#3");
686                                 Assert.IsNotNull (ex.Message, "#4");
687                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
688                         }
689                 }
690
691                 [Test] // GetFiles (String, SearchOption)
692                 public void GetFiles3_SearchPattern_Null ()
693                 {
694                         DirectoryInfo info = new DirectoryInfo (TempFolder);
695                         try {
696                                 info.GetFiles (null, SearchOption.TopDirectoryOnly);
697                                 Assert.Fail ("#1");
698                         } catch (ArgumentNullException ex) {
699                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
700                                 Assert.IsNull (ex.InnerException, "#3");
701                                 Assert.IsNotNull (ex.Message, "#4");
702                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
703                         }
704                 }
705
706                 [Test]
707                 public void GetFileSystemInfos2_SearchPattern_Null ()
708                 {
709                         DirectoryInfo info = new DirectoryInfo (TempFolder);
710                         try {
711                                 info.GetFileSystemInfos (null);
712                                 Assert.Fail ("#1");
713                         } catch (ArgumentNullException ex) {
714                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
715                                 Assert.IsNull (ex.InnerException, "#3");
716                                 Assert.IsNotNull (ex.Message, "#4");
717                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
718                         }
719                 }
720
721                 [Test]
722                 public void MoveTo ()
723                 {
724                         string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
725                         string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
726                         DeleteDir (path1);
727                         DeleteDir (path2);
728
729                         try {
730                                 DirectoryInfo info1 = Directory.CreateDirectory (path1);
731                                 DirectoryInfo info2 = new DirectoryInfo (path2);
732
733                                 Assert.IsTrue (info1.Exists, "#A1");
734                                 Assert.IsFalse (info2.Exists, "#A2");
735
736                                 info1.MoveTo (path2);
737                                 Assert.IsTrue (info1.Exists, "#B1");
738                                 Assert.IsFalse (info2.Exists, "#B2");
739
740                                 info1 = new DirectoryInfo (path1);
741                                 info2 = new DirectoryInfo (path2);
742                                 Assert.IsFalse (info1.Exists, "#C1");
743                                 Assert.IsTrue (info2.Exists, "#C2");
744                         } finally {
745                                 DeleteDir (path1);
746                                 DeleteDir (path2);
747                         }
748                 }
749
750                 [Test]
751                 public void MoveTo_DestDirName_Empty ()
752                 {
753                         string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
754                         DeleteDir (path);
755
756                         try {
757                                 DirectoryInfo info = Directory.CreateDirectory (path);
758                                 try {
759                                         info.MoveTo (string.Empty);
760                                         Assert.Fail ("#1");
761                                 } catch (ArgumentException ex) {
762                                         // Empty file name is not legal
763                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
764                                         Assert.IsNull (ex.InnerException, "#3");
765                                         Assert.IsNotNull (ex.Message, "#4");
766                                         Assert.AreEqual ("destDirName", ex.ParamName, "#5");
767                                 }
768                         } finally {
769                                 DeleteDir (path);
770                         }
771                 }
772
773                 [Test]
774                 public void MoveTo_DestDirName_Null ()
775                 {
776                         string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
777                         DeleteDir (path);
778
779                         try {
780                                 DirectoryInfo info = Directory.CreateDirectory (path);
781                                 try {
782                                         info.MoveTo (null);
783                                         Assert.Fail ("#1");
784                                 } catch (ArgumentNullException ex) {
785                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
786                                         Assert.IsNull (ex.InnerException, "#3");
787                                         Assert.IsNotNull (ex.Message, "#4");
788                                         Assert.AreEqual ("destDirName", ex.ParamName, "#5");
789                                 }
790                         } finally {
791                                 DeleteDir (path);
792                         }
793                 }
794
795                 [Test]
796                 public void MoveTo_DestDirName_InvalidPathChars ()
797                 {
798                         string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
799                         DeleteDir (path);
800                         
801                         try {
802                                 DirectoryInfo info = Directory.CreateDirectory (path);
803                                 try {
804                                         info.MoveTo (Path.InvalidPathChars [0].ToString ());
805                                         Assert.Fail ("#1");
806                                 } catch (ArgumentException ex) {
807                                         // The path contains illegal characters
808                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
809                                         Assert.IsNull (ex.InnerException, "#3");
810                                         Assert.IsNotNull (ex.Message, "#4");
811                                         Assert.IsNull (ex.ParamName, "#5");
812                                 }
813                         } finally {
814                                 DeleteDir (path);
815                         }
816                 }
817
818                 [Test]
819                 public void MoveTo_DestDirName_Whitespace ()
820                 {
821                         string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
822                         DeleteDir (path);
823
824                         try {
825                                 DirectoryInfo info = Directory.CreateDirectory (path);
826                                 try {
827                                         info.MoveTo ("    ");
828                                         Assert.Fail ("#1");
829                                 } catch (ArgumentException ex) {
830                                         // The path is not of a legal form
831                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
832                                         Assert.IsNull (ex.InnerException, "#3");
833                                         Assert.IsNotNull (ex.Message, "#4");
834                                         Assert.IsNull (ex.ParamName, "#5");
835                                 }
836                         } finally {
837                                 DeleteDir (path);
838                         }
839                 }
840
841                 [Test]
842                 public void MoveTo_SourceDest_NotDifferent ()
843                 {
844                         string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
845                         DeleteDir (path);
846
847                         try {
848                                 DirectoryInfo info = Directory.CreateDirectory (path);
849                                 try {
850                                         info.MoveTo (path);
851                                         Assert.Fail ("#A1");
852                                 } catch (IOException ex) {
853                                         // Source and destination path must be different
854                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
855                                         Assert.IsNull (ex.InnerException, "#A3");
856                                         Assert.IsNotNull (ex.Message, "#A4");
857                                 }
858                         } finally {
859                                 DeleteDir (path);
860                         }
861
862                         try {
863                                 DirectoryInfo info = new DirectoryInfo (path);
864                                 try {
865                                         info.MoveTo (path);
866                                         Assert.Fail ("#B1");
867                                 } catch (IOException ex) {
868                                         // Source and destination path must be different
869                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
870                                         Assert.IsNull (ex.InnerException, "#B3");
871                                         Assert.IsNotNull (ex.Message, "#B4");
872                                 }
873                         } finally {
874                                 DeleteDir (path);
875                         }
876                 }
877                 
878                 [Test]
879                 public void MoveTo_UpdateProperties ()
880                 {
881                         string path = TempFolder + DSC + "DIT.MoveToUpdateProperties.Test";
882                         string path2 = TempFolder + DSC + "DIT.MoveToUpdateProperties2.Test";
883                         string path3 = path2 + DSC + "DIT.MoveToUpdateProperties3.Test";
884                         DeleteDir (path);
885                         Directory.CreateDirectory (path);
886                         Directory.CreateDirectory (path2);
887
888                         DirectoryInfo info = new DirectoryInfo(path);
889                         
890                         Assert.IsTrue (Directory.Exists(info.FullName));
891                         Assert.AreEqual (path, info.FullName);
892                         Assert.AreEqual ("DIT.MoveToUpdateProperties.Test", info.Name);
893                         Assert.AreEqual (TempFolder, info.Parent.FullName);
894                         Assert.AreEqual (path, info.ToString ());
895
896                         info.MoveTo (path3);
897                         Assert.IsTrue (Directory.Exists(info.FullName));
898                         Assert.AreEqual (path3, info.FullName);
899                         Assert.AreEqual ("DIT.MoveToUpdateProperties3.Test", info.Name);
900                         Assert.AreEqual (path2, info.Parent.FullName);
901                         Assert.AreEqual (path3, info.ToString ());
902                 }
903
904                 [Test]
905                 public void DirectoryNameWithSpace ()
906                 {
907                         DeleteDir ("this has a space at the end ");
908                         string path = Path.Combine (TempFolder, "this has a space at the end ");
909                         Directory.CreateDirectory (path);
910                         DirectoryInfo i = new DirectoryInfo (path);
911                         string dummy = null;
912                         foreach (FileInfo f in i.GetFiles ())
913                                 dummy = f.Name;
914                 }
915
916                 [Test]
917                 public void LastWriteTime ()
918                 {
919                         DirectoryInfo info = new DirectoryInfo (TempFolder);
920                         info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
921
922                         DateTime time = Directory.GetLastWriteTime (TempFolder);
923                         Assert.AreEqual (2003, time.Year, "#A1");
924                         Assert.AreEqual (6, time.Month, "#A2");
925                         Assert.AreEqual (4, time.Day, "#A3");
926                         Assert.AreEqual (6, time.Hour, "#A4");
927                         Assert.AreEqual (4, time.Minute, "#A5");
928                         Assert.AreEqual (0, time.Second, "#A6");
929
930                         time = TimeZone.CurrentTimeZone.ToLocalTime (
931                                 Directory.GetLastWriteTimeUtc (TempFolder));
932                         Assert.AreEqual (2003, time.Year, "#B1");
933                         Assert.AreEqual (6, time.Month, "#B2");
934                         Assert.AreEqual (4, time.Day, "#B3");
935                         Assert.AreEqual (6, time.Hour, "#B4");
936                         Assert.AreEqual (4, time.Minute, "#B5");
937                         Assert.AreEqual (0, time.Second, "#B6");
938                 }
939
940                 [Test]
941                 public void LastWriteTimeUtc ()
942                 {
943                         DirectoryInfo info = new DirectoryInfo (TempFolder);
944                         info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
945
946                         DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
947                                 Directory.GetLastWriteTime (TempFolder));
948                         Assert.AreEqual (2003, time.Year, "#A1");
949                         Assert.AreEqual (6, time.Month, "#A2");
950                         Assert.AreEqual (4, time.Day, "#A3");
951                         Assert.AreEqual (6, time.Hour, "#A4");
952                         Assert.AreEqual (4, time.Minute, "#A5");
953                         Assert.AreEqual (0, time.Second, "#A6");
954
955                         time = Directory.GetLastWriteTimeUtc (TempFolder);
956                         Assert.AreEqual (2003, time.Year, "#B1");
957                         Assert.AreEqual (6, time.Month, "#B2");
958                         Assert.AreEqual (4, time.Day, "#B3");
959                         Assert.AreEqual (6, time.Hour, "#B4");
960                         Assert.AreEqual (4, time.Minute, "#B5");
961                         Assert.AreEqual (0, time.Second, "#B6");
962                 }
963
964                 [Test]
965                 public void LastAccessTime ()
966                 {
967                         DirectoryInfo info = new DirectoryInfo (TempFolder);
968                         info.LastAccessTime = DateTime.Now;
969                 }
970
971                 [Test]
972                 public void LastAccessTimeUtc ()
973                 {
974                         DirectoryInfo info = new DirectoryInfo (TempFolder);
975                         info.LastAccessTimeUtc = DateTime.Now;
976                 }
977
978                 [Test]
979                 public void CreationTime ()
980                 {
981                         DirectoryInfo info = new DirectoryInfo (TempFolder);
982                         info.CreationTime = DateTime.Now;
983                 }
984
985                 [Test]
986                 public void CreationTimeUtc ()
987                 {
988                         DirectoryInfo info = new DirectoryInfo (TempFolder);
989                         info.CreationTimeUtc = DateTime.Now;
990                 }
991
992                 [Test]
993                 public void Name_Bug76903 ()
994                 {
995                         CheckName ("/usr/share");
996                         CheckName ("/usr/share/");
997                         CheckName ("/usr/share/.");
998                         CheckName ("/usr/share/./");
999                         CheckName ("/usr/share/blabla/../");
1000                         CheckName ("/usr/lib/../share/.");
1001                 }
1002
1003                 [Test]
1004                 public void Hang_76191 ()
1005                 {
1006                         // from bug #76191 (hangs on Windows)
1007                         DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
1008                         Stack s = new Stack ();
1009                         s.Push (di);
1010                         while (di.Parent != null) {
1011                                 di = di.Parent;
1012                                 s.Push (di);
1013                         }
1014                         while (s.Count > 0) {
1015                                 di = (DirectoryInfo) s.Pop ();
1016                                 Assert.IsTrue (di.Exists, di.Name);
1017                         }
1018                 }
1019
1020                 [Test]
1021                 public void WindowsSystem32_76191 ()
1022                 {
1023                         if (RunningOnUnix)
1024                                 Assert.Ignore ("Running on Unix.");
1025
1026                         Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");
1027                         WindowsParentFullName ("C:", "C:\\WINDOWS");
1028                         WindowsParentFullName ("C:\\", null);
1029                         WindowsParentFullName ("C:\\dir", "C:\\");
1030                         WindowsParentFullName ("C:\\dir\\", "C:\\");
1031                         WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
1032                         WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
1033                 }
1034
1035                 [Test]
1036                 public void Parent_Bug77090 ()
1037                 {
1038                         DirectoryInfo di = new DirectoryInfo ("/home");
1039                         if (Path.DirectorySeparatorChar == '\\') {
1040                                 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1");
1041                         } else
1042                                 Assert.AreEqual ("/", di.Parent.Name, "#1");
1043                         Assert.IsNull (di.Parent.Parent, "#2");
1044                 }
1045
1046                 [Test]
1047                 public void ToStringTest ()
1048                 {
1049                         DirectoryInfo info;
1050
1051                         info = new DirectoryInfo ("Test");
1052                         Assert.AreEqual ("Test", info.ToString (), "#1");
1053                         info = new DirectoryInfo (TempFolder + DSC + "ToString.Test");
1054                         Assert.AreEqual (TempFolder + DSC + "ToString.Test", info.ToString ());
1055                 }
1056
1057                 [Test]
1058                 public void EnumerateFileSystemInfosTest ()
1059                 {
1060                         var dirInfo = new DirectoryInfo (TempFolder);
1061                         dirInfo.CreateSubdirectory ("1").CreateSubdirectory ("a");
1062                         dirInfo.CreateSubdirectory ("2").CreateSubdirectory ("b");
1063
1064                         var l = new List<string> ();
1065                         foreach (var info in dirInfo.EnumerateFileSystemInfos ("*", SearchOption.AllDirectories))
1066                                 l.Add (info.Name);
1067
1068                         l.Sort ();
1069                         Assert.AreEqual ("1,2,a,b", string.Join (",", l), "#1");
1070                 }
1071
1072 #if !MOBILE
1073                 [Test]
1074                 public void Serialization ()
1075                 {
1076                         DirectoryInfo info;
1077                         SerializationInfo si;
1078
1079                         info = new DirectoryInfo ("Test");
1080                         si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1081                         info.GetObjectData (si, new StreamingContext ());
1082
1083                         Assert.AreEqual (2, si.MemberCount, "#A1");
1084                         Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1085                         Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1086
1087                         info = new DirectoryInfo (TempFolder);
1088                         si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1089                         info.GetObjectData (si, new StreamingContext ());
1090
1091                         Assert.AreEqual (2, si.MemberCount, "#B1");
1092                         Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1093                         Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1094                 }
1095
1096                 [Test]
1097                 public void Deserialization ()
1098                 {
1099                         DirectoryInfo info = new DirectoryInfo ("Test");
1100
1101                         MemoryStream ms = new MemoryStream ();
1102                         BinaryFormatter bf = new BinaryFormatter ();
1103                         bf.Serialize (ms, info);
1104                         ms.Position = 0;
1105
1106                         DirectoryInfo clone = (DirectoryInfo) bf.Deserialize (ms);
1107                         Assert.AreEqual (info.Name, clone.Name, "#1");
1108                         Assert.AreEqual (info.FullName, clone.FullName, "#2");
1109                 }
1110
1111                 // Needed so that UnixSymbolicLinkInfo doesn't have to
1112                 // be JITted on windows
1113                 private void Symlink_helper ()
1114                 {
1115                         string path = TempFolder + DSC + "DIT.Symlink";
1116                         string dir = path + DSC + "dir";
1117                         string link = path + DSC + "link";
1118
1119                         DeleteDir (path);
1120
1121                         try {
1122                                 Directory.CreateDirectory (path);
1123                                 Directory.CreateDirectory (dir);
1124                                 global::Mono.Unix.UnixSymbolicLinkInfo li = new global::Mono.Unix.UnixSymbolicLinkInfo (link);
1125                                 li.CreateSymbolicLinkTo (dir);
1126
1127                                 DirectoryInfo info = new DirectoryInfo (path);
1128                                 DirectoryInfo[] dirs = info.GetDirectories ();
1129                                 Assert.AreEqual (2, dirs.Length, "#1");
1130                         } finally {
1131                                 DeleteDir (path);
1132                         }
1133                 }
1134
1135                 [Test]
1136                 [Category ("NotDotNet")]
1137                 public void Symlink ()
1138                 {
1139                         // This test only applies to Linux and
1140                         // Linux-like platforms but mono-on-windows
1141                         // doesn't set the NotDotNet category
1142                         if (!RunningOnUnix) {
1143                                 Assert.Ignore ("Not running on Unix.");
1144                         }
1145
1146                         Symlink_helper ();
1147                 }
1148 #endif
1149                 static bool RunningOnUnix {
1150                         get {
1151                                 int p = (int) Environment.OSVersion.Platform;
1152                                 return ((p == 4) || (p == 128) || (p == 6));
1153                         }
1154                 }
1155
1156                 void WindowsParentFullName (string name, string expected)
1157                 {
1158                         DirectoryInfo di = new DirectoryInfo (name);
1159                         if (di.Parent == null)
1160                                 Assert.IsNull (expected, name);
1161                         else
1162                                 Assert.AreEqual (expected, di.Parent.FullName, name);
1163                 }
1164
1165                 void CheckName (string name)
1166                 {
1167                         DirectoryInfo di = new DirectoryInfo (name);
1168                         Assert.AreEqual ("share", di.Name, name + ".Name");
1169                         Assert.AreEqual ("usr", di.Parent.Name, name + ".Parent.Name");
1170                 }
1171
1172                 void DeleteDir (string path)
1173                 {
1174                         if (Directory.Exists (path))
1175                                 Directory.Delete (path, true);
1176                 }
1177         }
1178 }