FileTest.cs 47 KB

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