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