FileTest.cs 49 KB

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