DirectoryTest.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. //
  2. // System.IO.Directory
  3. //
  4. // Authors:
  5. // Ville Palo ([email protected])
  6. //
  7. // (C) 2003 Ville Palo
  8. //
  9. // TODO: Find out why ArgumentOutOfRange tests does not release directories properly
  10. //
  11. using NUnit.Framework;
  12. using System.IO;
  13. using System.Text;
  14. using System;
  15. using System.Globalization;
  16. using System.Threading;
  17. namespace MonoTests.System.IO {
  18. [TestFixture]
  19. public class DirectoryTest : Assertion {
  20. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  21. [SetUp]
  22. public void SetUp ()
  23. {
  24. if (!Directory.Exists (TempFolder))
  25. Directory.CreateDirectory (TempFolder);
  26. Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
  27. }
  28. [TearDown]
  29. public void TearDown () {
  30. if (Directory.Exists (TempFolder))
  31. Directory.Delete (TempFolder, true);
  32. }
  33. [Test]
  34. public void CreateDirectory ()
  35. {
  36. string path = TempFolder + "/DirectoryTest.Test.1";
  37. DeleteDirectory (path);
  38. try {
  39. DirectoryInfo info = Directory.CreateDirectory (path);
  40. AssertEquals ("test#01", true, info.Exists);
  41. AssertEquals ("test#02", ".1", info.Extension);
  42. AssertEquals ("test#03", true, info.FullName.EndsWith ("DirectoryTest.Test.1"));
  43. AssertEquals ("test#04", "DirectoryTest.Test.1", info.Name);
  44. } finally {
  45. DeleteDirectory (path);
  46. }
  47. }
  48. [Test]
  49. [ExpectedException(typeof(ArgumentException))]
  50. public void CreateDirectoryNotSupportedException ()
  51. {
  52. DeleteDirectory (":");
  53. DirectoryInfo info = Directory.CreateDirectory (":");
  54. DeleteDirectory (":");
  55. }
  56. [Test]
  57. [ExpectedException(typeof(ArgumentNullException))]
  58. public void CreateDirectoryArgumentNullException ()
  59. {
  60. DirectoryInfo info = Directory.CreateDirectory (null as string);
  61. }
  62. [Test]
  63. [ExpectedException(typeof(ArgumentException))]
  64. public void CreateDirectoryArgumentException1 ()
  65. {
  66. DirectoryInfo info = Directory.CreateDirectory ("");
  67. }
  68. [Test]
  69. [ExpectedException(typeof(ArgumentException))]
  70. public void CreateDirectoryArgumentException2 ()
  71. {
  72. DirectoryInfo info = Directory.CreateDirectory (" ");
  73. }
  74. [Test]
  75. [ExpectedException(typeof(ArgumentException))]
  76. public void CreateDirectoryArgumentException3 ()
  77. {
  78. string path = TempFolder + "/DirectoryTest.Test";
  79. DeleteDirectory (path);
  80. try {
  81. path += Path.InvalidPathChars [0];
  82. path += ".2";
  83. DirectoryInfo info = Directory.CreateDirectory (path);
  84. } finally {
  85. DeleteDirectory (path);
  86. }
  87. }
  88. [Test]
  89. public void Delete ()
  90. {
  91. string path = TempFolder + "/DirectoryTest.Test.Delete.1";
  92. DeleteDirectory (path);
  93. try {
  94. Directory.CreateDirectory (path);
  95. AssertEquals ("test#01", true, Directory.Exists (path));
  96. Directory.CreateDirectory (path + "/DirectoryTest.Test.Delete.1.2");
  97. AssertEquals ("test#02", true, Directory.Exists (path + "/DirectoryTest.Test.Delete.1.2"));
  98. Directory.Delete (path + "/DirectoryTest.Test.Delete.1.2");
  99. AssertEquals ("test#03", false, Directory.Exists (path + "/DirectoryTest.Test.Delete.1.2"));
  100. AssertEquals ("test#04", true, Directory.Exists (path));
  101. Directory.Delete (path);
  102. AssertEquals ("test#05", false, Directory.Exists (path + "/DirectoryTest.Test.Delete.1.2"));
  103. AssertEquals ("test#06", false, Directory.Exists (path));
  104. Directory.CreateDirectory (path);
  105. Directory.CreateDirectory (path + "/DirectoryTest.Test.Delete.1.2");
  106. AssertEquals ("test#07", true, Directory.Exists (path + "/DirectoryTest.Test.Delete.1.2"));
  107. AssertEquals ("test#08", true, Directory.Exists (path));
  108. Directory.Delete (path, true);
  109. AssertEquals ("test#09", false, Directory.Exists (path + "/DirectoryTest.Test.Delete.1.2"));
  110. AssertEquals ("test#10", false, Directory.Exists (path));
  111. } finally {
  112. DeleteDirectory (path);
  113. }
  114. }
  115. [Test]
  116. [ExpectedException(typeof(ArgumentException))]
  117. public void DeleteArgumentException ()
  118. {
  119. Directory.Delete ("");
  120. }
  121. [Test]
  122. [ExpectedException(typeof(ArgumentException))]
  123. public void DeleteArgumentException2 ()
  124. {
  125. Directory.Delete (" ");
  126. }
  127. [Test]
  128. [ExpectedException(typeof(ArgumentException))]
  129. public void DeleteArgumentException3 ()
  130. {
  131. string path = TempFolder + "/DirectoryTest.Test.4";
  132. DeleteDirectory (path);
  133. path += Path.InvalidPathChars [0];
  134. Directory.Delete (path);
  135. }
  136. [Test]
  137. [ExpectedException(typeof(ArgumentNullException))]
  138. public void DeleteArgumentNullException ()
  139. {
  140. Directory.Delete (null as string);
  141. }
  142. [Test]
  143. [ExpectedException(typeof(DirectoryNotFoundException))]
  144. public void DeleteDirectoryNotFoundException ()
  145. {
  146. string path = TempFolder + "/DirectoryTest.Test.5";
  147. DeleteDirectory (path);
  148. Directory.Delete (path);
  149. }
  150. [Test]
  151. [ExpectedException(typeof(IOException))]
  152. public void DeleteArgumentException4 ()
  153. {
  154. string path = TempFolder + "/DirectoryTest.Test.6";
  155. DeleteDirectory (path);
  156. FileStream s = null;
  157. Directory.CreateDirectory (path);
  158. try {
  159. s = File.Create (path + "/DirectoryTest.Test.6");
  160. Directory.Delete (path);
  161. } finally {
  162. if (s != null)
  163. s.Close ();
  164. DeleteDirectory (path);
  165. };
  166. }
  167. [Test]
  168. public void Exists ()
  169. {
  170. AssertEquals ("test#01", false, Directory.Exists (null as string));
  171. }
  172. [Test]
  173. [ExpectedException(typeof(ArgumentNullException))]
  174. public void GetCreationTimeException1 ()
  175. {
  176. Directory.GetCreationTime (null as string);
  177. }
  178. [Test]
  179. [ExpectedException(typeof(ArgumentException))]
  180. public void GetCreationTimeException2 ()
  181. {
  182. Directory.GetCreationTime ("");
  183. }
  184. [Test]
  185. [ExpectedException(typeof(IOException))]
  186. public void GetCreationTimeException3 ()
  187. {
  188. string path = TempFolder + "/DirectoryTest.GetCreationTime.1";
  189. DeleteDirectory (path);
  190. try {
  191. Directory.GetCreationTime (path);
  192. } finally {
  193. DeleteDirectory (path);
  194. }
  195. }
  196. [Test]
  197. [ExpectedException(typeof(ArgumentException))]
  198. public void GetCreationTimeException4 ()
  199. {
  200. Directory.GetCreationTime (" ");
  201. }
  202. [Test]
  203. [ExpectedException(typeof(ArgumentException))]
  204. public void GetCreationTimeException5 ()
  205. {
  206. Directory.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  207. }
  208. [Test]
  209. [ExpectedException(typeof(ArgumentNullException))]
  210. public void GetCreationTimeUtcException1 ()
  211. {
  212. Directory.GetCreationTimeUtc (null as string);
  213. }
  214. [Test]
  215. [ExpectedException(typeof(ArgumentException))]
  216. public void GetCreationTimeUtcException2 ()
  217. {
  218. Directory.GetCreationTimeUtc ("");
  219. }
  220. [Test]
  221. [ExpectedException(typeof(IOException))]
  222. public void GetCreationTimeUtcException3 ()
  223. {
  224. string path = TempFolder + "/DirectoryTest.GetCreationTimeUtc.1";
  225. DeleteDirectory (path);
  226. try {
  227. Directory.GetCreationTimeUtc (path);
  228. } finally {
  229. DeleteDirectory (path);
  230. }
  231. }
  232. [Test]
  233. [ExpectedException(typeof(ArgumentException))]
  234. public void GetCreationTimeUtcException4 ()
  235. {
  236. Directory.GetCreationTimeUtc (" ");
  237. }
  238. [Test]
  239. [ExpectedException(typeof(ArgumentException))]
  240. public void GetCreationTimeUtcException5 ()
  241. {
  242. Directory.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  243. }
  244. [Test]
  245. [ExpectedException(typeof(ArgumentNullException))]
  246. public void GetLastAccessTimeException1 ()
  247. {
  248. Directory.GetLastAccessTime (null as string);
  249. }
  250. [Test]
  251. [ExpectedException(typeof(ArgumentException))]
  252. public void GetLastAccessTimeException2 ()
  253. {
  254. Directory.GetLastAccessTime ("");
  255. }
  256. [Test]
  257. [ExpectedException(typeof(IOException))]
  258. public void GetLastAccessTimeException3 ()
  259. {
  260. string path = TempFolder + "/DirectoryTest.GetLastAccessTime.1";
  261. DeleteDirectory (path);
  262. try {
  263. Directory.GetLastAccessTime (path);
  264. } finally {
  265. DeleteDirectory (path);
  266. }
  267. }
  268. [Test]
  269. [ExpectedException(typeof(ArgumentException))]
  270. public void GetLastAccessTimeException4 ()
  271. {
  272. Directory.GetLastAccessTime (" ");
  273. }
  274. [Test]
  275. [ExpectedException(typeof(ArgumentException))]
  276. public void GetLastAccessTimeException5 ()
  277. {
  278. Directory.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
  279. }
  280. [Test]
  281. [ExpectedException(typeof(ArgumentNullException))]
  282. public void GetLastAccessTimeUtcException1 ()
  283. {
  284. Directory.GetLastAccessTimeUtc (null as string);
  285. }
  286. [Test]
  287. [ExpectedException(typeof(ArgumentException))]
  288. public void GetLastAccessTimeUtcException2 ()
  289. {
  290. Directory.GetLastAccessTimeUtc ("");
  291. }
  292. [Test]
  293. [ExpectedException(typeof(IOException))]
  294. public void GetLastAccessTimeUtcException3 ()
  295. {
  296. string path = TempFolder + "/DirectoryTest.GetLastAccessTimeUtc.1";
  297. DeleteDirectory (path);
  298. try {
  299. Directory.GetLastAccessTimeUtc (path);
  300. } finally {
  301. DeleteDirectory (path);
  302. }
  303. }
  304. [Test]
  305. [ExpectedException(typeof(ArgumentException))]
  306. public void GetLastAccessTimeUtcException4 ()
  307. {
  308. Directory.GetLastAccessTimeUtc (" ");
  309. }
  310. [Test]
  311. [ExpectedException(typeof(ArgumentException))]
  312. public void GetLastAccessTimeUtcException5 ()
  313. {
  314. Directory.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  315. }
  316. [Test]
  317. [ExpectedException(typeof(ArgumentNullException))]
  318. public void GetLastWriteTimeException1 ()
  319. {
  320. Directory.GetLastWriteTime (null as string);
  321. }
  322. [Test]
  323. [ExpectedException(typeof(ArgumentException))]
  324. public void GetLastWriteTimeException2 ()
  325. {
  326. Directory.GetLastWriteTime ("");
  327. }
  328. [Test]
  329. [ExpectedException(typeof(IOException))]
  330. public void GetLastWriteTimeException3 ()
  331. {
  332. string path = TempFolder + "/DirectoryTest.GetLastWriteTime.1";
  333. DeleteDirectory (path);
  334. try {
  335. Directory.GetLastWriteTime (path);
  336. } finally {
  337. DeleteDirectory (path);
  338. }
  339. }
  340. [Test]
  341. [ExpectedException(typeof(ArgumentException))]
  342. public void GetLastWriteTimeException4 ()
  343. {
  344. Directory.GetLastWriteTime (" ");
  345. }
  346. [Test]
  347. [ExpectedException(typeof(ArgumentException))]
  348. public void GetLastWriteTimeException5 ()
  349. {
  350. Directory.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
  351. }
  352. [Test]
  353. [ExpectedException(typeof(ArgumentNullException))]
  354. public void GetLastWriteTimeUtcException1 ()
  355. {
  356. Directory.GetLastWriteTimeUtc (null as string);
  357. }
  358. [Test]
  359. [ExpectedException(typeof(ArgumentException))]
  360. public void GetLastWriteTimeUtcException2 ()
  361. {
  362. Directory.GetLastAccessTimeUtc ("");
  363. }
  364. [Test]
  365. [ExpectedException(typeof(IOException))]
  366. public void GetLastWriteTimeUtcException3 ()
  367. {
  368. string path = TempFolder + "/DirectoryTest.GetLastWriteTimeUtc.1";
  369. DeleteDirectory (path);
  370. try {
  371. Directory.GetLastAccessTimeUtc (path);
  372. } finally {
  373. DeleteDirectory (path);
  374. }
  375. }
  376. [Test]
  377. [ExpectedException(typeof(ArgumentException))]
  378. public void GetLastWriteTimeUtcException4 ()
  379. {
  380. Directory.GetLastAccessTimeUtc (" ");
  381. }
  382. [Test]
  383. [ExpectedException(typeof(ArgumentException))]
  384. public void GetLastWriteTimeUtcException5 ()
  385. {
  386. Directory.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  387. }
  388. [Test]
  389. public void Move ()
  390. {
  391. string path = TempFolder + "/DirectoryTest.Test.9";
  392. string path2 = TempFolder + "/DirectoryTest.Test.10";
  393. DeleteDirectory (path);
  394. DeleteDirectory (path2);
  395. try {
  396. Directory.CreateDirectory (path);
  397. Directory.CreateDirectory (path + "/" + "dir");
  398. AssertEquals ("test#01", true, Directory.Exists (path + "/" + "dir"));
  399. Directory.Move (path, path2);
  400. AssertEquals ("test#02", false, Directory.Exists (path + "/" + "dir"));
  401. AssertEquals ("test#03", true, Directory.Exists (path2 + "/" + "dir"));
  402. } finally {
  403. DeleteDirectory (path);
  404. DeleteDirectory (path2);
  405. if (Directory.Exists (path2 + "/" + "dir"))
  406. Directory.Delete (path2 + "/" + "dir", true);
  407. }
  408. }
  409. [Test]
  410. [ExpectedException(typeof(IOException))]
  411. public void MoveException1 ()
  412. {
  413. string path = TempFolder + "/DirectoryTest.Test.8";
  414. DeleteDirectory (path);
  415. try {
  416. Directory.Move (path, path);
  417. } finally {
  418. DeleteDirectory (path);
  419. }
  420. }
  421. [Test]
  422. [ExpectedException(typeof(ArgumentException))]
  423. public void MoveException2 ()
  424. {
  425. string path = TempFolder + "/DirectoryTest.Test.11";
  426. DeleteDirectory (path);
  427. try {
  428. Directory.Move ("", path);
  429. } finally {
  430. DeleteDirectory (path);
  431. }
  432. }
  433. [Test]
  434. [ExpectedException(typeof(ArgumentException))]
  435. public void MoveException3 ()
  436. {
  437. string path = TempFolder + "/DirectoryTest.Test.12";
  438. DeleteDirectory (path);
  439. try {
  440. Directory.Move (" ", path);
  441. } finally {
  442. DeleteDirectory (path);
  443. }
  444. }
  445. [Test]
  446. [ExpectedException(typeof(ArgumentException))]
  447. public void MoveException4 ()
  448. {
  449. string path = TempFolder + "/DirectoryTest.Test.13";
  450. path += Path.InvalidPathChars [0];
  451. string path2 = TempFolder + "/DirectoryTest.Test.13";
  452. DeleteDirectory (path);
  453. DeleteDirectory (path2);
  454. try {
  455. Directory.CreateDirectory (path2);
  456. Directory.Move (path2, path);
  457. } finally {
  458. DeleteDirectory (path);
  459. DeleteDirectory (path2);
  460. }
  461. }
  462. [Test]
  463. [ExpectedException(typeof(DirectoryNotFoundException))]
  464. public void MoveException5 ()
  465. {
  466. string path = TempFolder + "/DirectoryTest.Test.14";
  467. DeleteDirectory (path);
  468. try {
  469. Directory.Move (path, path + "Test.Test");
  470. } finally {
  471. DeleteDirectory (path);
  472. DeleteDirectory (path + "Test.Test");
  473. }
  474. }
  475. [Test]
  476. [ExpectedException(typeof(IOException))]
  477. public void MoveException6 ()
  478. {
  479. string path = TempFolder + "/DirectoryTest.Test.15";
  480. DeleteDirectory (path);
  481. try {
  482. Directory.CreateDirectory (path);
  483. Directory.Move (path, path + "/" + "dir");
  484. } finally {
  485. DeleteDirectory (path);
  486. DeleteDirectory (path + "/" + "dir");
  487. }
  488. }
  489. [Test]
  490. [ExpectedException(typeof(IOException))]
  491. public void MoveException7 ()
  492. {
  493. string path = TempFolder + "/DirectoryTest.Test.16";
  494. string path2 = TempFolder + "/DirectoryTest.Test.17";
  495. DeleteDirectory (path);
  496. DeleteDirectory (path2);
  497. try {
  498. Directory.CreateDirectory (path);
  499. Directory.CreateDirectory (path2);
  500. Directory.Move (path, path2);
  501. } finally {
  502. DeleteDirectory (path);
  503. DeleteDirectory (path2);
  504. }
  505. }
  506. [Test]
  507. [Ignore("Unix doesnt support CreationTime")]
  508. public void CreationTime ()
  509. {
  510. string path = TempFolder + "/DirectoryTest.CreationTime.1";
  511. DeleteDirectory (path);
  512. try {
  513. Directory.CreateDirectory (path);
  514. Directory.SetCreationTime (path, new DateTime (2003, 6, 4, 6, 4, 0));
  515. DateTime time = Directory.GetCreationTime (path);
  516. AssertEquals ("test#01", 2003, time.Year);
  517. AssertEquals ("test#02", 6, time.Month);
  518. AssertEquals ("test#03", 4, time.Day);
  519. AssertEquals ("test#04", 6, time.Hour);
  520. AssertEquals ("test#05", 4, time.Minute);
  521. AssertEquals ("test#06", 0, time.Second);
  522. time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetCreationTimeUtc (path));
  523. AssertEquals ("test#07", 2003, time.Year);
  524. AssertEquals ("test#08", 6, time.Month);
  525. AssertEquals ("test#09", 4, time.Day);
  526. AssertEquals ("test#10", 6, time.Hour);
  527. AssertEquals ("test#11", 4, time.Minute);
  528. AssertEquals ("test#12", 0, time.Second);
  529. Directory.SetCreationTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0));
  530. time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetCreationTime (path));
  531. AssertEquals ("test#13", 2003, time.Year);
  532. AssertEquals ("test#14", 6, time.Month);
  533. AssertEquals ("test#15", 4, time.Day);
  534. AssertEquals ("test#16", 6, time.Hour);
  535. AssertEquals ("test#17", 4, time.Minute);
  536. AssertEquals ("test#18", 0, time.Second);
  537. time = Directory.GetCreationTimeUtc (path);
  538. AssertEquals ("test#19", 2003, time.Year);
  539. AssertEquals ("test#20", 6, time.Month);
  540. AssertEquals ("test#21", 4, time.Day);
  541. AssertEquals ("test#22", 6, time.Hour);
  542. AssertEquals ("test#23", 4, time.Minute);
  543. AssertEquals ("test#24", 0, time.Second);
  544. } finally {
  545. DeleteDirectory (path);
  546. }
  547. }
  548. [Test]
  549. public void LastAccessTime ()
  550. {
  551. string path = TempFolder + "/DirectoryTest.AccessTime.1";
  552. DeleteDirectory (path);
  553. try {
  554. Directory.CreateDirectory (path);
  555. Directory.SetLastAccessTime (path, new DateTime (2003, 6, 4, 6, 4, 0));
  556. DateTime time = Directory.GetLastAccessTime (path);
  557. AssertEquals ("test#01", 2003, time.Year);
  558. AssertEquals ("test#02", 6, time.Month);
  559. AssertEquals ("test#03", 4, time.Day);
  560. AssertEquals ("test#04", 6, time.Hour);
  561. AssertEquals ("test#05", 4, time.Minute);
  562. AssertEquals ("test#06", 0, time.Second);
  563. time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetLastAccessTimeUtc (path));
  564. AssertEquals ("test#07", 2003, time.Year);
  565. AssertEquals ("test#08", 6, time.Month);
  566. AssertEquals ("test#09", 4, time.Day);
  567. AssertEquals ("test#10", 6, time.Hour);
  568. AssertEquals ("test#11", 4, time.Minute);
  569. AssertEquals ("test#12", 0, time.Second);
  570. Directory.SetLastAccessTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0));
  571. time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetLastAccessTime (path));
  572. AssertEquals ("test#13", 2003, time.Year);
  573. AssertEquals ("test#14", 6, time.Month);
  574. AssertEquals ("test#15", 4, time.Day);
  575. AssertEquals ("test#16", 6, time.Hour);
  576. AssertEquals ("test#17", 4, time.Minute);
  577. AssertEquals ("test#18", 0, time.Second);
  578. time = Directory.GetLastAccessTimeUtc (path);
  579. AssertEquals ("test#19", 2003, time.Year);
  580. AssertEquals ("test#20", 6, time.Month);
  581. AssertEquals ("test#21", 4, time.Day);
  582. AssertEquals ("test#22", 6, time.Hour);
  583. AssertEquals ("test#23", 4, time.Minute);
  584. AssertEquals ("test#24", 0, time.Second);
  585. } finally {
  586. DeleteDirectory (path);
  587. }
  588. }
  589. [Test]
  590. public void LastWriteTime ()
  591. {
  592. string path = TempFolder + "/DirectoryTest.WriteTime.1";
  593. DeleteDirectory (path);
  594. try {
  595. Directory.CreateDirectory (path);
  596. Directory.SetLastWriteTime (path, new DateTime (2003, 6, 4, 6, 4, 0));
  597. DateTime time = Directory.GetLastWriteTime (path);
  598. AssertEquals ("test#01", 2003, time.Year);
  599. AssertEquals ("test#02", 6, time.Month);
  600. AssertEquals ("test#03", 4, time.Day);
  601. AssertEquals ("test#04", 6, time.Hour);
  602. AssertEquals ("test#05", 4, time.Minute);
  603. AssertEquals ("test#06", 0, time.Second);
  604. time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetLastWriteTimeUtc (path));
  605. AssertEquals ("test#07", 2003, time.Year);
  606. AssertEquals ("test#08", 6, time.Month);
  607. AssertEquals ("test#09", 4, time.Day);
  608. AssertEquals ("test#10", 6, time.Hour);
  609. AssertEquals ("test#11", 4, time.Minute);
  610. AssertEquals ("test#12", 0, time.Second);
  611. Directory.SetLastWriteTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0));
  612. time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetLastWriteTime (path));
  613. AssertEquals ("test#13", 2003, time.Year);
  614. AssertEquals ("test#14", 6, time.Month);
  615. AssertEquals ("test#15", 4, time.Day);
  616. AssertEquals ("test#16", 6, time.Hour);
  617. AssertEquals ("test#17", 4, time.Minute);
  618. AssertEquals ("test#18", 0, time.Second);
  619. time = Directory.GetLastWriteTimeUtc (path);
  620. AssertEquals ("test#19", 2003, time.Year);
  621. AssertEquals ("test#20", 6, time.Month);
  622. AssertEquals ("test#21", 4, time.Day);
  623. AssertEquals ("test#22", 6, time.Hour);
  624. AssertEquals ("test#23", 4, time.Minute);
  625. AssertEquals ("test#24", 0, time.Second);
  626. } finally {
  627. DeleteDirectory (path);
  628. }
  629. }
  630. [Test]
  631. [ExpectedException(typeof(ArgumentNullException))]
  632. public void SetLastWriteTimeException1 ()
  633. {
  634. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  635. Directory.SetLastWriteTime (null as string, time);
  636. }
  637. [Test]
  638. [ExpectedException(typeof(ArgumentException))]
  639. public void SetLastWriteTimeException2 ()
  640. {
  641. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  642. Directory.SetLastWriteTime ("", time);
  643. }
  644. [Test]
  645. [ExpectedException(typeof(FileNotFoundException))]
  646. public void SetLastWriteTimeException3 ()
  647. {
  648. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  649. string path = TempFolder + "/DirectoryTest.SetLastWriteTime.2";
  650. DeleteDirectory (path);
  651. try {
  652. Directory.SetLastWriteTime (path, time);
  653. } finally {
  654. DeleteDirectory (path);
  655. }
  656. }
  657. [Test]
  658. [ExpectedException(typeof(ArgumentException))]
  659. public void SetLastWriteTimeException4 ()
  660. {
  661. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  662. Directory.SetLastWriteTime (" ", time);
  663. }
  664. [Test]
  665. [ExpectedException(typeof(ArgumentException))]
  666. public void SetLastWriteTimeException5 ()
  667. {
  668. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  669. Directory.SetLastWriteTime (Path.InvalidPathChars [0].ToString (), time);
  670. }
  671. // [Test]
  672. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  673. // public void SetLastWriteTimeException6 ()
  674. // {
  675. // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2);
  676. // string path = TempFolder + Path.DirectorySeparatorChar + "DirectoryTest.SetLastWriteTime.1";
  677. //
  678. // try {
  679. // if (!Directory.Exists (path))
  680. // Directory.CreateDirectory (path);
  681. //
  682. // Directory.SetLastWriteTime (path, time);
  683. // } finally {
  684. // DeleteDirectory (path);
  685. // }
  686. //
  687. // }
  688. [Test]
  689. [ExpectedException(typeof(ArgumentNullException))]
  690. public void SetLastWriteTimeUtcException1 ()
  691. {
  692. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  693. Directory.SetLastWriteTimeUtc (null as string, time);
  694. }
  695. [Test]
  696. [ExpectedException(typeof(ArgumentException))]
  697. public void SetLastWriteTimeUtcException2 ()
  698. {
  699. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  700. Directory.SetLastWriteTimeUtc ("", time);
  701. }
  702. [Test]
  703. [ExpectedException(typeof(FileNotFoundException))]
  704. public void SetLastWriteTimeUtcException3 ()
  705. {
  706. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  707. string path = TempFolder + "/DirectoryTest.SetLastWriteTimeUtc.2";
  708. DeleteDirectory (path);
  709. try {
  710. Directory.SetLastWriteTimeUtc (path, time);
  711. } finally {
  712. DeleteDirectory (path);
  713. }
  714. }
  715. [Test]
  716. [ExpectedException(typeof(ArgumentException))]
  717. public void SetLastWriteTimeUtcException4 ()
  718. {
  719. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  720. Directory.SetLastWriteTimeUtc (" ", time);
  721. }
  722. [Test]
  723. [ExpectedException(typeof(ArgumentException))]
  724. public void SetLastWriteTimeUtcException5 ()
  725. {
  726. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  727. Directory.SetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString (), time);
  728. }
  729. // [Test]
  730. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  731. // public void SetLastWriteTimeUtcException6 ()
  732. // {
  733. // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2);
  734. // string path = TempFolder + "/DirectoryTest.SetLastWriteTimeUtc.1";
  735. //
  736. // if (!Directory.Exists (path))
  737. // Directory.CreateDirectory (path);
  738. // try {
  739. // Directory.SetLastWriteTimeUtc (path, time);
  740. // } finally {
  741. // DeleteDirectory (path);
  742. // }
  743. // }
  744. [Test]
  745. [ExpectedException(typeof(ArgumentNullException))]
  746. public void SetLastAccessTimeException1 ()
  747. {
  748. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  749. Directory.SetLastAccessTime (null as string, time);
  750. }
  751. [Test]
  752. [ExpectedException(typeof(ArgumentException))]
  753. public void SetLastAccessTimeException2 ()
  754. {
  755. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  756. Directory.SetLastAccessTime ("", time);
  757. }
  758. [Test]
  759. [ExpectedException(typeof(FileNotFoundException))]
  760. public void SetLastAccessTimeException3 ()
  761. {
  762. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  763. string path = TempFolder + "/DirectoryTest.SetLastAccessTime.2";
  764. DeleteDirectory (path);
  765. try {
  766. Directory.SetLastAccessTime (path, time);
  767. } finally {
  768. DeleteDirectory (path);
  769. }
  770. }
  771. [Test]
  772. [ExpectedException(typeof(ArgumentException))]
  773. public void SetLastAccessTimeException4 ()
  774. {
  775. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  776. Directory.SetLastAccessTime (" ", time);
  777. }
  778. [Test]
  779. [ExpectedException(typeof(ArgumentException))]
  780. public void SetLastAccessTimeException5 ()
  781. {
  782. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  783. Directory.SetLastAccessTime (Path.InvalidPathChars [0].ToString (), time);
  784. }
  785. // [Test]
  786. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  787. // public void SetLastAccessTimeException6 ()
  788. // {
  789. // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2);
  790. // string path = TempFolder + "/DirectoryTest.SetLastAccessTime.1";
  791. //
  792. // if (!Directory.Exists (path))
  793. // Directory.CreateDirectory (path);
  794. // try {
  795. // Directory.SetLastAccessTime (path, time);
  796. // } finally {
  797. // DeleteDirectory (path);
  798. // }
  799. //
  800. // }
  801. [Test]
  802. [ExpectedException(typeof(ArgumentNullException))]
  803. public void SetLastAccessTimeUtcException1 ()
  804. {
  805. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  806. Directory.SetLastAccessTimeUtc (null as string, time);
  807. }
  808. [Test]
  809. [ExpectedException(typeof(ArgumentException))]
  810. public void SetLastAccessTimeUtcException2 ()
  811. {
  812. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  813. Directory.SetLastAccessTimeUtc ("", time);
  814. }
  815. [Test]
  816. [ExpectedException(typeof(FileNotFoundException))]
  817. public void SetLastAccessTimeUtcException3 ()
  818. {
  819. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  820. string path = TempFolder + "/DirectoryTest.SetLastAccessTimeUtc.2";
  821. DeleteDirectory (path);
  822. try {
  823. Directory.SetLastAccessTimeUtc (path, time);
  824. } finally {
  825. DeleteDirectory (path);
  826. }
  827. }
  828. [Test]
  829. [ExpectedException(typeof(ArgumentException))]
  830. public void SetLastAccessTimeUtcException4 ()
  831. {
  832. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  833. Directory.SetLastAccessTimeUtc (" ", time);
  834. }
  835. [Test]
  836. [ExpectedException(typeof(ArgumentException))]
  837. public void SetLastAccessTimeUtcException5 ()
  838. {
  839. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  840. Directory.SetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString (), time);
  841. }
  842. // [Test]
  843. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  844. // public void SetLastAccessTimeUtcException6 ()
  845. // {
  846. // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2);
  847. // string path = TempFolder + "/DirectoryTest.SetLastAccessTimeUtc.1";
  848. //
  849. // if (!Directory.Exists (path))
  850. // Directory.CreateDirectory (path);
  851. // try {
  852. // Directory.SetLastAccessTimeUtc (path, time);
  853. // } finally {
  854. // DeleteDirectory (path);
  855. // }
  856. // }
  857. [ExpectedException(typeof(ArgumentNullException))]
  858. public void SetCreationTimeException1 ()
  859. {
  860. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  861. Directory.SetCreationTime (null as string, time);
  862. }
  863. [Test]
  864. [ExpectedException(typeof(ArgumentException))]
  865. public void SetCreationTimeException2 ()
  866. {
  867. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  868. Directory.SetCreationTime ("", time);
  869. }
  870. [Test]
  871. [ExpectedException(typeof(FileNotFoundException))]
  872. public void SetCreationTimeException3 ()
  873. {
  874. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  875. string path = TempFolder + "/DirectoryTest.SetCreationTime.2";
  876. DeleteDirectory (path);
  877. try {
  878. Directory.SetCreationTime (path, time);
  879. } finally {
  880. DeleteDirectory (path);
  881. }
  882. }
  883. [Test]
  884. [ExpectedException(typeof(ArgumentException))]
  885. public void SetCreationTimeException4 ()
  886. {
  887. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  888. Directory.SetCreationTime (" ", time);
  889. }
  890. [Test]
  891. [ExpectedException(typeof(ArgumentException))]
  892. public void SetCreationTimeException5 ()
  893. {
  894. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  895. Directory.SetCreationTime (Path.InvalidPathChars [0].ToString (), time);
  896. }
  897. // [Test]
  898. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  899. // public void SetCreationTimeException6 ()
  900. // {
  901. // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2);
  902. // string path = TempFolder + "/DirectoryTest.SetCreationTime.1";
  903. //
  904. // if (!Directory.Exists (path))
  905. // Directory.CreateDirectory (path);
  906. // try {
  907. // Directory.SetCreationTime (path, time);
  908. // DeleteDirectory (path);
  909. // } finally {
  910. // DeleteDirectory (path);
  911. // }
  912. //
  913. // }
  914. [Test]
  915. [ExpectedException(typeof(ArgumentNullException))]
  916. public void SetCreationTimeUtcException1 ()
  917. {
  918. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  919. Directory.SetCreationTimeUtc (null as string, time);
  920. }
  921. [Test]
  922. [ExpectedException(typeof(ArgumentException))]
  923. public void SetCreationTimeUtcException2 ()
  924. {
  925. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  926. Directory.SetCreationTimeUtc ("", time);
  927. }
  928. [Test]
  929. [ExpectedException(typeof(FileNotFoundException))]
  930. public void SetCreationTimeUtcException3 ()
  931. {
  932. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  933. string path = TempFolder + "/DirectoryTest.SetLastAccessTimeUtc.2";
  934. DeleteDirectory (path);
  935. try {
  936. Directory.SetCreationTimeUtc (path, time);
  937. DeleteDirectory (path);
  938. } finally {
  939. DeleteDirectory (path);
  940. }
  941. }
  942. [Test]
  943. [ExpectedException(typeof(ArgumentException))]
  944. public void SetCreationTimeUtcException4 ()
  945. {
  946. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  947. Directory.SetCreationTimeUtc (" ", time);
  948. }
  949. [Test]
  950. [ExpectedException(typeof(ArgumentException))]
  951. public void SetCreationTimeUtcException5 ()
  952. {
  953. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  954. Directory.SetCreationTimeUtc (Path.InvalidPathChars [0].ToString (), time);
  955. }
  956. // [Test]
  957. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  958. // public void SetCreationTimeUtcException6 ()
  959. // {
  960. // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2);
  961. // string path = TempFolder + "/DirectoryTest.SetLastAccessTimeUtc.1";
  962. //
  963. // if (!Directory.Exists (path))
  964. // Directory.CreateDirectory (path);
  965. // try {
  966. // Directory.SetCreationTimeUtc (path, time);
  967. // DeleteDirectory (path);
  968. // } finally {
  969. // DeleteDirectory (path);
  970. // }
  971. // }
  972. [Test]
  973. public void GetDirectories ()
  974. {
  975. string path = TempFolder;
  976. string DirPath = TempFolder + Path.DirectorySeparatorChar + ".GetDirectories";
  977. DeleteDirectory (DirPath);
  978. try {
  979. Directory.CreateDirectory (DirPath);
  980. string [] dirs = Directory.GetDirectories (path);
  981. foreach (string directory in dirs) {
  982. if (directory == DirPath)
  983. return;
  984. }
  985. Assert ("Directory Not Found", false);
  986. } finally {
  987. DeleteDirectory (DirPath);
  988. }
  989. }
  990. [Test]
  991. public void GetFiles ()
  992. {
  993. string path = TempFolder;
  994. string DirPath = TempFolder + Path.DirectorySeparatorChar + ".GetFiles";
  995. if (File.Exists (DirPath))
  996. File.Delete (DirPath);
  997. try {
  998. File.Create (DirPath).Close ();
  999. string [] files = Directory.GetFiles (TempFolder);
  1000. foreach (string directory in files) {
  1001. if (directory == DirPath)
  1002. return;
  1003. }
  1004. Assert ("File Not Found", false);
  1005. } finally {
  1006. if (File.Exists (DirPath))
  1007. File.Delete (DirPath);
  1008. }
  1009. }
  1010. [Test]
  1011. [ExpectedException (typeof (ArgumentNullException))]
  1012. public void SetCurrentDirectoryNull ()
  1013. {
  1014. Directory.SetCurrentDirectory (null);
  1015. }
  1016. [Test]
  1017. [ExpectedException (typeof (ArgumentException))]
  1018. public void SetCurrentDirectoryEmpty ()
  1019. {
  1020. Directory.SetCurrentDirectory (String.Empty);
  1021. }
  1022. [Test]
  1023. [ExpectedException (typeof (ArgumentException))]
  1024. public void SetCurrentDirectoryWhitespace ()
  1025. {
  1026. Directory.SetCurrentDirectory (" ");
  1027. }
  1028. [Test]
  1029. public void GetNoFiles () // Bug 58875. This throwed an exception on windows.
  1030. {
  1031. DirectoryInfo dir = new DirectoryInfo (".");
  1032. dir.GetFiles ("*.nonext");
  1033. }
  1034. private void DeleteDirectory (string path)
  1035. {
  1036. if (Directory.Exists (path))
  1037. Directory.Delete (path, true);
  1038. }
  1039. }
  1040. }