FileTest.cs 45 KB

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