FileInfoTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // FileInfoTest.cs - NUnit Test Cases for System.IO.FileInfo class
  2. //
  3. // Ville Palo ([email protected])
  4. //
  5. // (C) 2003 Ville Palo
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.IO;
  10. namespace MonoTests.System.IO
  11. {
  12. [TestFixture]
  13. public class FileInfoTest
  14. {
  15. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  16. public FileInfoTest()
  17. {
  18. if (Directory.Exists (TempFolder))
  19. Directory.Delete (TempFolder, true);
  20. Directory.CreateDirectory (TempFolder);
  21. }
  22. ~FileInfoTest ()
  23. {
  24. if (Directory.Exists (TempFolder))
  25. Directory.Delete (TempFolder, true);
  26. }
  27. [SetUp]
  28. protected void SetUp() {
  29. if (!Directory.Exists (TempFolder))
  30. Directory.CreateDirectory (TempFolder);
  31. }
  32. [TearDown]
  33. protected void TearDown() {}
  34. [Test]
  35. public void Ctr ()
  36. {
  37. string path = TempFolder + "/FIT.Ctr.Test";
  38. DeleteFile (path);
  39. FileInfo info = new FileInfo (path);
  40. Assertion.AssertEquals ("test#01", true, info.DirectoryName.EndsWith (".Tests"));
  41. Assertion.AssertEquals ("test#02", false, info.Exists);
  42. Assertion.AssertEquals ("test#03", ".Test", info.Extension);
  43. Assertion.AssertEquals ("test#05", "FIT.Ctr.Test", info.Name);
  44. }
  45. [Test]
  46. [ExpectedException(typeof(ArgumentNullException))]
  47. public void CtorArgumentNullException ()
  48. {
  49. FileInfo info = new FileInfo (null);
  50. }
  51. [Test]
  52. [ExpectedException(typeof(ArgumentException))]
  53. public void CtorArgumentException1 ()
  54. {
  55. FileInfo info = new FileInfo ("");
  56. }
  57. [Test]
  58. [ExpectedException(typeof(ArgumentException))]
  59. public void CtorArgumentException2 ()
  60. {
  61. FileInfo info = new FileInfo (" ");
  62. }
  63. [Test]
  64. [ExpectedException(typeof(ArgumentException))]
  65. public void CtorArgumentException3 ()
  66. {
  67. string path = "";
  68. foreach (char c in Path.InvalidPathChars) {
  69. path += c;
  70. }
  71. FileInfo info = new FileInfo (path);
  72. }
  73. [Test]
  74. public void DirectoryTest ()
  75. {
  76. string path = TempFolder + "/FIT.Directory.Test";
  77. DeleteFile (path);
  78. FileInfo info = new FileInfo (path);
  79. DirectoryInfo dir = info.Directory;
  80. Assertion.AssertEquals ("test#01", "MonoTests.System.IO.Tests", dir.Name);
  81. }
  82. [Test]
  83. public void Exists ()
  84. {
  85. string path = TempFolder + "/FIT.Exists.Test";
  86. DeleteFile (path);
  87. try {
  88. FileInfo info = new FileInfo (path);
  89. Assertion.AssertEquals ("test#01", false, info.Exists);
  90. File.Create (path).Close ();
  91. Assertion.AssertEquals ("test#02", false, info.Exists);
  92. info = new FileInfo (path);
  93. Assertion.AssertEquals ("test#03", true, info.Exists);
  94. } finally {
  95. DeleteFile (path);
  96. }
  97. }
  98. [Test]
  99. public void Length ()
  100. {
  101. string path = TempFolder + "/FIT.Length.Test";
  102. DeleteFile (path);
  103. try {
  104. FileStream stream = File.Create (path);
  105. FileInfo info = new FileInfo (path);
  106. Assertion.AssertEquals ("test#01", 0, info.Length);
  107. stream.WriteByte (12);
  108. stream.Flush ();
  109. Assertion.AssertEquals ("test#02", 0, info.Length);
  110. info = new FileInfo (path);
  111. Assertion.AssertEquals ("test#03", 1, info.Length);
  112. stream.Close ();
  113. } finally {
  114. DeleteFile (path);
  115. }
  116. }
  117. [Test]
  118. [ExpectedException (typeof (FileNotFoundException))]
  119. public void LengthException ()
  120. {
  121. string path = TempFolder + "/FIT.LengthException.Test";
  122. DeleteFile (path);
  123. FileInfo info = new FileInfo (path);
  124. long l = info.Length;
  125. }
  126. [Test]
  127. public void AppendText ()
  128. {
  129. string path = TempFolder + "/FIT.AppendText.Test";
  130. DeleteFile (path);
  131. try {
  132. FileInfo info = new FileInfo (path);
  133. Assertion.AssertEquals ("test#01", false, info.Exists);
  134. StreamWriter writer = info.AppendText ();
  135. info = new FileInfo (path);
  136. Assertion.AssertEquals ("test#02", true, info.Exists );
  137. writer.Write ("aaa");
  138. writer.Flush ();
  139. writer.Close ();
  140. Assertion.AssertEquals ("test#03", 0, info.Length);
  141. info = new FileInfo (path);
  142. Assertion.AssertEquals ("test#04", 3, info.Length);
  143. } finally {
  144. DeleteFile (path);
  145. }
  146. }
  147. [Test]
  148. public void CopyTo ()
  149. {
  150. string path1 = TempFolder + "/FIT.CopyTo.Source.Test";
  151. string path2 = TempFolder + "/FIT.CopyTo.Dest.Test";
  152. DeleteFile (path1);
  153. DeleteFile (path2);
  154. try {
  155. File.Create (path1).Close ();
  156. FileInfo info = new FileInfo (path1);
  157. Assertion.AssertEquals ("test#01", true, info.Exists);
  158. FileInfo info2 = info.CopyTo (path2);
  159. info = new FileInfo (path1);
  160. Assertion.AssertEquals ("test#02", true, info2.Exists);
  161. } finally {
  162. DeleteFile (path1);
  163. DeleteFile (path2);
  164. }
  165. }
  166. [Test]
  167. public void CopyTo2 ()
  168. {
  169. string path1 = TempFolder + "/FIT.CopyTo2.Source.Test";
  170. string path2 = TempFolder + "/FIT.CopyTo2.Dest.Test";
  171. DeleteFile (path1);
  172. DeleteFile (path2);
  173. try {
  174. File.Create (path1).Close ();
  175. File.Create (path2).Close ();
  176. FileInfo info = new FileInfo (path1);
  177. FileInfo info2 = info.CopyTo (path2, true);
  178. info = new FileInfo (path1);
  179. Assertion.AssertEquals ("test#01", true, info.Exists);
  180. Assertion.AssertEquals ("test#02", true, info2.Exists);
  181. } finally {
  182. DeleteFile (path1);
  183. DeleteFile (path2);
  184. }
  185. }
  186. [Test]
  187. [ExpectedException (typeof (IOException))]
  188. public void CopyToIOException ()
  189. {
  190. string path1 = TempFolder + "/FIT.CopyToException.Source.Test";
  191. string path2 = TempFolder + "/FIT.CopyToException.Dest.Test";
  192. try {
  193. DeleteFile (path1);
  194. DeleteFile (path2);
  195. File.Create (path1).Close ();
  196. File.Create (path2).Close ();
  197. FileInfo info = new FileInfo (path1);
  198. FileInfo info2 = info.CopyTo (path2);
  199. } finally {
  200. DeleteFile (path1);
  201. DeleteFile (path2);
  202. }
  203. }
  204. [Test]
  205. [ExpectedException (typeof (IOException))]
  206. public void CopyToIOException2 ()
  207. {
  208. string path1 = TempFolder + "/FIT.CopyToException.Source.Test";
  209. string path2 = TempFolder + "/FIT.CopyToException.Dest.Test";
  210. try {
  211. DeleteFile (path1);
  212. DeleteFile (path2);
  213. File.Create (path1).Close ();
  214. File.Create (path2).Close ();
  215. FileInfo info = new FileInfo (path1);
  216. FileInfo info2 = info.CopyTo (path2, false);
  217. } finally {
  218. DeleteFile (path1);
  219. DeleteFile (path2);
  220. }
  221. }
  222. [Test]
  223. [ExpectedException (typeof (ArgumentNullException))]
  224. public void CopyToArgumentNullException ()
  225. {
  226. string path = TempFolder + "/FIT.CopyToArgumentNullException.Test";
  227. DeleteFile (path);
  228. try {
  229. File.Create (path).Close ();
  230. FileInfo info = new FileInfo (path);
  231. info.CopyTo (null, false);
  232. } finally {
  233. DeleteFile (path);
  234. }
  235. }
  236. [Test]
  237. [ExpectedException (typeof (ArgumentException))]
  238. public void CopyToArgumentException1 ()
  239. {
  240. string path = TempFolder + "/FIT.CopyToArgument1Exception.Test";
  241. DeleteFile (path);
  242. try {
  243. File.Create (path).Close ();
  244. FileInfo info = new FileInfo (path);
  245. info.CopyTo ("", false);
  246. } finally {
  247. DeleteFile (path);
  248. }
  249. }
  250. [Test]
  251. [ExpectedException (typeof (ArgumentException))]
  252. public void CopyToArgumentException2 ()
  253. {
  254. string path = TempFolder + "/FIT.CopyToArgument2Exception.Test";
  255. DeleteFile (path);
  256. try {
  257. File.Create (path).Close ();
  258. FileInfo info = new FileInfo (path);
  259. info.CopyTo (" ", false);
  260. } finally {
  261. DeleteFile (path);
  262. }
  263. }
  264. [Test]
  265. [ExpectedException (typeof (ArgumentException))]
  266. public void CopyToArgumentException3 ()
  267. {
  268. string path = TempFolder + "/FIT.CopyToArgument3Exception.Test";
  269. DeleteFile (path);
  270. try {
  271. File.Create (path).Close ();
  272. FileInfo info = new FileInfo (path);
  273. info.CopyTo (" ", false);
  274. } finally {
  275. DeleteFile (path);
  276. }
  277. }
  278. [Test]
  279. [ExpectedException (typeof (ArgumentException))]
  280. public void CopyToArgumentException4 ()
  281. {
  282. string path = TempFolder + "/FIT.CopyToArgument4Exception.Test";
  283. string path2 = "";
  284. DeleteFile (path);
  285. try {
  286. File.Create (path).Close ();
  287. FileInfo info = new FileInfo (path);
  288. foreach (char c in Path.InvalidPathChars) {
  289. path2 += c;
  290. }
  291. info.CopyTo (path2, false);
  292. } finally {
  293. DeleteFile (path);
  294. }
  295. }
  296. [Test]
  297. public void Create ()
  298. {
  299. string path = TempFolder + "/FIT.Create.Test";
  300. DeleteFile (path);
  301. try {
  302. FileInfo info = new FileInfo (path);
  303. Assertion.AssertEquals ("test#01", false, info.Exists);
  304. FileStream stream = info.Create ();
  305. Assertion.AssertEquals ("test#02", false, info.Exists);
  306. info = new FileInfo (path);
  307. Assertion.AssertEquals ("test#03", true, info.Exists);
  308. Assertion.AssertEquals ("test#04", true, stream.CanRead);
  309. Assertion.AssertEquals ("test#05", true, stream.CanWrite);
  310. Assertion.AssertEquals ("test#06", true, stream.CanSeek);
  311. stream.Close ();
  312. } finally {
  313. DeleteFile (path);
  314. }
  315. }
  316. [Test]
  317. public void CreateText ()
  318. {
  319. string path = TempFolder + "/FIT.CreateText.Test";
  320. DeleteFile (path);
  321. try {
  322. FileInfo info = new FileInfo (path);
  323. Assertion.AssertEquals ("test#01", false, info.Exists);
  324. StreamWriter writer = info.CreateText ();
  325. writer.WriteLine ("test");
  326. writer.Close ();
  327. info = new FileInfo (path);
  328. Assertion.AssertEquals ("test#02", true, info.Exists);
  329. } finally {
  330. DeleteFile (path);
  331. }
  332. }
  333. [Test]
  334. [ExpectedException (typeof (UnauthorizedAccessException))]
  335. public void CreateTextUnauthorizedAccessException ()
  336. {
  337. string path = TempFolder;
  338. FileInfo info = new FileInfo (path);
  339. Assertion.AssertEquals ("test#01", false, info.Exists);
  340. StreamWriter writer = info.CreateText ();
  341. }
  342. [Test]
  343. public void Delete ()
  344. {
  345. string path = TempFolder + "/FIT.Delete.Test";
  346. DeleteFile (path);
  347. try {
  348. FileInfo info = new FileInfo (path);
  349. Assertion.AssertEquals ("test#01", false, info.Exists);
  350. info.Create ().Close ();
  351. info = new FileInfo (path);
  352. Assertion.AssertEquals ("test#02", true, info.Exists);
  353. info.Delete ();
  354. Assertion.AssertEquals ("test#03", true, info.Exists);
  355. info = new FileInfo (path);
  356. Assertion.AssertEquals ("test#04", false, info.Exists);
  357. } finally {
  358. DeleteFile (path);
  359. }
  360. }
  361. [Test]
  362. [ExpectedException (typeof (UnauthorizedAccessException))]
  363. public void DeleteUnauthorizedAccessException ()
  364. {
  365. string path = TempFolder;
  366. FileInfo info = new FileInfo (path);
  367. info.Delete ();
  368. }
  369. [Test]
  370. public void MoveTo ()
  371. {
  372. string path1 = TempFolder + "/FIT.MoveTo.Source.Test";
  373. string path2 = TempFolder + "/FIT.MoveTo.Dest.Test";
  374. DeleteFile (path1);
  375. DeleteFile (path2);
  376. try {
  377. File.Create (path1).Close ();
  378. FileInfo info = new FileInfo (path1);
  379. FileInfo info2 = new FileInfo (path2);
  380. Assertion.AssertEquals ("test#01", false, info2.Exists);
  381. info.MoveTo (path2);
  382. info2 = new FileInfo (path2);
  383. Assertion.AssertEquals ("test#02", true, info2.Exists);
  384. } finally {
  385. DeleteFile (path1);
  386. DeleteFile (path2);
  387. }
  388. }
  389. [Test]
  390. [ExpectedException (typeof (IOException))]
  391. public void MoveToIOException ()
  392. {
  393. string path1 = TempFolder + "/FIT.MoveToIOException.Source.Test";
  394. string path2 = TempFolder + "/FIT.MoveToIOException.Dest.Test";
  395. DeleteFile (path1);
  396. DeleteFile (path2);
  397. try {
  398. File.Create (path1).Close ();
  399. File.Create (path2).Close ();
  400. FileInfo info = new FileInfo (path1);
  401. info.MoveTo (path2);
  402. } finally {
  403. DeleteFile (path1);
  404. DeleteFile (path2);
  405. }
  406. }
  407. [Test]
  408. [ExpectedException (typeof (ArgumentNullException))]
  409. public void MoveToArgumentNullException ()
  410. {
  411. string path = TempFolder + "/FIT.MoveToArgumentNullException.Test";
  412. DeleteFile (path);
  413. try {
  414. File.Create (path).Close ();
  415. FileInfo info = new FileInfo (path);
  416. info.MoveTo (null);
  417. } finally {
  418. DeleteFile (path);
  419. }
  420. }
  421. [Test]
  422. [ExpectedException (typeof (ArgumentException))]
  423. public void MoveToArgumentException ()
  424. {
  425. string path = TempFolder + "/FIT.MoveToArgumentException.Test";
  426. DeleteFile (path);
  427. try {
  428. File.Create (path).Close ();
  429. FileInfo info = new FileInfo (path);
  430. info.MoveTo (" ");
  431. } finally {
  432. DeleteFile (path);
  433. }
  434. }
  435. /// <summary>
  436. /// msdn says this should throw UnauthorizedAccessException, byt
  437. /// it throws IOException.
  438. /// </summary>
  439. [Test]
  440. [ExpectedException (typeof (IOException))]
  441. public void MoveToUnauthorizedAccessException ()
  442. {
  443. string path = TempFolder + "/FIT.UnauthorizedAccessException.Test";
  444. DeleteFile (path);
  445. try {
  446. File.Create (path).Close ();
  447. FileInfo info = new FileInfo (path);
  448. info.MoveTo (TempFolder);
  449. } finally {
  450. DeleteFile (path);
  451. }
  452. }
  453. [Test]
  454. [ExpectedException (typeof (FileNotFoundException))]
  455. public void MoveToFileNotFoundException ()
  456. {
  457. string path1 = TempFolder + "/FIT.MoveToFileNotFoundException.Src";
  458. string path2 = TempFolder + "/FIT.MoveToFileNotFoundException.Dst";
  459. DeleteFile (path1);
  460. DeleteFile (path2);
  461. try {
  462. FileInfo info = new FileInfo (path1);
  463. info.MoveTo (path2);
  464. } finally {
  465. DeleteFile (path1);
  466. DeleteFile (path2);
  467. }
  468. }
  469. [Test]
  470. public void Open ()
  471. {
  472. string path = TempFolder + "/FIT.Open.Test";
  473. DeleteFile (path);
  474. FileStream stream = null;
  475. try {
  476. FileInfo info = new FileInfo (path);
  477. stream = info.Open (FileMode.CreateNew);
  478. Assertion.AssertEquals ("test#01", true, stream.CanRead);
  479. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  480. Assertion.AssertEquals ("test#03", true, stream.CanWrite);
  481. stream.Close ();
  482. stream = info.Open (FileMode.Open);
  483. Assertion.AssertEquals ("test#04", true, stream.CanRead);
  484. Assertion.AssertEquals ("test#05", true, stream.CanSeek);
  485. Assertion.AssertEquals ("test#06", true, stream.CanWrite);
  486. stream.Close ();
  487. stream = info.Open (FileMode.Append, FileAccess.Write);
  488. Assertion.AssertEquals ("test#07", false, stream.CanRead);
  489. Assertion.AssertEquals ("test#08", true, stream.CanSeek);
  490. Assertion.AssertEquals ("test#09", true, stream.CanWrite);
  491. stream.Close ();
  492. stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  493. Assertion.AssertEquals ("test#10", true, stream.CanRead);
  494. Assertion.AssertEquals ("test#11", true, stream.CanSeek);
  495. Assertion.AssertEquals ("test#12", true, stream.CanWrite);
  496. stream.Close ();
  497. } finally {
  498. if (stream != null)
  499. stream.Close ();
  500. DeleteFile (path);
  501. }
  502. }
  503. [Test]
  504. [ExpectedException(typeof(FileNotFoundException))]
  505. public void OpenFileNotFoundException ()
  506. {
  507. string path = TempFolder + "/FIT.OpenFileNotFoundException.Test";
  508. DeleteFile (path);
  509. FileInfo info = new FileInfo (path);
  510. info.Open (FileMode.Open);
  511. }
  512. [Test]
  513. public void OpenRead ()
  514. {
  515. string path = TempFolder + "/FIT.OpenRead.Test";
  516. DeleteFile (path);
  517. FileStream stream = null;
  518. try {
  519. File.Create (path).Close ();
  520. FileInfo info = new FileInfo (path);
  521. stream = info.OpenRead ();
  522. Assertion.AssertEquals ("test#01", true, stream.CanRead);
  523. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  524. Assertion.AssertEquals ("test#03", false, stream.CanWrite);
  525. stream.Close ();
  526. } finally {
  527. if (stream != null)
  528. stream.Close ();
  529. DeleteFile (path);
  530. }
  531. }
  532. [Test]
  533. [ExpectedException(typeof (IOException))]
  534. public void OpenReadIOException ()
  535. {
  536. string path = TempFolder + "/FIT.OpenReadIOException.Test";
  537. DeleteFile (path);
  538. FileStream stream = null;
  539. try {
  540. stream = File.Create (path);
  541. FileInfo info = new FileInfo (path);
  542. info.OpenRead ();
  543. } finally {
  544. if (stream != null)
  545. stream.Close ();
  546. DeleteFile (path);
  547. }
  548. }
  549. [Test]
  550. [ExpectedException(typeof (UnauthorizedAccessException))]
  551. public void OpenReadUnauthorizedAccessException ()
  552. {
  553. string path = TempFolder;
  554. FileInfo info = new FileInfo (path);
  555. info.OpenRead ();
  556. }
  557. [Test]
  558. public void OpenText ()
  559. {
  560. string path = TempFolder + "/FIT.OpenText.Test";
  561. DeleteFile (path);
  562. StreamReader reader = null;
  563. try {
  564. File.Create (path).Close ();
  565. FileInfo info = new FileInfo (path);
  566. reader = info.OpenText ();
  567. Assertion.AssertEquals ("test#01", -1, reader.Peek ());
  568. } finally {
  569. if (reader != null)
  570. reader.Close ();
  571. DeleteFile (path);
  572. }
  573. }
  574. [Test]
  575. [ExpectedException(typeof (FileNotFoundException))]
  576. public void OpenTextFileNotFoundException ()
  577. {
  578. string path = TempFolder + "/FIT.OpenTextFileNotFoundException.Test";
  579. DeleteFile (path);
  580. FileInfo info = new FileInfo (path);
  581. info.OpenText ();
  582. }
  583. [Test]
  584. [ExpectedException(typeof (UnauthorizedAccessException))]
  585. public void OpenTextUnauthorizedAccessException ()
  586. {
  587. string path = TempFolder;
  588. FileInfo info = new FileInfo (path);
  589. info.OpenText ();
  590. }
  591. [Test]
  592. public void OpenWrite ()
  593. {
  594. string path = TempFolder + "/FIT.OpenWrite.Test";
  595. DeleteFile (path);
  596. FileStream stream = null;
  597. try {
  598. File.Create (path).Close ();
  599. FileInfo info = new FileInfo (path);
  600. stream = info.OpenWrite ();
  601. Assertion.AssertEquals ("test#01", false, stream.CanRead);
  602. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  603. Assertion.AssertEquals ("test#03", true, stream.CanWrite);
  604. } finally {
  605. if (stream != null)
  606. stream.Close ();
  607. DeleteFile (path);
  608. }
  609. }
  610. [Test]
  611. [ExpectedException(typeof (UnauthorizedAccessException))]
  612. public void OpenWriteUnauthorizedAccessException ()
  613. {
  614. string path = TempFolder;
  615. FileInfo info = new FileInfo (path);
  616. info.OpenWrite ();
  617. }
  618. private void DeleteFile (string path)
  619. {
  620. if (File.Exists (path))
  621. File.Delete (path);
  622. }
  623. }
  624. }