FileTest.cs 46 KB

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