FileTest.cs 46 KB

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