FileTest.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. #if TARGET_JVM
  525. [Category("NotWorking")]
  526. #endif
  527. public void TestGetCreationTime ()
  528. {
  529. string path = TempFolder + Path.DirectorySeparatorChar + "baz";
  530. DeleteFile (path);
  531. try {
  532. File.Create (path).Close();
  533. DateTime time = File.GetCreationTime (path);
  534. Assert ("GetCreationTime incorrect", (DateTime.Now - time).TotalSeconds < 10);
  535. } finally {
  536. DeleteFile (path);
  537. }
  538. }
  539. // Setting the creation time on Unix is not possible
  540. [Test]
  541. #if TARGET_JVM
  542. [Category("NotWorking")]
  543. #endif
  544. public void CreationTime ()
  545. {
  546. int platform = (int) Environment.OSVersion.Platform;
  547. if ((platform == 4) || (platform == 128))
  548. return;
  549. string path = Path.GetTempFileName ();
  550. try {
  551. File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  552. DateTime time = File.GetCreationTime (path);
  553. Assertion.AssertEquals ("test#01", 2002, time.Year);
  554. Assertion.AssertEquals ("test#02", 4, time.Month);
  555. Assertion.AssertEquals ("test#03", 6, time.Day);
  556. Assertion.AssertEquals ("test#04", 4, time.Hour);
  557. Assertion.AssertEquals ("test#05", 4, time.Second);
  558. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
  559. Assertion.AssertEquals ("test#06", 2002, time.Year);
  560. Assertion.AssertEquals ("test#07", 4, time.Month);
  561. Assertion.AssertEquals ("test#08", 6, time.Day);
  562. Assertion.AssertEquals ("test#09", 4, time.Hour);
  563. Assertion.AssertEquals ("test#10", 4, time.Second);
  564. File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  565. time = File.GetCreationTimeUtc (path);
  566. Assertion.AssertEquals ("test#11", 2002, time.Year);
  567. Assertion.AssertEquals ("test#12", 4, time.Month);
  568. Assertion.AssertEquals ("test#13", 6, time.Day);
  569. Assertion.AssertEquals ("test#14", 4, time.Hour);
  570. Assertion.AssertEquals ("test#15", 4, time.Second);
  571. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
  572. Assertion.AssertEquals ("test#16", 2002, time.Year);
  573. Assertion.AssertEquals ("test#17", 4, time.Month);
  574. Assertion.AssertEquals ("test#18", 6, time.Day);
  575. Assertion.AssertEquals ("test#19", 4, time.Hour);
  576. Assertion.AssertEquals ("test#20", 4, time.Second);
  577. } finally {
  578. DeleteFile (path);
  579. }
  580. }
  581. [Test]
  582. #if TARGET_JVM
  583. [Category("NotWorking")]
  584. #endif
  585. public void LastAccessTime ()
  586. {
  587. string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
  588. if (File.Exists (path))
  589. File.Delete (path);
  590. FileStream stream = null;
  591. try {
  592. stream = File.Create (path);
  593. stream.Close ();
  594. File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  595. DateTime time = File.GetLastAccessTime (path);
  596. Assertion.AssertEquals ("test#01", 2002, time.Year);
  597. Assertion.AssertEquals ("test#02", 4, time.Month);
  598. Assertion.AssertEquals ("test#03", 6, time.Day);
  599. Assertion.AssertEquals ("test#04", 4, time.Hour);
  600. Assertion.AssertEquals ("test#05", 4, time.Second);
  601. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
  602. Assertion.AssertEquals ("test#06", 2002, time.Year);
  603. Assertion.AssertEquals ("test#07", 4, time.Month);
  604. Assertion.AssertEquals ("test#08", 6, time.Day);
  605. Assertion.AssertEquals ("test#09", 4, time.Hour);
  606. Assertion.AssertEquals ("test#10", 4, time.Second);
  607. File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  608. time = File.GetLastAccessTimeUtc (path);
  609. Assertion.AssertEquals ("test#11", 2002, time.Year);
  610. Assertion.AssertEquals ("test#12", 4, time.Month);
  611. Assertion.AssertEquals ("test#13", 6, time.Day);
  612. Assertion.AssertEquals ("test#14", 4, time.Hour);
  613. Assertion.AssertEquals ("test#15", 4, time.Second);
  614. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
  615. Assertion.AssertEquals ("test#16", 2002, time.Year);
  616. Assertion.AssertEquals ("test#17", 4, time.Month);
  617. Assertion.AssertEquals ("test#18", 6, time.Day);
  618. Assertion.AssertEquals ("test#19", 4, time.Hour);
  619. Assertion.AssertEquals ("test#20", 4, time.Second);
  620. } finally {
  621. if (stream != null)
  622. stream.Close ();
  623. DeleteFile (path);
  624. }
  625. }
  626. [Test]
  627. public void LastWriteTime ()
  628. {
  629. string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
  630. if (File.Exists (path))
  631. File.Delete (path);
  632. FileStream stream = null;
  633. try {
  634. stream = File.Create (path);
  635. stream.Close ();
  636. File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  637. DateTime time = File.GetLastWriteTime (path);
  638. Assertion.AssertEquals ("test#01", 2002, time.Year);
  639. Assertion.AssertEquals ("test#02", 4, time.Month);
  640. Assertion.AssertEquals ("test#03", 6, time.Day);
  641. Assertion.AssertEquals ("test#04", 4, time.Hour);
  642. Assertion.AssertEquals ("test#05", 4, time.Second);
  643. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
  644. Assertion.AssertEquals ("test#06", 2002, time.Year);
  645. Assertion.AssertEquals ("test#07", 4, time.Month);
  646. Assertion.AssertEquals ("test#08", 6, time.Day);
  647. Assertion.AssertEquals ("test#09", 4, time.Hour);
  648. Assertion.AssertEquals ("test#10", 4, time.Second);
  649. File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  650. time = File.GetLastWriteTimeUtc (path);
  651. Assertion.AssertEquals ("test#11", 2002, time.Year);
  652. Assertion.AssertEquals ("test#12", 4, time.Month);
  653. Assertion.AssertEquals ("test#13", 6, time.Day);
  654. Assertion.AssertEquals ("test#14", 4, time.Hour);
  655. Assertion.AssertEquals ("test#15", 4, time.Second);
  656. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
  657. Assertion.AssertEquals ("test#16", 2002, time.Year);
  658. Assertion.AssertEquals ("test#17", 4, time.Month);
  659. Assertion.AssertEquals ("test#18", 6, time.Day);
  660. Assertion.AssertEquals ("test#19", 4, time.Hour);
  661. Assertion.AssertEquals ("test#20", 4, time.Second);
  662. } finally {
  663. if (stream != null)
  664. stream.Close ();
  665. DeleteFile (path);
  666. }
  667. }
  668. [Test]
  669. [ExpectedException(typeof(ArgumentNullException))]
  670. #if TARGET_JVM
  671. [Category("NotWorking")]
  672. #endif
  673. public void GetCreationTimeException1 ()
  674. {
  675. File.GetCreationTime (null as string);
  676. }
  677. [Test]
  678. [ExpectedException(typeof(ArgumentException))]
  679. #if TARGET_JVM
  680. [Category("NotWorking")]
  681. #endif
  682. public void GetCreationTimeException2 ()
  683. {
  684. File.GetCreationTime ("");
  685. }
  686. [Test]
  687. #if !NET_2_0
  688. [ExpectedException(typeof(IOException))]
  689. #endif
  690. #if TARGET_JVM
  691. [Category("NotWorking")]
  692. #endif
  693. public void GetCreationTime_NonExistingPath ()
  694. {
  695. string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
  696. DeleteFile (path);
  697. DateTime time = File.GetCreationTime (path);
  698. #if NET_2_0
  699. DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
  700. Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
  701. Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
  702. Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
  703. Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
  704. Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
  705. Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
  706. #endif
  707. }
  708. [Test]
  709. [ExpectedException(typeof(ArgumentException))]
  710. #if TARGET_JVM
  711. [Category("NotWorking")]
  712. #endif
  713. public void GetCreationTimeException4 ()
  714. {
  715. File.GetCreationTime (" ");
  716. }
  717. [Test]
  718. [ExpectedException(typeof(ArgumentException))]
  719. #if TARGET_JVM
  720. [Category("NotWorking")]
  721. #endif
  722. public void GetCreationTimeException5 ()
  723. {
  724. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  725. }
  726. [Test]
  727. [ExpectedException(typeof(ArgumentNullException))]
  728. #if TARGET_JVM
  729. [Category("NotWorking")]
  730. #endif
  731. public void GetCreationTimeUtcException1 ()
  732. {
  733. File.GetCreationTimeUtc (null as string);
  734. }
  735. [Test]
  736. [ExpectedException(typeof(ArgumentException))]
  737. #if TARGET_JVM
  738. [Category("NotWorking")]
  739. #endif
  740. public void GetCreationTimeUtcException2 ()
  741. {
  742. File.GetCreationTimeUtc ("");
  743. }
  744. [Test]
  745. #if !NET_2_0
  746. [ExpectedException (typeof (IOException))]
  747. #endif
  748. #if TARGET_JVM
  749. [Category("NotWorking")]
  750. #endif
  751. public void GetCreationTimeUtc_NonExistingPath ()
  752. {
  753. string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
  754. DeleteFile (path);
  755. DateTime time = File.GetCreationTimeUtc (path);
  756. #if NET_2_0
  757. Assertion.AssertEquals ("#1", 1601, time.Year);
  758. Assertion.AssertEquals ("#2", 1, time.Month);
  759. Assertion.AssertEquals ("#3", 1, time.Day);
  760. Assertion.AssertEquals ("#4", 0, time.Hour);
  761. Assertion.AssertEquals ("#5", 0, time.Second);
  762. Assertion.AssertEquals ("#6", 0, time.Millisecond);
  763. #endif
  764. }
  765. [Test]
  766. [ExpectedException(typeof(ArgumentException))]
  767. #if TARGET_JVM
  768. [Category("NotWorking")]
  769. #endif
  770. public void GetCreationTimeUtcException4 ()
  771. {
  772. File.GetCreationTimeUtc (" ");
  773. }
  774. [Test]
  775. [ExpectedException(typeof(ArgumentException))]
  776. #if TARGET_JVM
  777. [Category("NotWorking")]
  778. #endif
  779. public void GetCreationTimeUtcException5 ()
  780. {
  781. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  782. }
  783. [Test]
  784. [ExpectedException(typeof(ArgumentNullException))]
  785. #if TARGET_JVM
  786. [Category("NotWorking")]
  787. #endif
  788. public void GetLastAccessTimeException1 ()
  789. {
  790. File.GetLastAccessTime (null as string);
  791. }
  792. [Test]
  793. [ExpectedException(typeof(ArgumentException))]
  794. #if TARGET_JVM
  795. [Category("NotWorking")]
  796. #endif
  797. public void GetLastAccessTimeException2 ()
  798. {
  799. File.GetLastAccessTime ("");
  800. }
  801. [Test]
  802. #if !NET_2_0
  803. [ExpectedException (typeof (IOException))]
  804. #endif
  805. #if TARGET_JVM
  806. [Category("NotWorking")]
  807. #endif
  808. public void GetLastAccessTime_NonExistingPath ()
  809. {
  810. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
  811. DeleteFile (path);
  812. DateTime time = File.GetLastAccessTime (path);
  813. #if NET_2_0
  814. DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
  815. Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
  816. Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
  817. Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
  818. Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
  819. Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
  820. Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
  821. #endif
  822. }
  823. [Test]
  824. [ExpectedException(typeof(ArgumentException))]
  825. #if TARGET_JVM
  826. [Category("NotWorking")]
  827. #endif
  828. public void GetLastAccessTimeException4 ()
  829. {
  830. File.GetLastAccessTime (" ");
  831. }
  832. [Test]
  833. [ExpectedException(typeof(ArgumentException))]
  834. #if TARGET_JVM
  835. [Category("NotWorking")]
  836. #endif
  837. public void GetLastAccessTimeException5 ()
  838. {
  839. File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
  840. }
  841. [Test]
  842. [ExpectedException(typeof(ArgumentNullException))]
  843. #if TARGET_JVM
  844. [Category("NotWorking")]
  845. #endif
  846. public void GetLastAccessTimeUtcException1 ()
  847. {
  848. File.GetLastAccessTimeUtc (null as string);
  849. }
  850. [Test]
  851. [ExpectedException(typeof(ArgumentException))]
  852. #if TARGET_JVM
  853. [Category("NotWorking")]
  854. #endif
  855. public void GetLastAccessTimeUtcException2 ()
  856. {
  857. File.GetLastAccessTimeUtc ("");
  858. }
  859. [Test]
  860. #if !NET_2_0
  861. [ExpectedException (typeof (IOException))]
  862. #endif
  863. #if TARGET_JVM
  864. [Category("NotWorking")]
  865. #endif
  866. public void GetLastAccessTimeUtc_NonExistingPath ()
  867. {
  868. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  869. DeleteFile (path);
  870. DateTime time = File.GetLastAccessTimeUtc (path);
  871. #if NET_2_0
  872. Assertion.AssertEquals ("#1", 1601, time.Year);
  873. Assertion.AssertEquals ("#2", 1, time.Month);
  874. Assertion.AssertEquals ("#3", 1, time.Day);
  875. Assertion.AssertEquals ("#4", 0, time.Hour);
  876. Assertion.AssertEquals ("#5", 0, time.Second);
  877. Assertion.AssertEquals ("#6", 0, time.Millisecond);
  878. #endif
  879. }
  880. [Test]
  881. [ExpectedException(typeof(ArgumentException))]
  882. #if TARGET_JVM
  883. [Category("NotWorking")]
  884. #endif
  885. public void GetLastAccessTimeUtcException4 ()
  886. {
  887. File.GetLastAccessTimeUtc (" ");
  888. }
  889. [Test]
  890. [ExpectedException(typeof(ArgumentException))]
  891. #if TARGET_JVM
  892. [Category("NotWorking")]
  893. #endif
  894. public void GetLastAccessTimeUtcException5 ()
  895. {
  896. File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  897. }
  898. [Test]
  899. [ExpectedException(typeof(ArgumentNullException))]
  900. public void GetLastWriteTimeException1 ()
  901. {
  902. File.GetLastWriteTime (null as string);
  903. }
  904. [Test]
  905. [ExpectedException(typeof(ArgumentException))]
  906. public void GetLastWriteTimeException2 ()
  907. {
  908. File.GetLastWriteTime ("");
  909. }
  910. [Test]
  911. #if !NET_2_0
  912. [ExpectedException (typeof (IOException))]
  913. #endif
  914. public void GetLastWriteTime_NonExistingPath ()
  915. {
  916. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  917. DeleteFile (path);
  918. DateTime time = File.GetLastWriteTime (path);
  919. #if NET_2_0
  920. DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
  921. Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
  922. Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
  923. Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
  924. Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
  925. Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
  926. Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
  927. #endif
  928. }
  929. [Test]
  930. [ExpectedException(typeof(ArgumentException))]
  931. public void GetLastWriteTimeException4 ()
  932. {
  933. File.GetLastWriteTime (" ");
  934. }
  935. [Test]
  936. [ExpectedException(typeof(ArgumentException))]
  937. public void GetLastWriteTimeException5 ()
  938. {
  939. File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
  940. }
  941. [Test]
  942. [ExpectedException(typeof(ArgumentNullException))]
  943. public void GetLastWriteTimeUtcException1 ()
  944. {
  945. File.GetLastWriteTimeUtc (null as string);
  946. }
  947. [Test]
  948. [ExpectedException(typeof(ArgumentException))]
  949. public void GetLastWriteTimeUtcException2 ()
  950. {
  951. File.GetLastWriteTimeUtc ("");
  952. }
  953. [Test]
  954. #if !NET_2_0
  955. [ExpectedException (typeof (IOException))]
  956. #endif
  957. public void GetLastWriteTimeUtc_NonExistingPath ()
  958. {
  959. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
  960. DeleteFile (path);
  961. DateTime time = File.GetLastWriteTimeUtc (path);
  962. #if NET_2_0
  963. Assertion.AssertEquals ("#1", 1601, time.Year);
  964. Assertion.AssertEquals ("#2", 1, time.Month);
  965. Assertion.AssertEquals ("#3", 1, time.Day);
  966. Assertion.AssertEquals ("#4", 0, time.Hour);
  967. Assertion.AssertEquals ("#5", 0, time.Second);
  968. Assertion.AssertEquals ("#6", 0, time.Millisecond);
  969. #endif
  970. }
  971. [Test]
  972. [ExpectedException(typeof(ArgumentException))]
  973. public void GetLastWriteTimeUtcException4 ()
  974. {
  975. File.GetLastWriteTimeUtc (" ");
  976. }
  977. [Test]
  978. [ExpectedException(typeof(ArgumentException))]
  979. public void GetLastWriteTimeUtcException5 ()
  980. {
  981. File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
  982. }
  983. [Test]
  984. public void FileStreamClose ()
  985. {
  986. string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
  987. FileStream stream = null;
  988. try {
  989. stream = File.Create (path);
  990. stream.Close ();
  991. File.Delete (path);
  992. } finally {
  993. if (stream != null)
  994. stream.Close ();
  995. DeleteFile (path);
  996. }
  997. }
  998. // SetCreationTime and SetCreationTimeUtc exceptions
  999. [Test]
  1000. [ExpectedException(typeof (ArgumentNullException))]
  1001. #if TARGET_JVM
  1002. [Category("NotWorking")]
  1003. #endif
  1004. public void SetCreationTimeArgumentNullException1 ()
  1005. {
  1006. File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1007. }
  1008. [Test]
  1009. [ExpectedException(typeof (ArgumentException))]
  1010. #if TARGET_JVM
  1011. [Category("NotWorking")]
  1012. #endif
  1013. public void SetCreationTimeArgumenException1 ()
  1014. {
  1015. File.SetCreationTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1016. }
  1017. [Test]
  1018. [ExpectedException(typeof (ArgumentException))]
  1019. #if TARGET_JVM
  1020. [Category("NotWorking")]
  1021. #endif
  1022. public void SetCreationTimeArgumenException2 ()
  1023. {
  1024. File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1025. }
  1026. [Test]
  1027. // On Unix there are no invalid path chars.
  1028. #if TARGET_JVM
  1029. [Category("NotWorking")]
  1030. #endif
  1031. public void SetCreationTimeArgumenException3 ()
  1032. {
  1033. if (Path.InvalidPathChars.Length > 1) {
  1034. bool pass = false;
  1035. try {
  1036. File.SetCreationTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1037. } catch (ArgumentException) {
  1038. pass = true;
  1039. }
  1040. Assertion.Assert ("#01", pass);
  1041. }
  1042. }
  1043. [Test]
  1044. [ExpectedException(typeof (FileNotFoundException))]
  1045. #if TARGET_JVM
  1046. [Category("NotWorking")]
  1047. #endif
  1048. public void SetCreationTimeFileNotFoundException1 ()
  1049. {
  1050. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
  1051. DeleteFile (path);
  1052. File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1053. }
  1054. // [Test]
  1055. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1056. // public void SetCreationTimeArgumentOutOfRangeException1 ()
  1057. // {
  1058. // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
  1059. // FileStream stream = null;
  1060. // DeleteFile (path);
  1061. // try {
  1062. // stream = File.Create (path);
  1063. // stream.Close ();
  1064. // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1065. // } finally {
  1066. // if (stream != null)
  1067. // stream.Close ();
  1068. // DeleteFile (path);
  1069. // }
  1070. // }
  1071. [Test]
  1072. [ExpectedException(typeof (IOException))]
  1073. #if TARGET_JVM
  1074. [Category("NotWorking")]
  1075. #endif
  1076. public void SetCreationTimeIOException1 ()
  1077. {
  1078. string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
  1079. DeleteFile (path);
  1080. FileStream stream = null;
  1081. try {
  1082. stream = File.Create (path);
  1083. File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1084. } finally {
  1085. if (stream != null)
  1086. stream.Close ();
  1087. DeleteFile (path);
  1088. }
  1089. }
  1090. [Test]
  1091. [ExpectedException(typeof (ArgumentNullException))]
  1092. #if TARGET_JVM
  1093. [Category("NotWorking")]
  1094. #endif
  1095. public void SetCreationTimeUtcArgumentNullException1 ()
  1096. {
  1097. File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1098. }
  1099. [Test]
  1100. [ExpectedException(typeof (ArgumentException))]
  1101. #if TARGET_JVM
  1102. [Category("NotWorking")]
  1103. #endif
  1104. public void SetCreationTimeUtcArgumenException1 ()
  1105. {
  1106. File.SetCreationTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1107. }
  1108. [Test]
  1109. [ExpectedException(typeof (ArgumentException))]
  1110. #if TARGET_JVM
  1111. [Category("NotWorking")]
  1112. #endif
  1113. public void SetCreationTimeUtcArgumenException2 ()
  1114. {
  1115. File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1116. }
  1117. [Test]
  1118. // On Unix there are no invalid path chars.
  1119. #if TARGET_JVM
  1120. [Category("NotWorking")]
  1121. #endif
  1122. public void SetCreationTimeUtcArgumentException3 ()
  1123. {
  1124. if (Path.InvalidPathChars.Length > 1) {
  1125. bool pass = false;
  1126. try {
  1127. File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1128. } catch (ArgumentException) {
  1129. pass = true;
  1130. }
  1131. Assertion.Assert ("#01", pass);
  1132. }
  1133. }
  1134. [Test]
  1135. [ExpectedException(typeof (FileNotFoundException))]
  1136. #if TARGET_JVM
  1137. [Category("NotWorking")]
  1138. #endif
  1139. public void SetCreationTimeUtcFileNotFoundException1 ()
  1140. {
  1141. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
  1142. DeleteFile (path);
  1143. File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1144. }
  1145. // [Test]
  1146. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1147. // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
  1148. // {
  1149. // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
  1150. // DeleteFile (path);
  1151. // FileStream stream = null;
  1152. // try {
  1153. // stream = File.Create (path);
  1154. // stream.Close ();
  1155. // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1156. // } finally {
  1157. // if (stream != null)
  1158. // stream.Close();
  1159. // DeleteFile (path);
  1160. // }
  1161. // }
  1162. [Test]
  1163. [ExpectedException(typeof (IOException))]
  1164. #if TARGET_JVM
  1165. [Category("NotWorking")]
  1166. #endif
  1167. public void SetCreationTimeUtcIOException1 ()
  1168. {
  1169. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
  1170. DeleteFile (path);
  1171. FileStream stream = null;
  1172. try {
  1173. stream = File.Create (path);
  1174. File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1175. } finally {
  1176. if (stream != null)
  1177. stream.Close ();
  1178. DeleteFile (path);
  1179. }
  1180. }
  1181. // SetLastAccessTime and SetLastAccessTimeUtc exceptions
  1182. [Test]
  1183. [ExpectedException(typeof (ArgumentNullException))]
  1184. #if TARGET_JVM
  1185. [Category("NotWorking")]
  1186. #endif
  1187. public void SetLastAccessTimeArgumentNullException1 ()
  1188. {
  1189. File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1190. }
  1191. [Test]
  1192. [ExpectedException(typeof (ArgumentException))]
  1193. #if TARGET_JVM
  1194. [Category("NotWorking")]
  1195. #endif
  1196. public void SetLastAccessTimeArgumenException1 ()
  1197. {
  1198. File.SetLastAccessTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1199. }
  1200. [Test]
  1201. [ExpectedException(typeof (ArgumentException))]
  1202. #if TARGET_JVM
  1203. [Category("NotWorking")]
  1204. #endif
  1205. public void SetLastAccessTimeArgumenException2 ()
  1206. {
  1207. File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1208. }
  1209. [Test]
  1210. // On Unix there are no invalid path chars.
  1211. #if TARGET_JVM
  1212. [Category("NotWorking")]
  1213. #endif
  1214. public void SetLastAccessTimeArgumenException3 ()
  1215. {
  1216. if (Path.InvalidPathChars.Length > 1) {
  1217. bool pass = false;
  1218. try {
  1219. File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1220. } catch (ArgumentException) {
  1221. pass = true;
  1222. }
  1223. Assertion.Assert ("#01", pass);
  1224. }
  1225. }
  1226. [Test]
  1227. [ExpectedException(typeof (FileNotFoundException))]
  1228. #if TARGET_JVM
  1229. [Category("NotWorking")]
  1230. #endif
  1231. public void SetLastAccessTimeFileNotFoundException1 ()
  1232. {
  1233. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
  1234. DeleteFile (path);
  1235. File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1236. }
  1237. // [Test]
  1238. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1239. // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
  1240. // {
  1241. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
  1242. // DeleteFile (path);
  1243. // FileStream stream = null;
  1244. // try {
  1245. // stream = File.Create (path);
  1246. // stream.Close ();
  1247. // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1248. // } finally {
  1249. // if (stream != null)
  1250. // stream.Close ();
  1251. // DeleteFile (path);
  1252. // }
  1253. // }
  1254. [Test]
  1255. [ExpectedException(typeof (IOException))]
  1256. #if TARGET_JVM
  1257. [Category("NotWorking")]
  1258. #endif
  1259. public void SetLastAccessTimeIOException1 ()
  1260. {
  1261. string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
  1262. DeleteFile (path);
  1263. FileStream stream = null;
  1264. try {
  1265. stream = File.Create (path);
  1266. File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1267. } finally {
  1268. if (stream != null)
  1269. stream.Close ();
  1270. DeleteFile (path);
  1271. }
  1272. }
  1273. [Test]
  1274. [ExpectedException(typeof (ArgumentNullException))]
  1275. #if TARGET_JVM
  1276. [Category("NotWorking")]
  1277. #endif
  1278. public void SetLastAccessTimeUtcArgumentNullException1 ()
  1279. {
  1280. File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1281. }
  1282. [Test]
  1283. [ExpectedException(typeof (ArgumentException))]
  1284. #if TARGET_JVM
  1285. [Category("NotWorking")]
  1286. #endif
  1287. public void SetCLastAccessTimeUtcArgumenException1 ()
  1288. {
  1289. File.SetLastAccessTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1290. }
  1291. [Test]
  1292. [ExpectedException(typeof (ArgumentException))]
  1293. #if TARGET_JVM
  1294. [Category("NotWorking")]
  1295. #endif
  1296. public void SetLastAccessTimeUtcArgumenException2 ()
  1297. {
  1298. File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1299. }
  1300. [Test]
  1301. // On Unix there are no invalid path chars.
  1302. #if TARGET_JVM
  1303. [Category("NotWorking")]
  1304. #endif
  1305. public void SetLastAccessTimeUtcArgumenException3 ()
  1306. {
  1307. if (Path.InvalidPathChars.Length > 1) {
  1308. bool pass = false;
  1309. try {
  1310. File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1311. } catch (ArgumentException) {
  1312. pass = true;
  1313. }
  1314. Assertion.Assert ("#01", pass);
  1315. }
  1316. }
  1317. [Test]
  1318. [ExpectedException(typeof (FileNotFoundException))]
  1319. #if TARGET_JVM
  1320. [Category("NotWorking")]
  1321. #endif
  1322. public void SetLastAccessTimeUtcFileNotFoundException1 ()
  1323. {
  1324. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
  1325. DeleteFile (path);
  1326. File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1327. }
  1328. // [Test]
  1329. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1330. // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
  1331. // {
  1332. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
  1333. // DeleteFile (path);
  1334. // FileStream stream = null;
  1335. // try {
  1336. // stream = File.Create (path);
  1337. // stream.Close ();
  1338. // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1339. // } finally {
  1340. // if (stream != null)
  1341. // stream.Close ();
  1342. // DeleteFile (path);
  1343. // }
  1344. // }
  1345. [Test]
  1346. [ExpectedException(typeof (IOException))]
  1347. #if TARGET_JVM
  1348. [Category("NotWorking")]
  1349. #endif
  1350. public void SetLastAccessTimeUtcIOException1 ()
  1351. {
  1352. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
  1353. DeleteFile (path);
  1354. FileStream stream = null;
  1355. try {
  1356. stream = File.Create (path);
  1357. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1358. } finally {
  1359. if (stream != null)
  1360. stream.Close ();
  1361. DeleteFile (path);
  1362. }
  1363. }
  1364. // SetLastWriteTime and SetLastWriteTimeUtc exceptions
  1365. [Test]
  1366. [ExpectedException(typeof (ArgumentNullException))]
  1367. public void SetLastWriteTimeArgumentNullException1 ()
  1368. {
  1369. File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1370. }
  1371. [Test]
  1372. [ExpectedException(typeof (ArgumentException))]
  1373. public void SetLastWriteTimeArgumenException1 ()
  1374. {
  1375. File.SetLastWriteTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1376. }
  1377. [Test]
  1378. [ExpectedException(typeof (ArgumentException))]
  1379. public void SetLastWriteTimeArgumenException2 ()
  1380. {
  1381. File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1382. }
  1383. [Test]
  1384. // On Unix there are no invalid path chars.
  1385. public void SetLastWriteTimeArgumenException3 ()
  1386. {
  1387. if (Path.InvalidPathChars.Length > 1) {
  1388. bool pass = false;
  1389. try {
  1390. File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1391. } catch (ArgumentException) {
  1392. pass = true;
  1393. }
  1394. Assertion.Assert ("#01", pass);
  1395. }
  1396. }
  1397. [Test]
  1398. [ExpectedException(typeof (FileNotFoundException))]
  1399. public void SetLastWriteTimeFileNotFoundException1 ()
  1400. {
  1401. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
  1402. DeleteFile (path);
  1403. File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1404. }
  1405. // [Test]
  1406. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1407. // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
  1408. // {
  1409. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
  1410. // DeleteFile (path);
  1411. // FileStream stream = null;
  1412. // try {
  1413. // stream = File.Create (path);
  1414. // stream.Close ();
  1415. // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1416. // } finally {
  1417. // if (stream != null)
  1418. // stream.Close ();
  1419. // DeleteFile (path);
  1420. // }
  1421. // }
  1422. [Test]
  1423. [ExpectedException(typeof (IOException))]
  1424. #if TARGET_JVM
  1425. [Category("NotWorking")]
  1426. #endif
  1427. public void SetLastWriteTimeIOException1 ()
  1428. {
  1429. string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
  1430. DeleteFile (path);
  1431. FileStream stream = null;
  1432. try {
  1433. stream = File.Create (path);
  1434. File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1435. } finally {
  1436. if (stream != null)
  1437. stream.Close ();
  1438. DeleteFile (path);
  1439. }
  1440. }
  1441. [Test]
  1442. [ExpectedException(typeof (ArgumentNullException))]
  1443. public void SetLastWriteTimeUtcArgumentNullException1 ()
  1444. {
  1445. File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1446. }
  1447. [Test]
  1448. [ExpectedException(typeof (ArgumentException))]
  1449. public void SetCLastWriteTimeUtcArgumenException1 ()
  1450. {
  1451. File.SetLastWriteTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1452. }
  1453. [Test]
  1454. [ExpectedException(typeof (ArgumentException))]
  1455. public void SetLastWriteTimeUtcArgumenException2 ()
  1456. {
  1457. File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1458. }
  1459. [Test]
  1460. // On Unix there are no invalid path chars.
  1461. public void SetLastWriteTimeUtcArgumenException3 ()
  1462. {
  1463. if (Path.InvalidPathChars.Length > 1) {
  1464. bool pass = false;
  1465. try {
  1466. File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1467. } catch (ArgumentException) {
  1468. pass = true;
  1469. }
  1470. Assertion.Assert ("#01", pass);
  1471. }
  1472. }
  1473. [Test]
  1474. [ExpectedException(typeof (FileNotFoundException))]
  1475. public void SetLastWriteTimeUtcFileNotFoundException1 ()
  1476. {
  1477. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
  1478. DeleteFile (path);
  1479. File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1480. }
  1481. // [Test]
  1482. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1483. // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
  1484. // {
  1485. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
  1486. // DeleteFile (path);
  1487. // FileStream stream = null;
  1488. // try {
  1489. // stream = File.Create (path);
  1490. // stream.Close ();
  1491. // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1492. // } finally {
  1493. // if (stream != null)
  1494. // stream.Close ();
  1495. // DeleteFile (path);
  1496. // }
  1497. // }
  1498. //
  1499. [Test]
  1500. [ExpectedException(typeof (IOException))]
  1501. #if TARGET_JVM
  1502. [Category("NotWorking")]
  1503. #endif
  1504. public void SetLastWriteTimeUtcIOException1 ()
  1505. {
  1506. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
  1507. DeleteFile (path);
  1508. FileStream stream = null;
  1509. try {
  1510. stream = File.Create (path);
  1511. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1512. } finally {
  1513. if (stream != null)
  1514. stream.Close ();
  1515. DeleteFile (path);
  1516. }
  1517. }
  1518. [Test]
  1519. public void OpenAppend ()
  1520. {
  1521. string fn = Path.GetTempFileName ();
  1522. using (FileStream s = File.Open (fn, FileMode.Append))
  1523. ;
  1524. DeleteFile (fn);
  1525. }
  1526. #if NET_2_0
  1527. void TestRWAT (string s)
  1528. {
  1529. string f = Path.GetTempFileName ();
  1530. try {
  1531. File.WriteAllText (f, s);
  1532. string r = File.ReadAllText (f);
  1533. AssertEquals (r, s);
  1534. } finally {
  1535. DeleteFile (f);
  1536. }
  1537. }
  1538. [Test]
  1539. public void ReadWriteAllText ()
  1540. {
  1541. // The MSDN docs said something about
  1542. // not including a final new line. it looks
  1543. // like that was not true. I'm not sure what
  1544. // that was talking about
  1545. TestRWAT ("");
  1546. TestRWAT ("\r");
  1547. TestRWAT ("\n");
  1548. TestRWAT ("\r\n");
  1549. TestRWAT ("a\r");
  1550. TestRWAT ("a\n");
  1551. TestRWAT ("a\r\n");
  1552. TestRWAT ("a\ra");
  1553. TestRWAT ("a\na");
  1554. TestRWAT ("a\r\na");
  1555. TestRWAT ("a");
  1556. TestRWAT ("\r\r");
  1557. TestRWAT ("\n\n");
  1558. TestRWAT ("\r\n\r\n");
  1559. }
  1560. #endif
  1561. private void DeleteFile (string path)
  1562. {
  1563. if (File.Exists (path))
  1564. File.Delete (path);
  1565. }
  1566. }
  1567. }