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