Add missing 3rd System reference
[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 #if NET_2_0
562                 [Test] // GetDirectories (String, SearchOption)
563                 public void GetDirectories3_SearchPattern_Null ()
564                 {
565                         DirectoryInfo info = new DirectoryInfo (TempFolder);
566                         try {
567                                 info.GetDirectories (null, SearchOption.AllDirectories);
568                                 Assert.Fail ("#1");
569                         } catch (ArgumentNullException ex) {
570                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
571                                 Assert.IsNull (ex.InnerException, "#3");
572                                 Assert.IsNotNull (ex.Message, "#4");
573                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
574                         }
575                 }
576 #endif
577
578                 [Test] // GetFiles ()
579                 public void GetFiles1 ()
580                 {
581                         string path = TempFolder + DSC + "DIT.GetFiles1.Test";
582                         DeleteDir (path);
583
584                         try {
585                                 DirectoryInfo info = Directory.CreateDirectory (path);
586                                 Assert.AreEqual (0, info.GetFiles ().Length, "#1");
587                                 File.Create (path + DSC + "file1").Close ();
588                                 File.Create (path + DSC + "file2").Close ();
589                                 Directory.CreateDirectory (path + DSC + "directory1");
590                                 Assert.AreEqual (2, info.GetFiles ().Length, "#2");
591                         } finally {
592                                 DeleteDir (path);
593                         }
594                 }
595
596                 [Test] // GetFiles ()
597                 public void GetFiles1_DirectoryDoesNotExist ()
598                 {
599                         string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
600                         DeleteDir (path);
601
602                         try {
603                                 DirectoryInfo info = new DirectoryInfo (path);
604                                 info.GetFiles ();
605                                 Assert.Fail ("#1");
606                         } catch (DirectoryNotFoundException ex) {
607                                 // Could not find a part of '...'
608                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
609                                 Assert.IsNull (ex.InnerException, "#3");
610                                 Assert.IsNotNull (ex.Message, "#4");
611                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
612                         } finally {
613                                 DeleteDir (path);
614                         }
615                 }
616
617                 [Test] // GetFiles (String)
618                 public void GetFiles2 ()
619                 {
620                         string path = TempFolder + DSC + "DIT.GetFiles2.Test";
621                         DeleteDir (path);
622
623                         try {
624                                 DirectoryInfo info = Directory.CreateDirectory (path);
625                                 Assert.AreEqual (0, info.GetFiles ("*").Length, "#1");
626                                 File.Create (path + DSC + "file120file").Close ();
627                                 File.Create (path + DSC + "file220file").Close ();
628                                 File.Create (path + DSC + "afile330file").Close ();
629                                 File.Create (path + DSC + "test.abc").Close ();
630                                 File.Create (path + DSC + "test.abcd").Close ();
631                                 File.Create (path + DSC + "test.abcdef").Close ();
632                                 Directory.CreateDirectory (path + DSC + "dir");
633
634                                 Assert.AreEqual (6, info.GetFiles ("*").Length, "#2");
635                                 Assert.AreEqual (2, info.GetFiles ("file*file").Length, "#3");
636                                 Assert.AreEqual (3, info.GetFiles ("*file*").Length, "#4");
637                                 Assert.AreEqual (2, info.GetFiles ("file?20file").Length, "#5");
638                                 Assert.AreEqual (1, info.GetFiles ("*.abcd").Length, "#6");
639                                 Assert.AreEqual (2, info.GetFiles ("*.abcd*").Length, "#7");
640                         } finally {
641                                 DeleteDir (path);
642                         }
643                 }
644
645                 [Test] // GetFiles (String)
646                 public void GetFiles2_DirectoryDoesNotExist ()
647                 {
648                         string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
649                         DeleteDir (path);
650
651                         try {
652                                 DirectoryInfo info = new DirectoryInfo (path);
653                                 info.GetFiles ("*");
654                                 Assert.Fail ("#1");
655                         } catch (DirectoryNotFoundException ex) {
656                                 // Could not find a part of '...'
657                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
658                                 Assert.IsNull (ex.InnerException, "#3");
659                                 Assert.IsNotNull (ex.Message, "#4");
660                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
661                         } finally {
662                                 DeleteDir (path);
663                         }
664                 }
665
666                 [Test] // GetFiles (String)
667                 public void GetFiles2_SearchPattern_Null ()
668                 {
669                         DirectoryInfo info = new DirectoryInfo (TempFolder);
670                         try {
671                                 info.GetFiles (null);
672                                 Assert.Fail ("#1");
673                         } catch (ArgumentNullException ex) {
674                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
675                                 Assert.IsNull (ex.InnerException, "#3");
676                                 Assert.IsNotNull (ex.Message, "#4");
677                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
678                         }
679                 }
680
681 #if NET_2_0
682                 [Test] // GetFiles (String, SearchOption)
683                 public void GetFiles3_SearchPattern_Null ()
684                 {
685                         DirectoryInfo info = new DirectoryInfo (TempFolder);
686                         try {
687                                 info.GetFiles (null, SearchOption.TopDirectoryOnly);
688                                 Assert.Fail ("#1");
689                         } catch (ArgumentNullException ex) {
690                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
691                                 Assert.IsNull (ex.InnerException, "#3");
692                                 Assert.IsNotNull (ex.Message, "#4");
693                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
694                         }
695                 }
696 #endif
697
698                 [Test]
699                 public void GetFileSystemInfos2_SearchPattern_Null ()
700                 {
701                         DirectoryInfo info = new DirectoryInfo (TempFolder);
702                         try {
703                                 info.GetFileSystemInfos (null);
704                                 Assert.Fail ("#1");
705                         } catch (ArgumentNullException ex) {
706                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
707                                 Assert.IsNull (ex.InnerException, "#3");
708                                 Assert.IsNotNull (ex.Message, "#4");
709                                 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
710                         }
711                 }
712
713                 [Test]
714                 public void MoveTo ()
715                 {
716                         string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
717                         string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
718                         DeleteDir (path1);
719                         DeleteDir (path2);
720
721                         try {
722                                 DirectoryInfo info1 = Directory.CreateDirectory (path1);
723                                 DirectoryInfo info2 = new DirectoryInfo (path2);
724
725                                 Assert.IsTrue (info1.Exists, "#A1");
726                                 Assert.IsFalse (info2.Exists, "#A2");
727
728                                 info1.MoveTo (path2);
729                                 Assert.IsTrue (info1.Exists, "#B1");
730                                 Assert.IsFalse (info2.Exists, "#B2");
731
732                                 info1 = new DirectoryInfo (path1);
733                                 info2 = new DirectoryInfo (path2);
734                                 Assert.IsFalse (info1.Exists, "#C1");
735                                 Assert.IsTrue (info2.Exists, "#C2");
736                         } finally {
737                                 DeleteDir (path1);
738                                 DeleteDir (path2);
739                         }
740                 }
741
742                 [Test]
743                 public void MoveTo_DestDirName_Empty ()
744                 {
745                         string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
746                         DeleteDir (path);
747
748                         try {
749                                 DirectoryInfo info = Directory.CreateDirectory (path);
750                                 try {
751                                         info.MoveTo (string.Empty);
752                                         Assert.Fail ("#1");
753                                 } catch (ArgumentException ex) {
754                                         // Empty file name is not legal
755                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
756                                         Assert.IsNull (ex.InnerException, "#3");
757                                         Assert.IsNotNull (ex.Message, "#4");
758                                         Assert.AreEqual ("destDirName", ex.ParamName, "#5");
759                                 }
760                         } finally {
761                                 DeleteDir (path);
762                         }
763                 }
764
765                 [Test]
766                 public void MoveTo_DestDirName_Null ()
767                 {
768                         string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
769                         DeleteDir (path);
770
771                         try {
772                                 DirectoryInfo info = Directory.CreateDirectory (path);
773                                 try {
774                                         info.MoveTo (null);
775                                         Assert.Fail ("#1");
776                                 } catch (ArgumentNullException ex) {
777                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
778                                         Assert.IsNull (ex.InnerException, "#3");
779                                         Assert.IsNotNull (ex.Message, "#4");
780                                         Assert.AreEqual ("destDirName", ex.ParamName, "#5");
781                                 }
782                         } finally {
783                                 DeleteDir (path);
784                         }
785                 }
786
787                 [Test]
788                 public void MoveTo_DestDirName_InvalidPathChars ()
789                 {
790                         string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
791                         DeleteDir (path);
792                         
793                         try {
794                                 DirectoryInfo info = Directory.CreateDirectory (path);
795                                 try {
796                                         info.MoveTo (Path.InvalidPathChars [0].ToString ());
797                                         Assert.Fail ("#1");
798                                 } catch (ArgumentException ex) {
799                                         // The path contains illegal characters
800                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
801                                         Assert.IsNull (ex.InnerException, "#3");
802                                         Assert.IsNotNull (ex.Message, "#4");
803                                         Assert.IsNull (ex.ParamName, "#5");
804                                 }
805                         } finally {
806                                 DeleteDir (path);
807                         }
808                 }
809
810                 [Test]
811                 public void MoveTo_DestDirName_Whitespace ()
812                 {
813                         string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
814                         DeleteDir (path);
815
816                         try {
817                                 DirectoryInfo info = Directory.CreateDirectory (path);
818                                 try {
819                                         info.MoveTo ("    ");
820                                         Assert.Fail ("#1");
821                                 } catch (ArgumentException ex) {
822                                         // The path is not of a legal form
823                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
824                                         Assert.IsNull (ex.InnerException, "#3");
825                                         Assert.IsNotNull (ex.Message, "#4");
826                                         Assert.IsNull (ex.ParamName, "#5");
827                                 }
828                         } finally {
829                                 DeleteDir (path);
830                         }
831                 }
832
833                 [Test]
834                 public void MoveTo_SourceDest_NotDifferent ()
835                 {
836                         string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
837                         DeleteDir (path);
838
839                         try {
840                                 DirectoryInfo info = Directory.CreateDirectory (path);
841                                 try {
842                                         info.MoveTo (path);
843                                         Assert.Fail ("#A1");
844                                 } catch (IOException ex) {
845                                         // Source and destination path must be different
846                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
847                                         Assert.IsNull (ex.InnerException, "#A3");
848                                         Assert.IsNotNull (ex.Message, "#A4");
849                                 }
850                         } finally {
851                                 DeleteDir (path);
852                         }
853
854                         try {
855                                 DirectoryInfo info = new DirectoryInfo (path);
856                                 try {
857                                         info.MoveTo (path);
858                                         Assert.Fail ("#B1");
859                                 } catch (IOException ex) {
860                                         // Source and destination path must be different
861                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
862                                         Assert.IsNull (ex.InnerException, "#B3");
863                                         Assert.IsNotNull (ex.Message, "#B4");
864                                 }
865                         } finally {
866                                 DeleteDir (path);
867                         }
868                 }
869
870                 [Test]
871                 public void DirectoryNameWithSpace ()
872                 {
873                         DeleteDir ("this has a space at the end ");
874                         string path = Path.Combine (TempFolder, "this has a space at the end ");
875                         Directory.CreateDirectory (path);
876                         DirectoryInfo i = new DirectoryInfo (path);
877                         string dummy = null;
878                         foreach (FileInfo f in i.GetFiles ())
879                                 dummy = f.Name;
880                 }
881
882                 [Test]
883                 public void LastWriteTime ()
884                 {
885                         DirectoryInfo info = new DirectoryInfo (TempFolder);
886                         info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
887
888                         DateTime time = Directory.GetLastWriteTime (TempFolder);
889                         Assert.AreEqual (2003, time.Year, "#A1");
890                         Assert.AreEqual (6, time.Month, "#A2");
891                         Assert.AreEqual (4, time.Day, "#A3");
892                         Assert.AreEqual (6, time.Hour, "#A4");
893                         Assert.AreEqual (4, time.Minute, "#A5");
894                         Assert.AreEqual (0, time.Second, "#A6");
895
896                         time = TimeZone.CurrentTimeZone.ToLocalTime (
897                                 Directory.GetLastWriteTimeUtc (TempFolder));
898                         Assert.AreEqual (2003, time.Year, "#B1");
899                         Assert.AreEqual (6, time.Month, "#B2");
900                         Assert.AreEqual (4, time.Day, "#B3");
901                         Assert.AreEqual (6, time.Hour, "#B4");
902                         Assert.AreEqual (4, time.Minute, "#B5");
903                         Assert.AreEqual (0, time.Second, "#B6");
904                 }
905
906                 [Test]
907                 public void LastWriteTimeUtc ()
908                 {
909                         DirectoryInfo info = new DirectoryInfo (TempFolder);
910                         info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
911
912                         DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
913                                 Directory.GetLastWriteTime (TempFolder));
914                         Assert.AreEqual (2003, time.Year, "#A1");
915                         Assert.AreEqual (6, time.Month, "#A2");
916                         Assert.AreEqual (4, time.Day, "#A3");
917                         Assert.AreEqual (6, time.Hour, "#A4");
918                         Assert.AreEqual (4, time.Minute, "#A5");
919                         Assert.AreEqual (0, time.Second, "#A6");
920
921                         time = Directory.GetLastWriteTimeUtc (TempFolder);
922                         Assert.AreEqual (2003, time.Year, "#B1");
923                         Assert.AreEqual (6, time.Month, "#B2");
924                         Assert.AreEqual (4, time.Day, "#B3");
925                         Assert.AreEqual (6, time.Hour, "#B4");
926                         Assert.AreEqual (4, time.Minute, "#B5");
927                         Assert.AreEqual (0, time.Second, "#B6");
928                 }
929
930                 [Test]
931                 [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
932                 public void LastAccessTime ()
933                 {
934                         DirectoryInfo info = new DirectoryInfo (TempFolder);
935                         info.LastAccessTime = DateTime.Now;
936                 }
937
938                 [Test]
939                 [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
940                 public void LastAccessTimeUtc ()
941                 {
942                         DirectoryInfo info = new DirectoryInfo (TempFolder);
943                         info.LastAccessTimeUtc = DateTime.Now;
944                 }
945
946                 [Test]
947                 [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
948                 public void CreationTime ()
949                 {
950                         DirectoryInfo info = new DirectoryInfo (TempFolder);
951                         info.CreationTime = DateTime.Now;
952                 }
953
954                 [Test]
955                 [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
956                 public void CreationTimeUtc ()
957                 {
958                         DirectoryInfo info = new DirectoryInfo (TempFolder);
959                         info.CreationTimeUtc = DateTime.Now;
960                 }
961
962                 [Test]
963                 public void Name_Bug76903 ()
964                 {
965                         CheckName ("/usr/share");
966                         CheckName ("/usr/share/");
967                         CheckName ("/usr/share/.");
968                         CheckName ("/usr/share/./");
969                         CheckName ("/usr/share/blabla/../");
970                         CheckName ("/usr/lib/../share/.");
971                 }
972
973                 [Test]
974                 public void Hang_76191 ()
975                 {
976                         // from bug #76191 (hangs on Windows)
977                         DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
978                         Stack s = new Stack ();
979                         s.Push (di);
980                         while (di.Parent != null) {
981                                 di = di.Parent;
982                                 s.Push (di);
983                         }
984                         while (s.Count > 0) {
985                                 di = (DirectoryInfo) s.Pop ();
986                                 Assert.IsTrue (di.Exists, di.Name);
987                         }
988                 }
989
990                 [Test]
991                 public void WindowsSystem32_76191 ()
992                 {
993                         if (RunningOnUnix)
994                                 return;
995
996                         Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");
997                         WindowsParentFullName ("C:", "C:\\WINDOWS");
998                         WindowsParentFullName ("C:\\", null);
999                         WindowsParentFullName ("C:\\dir", "C:\\");
1000                         WindowsParentFullName ("C:\\dir\\", "C:\\");
1001                         WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
1002                         WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
1003                 }
1004
1005                 [Test]
1006                 public void Parent_Bug77090 ()
1007                 {
1008                         DirectoryInfo di = new DirectoryInfo ("/home");
1009                         if (Path.DirectorySeparatorChar == '\\') {
1010                                 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1");
1011                         } else
1012                                 Assert.AreEqual ("/", di.Parent.Name, "#1");
1013                         Assert.IsNull (di.Parent.Parent, "#2");
1014                 }
1015
1016                 [Test]
1017                 public void ToStringTest ()
1018                 {
1019                         DirectoryInfo info;
1020
1021                         info = new DirectoryInfo ("Test");
1022                         Assert.AreEqual ("Test", info.ToString (), "#1");
1023                         info = new DirectoryInfo (TempFolder + DSC + "ToString.Test");
1024                         Assert.AreEqual (TempFolder + DSC + "ToString.Test", info.ToString ());
1025                 }
1026
1027                 [Test]
1028                 public void Serialization ()
1029                 {
1030                         DirectoryInfo info;
1031                         SerializationInfo si;
1032
1033                         info = new DirectoryInfo ("Test");
1034                         si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1035                         info.GetObjectData (si, new StreamingContext ());
1036
1037                         Assert.AreEqual (2, si.MemberCount, "#A1");
1038                         Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1039                         Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1040
1041                         info = new DirectoryInfo (TempFolder);
1042                         si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1043                         info.GetObjectData (si, new StreamingContext ());
1044
1045                         Assert.AreEqual (2, si.MemberCount, "#B1");
1046                         Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1047                         Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1048                 }
1049
1050                 [Test]
1051                 public void Deserialization ()
1052                 {
1053                         DirectoryInfo info = new DirectoryInfo ("Test");
1054
1055                         MemoryStream ms = new MemoryStream ();
1056                         BinaryFormatter bf = new BinaryFormatter ();
1057                         bf.Serialize (ms, info);
1058                         ms.Position = 0;
1059
1060                         DirectoryInfo clone = (DirectoryInfo) bf.Deserialize (ms);
1061                         Assert.AreEqual (info.Name, clone.Name, "#1");
1062                         Assert.AreEqual (info.FullName, clone.FullName, "#2");
1063                 }
1064                 
1065                 // Needed so that UnixSymbolicLinkInfo doesn't have to
1066                 // be JITted on windows
1067                 private void Symlink_helper ()
1068                 {
1069                         string path = TempFolder + DSC + "DIT.Symlink";
1070                         string dir = path + DSC + "dir";
1071                         string link = path + DSC + "link";
1072
1073                         DeleteDir (path);
1074
1075                         try {
1076                                 Directory.CreateDirectory (path);
1077                                 Directory.CreateDirectory (dir);
1078                                 Mono.Unix.UnixSymbolicLinkInfo li = new Mono.Unix.UnixSymbolicLinkInfo (link);
1079                                 li.CreateSymbolicLinkTo (dir);
1080
1081                                 DirectoryInfo info = new DirectoryInfo (path);
1082                                 DirectoryInfo[] dirs = info.GetDirectories ();
1083                                 Assert.AreEqual (2, dirs.Length, "#1");
1084                         } finally {
1085                                 DeleteDir (path);
1086                         }
1087                 }
1088
1089                 [Test]
1090                 [Category ("NotDotNet")]
1091                 public void Symlink ()
1092                 {
1093                         // This test only applies to Linux and
1094                         // Linux-like platforms but mono-on-windows
1095                         // doesn't set the NotDotNet category
1096                         if (!RunningOnUnix) {
1097                                 return;
1098                         }
1099
1100                         Symlink_helper ();
1101                 }
1102
1103                 static bool RunningOnUnix {
1104                         get {
1105                                 int p = (int) Environment.OSVersion.Platform;
1106                                 return ((p == 4) || (p == 128) || (p == 6));
1107                         }
1108                 }
1109
1110                 void WindowsParentFullName (string name, string expected)
1111                 {
1112                         DirectoryInfo di = new DirectoryInfo (name);
1113                         if (di.Parent == null)
1114                                 Assert.IsNull (expected, name);
1115                         else
1116                                 Assert.AreEqual (expected, di.Parent.FullName, name);
1117                 }
1118
1119                 void CheckName (string name)
1120                 {
1121                         DirectoryInfo di = new DirectoryInfo (name);
1122                         Assert.AreEqual ("share", di.Name, name + ".Name");
1123                         Assert.AreEqual ("usr", di.Parent.Name, name + ".Parent.Name");
1124                 }
1125
1126                 void DeleteDir (string path)
1127                 {
1128                         if (Directory.Exists (path))
1129                                 Directory.Delete (path, true);
1130                 }
1131         }
1132 }