FileTest.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. //
  2. // FileTest.cs: Test cases for System.IO.File
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. // Ville Palo ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. http://www.ximian.com
  9. //
  10. // TODO: Find out why ArgumentOutOfRangeExceptions does not manage to close streams properly
  11. //
  12. using NUnit.Framework;
  13. using System;
  14. using System.IO;
  15. using System.Globalization;
  16. using System.Threading;
  17. namespace MonoTests.System.IO
  18. {
  19. [TestFixture]
  20. public class FileTest : Assertion
  21. {
  22. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  23. [SetUp]
  24. public void SetUp ()
  25. {
  26. if (Directory.Exists (TempFolder))
  27. Directory.Delete (TempFolder, true);
  28. Directory.CreateDirectory (TempFolder);
  29. Thread.CurrentThread.CurrentCulture = new CultureInfo ("EN-us");
  30. }
  31. [TearDown]
  32. public void TearDown ()
  33. {
  34. if (Directory.Exists (TempFolder))
  35. Directory.Delete (TempFolder, true);
  36. }
  37. [Test]
  38. public void TestExists ()
  39. {
  40. int i = 0;
  41. FileStream s = null;
  42. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  43. try {
  44. Assert ("null filename should not exist", !File.Exists (null));
  45. i++;
  46. Assert ("empty filename should not exist", !File.Exists (""));
  47. i++;
  48. Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
  49. i++;
  50. DeleteFile (path);
  51. s = File.Create (path);
  52. s.Close ();
  53. Assert ("File " + path + " should exists", File.Exists (path));
  54. i++;
  55. Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"));
  56. } catch (Exception e) {
  57. Fail ("Unexpected exception at i = " + i + ". e=" + e);
  58. } finally {
  59. if (s != null)
  60. s.Close ();
  61. DeleteFile (path);
  62. }
  63. }
  64. [Test]
  65. public void Exists_InvalidFileName ()
  66. {
  67. Assert ("><|", !File.Exists ("><|"));
  68. }
  69. [Test]
  70. public void Exists_InvalidDirectory ()
  71. {
  72. Assert ("InvalidDirectory", !File.Exists (Path.Combine ("does not exist", "file.txt")));
  73. }
  74. [Test]
  75. [ExpectedException(typeof (ArgumentNullException))]
  76. public void CtorArgumentNullException1 ()
  77. {
  78. FileStream stream = File.Create (null);
  79. }
  80. [Test]
  81. [ExpectedException(typeof (ArgumentException))]
  82. public void CtorArgumentException1 ()
  83. {
  84. FileStream stream = File.Create ("");
  85. }
  86. [Test]
  87. [ExpectedException(typeof (ArgumentException))]
  88. public void CtorArgumentException2 ()
  89. {
  90. FileStream stream = File.Create (" ");
  91. }
  92. [Test]
  93. [ExpectedException(typeof (DirectoryNotFoundException))]
  94. public void CtorDirectoryNotFoundException ()
  95. {
  96. FileStream stream = null;
  97. string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
  98. try {
  99. stream = File.Create (path);
  100. } finally {
  101. if (stream != null)
  102. stream.Close ();
  103. DeleteFile (path);
  104. }
  105. }
  106. [Test]
  107. public void TestCreate ()
  108. {
  109. FileStream stream = null;
  110. string path = "";
  111. /* positive test: create resources/foo */
  112. try {
  113. path = TempFolder + Path.DirectorySeparatorChar + "foo";
  114. stream = File.Create (path);
  115. Assert ("File should exist", File.Exists (path));
  116. stream.Close ();
  117. } catch (Exception e) {
  118. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  119. } finally {
  120. if (stream != null)
  121. stream.Close ();
  122. DeleteFile (path);
  123. }
  124. path = "";
  125. stream = null;
  126. /* positive test: repeat test above again to test for overwriting file */
  127. try {
  128. path = TempFolder + Path.DirectorySeparatorChar + "foo";
  129. stream = File.Create (path);
  130. Assert ("File should exist", File.Exists (path));
  131. stream.Close ();
  132. } catch (Exception e) {
  133. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  134. } finally {
  135. if (stream != null)
  136. stream.Close ();
  137. DeleteFile (path);
  138. }
  139. }
  140. [Test]
  141. [ExpectedException(typeof(ArgumentNullException))]
  142. public void CopyArgumentNullException1 ()
  143. {
  144. File.Copy (null, "b");
  145. }
  146. [Test]
  147. [ExpectedException(typeof(ArgumentNullException))]
  148. public void CopyArgumentNullException2 ()
  149. {
  150. File.Copy ("a", null);
  151. }
  152. [Test]
  153. [ExpectedException(typeof(ArgumentException))]
  154. public void CopyArgumentException1 ()
  155. {
  156. File.Copy ("", "b");
  157. }
  158. [Test]
  159. [ExpectedException(typeof(ArgumentException))]
  160. public void CopyArgumentException2 ()
  161. {
  162. File.Copy ("a", "");
  163. }
  164. [Test]
  165. [ExpectedException(typeof(ArgumentException))]
  166. public void CopyArgumentException3 ()
  167. {
  168. File.Copy (" ", "b");
  169. }
  170. [Test]
  171. [ExpectedException(typeof(ArgumentException))]
  172. public void CopyArgumentException4 ()
  173. {
  174. File.Copy ("a", " ");
  175. }
  176. [Test]
  177. [ExpectedException(typeof(FileNotFoundException))]
  178. public void CopyFileNotFoundException ()
  179. {
  180. File.Copy ("doesnotexist", "b");
  181. }
  182. [ExpectedException(typeof(IOException))]
  183. public void CopyIOException ()
  184. {
  185. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
  186. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  187. try {
  188. File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
  189. File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
  190. File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
  191. } finally {
  192. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
  193. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  194. }
  195. }
  196. [Test]
  197. public void TestCopy ()
  198. {
  199. string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
  200. string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  201. /* positive test: copy resources/AFile.txt to resources/bar */
  202. try {
  203. try {
  204. DeleteFile (path1);
  205. DeleteFile (path2);
  206. File.Create (path2).Close ();
  207. File.Copy (path2, path1);
  208. Assert ("File AFile.txt should still exist", File.Exists (path2));
  209. Assert ("File bar should exist after File.Copy", File.Exists (path1));
  210. } catch (Exception e) {
  211. Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
  212. }
  213. /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
  214. try {
  215. Assert ("File bar should exist before File.Copy", File.Exists (path1));
  216. File.Copy (path2, path1, true);
  217. Assert ("File AFile.txt should still exist", File.Exists (path2));
  218. Assert ("File bar should exist after File.Copy", File.Exists (path1));
  219. } catch (Exception e) {
  220. Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
  221. }
  222. }finally {
  223. DeleteFile (path1);
  224. DeleteFile (path2);
  225. }
  226. }
  227. [Test]
  228. [ExpectedException (typeof (ArgumentNullException))]
  229. public void DeleteArgumentNullException ()
  230. {
  231. File.Delete (null);
  232. }
  233. [Test]
  234. [ExpectedException (typeof (ArgumentException))]
  235. public void DeleteArgumentException1 ()
  236. {
  237. File.Delete ("");
  238. }
  239. [Test]
  240. [ExpectedException (typeof (ArgumentException))]
  241. public void DeleteArgumentException2 ()
  242. {
  243. File.Delete (" ");
  244. }
  245. [Test]
  246. [ExpectedException (typeof (DirectoryNotFoundException))]
  247. public void DeleteDirectoryNotFoundException ()
  248. {
  249. string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
  250. if (Directory.Exists (path))
  251. Directory.Delete (path, true);
  252. File.Delete (path);
  253. }
  254. [Test]
  255. public void TestDelete ()
  256. {
  257. string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
  258. DeleteFile (foopath);
  259. try {
  260. File.Create (foopath).Close ();
  261. try {
  262. File.Delete (foopath);
  263. } catch (Exception e) {
  264. Fail ("Unable to delete " + foopath + " e=" + e.ToString());
  265. }
  266. Assert ("File " + foopath + " should not exist after File.Delete", !File.Exists (foopath));
  267. } finally {
  268. DeleteFile (foopath);
  269. }
  270. }
  271. [Test]
  272. [ExpectedException(typeof (IOException))]
  273. [Category("NotWorking")]
  274. public void DeleteOpenStreamException ()
  275. {
  276. string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
  277. DeleteFile (path);
  278. FileStream stream = null;
  279. try {
  280. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  281. File.Delete (path);
  282. } finally {
  283. if (stream != null)
  284. stream.Close ();
  285. DeleteFile (path);
  286. }
  287. }
  288. [Test]
  289. [ExpectedException(typeof (ArgumentNullException))]
  290. public void MoveException1 ()
  291. {
  292. File.Move (null, "b");
  293. }
  294. [Test]
  295. [ExpectedException(typeof (ArgumentNullException))]
  296. public void MoveException2 ()
  297. {
  298. File.Move ("a", null);
  299. }
  300. [Test]
  301. [ExpectedException(typeof (ArgumentException))]
  302. public void MoveException3 ()
  303. {
  304. File.Move ("", "b");
  305. }
  306. [Test]
  307. [ExpectedException(typeof (ArgumentException))]
  308. public void MoveException4 ()
  309. {
  310. File.Move ("a", "");
  311. }
  312. [Test]
  313. [ExpectedException(typeof (ArgumentException))]
  314. public void MoveException5 ()
  315. {
  316. File.Move (" ", "b");
  317. }
  318. [Test]
  319. [ExpectedException(typeof (ArgumentException))]
  320. public void MoveException6 ()
  321. {
  322. File.Move ("a", " ");
  323. }
  324. [Test]
  325. [ExpectedException(typeof (FileNotFoundException))]
  326. public void MoveException7 ()
  327. {
  328. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist");
  329. File.Move (TempFolder + Path.DirectorySeparatorChar + "doesnotexist", "b");
  330. }
  331. [Test]
  332. [ExpectedException(typeof (DirectoryNotFoundException))]
  333. public void MoveException8 ()
  334. {
  335. string path = TempFolder + Path.DirectorySeparatorChar + "foo";
  336. DeleteFile (path);
  337. try {
  338. File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
  339. File.Copy(TempFolder + Path.DirectorySeparatorChar + "AFile.txt", path, true);
  340. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
  341. File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
  342. } finally {
  343. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  344. DeleteFile (path);
  345. }
  346. }
  347. [Test]
  348. [ExpectedException(typeof (IOException))]
  349. public void MoveException9 ()
  350. {
  351. File.Create (TempFolder + Path.DirectorySeparatorChar + "foo").Close ();
  352. try {
  353. File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder);
  354. } finally {
  355. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "foo");
  356. }
  357. }
  358. [Test]
  359. public void TestMove ()
  360. {
  361. string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
  362. string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
  363. if (!File.Exists (bar)) {
  364. FileStream f = File.Create(bar);
  365. f.Close();
  366. }
  367. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should exist", File.Exists (bar));
  368. File.Move (bar, baz);
  369. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists (bar));
  370. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "baz should exist", File.Exists (baz));
  371. // Test moving of directories
  372. string dir = Path.Combine (TempFolder, "dir");
  373. string dir2 = Path.Combine (TempFolder, "dir2");
  374. string dir_foo = Path.Combine (dir, "foo");
  375. string dir2_foo = Path.Combine (dir2, "foo");
  376. if (Directory.Exists (dir))
  377. Directory.Delete (dir, true);
  378. Directory.CreateDirectory (dir);
  379. Directory.CreateDirectory (dir2);
  380. File.Create (dir_foo).Close ();
  381. File.Move (dir_foo, dir2_foo);
  382. Assert (File.Exists (dir2_foo));
  383. Directory.Delete (dir, true);
  384. Directory.Delete (dir2, true);
  385. DeleteFile (dir_foo);
  386. DeleteFile (dir2_foo);
  387. }
  388. [Test]
  389. public void TestOpen ()
  390. {
  391. string path = "";
  392. FileStream stream = null;
  393. try {
  394. path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  395. if (!File.Exists (path))
  396. stream = File.Create (path);
  397. stream.Close ();
  398. stream = File.Open (path, FileMode.Open);
  399. stream.Close ();
  400. } catch (Exception e) {
  401. Fail ("Unable to open " + TempFolder + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
  402. } finally {
  403. if (stream != null)
  404. stream.Close ();
  405. DeleteFile (path);
  406. }
  407. path = "";
  408. stream = null;
  409. /* Exception tests */
  410. try {
  411. path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
  412. stream = File.Open (path, FileMode.Open);
  413. Fail ("File 'filedoesnotexist' should not exist");
  414. } catch (FileNotFoundException) {
  415. // do nothing, this is what we expect
  416. } catch (Exception e) {
  417. Fail ("Unexpect exception caught: e=" + e.ToString());
  418. } finally {
  419. if (stream != null)
  420. stream.Close ();
  421. DeleteFile (path);
  422. }
  423. }
  424. [Test]
  425. public void Open ()
  426. {
  427. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  428. if (!File.Exists (path))
  429. File.Create (path).Close ();
  430. FileStream stream = null;
  431. try {
  432. stream = File.Open (path, FileMode.Open);
  433. Assertion.AssertEquals ("test#01", true, stream.CanRead);
  434. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  435. Assertion.AssertEquals ("test#03", true, stream.CanWrite);
  436. stream.Close ();
  437. stream = File.Open (path, FileMode.Open, FileAccess.Write);
  438. Assertion.AssertEquals ("test#04", false, stream.CanRead);
  439. Assertion.AssertEquals ("test#05", true, stream.CanSeek);
  440. Assertion.AssertEquals ("test#06", true, stream.CanWrite);
  441. stream.Close ();
  442. stream = File.Open (path, FileMode.Open, FileAccess.Read);
  443. Assertion.AssertEquals ("test#04", true, stream.CanRead);
  444. Assertion.AssertEquals ("test#05", true, stream.CanSeek);
  445. Assertion.AssertEquals ("test#06", false, stream.CanWrite);
  446. stream.Close ();
  447. } finally {
  448. if (stream != null)
  449. stream.Close ();
  450. DeleteFile (path);
  451. }
  452. }
  453. [Test]
  454. [ExpectedException(typeof(ArgumentException))]
  455. public void OpenException1 ()
  456. {
  457. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  458. FileStream stream = null;
  459. // CreateNew + Read throws an exceptoin
  460. try {
  461. stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
  462. } finally {
  463. if (stream != null)
  464. stream.Close ();
  465. DeleteFile (path);
  466. }
  467. }
  468. [Test]
  469. [ExpectedException(typeof(ArgumentException))]
  470. public void OpenException2 ()
  471. {
  472. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  473. FileStream s = null;
  474. // Append + Read throws an exceptoin
  475. if (!File.Exists (path))
  476. File.Create (path).Close ();
  477. try {
  478. s = File.Open (path, FileMode.Append, FileAccess.Read);
  479. } finally {
  480. if (s != null)
  481. s.Close ();
  482. DeleteFile (path);
  483. }
  484. }
  485. [Test]
  486. public void OpenRead ()
  487. {
  488. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  489. if (!File.Exists (path))
  490. File.Create (path).Close ();
  491. FileStream stream = null;
  492. try {
  493. stream = File.OpenRead (path);
  494. Assertion.AssertEquals ("test#01", true, stream.CanRead);
  495. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  496. Assertion.AssertEquals ("test#03", false, stream.CanWrite);
  497. } finally {
  498. if (stream != null)
  499. stream.Close ();
  500. DeleteFile (path);
  501. }
  502. }
  503. [Test]
  504. public void OpenWrite ()
  505. {
  506. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  507. if (!File.Exists (path))
  508. File.Create (path).Close ();
  509. FileStream stream = null;
  510. try {
  511. stream = File.OpenWrite (path);
  512. Assertion.AssertEquals ("test#01", false, stream.CanRead);
  513. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  514. Assertion.AssertEquals ("test#03", true, stream.CanWrite);
  515. stream.Close ();
  516. } finally {
  517. if (stream != null)
  518. stream.Close ();
  519. DeleteFile (path);
  520. }
  521. }
  522. [Test]
  523. public void TestGetCreationTime ()
  524. {
  525. string path = TempFolder + Path.DirectorySeparatorChar + "baz";
  526. DeleteFile (path);
  527. try {
  528. File.Create (path).Close();
  529. DateTime time = File.GetCreationTime (path);
  530. Assert ("GetCreationTime incorrect", (DateTime.Now - time).TotalSeconds < 10);
  531. } finally {
  532. DeleteFile (path);
  533. }
  534. }
  535. [Test]
  536. [ExpectedException(typeof(IOException))]
  537. public void TestGetCreationTimeException ()
  538. {
  539. // Test nonexistent files
  540. string path2 = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
  541. DeleteFile (path2);
  542. // should throw an exception
  543. File.GetCreationTime (path2);
  544. }
  545. // Setting the creation time on Unix is not possible
  546. [Test][Category("NotWorking")]
  547. public void CreationTime ()
  548. {
  549. string path = TempFolder + Path.DirectorySeparatorChar + "creationTime";
  550. if (File.Exists (path))
  551. File.Delete (path);
  552. FileStream stream = null;
  553. try {
  554. stream = File.Create (path);
  555. stream.Close ();
  556. File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  557. DateTime time = File.GetCreationTime (path);
  558. Assertion.AssertEquals ("test#01", 2002, time.Year);
  559. Assertion.AssertEquals ("test#02", 4, time.Month);
  560. Assertion.AssertEquals ("test#03", 6, time.Day);
  561. Assertion.AssertEquals ("test#04", 4, time.Hour);
  562. Assertion.AssertEquals ("test#05", 4, time.Second);
  563. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
  564. Assertion.AssertEquals ("test#06", 2002, time.Year);
  565. Assertion.AssertEquals ("test#07", 4, time.Month);
  566. Assertion.AssertEquals ("test#08", 6, time.Day);
  567. Assertion.AssertEquals ("test#09", 4, time.Hour);
  568. Assertion.AssertEquals ("test#10", 4, time.Second);
  569. File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  570. time = File.GetCreationTimeUtc (path);
  571. Assertion.AssertEquals ("test#11", 2002, time.Year);
  572. Assertion.AssertEquals ("test#12", 4, time.Month);
  573. Assertion.AssertEquals ("test#13", 6, time.Day);
  574. Assertion.AssertEquals ("test#14", 4, time.Hour);
  575. Assertion.AssertEquals ("test#15", 4, time.Second);
  576. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
  577. Assertion.AssertEquals ("test#16", 2002, time.Year);
  578. Assertion.AssertEquals ("test#17", 4, time.Month);
  579. Assertion.AssertEquals ("test#18", 6, time.Day);
  580. Assertion.AssertEquals ("test#19", 4, time.Hour);
  581. Assertion.AssertEquals ("test#20", 4, time.Second);
  582. } finally {
  583. if (stream != null)
  584. stream.Close ();
  585. DeleteFile (path);
  586. }
  587. }
  588. [Test]
  589. public void LastAccessTime ()
  590. {
  591. string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
  592. if (File.Exists (path))
  593. File.Delete (path);
  594. FileStream stream = null;
  595. try {
  596. stream = File.Create (path);
  597. stream.Close ();
  598. File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  599. DateTime time = File.GetLastAccessTime (path);
  600. Assertion.AssertEquals ("test#01", 2002, time.Year);
  601. Assertion.AssertEquals ("test#02", 4, time.Month);
  602. Assertion.AssertEquals ("test#03", 6, time.Day);
  603. Assertion.AssertEquals ("test#04", 4, time.Hour);
  604. Assertion.AssertEquals ("test#05", 4, time.Second);
  605. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
  606. Assertion.AssertEquals ("test#06", 2002, time.Year);
  607. Assertion.AssertEquals ("test#07", 4, time.Month);
  608. Assertion.AssertEquals ("test#08", 6, time.Day);
  609. Assertion.AssertEquals ("test#09", 4, time.Hour);
  610. Assertion.AssertEquals ("test#10", 4, time.Second);
  611. File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  612. time = File.GetLastAccessTimeUtc (path);
  613. Assertion.AssertEquals ("test#11", 2002, time.Year);
  614. Assertion.AssertEquals ("test#12", 4, time.Month);
  615. Assertion.AssertEquals ("test#13", 6, time.Day);
  616. Assertion.AssertEquals ("test#14", 4, time.Hour);
  617. Assertion.AssertEquals ("test#15", 4, time.Second);
  618. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
  619. Assertion.AssertEquals ("test#16", 2002, time.Year);
  620. Assertion.AssertEquals ("test#17", 4, time.Month);
  621. Assertion.AssertEquals ("test#18", 6, time.Day);
  622. Assertion.AssertEquals ("test#19", 4, time.Hour);
  623. Assertion.AssertEquals ("test#20", 4, time.Second);
  624. } finally {
  625. if (stream != null)
  626. stream.Close ();
  627. DeleteFile (path);
  628. }
  629. }
  630. [Test]
  631. public void LastWriteTime ()
  632. {
  633. string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
  634. if (File.Exists (path))
  635. File.Delete (path);
  636. FileStream stream = null;
  637. try {
  638. stream = File.Create (path);
  639. stream.Close ();
  640. File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  641. DateTime time = File.GetLastWriteTime (path);
  642. Assertion.AssertEquals ("test#01", 2002, time.Year);
  643. Assertion.AssertEquals ("test#02", 4, time.Month);
  644. Assertion.AssertEquals ("test#03", 6, time.Day);
  645. Assertion.AssertEquals ("test#04", 4, time.Hour);
  646. Assertion.AssertEquals ("test#05", 4, time.Second);
  647. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
  648. Assertion.AssertEquals ("test#06", 2002, time.Year);
  649. Assertion.AssertEquals ("test#07", 4, time.Month);
  650. Assertion.AssertEquals ("test#08", 6, time.Day);
  651. Assertion.AssertEquals ("test#09", 4, time.Hour);
  652. Assertion.AssertEquals ("test#10", 4, time.Second);
  653. File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  654. time = File.GetLastWriteTimeUtc (path);
  655. Assertion.AssertEquals ("test#11", 2002, time.Year);
  656. Assertion.AssertEquals ("test#12", 4, time.Month);
  657. Assertion.AssertEquals ("test#13", 6, time.Day);
  658. Assertion.AssertEquals ("test#14", 4, time.Hour);
  659. Assertion.AssertEquals ("test#15", 4, time.Second);
  660. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
  661. Assertion.AssertEquals ("test#16", 2002, time.Year);
  662. Assertion.AssertEquals ("test#17", 4, time.Month);
  663. Assertion.AssertEquals ("test#18", 6, time.Day);
  664. Assertion.AssertEquals ("test#19", 4, time.Hour);
  665. Assertion.AssertEquals ("test#20", 4, time.Second);
  666. } finally {
  667. if (stream != null)
  668. stream.Close ();
  669. DeleteFile (path);
  670. }
  671. }
  672. [Test]
  673. [ExpectedException(typeof(ArgumentNullException))]
  674. public void GetCreationTimeException1 ()
  675. {
  676. File.GetCreationTime (null as string);
  677. }
  678. [Test]
  679. [ExpectedException(typeof(ArgumentException))]
  680. public void GetCreationTimeException2 ()
  681. {
  682. File.GetCreationTime ("");
  683. }
  684. [Test]
  685. [ExpectedException(typeof(IOException))]
  686. public void GetCreationTimeException3 ()
  687. {
  688. string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
  689. DeleteFile (path);
  690. File.GetCreationTime (path);
  691. }
  692. [Test]
  693. [ExpectedException(typeof(ArgumentException))]
  694. public void GetCreationTimeException4 ()
  695. {
  696. File.GetCreationTime (" ");
  697. }
  698. [Test]
  699. [ExpectedException(typeof(ArgumentException))]
  700. public void GetCreationTimeException5 ()
  701. {
  702. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  703. }
  704. [Test]
  705. [ExpectedException(typeof(ArgumentNullException))]
  706. public void GetCreationTimeUtcException1 ()
  707. {
  708. File.GetCreationTimeUtc (null as string);
  709. }
  710. [Test]
  711. [ExpectedException(typeof(ArgumentException))]
  712. public void GetCreationTimeUtcException2 ()
  713. {
  714. File.GetCreationTimeUtc ("");
  715. }
  716. [Test]
  717. [ExpectedException(typeof(IOException))]
  718. public void GetCreationTimeUtcException3 ()
  719. {
  720. string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
  721. DeleteFile (path);
  722. File.GetCreationTimeUtc (path);
  723. }
  724. [Test]
  725. [ExpectedException(typeof(ArgumentException))]
  726. public void GetCreationTimeUtcException4 ()
  727. {
  728. File.GetCreationTimeUtc (" ");
  729. }
  730. [Test]
  731. [ExpectedException(typeof(ArgumentException))]
  732. public void GetCreationTimeUtcException5 ()
  733. {
  734. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  735. }
  736. [Test]
  737. [ExpectedException(typeof(ArgumentNullException))]
  738. public void GetLastAccessTimeException1 ()
  739. {
  740. File.GetLastAccessTime (null as string);
  741. }
  742. [Test]
  743. [ExpectedException(typeof(ArgumentException))]
  744. public void GetLastAccessTimeException2 ()
  745. {
  746. File.GetLastAccessTime ("");
  747. }
  748. [Test]
  749. [ExpectedException(typeof(IOException))]
  750. public void GetLastAccessTimeException3 ()
  751. {
  752. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
  753. DeleteFile (path);
  754. File.GetLastAccessTime (path);
  755. }
  756. [Test]
  757. [ExpectedException(typeof(ArgumentException))]
  758. public void GetLastAccessTimeException4 ()
  759. {
  760. File.GetLastAccessTime (" ");
  761. }
  762. [Test]
  763. [ExpectedException(typeof(ArgumentException))]
  764. public void GetLastAccessTimeException5 ()
  765. {
  766. File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
  767. }
  768. [Test]
  769. [ExpectedException(typeof(ArgumentNullException))]
  770. public void GetLastAccessTimeUtcException1 ()
  771. {
  772. File.GetLastAccessTimeUtc (null as string);
  773. }
  774. [Test]
  775. [ExpectedException(typeof(ArgumentException))]
  776. public void GetLastAccessTimeUtcException2 ()
  777. {
  778. File.GetLastAccessTimeUtc ("");
  779. }
  780. [Test]
  781. [ExpectedException(typeof(IOException))]
  782. public void GetLastAccessTimeUtcException3 ()
  783. {
  784. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  785. DeleteFile (path);
  786. File.GetLastAccessTimeUtc (path);
  787. }
  788. [Test]
  789. [ExpectedException(typeof(ArgumentException))]
  790. public void GetLastAccessTimeUtcException4 ()
  791. {
  792. File.GetLastAccessTimeUtc (" ");
  793. }
  794. [Test]
  795. [ExpectedException(typeof(ArgumentException))]
  796. public void GetLastAccessTimeUtcException5 ()
  797. {
  798. File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  799. }
  800. [Test]
  801. [ExpectedException(typeof(ArgumentNullException))]
  802. public void GetLastWriteTimeException1 ()
  803. {
  804. File.GetLastWriteTime (null as string);
  805. }
  806. [Test]
  807. [ExpectedException(typeof(ArgumentException))]
  808. public void GetLastWriteTimeException2 ()
  809. {
  810. File.GetLastWriteTime ("");
  811. }
  812. [Test]
  813. [ExpectedException(typeof(IOException))]
  814. public void GetLastWriteTimeException3 ()
  815. {
  816. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  817. DeleteFile (path);
  818. File.GetLastWriteTime (path);
  819. }
  820. [Test]
  821. [ExpectedException(typeof(ArgumentException))]
  822. public void GetLastWriteTimeException4 ()
  823. {
  824. File.GetLastWriteTime (" ");
  825. }
  826. [Test]
  827. [ExpectedException(typeof(ArgumentException))]
  828. public void GetLastWriteTimeException5 ()
  829. {
  830. File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
  831. }
  832. [Test]
  833. [ExpectedException(typeof(ArgumentNullException))]
  834. public void GetLastWriteTimeUtcException1 ()
  835. {
  836. File.GetLastWriteTimeUtc (null as string);
  837. }
  838. [Test]
  839. [ExpectedException(typeof(ArgumentException))]
  840. public void GetLastWriteTimeUtcException2 ()
  841. {
  842. File.GetLastAccessTimeUtc ("");
  843. }
  844. [Test]
  845. [ExpectedException(typeof(IOException))]
  846. public void GetLastWriteTimeUtcException3 ()
  847. {
  848. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
  849. DeleteFile (path);
  850. File.GetLastAccessTimeUtc (path);
  851. }
  852. [Test]
  853. [ExpectedException(typeof(ArgumentException))]
  854. public void GetLastWriteTimeUtcException4 ()
  855. {
  856. File.GetLastAccessTimeUtc (" ");
  857. }
  858. [Test]
  859. [ExpectedException(typeof(ArgumentException))]
  860. public void GetLastWriteTimeUtcException5 ()
  861. {
  862. File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  863. }
  864. [Test]
  865. [ExpectedException(typeof(IOException))]
  866. [Category("ValueAdd")]
  867. //
  868. // This is category ValueAdd, since in Unix the semantics allow for
  869. // a file to be deleted while a handle to it remains.
  870. //
  871. public void FileStreamCloseException ()
  872. {
  873. string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamCloseException";
  874. DeleteFile (path);
  875. FileStream stream = null;
  876. try {
  877. stream = File.Create (path);
  878. File.Delete (path);
  879. } finally {
  880. if (stream != null)
  881. stream.Close ();
  882. DeleteFile (path);
  883. }
  884. }
  885. [Test]
  886. public void FileStreamClose ()
  887. {
  888. string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
  889. FileStream stream = null;
  890. try {
  891. stream = File.Create (path);
  892. stream.Close ();
  893. File.Delete (path);
  894. } finally {
  895. if (stream != null)
  896. stream.Close ();
  897. DeleteFile (path);
  898. }
  899. }
  900. // SetCreationTime and SetCreationTimeUtc exceptions
  901. [Test]
  902. [ExpectedException(typeof (ArgumentNullException))]
  903. public void SetCreationTimeArgumentNullException1 ()
  904. {
  905. File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  906. }
  907. [Test]
  908. [ExpectedException(typeof (ArgumentException))]
  909. public void SetCreationTimeArgumenException1 ()
  910. {
  911. File.SetCreationTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  912. }
  913. [Test]
  914. [ExpectedException(typeof (ArgumentException))]
  915. public void SetCreationTimeArgumenException2 ()
  916. {
  917. File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  918. }
  919. [Test]
  920. [ExpectedException(typeof (ArgumentException))]
  921. [Category("ValueAdd")]
  922. // On Unix there are no invalid path chars.
  923. public void SetCreationTimeArgumenException3 ()
  924. {
  925. File.SetCreationTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  926. }
  927. [Test]
  928. [ExpectedException(typeof (FileNotFoundException))]
  929. public void SetCreationTimeFileNotFoundException1 ()
  930. {
  931. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
  932. DeleteFile (path);
  933. File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  934. }
  935. // [Test]
  936. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  937. // public void SetCreationTimeArgumentOutOfRangeException1 ()
  938. // {
  939. // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
  940. // FileStream stream = null;
  941. // DeleteFile (path);
  942. // try {
  943. // stream = File.Create (path);
  944. // stream.Close ();
  945. // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  946. // } finally {
  947. // if (stream != null)
  948. // stream.Close ();
  949. // DeleteFile (path);
  950. // }
  951. // }
  952. [Test]
  953. [ExpectedException(typeof (IOException))]
  954. public void SetCreationTimeIOException1 ()
  955. {
  956. string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
  957. DeleteFile (path);
  958. FileStream stream = null;
  959. try {
  960. stream = File.Create (path);
  961. File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  962. } finally {
  963. if (stream != null)
  964. stream.Close ();
  965. DeleteFile (path);
  966. }
  967. }
  968. [Test]
  969. [ExpectedException(typeof (ArgumentNullException))]
  970. public void SetCreationTimeUtcArgumentNullException1 ()
  971. {
  972. File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  973. }
  974. [Test]
  975. [ExpectedException(typeof (ArgumentException))]
  976. public void SetCreationTimeUtcArgumenException1 ()
  977. {
  978. File.SetCreationTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  979. }
  980. [Test]
  981. [ExpectedException(typeof (ArgumentException))]
  982. public void SetCreationTimeUtcArgumenException2 ()
  983. {
  984. File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  985. }
  986. [Test]
  987. [ExpectedException(typeof (ArgumentException))]
  988. [Category("ValueAdd")]
  989. // On Unix there are no invalid path chars.
  990. public void SetCreationTimeUtcArgumenException3 ()
  991. {
  992. File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  993. }
  994. [Test]
  995. [ExpectedException(typeof (FileNotFoundException))]
  996. public void SetCreationTimeUtcFileNotFoundException1 ()
  997. {
  998. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
  999. DeleteFile (path);
  1000. File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1001. }
  1002. // [Test]
  1003. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1004. // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
  1005. // {
  1006. // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
  1007. // DeleteFile (path);
  1008. // FileStream stream = null;
  1009. // try {
  1010. // stream = File.Create (path);
  1011. // stream.Close ();
  1012. // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1013. // } finally {
  1014. // if (stream != null)
  1015. // stream.Close();
  1016. // DeleteFile (path);
  1017. // }
  1018. // }
  1019. [Test]
  1020. [ExpectedException(typeof (IOException))]
  1021. public void SetCreationTimeUtcIOException1 ()
  1022. {
  1023. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
  1024. DeleteFile (path);
  1025. FileStream stream = null;
  1026. try {
  1027. stream = File.Create (path);
  1028. File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1029. } finally {
  1030. if (stream != null)
  1031. stream.Close ();
  1032. DeleteFile (path);
  1033. }
  1034. }
  1035. // SetLastAccessTime and SetLastAccessTimeUtc exceptions
  1036. [Test]
  1037. [ExpectedException(typeof (ArgumentNullException))]
  1038. public void SetLastAccessTimeArgumentNullException1 ()
  1039. {
  1040. File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1041. }
  1042. [Test]
  1043. [ExpectedException(typeof (ArgumentException))]
  1044. public void SetLastAccessTimeArgumenException1 ()
  1045. {
  1046. File.SetLastAccessTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1047. }
  1048. [Test]
  1049. [ExpectedException(typeof (ArgumentException))]
  1050. public void SetLastAccessTimeArgumenException2 ()
  1051. {
  1052. File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1053. }
  1054. [Test]
  1055. [ExpectedException(typeof (ArgumentException))]
  1056. [Category("ValueAdd")]
  1057. // On Unix there are no invalid path chars.
  1058. public void SetLastAccessTimeArgumenException3 ()
  1059. {
  1060. File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1061. }
  1062. [Test]
  1063. [ExpectedException(typeof (FileNotFoundException))]
  1064. public void SetLastAccessTimeFileNotFoundException1 ()
  1065. {
  1066. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
  1067. DeleteFile (path);
  1068. File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1069. }
  1070. // [Test]
  1071. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1072. // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
  1073. // {
  1074. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
  1075. // DeleteFile (path);
  1076. // FileStream stream = null;
  1077. // try {
  1078. // stream = File.Create (path);
  1079. // stream.Close ();
  1080. // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1081. // } finally {
  1082. // if (stream != null)
  1083. // stream.Close ();
  1084. // DeleteFile (path);
  1085. // }
  1086. // }
  1087. [Test]
  1088. [ExpectedException(typeof (IOException))]
  1089. public void SetLastAccessTimeIOException1 ()
  1090. {
  1091. string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
  1092. DeleteFile (path);
  1093. FileStream stream = null;
  1094. try {
  1095. stream = File.Create (path);
  1096. File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1097. } finally {
  1098. if (stream != null)
  1099. stream.Close ();
  1100. DeleteFile (path);
  1101. }
  1102. }
  1103. [Test]
  1104. [ExpectedException(typeof (ArgumentNullException))]
  1105. public void SetLastAccessTimeUtcArgumentNullException1 ()
  1106. {
  1107. File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1108. }
  1109. [Test]
  1110. [ExpectedException(typeof (ArgumentException))]
  1111. public void SetCLastAccessTimeUtcArgumenException1 ()
  1112. {
  1113. File.SetLastAccessTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1114. }
  1115. [Test]
  1116. [ExpectedException(typeof (ArgumentException))]
  1117. public void SetLastAccessTimeUtcArgumenException2 ()
  1118. {
  1119. File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1120. }
  1121. [Test]
  1122. [ExpectedException(typeof (ArgumentException))]
  1123. [Category("ValueAdd")]
  1124. // On Unix there are no invalid path chars.
  1125. public void SetLastAccessTimeUtcArgumenException3 ()
  1126. {
  1127. File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1128. }
  1129. [Test]
  1130. [ExpectedException(typeof (FileNotFoundException))]
  1131. public void SetLastAccessTimeUtcFileNotFoundException1 ()
  1132. {
  1133. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
  1134. DeleteFile (path);
  1135. File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1136. }
  1137. // [Test]
  1138. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1139. // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
  1140. // {
  1141. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
  1142. // DeleteFile (path);
  1143. // FileStream stream = null;
  1144. // try {
  1145. // stream = File.Create (path);
  1146. // stream.Close ();
  1147. // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1148. // } finally {
  1149. // if (stream != null)
  1150. // stream.Close ();
  1151. // DeleteFile (path);
  1152. // }
  1153. // }
  1154. [Test]
  1155. [ExpectedException(typeof (IOException))]
  1156. public void SetLastAccessTimeUtcIOException1 ()
  1157. {
  1158. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
  1159. DeleteFile (path);
  1160. FileStream stream = null;
  1161. try {
  1162. stream = File.Create (path);
  1163. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1164. } finally {
  1165. if (stream != null)
  1166. stream.Close ();
  1167. DeleteFile (path);
  1168. }
  1169. }
  1170. // SetLastWriteTime and SetLastWriteTimeUtc exceptions
  1171. [Test]
  1172. [ExpectedException(typeof (ArgumentNullException))]
  1173. public void SetLastWriteTimeArgumentNullException1 ()
  1174. {
  1175. File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1176. }
  1177. [Test]
  1178. [ExpectedException(typeof (ArgumentException))]
  1179. public void SetLastWriteTimeArgumenException1 ()
  1180. {
  1181. File.SetLastWriteTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1182. }
  1183. [Test]
  1184. [ExpectedException(typeof (ArgumentException))]
  1185. public void SetLastWriteTimeArgumenException2 ()
  1186. {
  1187. File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1188. }
  1189. [Test]
  1190. [ExpectedException(typeof (ArgumentException))]
  1191. [Category("ValueAdd")]
  1192. // On Unix there are no invalid path chars.
  1193. public void SetLastWriteTimeArgumenException3 ()
  1194. {
  1195. File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1196. }
  1197. [Test]
  1198. [ExpectedException(typeof (FileNotFoundException))]
  1199. public void SetLastWriteTimeFileNotFoundException1 ()
  1200. {
  1201. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
  1202. DeleteFile (path);
  1203. File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1204. }
  1205. // [Test]
  1206. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1207. // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
  1208. // {
  1209. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
  1210. // DeleteFile (path);
  1211. // FileStream stream = null;
  1212. // try {
  1213. // stream = File.Create (path);
  1214. // stream.Close ();
  1215. // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1216. // } finally {
  1217. // if (stream != null)
  1218. // stream.Close ();
  1219. // DeleteFile (path);
  1220. // }
  1221. // }
  1222. [Test]
  1223. [ExpectedException(typeof (IOException))]
  1224. public void SetLastWriteTimeIOException1 ()
  1225. {
  1226. string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
  1227. DeleteFile (path);
  1228. FileStream stream = null;
  1229. try {
  1230. stream = File.Create (path);
  1231. File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1232. } finally {
  1233. if (stream != null)
  1234. stream.Close ();
  1235. DeleteFile (path);
  1236. }
  1237. }
  1238. [Test]
  1239. [ExpectedException(typeof (ArgumentNullException))]
  1240. public void SetLastWriteTimeUtcArgumentNullException1 ()
  1241. {
  1242. File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1243. }
  1244. [Test]
  1245. [ExpectedException(typeof (ArgumentException))]
  1246. public void SetCLastWriteTimeUtcArgumenException1 ()
  1247. {
  1248. File.SetLastWriteTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1249. }
  1250. [Test]
  1251. [ExpectedException(typeof (ArgumentException))]
  1252. public void SetLastWriteTimeUtcArgumenException2 ()
  1253. {
  1254. File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1255. }
  1256. [Test]
  1257. [ExpectedException(typeof (ArgumentException))]
  1258. [Category("ValueAdd")]
  1259. // On Unix there are no invalid path chars.
  1260. public void SetLastWriteTimeUtcArgumenException3 ()
  1261. {
  1262. File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1263. }
  1264. [Test]
  1265. [ExpectedException(typeof (FileNotFoundException))]
  1266. public void SetLastWriteTimeUtcFileNotFoundException1 ()
  1267. {
  1268. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
  1269. DeleteFile (path);
  1270. File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1271. }
  1272. // [Test]
  1273. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1274. // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
  1275. // {
  1276. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
  1277. // DeleteFile (path);
  1278. // FileStream stream = null;
  1279. // try {
  1280. // stream = File.Create (path);
  1281. // stream.Close ();
  1282. // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1283. // } finally {
  1284. // if (stream != null)
  1285. // stream.Close ();
  1286. // DeleteFile (path);
  1287. // }
  1288. // }
  1289. //
  1290. [Test]
  1291. [ExpectedException(typeof (IOException))]
  1292. public void SetLastWriteTimeUtcIOException1 ()
  1293. {
  1294. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
  1295. DeleteFile (path);
  1296. FileStream stream = null;
  1297. try {
  1298. stream = File.Create (path);
  1299. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1300. } finally {
  1301. if (stream != null)
  1302. stream.Close ();
  1303. DeleteFile (path);
  1304. }
  1305. }
  1306. private void DeleteFile (string path)
  1307. {
  1308. if (File.Exists (path))
  1309. File.Delete (path);
  1310. }
  1311. }
  1312. }