FileInfoTest.cs 18 KB

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