FileInfoTest.cs 18 KB

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