FileInfoTest.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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 System;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.IO
  13. {
  14. [TestFixture]
  15. public class FileInfoTest
  16. {
  17. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  18. static readonly char DSC = Path.DirectorySeparatorChar;
  19. [SetUp]
  20. public void SetUp ()
  21. {
  22. DeleteDirectory (TempFolder);
  23. Directory.CreateDirectory (TempFolder);
  24. }
  25. [TearDown]
  26. public void TearDown ()
  27. {
  28. DeleteDirectory (TempFolder);
  29. }
  30. [Test] // ctor (String)
  31. public void Constructor1 ()
  32. {
  33. string path = TempFolder + DSC + "FIT.Ctr.Test";
  34. DeleteFile (path);
  35. FileInfo info = new FileInfo (path);
  36. Assert.IsTrue (info.DirectoryName.EndsWith (".Tests"), "#1");
  37. Assert.IsFalse (info.Exists, "#2");
  38. Assert.AreEqual (".Test", info.Extension, "#3");
  39. Assert.AreEqual ("FIT.Ctr.Test", info.Name, "#4");
  40. }
  41. [Test] // ctor (String)
  42. public void Constructor1_FileName_Null ()
  43. {
  44. try {
  45. new FileInfo (null);
  46. Assert.Fail ("#1");
  47. } catch (ArgumentNullException ex) {
  48. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  49. Assert.IsNull (ex.InnerException, "#3");
  50. Assert.IsNotNull (ex.Message, "#4");
  51. Assert.AreEqual ("fileName", ex.ParamName, "#5");
  52. }
  53. }
  54. [Test] // ctor (String)
  55. public void Constructor1_FileName_Empty ()
  56. {
  57. try {
  58. new FileInfo (string.Empty);
  59. Assert.Fail ("#1");
  60. } catch (ArgumentException ex) {
  61. // Empty file name is not legal
  62. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  63. Assert.IsNull (ex.InnerException, "#3");
  64. Assert.IsNotNull (ex.Message, "#4");
  65. Assert.IsNull (ex.ParamName, "#5");
  66. }
  67. }
  68. [Test] // ctor (String)
  69. public void Constructor1_FileName_InvalidPathChars ()
  70. {
  71. string path = string.Empty;
  72. foreach (char c in Path.InvalidPathChars)
  73. path += c;
  74. try {
  75. new FileInfo (path);
  76. } catch (ArgumentException ex) {
  77. // The path contains illegal characters
  78. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  79. Assert.IsNull (ex.InnerException, "#3");
  80. Assert.IsNotNull (ex.Message, "#4");
  81. Assert.IsNull (ex.ParamName, "#5");
  82. }
  83. }
  84. [Test] // ctor (String)
  85. public void Constructor1_FileName_Whitespace ()
  86. {
  87. try {
  88. new FileInfo (" ");
  89. Assert.Fail ("#1");
  90. } catch (ArgumentException ex) {
  91. // The path is not of a legal form
  92. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  93. Assert.IsNull (ex.InnerException, "#3");
  94. Assert.IsNotNull (ex.Message, "#4");
  95. Assert.IsNull (ex.ParamName, "#5");
  96. }
  97. }
  98. [Test]
  99. public void DirectoryTest ()
  100. {
  101. string path = TempFolder + DSC + "FIT.Directory.Test";
  102. DeleteFile (path);
  103. FileInfo info = new FileInfo (path);
  104. DirectoryInfo dir = info.Directory;
  105. Assert.AreEqual ("MonoTests.System.IO.Tests", dir.Name);
  106. }
  107. [Test]
  108. public void Exists ()
  109. {
  110. string path = TempFolder + DSC + "FIT.Exists.Test";
  111. DeleteFile (path);
  112. try {
  113. FileInfo info = new FileInfo (path);
  114. Assert.IsFalse (info.Exists, "#1");
  115. File.Create (path).Close ();
  116. Assert.IsFalse (info.Exists, "#2");
  117. info = new FileInfo (path);
  118. Assert.IsTrue (info.Exists, "#3");
  119. info = new FileInfo (TempFolder);
  120. Assert.IsFalse (info.Exists, "#4");
  121. } finally {
  122. DeleteFile (path);
  123. }
  124. }
  125. #if !MOBILE
  126. [Test]
  127. [Category ("NotWorking")]
  128. public void IsReadOnly ()
  129. {
  130. string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
  131. DeleteFile (path);
  132. try {
  133. using (FileStream stream = File.Create (path)) {
  134. stream.WriteByte (12);
  135. stream.Close ();
  136. }
  137. FileInfo info1 = new FileInfo (path);
  138. Assert.IsFalse (info1.IsReadOnly, "#1");
  139. FileInfo info2 = new FileInfo (path);
  140. info2.IsReadOnly = true;
  141. Assert.IsFalse (info1.IsReadOnly, "#2");
  142. Assert.IsTrue (info2.IsReadOnly, "#3");
  143. FileInfo info3 = new FileInfo (path);
  144. Assert.IsTrue (info3.IsReadOnly, "#4");
  145. info3.IsReadOnly = false;
  146. Assert.IsFalse (info1.IsReadOnly, "#4");
  147. Assert.IsTrue (info2.IsReadOnly, "#5");
  148. Assert.IsFalse (info3.IsReadOnly, "#6");
  149. } finally {
  150. File.SetAttributes (path, FileAttributes.Normal);
  151. DeleteFile (path);
  152. }
  153. }
  154. #endif
  155. [Test]
  156. public void Length ()
  157. {
  158. string path = TempFolder + DSC + "FIT.Length.Test";
  159. DeleteFile (path);
  160. try {
  161. FileStream stream = File.Create (path);
  162. FileInfo info = new FileInfo (path);
  163. Assert.AreEqual (0, info.Length, "#1");
  164. stream.WriteByte (12);
  165. stream.Flush ();
  166. Assert.AreEqual (0, info.Length, "#2");
  167. info = new FileInfo (path);
  168. Assert.AreEqual (1, info.Length, "#3");
  169. stream.Close ();
  170. } finally {
  171. DeleteFile (path);
  172. }
  173. }
  174. [Test]
  175. public void Length_FileDoesNotExist ()
  176. {
  177. string path = TempFolder + DSC + "FIT.LengthException.Test";
  178. DeleteFile (path);
  179. FileInfo info = new FileInfo (path);
  180. try {
  181. long l = info.Length;
  182. Assert.Fail ("#1:" + l);
  183. } catch (FileNotFoundException ex) {
  184. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  185. Assert.AreEqual (path, ex.FileName, "#3");
  186. Assert.IsNull (ex.InnerException, "#4");
  187. Assert.IsNotNull (ex.Message, "#5");
  188. }
  189. }
  190. [Test]
  191. public void AppendText ()
  192. {
  193. string path = TempFolder + DSC + "FIT.AppendText.Test";
  194. DeleteFile (path);
  195. try {
  196. FileInfo info = new FileInfo (path);
  197. Assert.IsFalse (info.Exists, "#1");
  198. StreamWriter writer = info.AppendText ();
  199. info = new FileInfo (path);
  200. Assert.IsTrue (info.Exists, "#2");
  201. writer.Write ("aaa");
  202. writer.Flush ();
  203. writer.Close ();
  204. Assert.AreEqual (0, info.Length, "#3");
  205. info = new FileInfo (path);
  206. Assert.AreEqual (3, info.Length, "#4");
  207. } finally {
  208. DeleteFile (path);
  209. }
  210. }
  211. [Test] // CopyTo (String)
  212. public void CopyTo1 ()
  213. {
  214. string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
  215. string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
  216. DeleteFile (path1);
  217. DeleteFile (path2);
  218. try {
  219. File.Create (path1).Close ();
  220. FileInfo info = new FileInfo (path1);
  221. Assert.IsTrue (info.Exists, "#1");
  222. FileInfo info2 = info.CopyTo (path2);
  223. info = new FileInfo (path1);
  224. Assert.IsTrue (info2.Exists, "#2");
  225. } finally {
  226. DeleteFile (path1);
  227. DeleteFile (path2);
  228. }
  229. }
  230. [Test] // CopyTo (String)
  231. public void CopyTo1_DestFileName_AlreadyExists ()
  232. {
  233. string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
  234. string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
  235. try {
  236. DeleteFile (path1);
  237. DeleteFile (path2);
  238. File.Create (path1).Close ();
  239. File.Create (path2).Close ();
  240. FileInfo info = new FileInfo (path1);
  241. try {
  242. info.CopyTo (path2);
  243. Assert.Fail ("#1");
  244. } catch (IOException ex) {
  245. // The file '...' already exists.
  246. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  247. Assert.IsNull (ex.InnerException, "#3");
  248. Assert.IsNotNull (ex.Message, "#4");
  249. Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
  250. }
  251. } finally {
  252. DeleteFile (path1);
  253. DeleteFile (path2);
  254. }
  255. }
  256. [Test] // CopyTo (String)
  257. public void CopyTo1_DestFileName_Null ()
  258. {
  259. string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
  260. DeleteFile (path);
  261. try {
  262. File.Create (path).Close ();
  263. FileInfo info = new FileInfo (path);
  264. try {
  265. info.CopyTo (null);
  266. Assert.Fail ("#1");
  267. } catch (ArgumentNullException ex) {
  268. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  269. Assert.IsNull (ex.InnerException, "#3");
  270. Assert.IsNotNull (ex.Message, "#4");
  271. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  272. }
  273. } finally {
  274. DeleteFile (path);
  275. }
  276. }
  277. [Test] // CopyTo (String)
  278. public void CopyTo1_DestFileName_Empty ()
  279. {
  280. string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
  281. DeleteFile (path);
  282. try {
  283. File.Create (path).Close ();
  284. FileInfo info = new FileInfo (path);
  285. try {
  286. info.CopyTo (string.Empty);
  287. Assert.Fail ("#1");
  288. } catch (ArgumentException ex) {
  289. // Empty file name is not legal
  290. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  291. Assert.IsNull (ex.InnerException, "#3");
  292. Assert.IsNotNull (ex.Message, "#4");
  293. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  294. }
  295. } finally {
  296. DeleteFile (path);
  297. }
  298. }
  299. [Test] // CopyTo (String)
  300. public void CopyTo1_DestFileName_Whitespace ()
  301. {
  302. string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
  303. DeleteFile (path);
  304. try {
  305. File.Create (path).Close ();
  306. FileInfo info = new FileInfo (path);
  307. try {
  308. info.CopyTo (" ");
  309. Assert.Fail ("#1");
  310. } catch (ArgumentException ex) {
  311. // The path is not of a legal form
  312. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  313. Assert.IsNull (ex.InnerException, "#3");
  314. Assert.IsNotNull (ex.Message, "#4");
  315. Assert.IsNull (ex.ParamName, "#5");
  316. }
  317. } finally {
  318. DeleteFile (path);
  319. }
  320. }
  321. [Test] // CopyTo (String)
  322. public void CopyTo1_DestFileName_InvalidPathChars ()
  323. {
  324. string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
  325. string path2 = string.Empty;
  326. DeleteFile (path);
  327. try {
  328. File.Create (path).Close ();
  329. FileInfo info = new FileInfo (path);
  330. foreach (char c in Path.InvalidPathChars)
  331. path2 += c;
  332. try {
  333. info.CopyTo (path2);
  334. Assert.Fail ("#1");
  335. } catch (ArgumentException ex) {
  336. // Illegal characters in path
  337. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  338. Assert.IsNull (ex.InnerException, "#3");
  339. Assert.IsNotNull (ex.Message, "#4");
  340. Assert.IsNull (ex.ParamName, "#5");
  341. }
  342. } finally {
  343. DeleteFile (path);
  344. }
  345. }
  346. [Test] // CopyTo (String, Boolean)
  347. public void CopyTo2 ()
  348. {
  349. string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
  350. string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
  351. DeleteFile (path1);
  352. DeleteFile (path2);
  353. try {
  354. File.Create (path1).Close ();
  355. File.Create (path2).Close ();
  356. FileInfo info = new FileInfo (path1);
  357. FileInfo info2 = info.CopyTo (path2, true);
  358. info = new FileInfo (path1);
  359. Assert.IsTrue (info.Exists, "#1");
  360. Assert.IsTrue (info2.Exists, "#2");
  361. } finally {
  362. DeleteFile (path1);
  363. DeleteFile (path2);
  364. }
  365. }
  366. [Test] // CopyTo (String, Boolean)
  367. public void CopyTo2_DestFileName_AlreadyExists ()
  368. {
  369. string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
  370. string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
  371. try {
  372. DeleteFile (path1);
  373. DeleteFile (path2);
  374. File.Create (path1).Close ();
  375. File.Create (path2).Close ();
  376. FileInfo info = new FileInfo (path1);
  377. try {
  378. info.CopyTo (path2, false);
  379. Assert.Fail ("#1");
  380. } catch (IOException ex) {
  381. // The file '...' already exists.
  382. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  383. Assert.IsNull (ex.InnerException, "#3");
  384. Assert.IsNotNull (ex.Message, "#4");
  385. Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
  386. }
  387. } finally {
  388. DeleteFile (path1);
  389. DeleteFile (path2);
  390. }
  391. }
  392. [Test] // CopyTo (String, Boolean)
  393. public void CopyTo2_DestFileName_Null ()
  394. {
  395. string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
  396. DeleteFile (path);
  397. try {
  398. File.Create (path).Close ();
  399. FileInfo info = new FileInfo (path);
  400. try {
  401. info.CopyTo (null, false);
  402. Assert.Fail ("#1");
  403. } catch (ArgumentNullException ex) {
  404. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  405. Assert.IsNull (ex.InnerException, "#3");
  406. Assert.IsNotNull (ex.Message, "#4");
  407. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  408. }
  409. } finally {
  410. DeleteFile (path);
  411. }
  412. }
  413. [Test] // CopyTo (String, Boolean)
  414. public void CopyTo2_DestFileName_Empty ()
  415. {
  416. string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
  417. DeleteFile (path);
  418. try {
  419. File.Create (path).Close ();
  420. FileInfo info = new FileInfo (path);
  421. try {
  422. info.CopyTo (string.Empty, false);
  423. Assert.Fail ("#1");
  424. } catch (ArgumentException ex) {
  425. // Empty file name is not legal
  426. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  427. Assert.IsNull (ex.InnerException, "#3");
  428. Assert.IsNotNull (ex.Message, "#4");
  429. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  430. }
  431. } finally {
  432. DeleteFile (path);
  433. }
  434. }
  435. [Test] // CopyTo (String, Boolean)
  436. public void CopyTo2_DestFileName_Whitespace ()
  437. {
  438. string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
  439. DeleteFile (path);
  440. try {
  441. File.Create (path).Close ();
  442. FileInfo info = new FileInfo (path);
  443. try {
  444. info.CopyTo (" ", false);
  445. Assert.Fail ("#1");
  446. } catch (ArgumentException ex) {
  447. // The path is not of a legal form
  448. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  449. Assert.IsNull (ex.InnerException, "#3");
  450. Assert.IsNotNull (ex.Message, "#4");
  451. Assert.IsNull (ex.ParamName, "#5");
  452. }
  453. } finally {
  454. DeleteFile (path);
  455. }
  456. }
  457. [Test] // CopyTo (String, Boolean)
  458. public void CopyTo2_DestFileName_InvalidPathChars ()
  459. {
  460. string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
  461. string path2 = string.Empty;
  462. DeleteFile (path);
  463. try {
  464. File.Create (path).Close ();
  465. FileInfo info = new FileInfo (path);
  466. foreach (char c in Path.InvalidPathChars)
  467. path2 += c;
  468. try {
  469. info.CopyTo (path2, false);
  470. Assert.Fail ("#1");
  471. } catch (ArgumentException ex) {
  472. // Illegal characters in path
  473. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  474. Assert.IsNull (ex.InnerException, "#3");
  475. Assert.IsNotNull (ex.Message, "#4");
  476. Assert.IsNull (ex.ParamName, "#5");
  477. }
  478. } finally {
  479. DeleteFile (path);
  480. }
  481. }
  482. [Test]
  483. public void Create ()
  484. {
  485. string path = TempFolder + DSC + "FIT.Create.Test";
  486. DeleteFile (path);
  487. try {
  488. FileInfo info = new FileInfo (path);
  489. Assert.IsFalse (info.Exists, "#1");
  490. FileStream stream = info.Create ();
  491. Assert.IsFalse (info.Exists, "#2");
  492. info = new FileInfo (path);
  493. Assert.IsTrue (info.Exists, "#3");
  494. Assert.IsTrue (stream.CanRead, "#4");
  495. Assert.IsTrue (stream.CanWrite, "#5");
  496. Assert.IsTrue (stream.CanSeek, "#6");
  497. stream.Close ();
  498. } finally {
  499. DeleteFile (path);
  500. }
  501. }
  502. [Test]
  503. public void CreateText ()
  504. {
  505. string path = TempFolder + DSC + "FIT.CreateText.Test";
  506. DeleteFile (path);
  507. try {
  508. FileInfo info = new FileInfo (path);
  509. Assert.IsFalse (info.Exists, "#1");
  510. StreamWriter writer = info.CreateText ();
  511. writer.WriteLine ("test");
  512. writer.Close ();
  513. info = new FileInfo (path);
  514. Assert.IsTrue (info.Exists, "#2");
  515. } finally {
  516. DeleteFile (path);
  517. }
  518. }
  519. [Test]
  520. public void CreateText_Directory ()
  521. {
  522. FileInfo info = new FileInfo (TempFolder);
  523. try {
  524. info.CreateText ();
  525. Assert.Fail ("#1");
  526. } catch (UnauthorizedAccessException ex) {
  527. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  528. Assert.IsNull (ex.InnerException, "#3");
  529. Assert.IsNotNull (ex.Message, "#4");
  530. }
  531. }
  532. [Test]
  533. public void Delete ()
  534. {
  535. string path = TempFolder + DSC + "FIT.Delete.Test";
  536. DeleteFile (path);
  537. try {
  538. FileInfo info = new FileInfo (path);
  539. Assert.IsFalse (info.Exists, "#1");
  540. info.Create ().Close ();
  541. info = new FileInfo (path);
  542. Assert.IsTrue (info.Exists, "#2");
  543. info.Delete ();
  544. Assert.IsTrue (info.Exists, "#3");
  545. info = new FileInfo (path);
  546. Assert.IsFalse (info.Exists, "#4");
  547. } finally {
  548. DeleteFile (path);
  549. }
  550. }
  551. [Test]
  552. public void Delete_Directory ()
  553. {
  554. FileInfo info = new FileInfo (TempFolder);
  555. try {
  556. info.Delete ();
  557. Assert.Fail ("#1");
  558. } catch (UnauthorizedAccessException ex) {
  559. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  560. Assert.IsNull (ex.InnerException, "#3");
  561. Assert.IsNotNull (ex.Message, "#4");
  562. }
  563. }
  564. [Test]
  565. public void MoveTo ()
  566. {
  567. string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
  568. string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
  569. DeleteFile (path1);
  570. DeleteFile (path2);
  571. try {
  572. File.Create (path1).Close ();
  573. FileInfo info1 = new FileInfo (path1);
  574. FileInfo info2 = new FileInfo (path2);
  575. Assert.IsTrue (info1.Exists, "#A1");
  576. Assert.AreEqual (path1, info1.FullName, "#A2");
  577. Assert.IsFalse (info2.Exists, "#A3");
  578. Assert.AreEqual (path2, info2.FullName, "#A4");
  579. info1.MoveTo (path2);
  580. info2 = new FileInfo (path2);
  581. Assert.IsTrue (info1.Exists, "#B1");
  582. Assert.AreEqual (path2, info1.FullName, "#B2");
  583. Assert.IsTrue (info2.Exists, "#B3");
  584. Assert.AreEqual (path2, info2.FullName, "#B4");
  585. } finally {
  586. DeleteFile (path1);
  587. DeleteFile (path2);
  588. }
  589. }
  590. [Test] //Covers #18361
  591. public void MoveTo_SameName ()
  592. {
  593. string name = "FIT.MoveTo.SameName.Test";
  594. string path1 = TempFolder + DSC + name;
  595. string path2 = name;
  596. DeleteFile (path1);
  597. DeleteFile (path2);
  598. try {
  599. File.Create (path1).Close ();
  600. FileInfo info1 = new FileInfo (path1);
  601. FileInfo info2 = new FileInfo (path2);
  602. Assert.IsTrue (info1.Exists, "#A1");
  603. Assert.IsFalse (info2.Exists, "#A2");
  604. info1.MoveTo (path2);
  605. info1 = new FileInfo (path1);
  606. info2 = new FileInfo (path2);
  607. Assert.IsFalse (info1.Exists, "#B1");
  608. Assert.IsTrue (info2.Exists, "#B2");
  609. } finally {
  610. DeleteFile (path1);
  611. DeleteFile (path2);
  612. }
  613. }
  614. [Test]
  615. public void MoveTo_DestFileName_AlreadyExists ()
  616. {
  617. string sourceFile = TempFolder + DSC + "FIT.MoveTo.Source.Test";
  618. string destFile;
  619. FileInfo info;
  620. // move to same directory
  621. File.Create (sourceFile).Close ();
  622. info = new FileInfo (sourceFile);
  623. try {
  624. info.MoveTo (TempFolder);
  625. Assert.Fail ("#A1");
  626. } catch (IOException ex) {
  627. // Cannot create a file when that file already exists
  628. Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
  629. Assert.IsNull (ex.InnerException, "#A3");
  630. Assert.IsNotNull (ex.Message, "#A4");
  631. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
  632. Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
  633. } finally {
  634. DeleteFile (sourceFile);
  635. }
  636. // move to exist file
  637. File.Create (sourceFile).Close ();
  638. destFile = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
  639. File.Create (destFile).Close ();
  640. info = new FileInfo (sourceFile);
  641. try {
  642. info.MoveTo (destFile);
  643. Assert.Fail ("#B1");
  644. } catch (IOException ex) {
  645. // Cannot create a file when that file already exists
  646. Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
  647. Assert.IsNull (ex.InnerException, "#B3");
  648. Assert.IsNotNull (ex.Message, "#B4");
  649. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
  650. Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
  651. } finally {
  652. DeleteFile (sourceFile);
  653. DeleteFile (destFile);
  654. }
  655. // move to existing directory
  656. File.Create (sourceFile).Close ();
  657. destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
  658. Directory.CreateDirectory (destFile);
  659. info = new FileInfo (sourceFile);
  660. try {
  661. info.MoveTo (destFile);
  662. Assert.Fail ("#C1");
  663. } catch (IOException ex) {
  664. // Cannot create a file when that file already exists
  665. Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
  666. Assert.IsNull (ex.InnerException, "#C3");
  667. Assert.IsNotNull (ex.Message, "#C4");
  668. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
  669. Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
  670. } finally {
  671. DeleteFile (sourceFile);
  672. DeleteDirectory (destFile);
  673. }
  674. }
  675. [Test]
  676. public void MoveTo_DestFileName_DirectoryDoesNotExist ()
  677. {
  678. string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
  679. string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
  680. DeleteFile (sourceFile);
  681. try {
  682. File.Create (sourceFile).Close ();
  683. FileInfo info = new FileInfo (sourceFile);
  684. try {
  685. info.MoveTo (destFile);
  686. Assert.Fail ("#1");
  687. } catch (FileNotFoundException ex) {
  688. // Could not find a part of the path
  689. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  690. Assert.IsNull (ex.InnerException, "#3");
  691. Assert.IsNotNull (ex.Message, "#4");
  692. }
  693. } finally {
  694. DeleteFile (sourceFile);
  695. }
  696. }
  697. [Test]
  698. public void MoveTo_DestFileName_Null ()
  699. {
  700. string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
  701. DeleteFile (path);
  702. try {
  703. File.Create (path).Close ();
  704. FileInfo info = new FileInfo (path);
  705. try {
  706. info.MoveTo (null);
  707. Assert.Fail ("#1");
  708. } catch (ArgumentNullException ex) {
  709. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  710. Assert.IsNull (ex.InnerException, "#3");
  711. Assert.IsNotNull (ex.Message, "#4");
  712. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  713. }
  714. } finally {
  715. DeleteFile (path);
  716. }
  717. }
  718. [Test]
  719. public void MoveTo_DestFileName_Empty ()
  720. {
  721. string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
  722. DeleteFile (path);
  723. try {
  724. File.Create (path).Close ();
  725. FileInfo info = new FileInfo (path);
  726. try {
  727. info.MoveTo (string.Empty);
  728. Assert.Fail ("#1");
  729. } catch (ArgumentException ex) {
  730. // Empty file name is not legal
  731. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  732. Assert.IsNull (ex.InnerException, "#3");
  733. Assert.IsNotNull (ex.Message, "#4");
  734. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  735. }
  736. } finally {
  737. DeleteFile (path);
  738. }
  739. }
  740. [Test]
  741. public void MoveTo_DestFileName_Whitespace ()
  742. {
  743. string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
  744. DeleteFile (path);
  745. try {
  746. File.Create (path).Close ();
  747. FileInfo info = new FileInfo (path);
  748. try {
  749. info.MoveTo (" ");
  750. Assert.Fail ("#1");
  751. } catch (ArgumentException ex) {
  752. // The path is not of a legal form
  753. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  754. Assert.IsNull (ex.InnerException, "#3");
  755. Assert.IsNotNull (ex.Message, "#4");
  756. Assert.IsNull (ex.ParamName, "#5");
  757. }
  758. } finally {
  759. DeleteFile (path);
  760. }
  761. }
  762. [Test]
  763. public void MoveTo_FileDoesNotExist ()
  764. {
  765. string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
  766. string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
  767. DeleteFile (path1);
  768. DeleteFile (path2);
  769. try {
  770. FileInfo info = new FileInfo (path1);
  771. try {
  772. info.MoveTo (path2);
  773. Assert.Fail ("#1");
  774. } catch (FileNotFoundException ex) {
  775. // Unable to find the specified file
  776. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  777. Assert.IsNull (ex.FileName, "#2");
  778. Assert.IsNull (ex.InnerException, "#3");
  779. Assert.IsNotNull (ex.Message, "#4");
  780. }
  781. } finally {
  782. DeleteFile (path1);
  783. DeleteFile (path2);
  784. }
  785. }
  786. [Test]
  787. public void MoveTo_Same ()
  788. {
  789. string path = TempFolder + DSC + "FIT.MoveToSame.Test";
  790. DeleteFile (path);
  791. try {
  792. File.Create (path).Close ();
  793. FileInfo info = new FileInfo (path);
  794. info.MoveTo (path);
  795. Assert.IsTrue (info.Exists, "#1");
  796. Assert.IsTrue (File.Exists (path), "#2");
  797. } finally {
  798. DeleteFile (path);
  799. }
  800. }
  801. [Test] //Covers #38796
  802. public void ToStringAfterMoveTo ()
  803. {
  804. string name1 = "FIT.ToStringAfterMoveTo.Test";
  805. string name2 = "FIT.ToStringAfterMoveTo.Test.Alt";
  806. string path1 = TempFolder + DSC + name1;
  807. string path2 = TempFolder + DSC + name2;
  808. DeleteFile (path1);
  809. DeleteFile (path2);
  810. try {
  811. File.Create (path1).Close ();
  812. FileInfo info = new FileInfo (path1);
  813. Assert.AreEqual (path1, info.ToString (), "#A");
  814. info.MoveTo (path2);
  815. Assert.AreEqual (path2, info.ToString (), "#B");
  816. } finally {
  817. DeleteFile (path1);
  818. DeleteFile (path2);
  819. }
  820. }
  821. #if !MOBILE
  822. [Test]
  823. public void Replace1 ()
  824. {
  825. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  826. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  827. string path3 = TempFolder + DSC + "FIT.Replace.Back.Test";
  828. DeleteFile (path1);
  829. DeleteFile (path2);
  830. DeleteFile (path3);
  831. try {
  832. File.Create (path1).Close ();
  833. File.Create (path2).Close ();
  834. File.Create (path3).Close ();
  835. FileInfo info = new FileInfo (path1);
  836. Assert.IsTrue (info.Exists, "#1");
  837. FileInfo info2 = info.Replace (path2, path3);
  838. Assert.IsTrue (info2.Exists, "#2");
  839. FileInfo info3 = new FileInfo (path3);
  840. Assert.IsTrue (info3.Exists, "#3");
  841. } finally {
  842. DeleteFile (path2);
  843. DeleteFile (path3);
  844. }
  845. }
  846. [Test]
  847. public void Replace1_Backup_Null ()
  848. {
  849. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  850. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  851. DeleteFile (path1);
  852. DeleteFile (path2);
  853. try {
  854. File.Create (path1).Close ();
  855. File.Create (path2).Close ();
  856. FileInfo info = new FileInfo (path1);
  857. Assert.IsTrue (info.Exists, "#1");
  858. FileInfo info2 = info.Replace (path2, null);
  859. Assert.IsTrue (info2.Exists, "#2");
  860. info = new FileInfo (path1);
  861. Assert.IsFalse (info.Exists, "#3");
  862. } finally {
  863. DeleteFile (path2);
  864. }
  865. }
  866. [Test]
  867. public void Replace1_DestFileName_Null ()
  868. {
  869. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  870. DeleteFile (path1);
  871. try {
  872. try {
  873. File.Create (path1).Close ();
  874. FileInfo info = new FileInfo (path1);
  875. info.Replace (null, null);
  876. Assert.Fail ("#1");
  877. } catch (ArgumentNullException ex) {
  878. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  879. Assert.IsNull (ex.InnerException, "#3");
  880. Assert.IsNotNull (ex.Message, "#4");
  881. }
  882. } finally {
  883. DeleteFile (path1);
  884. }
  885. }
  886. [Test]
  887. public void Replace1_DestFileName_Empty ()
  888. {
  889. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  890. DeleteFile (path1);
  891. try {
  892. try {
  893. File.Create (path1).Close ();
  894. FileInfo info = new FileInfo (path1);
  895. info.Replace (string.Empty, null);
  896. Assert.Fail ("#1");
  897. } catch (ArgumentException ex) {
  898. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  899. Assert.IsNull (ex.InnerException, "#3");
  900. Assert.IsNotNull (ex.Message, "#4");
  901. }
  902. } finally {
  903. DeleteFile (path1);
  904. }
  905. }
  906. [Test]
  907. public void Replace1_DestFileName_WhiteSpace ()
  908. {
  909. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  910. DeleteFile (path1);
  911. try {
  912. try {
  913. File.Create (path1).Close ();
  914. FileInfo info = new FileInfo (path1);
  915. info.Replace (" ", null);
  916. Assert.Fail ("#1");
  917. } catch (ArgumentException ex) {
  918. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  919. Assert.IsNull (ex.InnerException, "#3");
  920. Assert.IsNotNull (ex.Message, "#4");
  921. }
  922. } finally {
  923. DeleteFile (path1);
  924. }
  925. }
  926. [Test]
  927. public void Replace1_DestFileName_InvalidPathChars ()
  928. {
  929. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  930. string path2 = string.Empty;
  931. DeleteFile (path1);
  932. try {
  933. File.Create (path1).Close ();
  934. FileInfo info = new FileInfo (path1);
  935. foreach (char c in Path.InvalidPathChars)
  936. path2 += c;
  937. try {
  938. info.Replace (path2, null);
  939. Assert.Fail ("#1");
  940. } catch (ArgumentException ex) {
  941. // Illegal characters in path
  942. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  943. Assert.IsNull (ex.InnerException, "#3");
  944. Assert.IsNotNull (ex.Message, "#4");
  945. Assert.IsNull (ex.ParamName, "#5");
  946. }
  947. } finally {
  948. DeleteFile (path1);
  949. }
  950. }
  951. [Test]
  952. public void Replace1_DestFileName_FileNotFound ()
  953. {
  954. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  955. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  956. DeleteFile (path1);
  957. DeleteFile (path2);
  958. try {
  959. try {
  960. File.Create (path1).Close ();
  961. FileInfo info = new FileInfo (path1);
  962. info.Replace (path2, null);
  963. Assert.Fail ("#1");
  964. } catch (FileNotFoundException ex) {
  965. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  966. Assert.IsNull (ex.InnerException, "#3");
  967. Assert.IsNotNull (ex.Message, "#4");
  968. }
  969. } finally {
  970. DeleteFile (path1);
  971. }
  972. }
  973. [Test]
  974. public void Replace1_DestFileName_IsReadOnly ()
  975. {
  976. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  977. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  978. DeleteFile (path1);
  979. DeleteFile (path2);
  980. try {
  981. try {
  982. File.Create (path1).Close ();
  983. File.Create (path2).Close ();
  984. File.SetAttributes (path2, FileAttributes.ReadOnly);
  985. FileInfo info = new FileInfo (path1);
  986. info.Replace (path2, null);
  987. Assert.Fail ("#1");
  988. } catch (UnauthorizedAccessException ex) {
  989. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  990. Assert.IsNull (ex.InnerException, "#3");
  991. Assert.IsNotNull (ex.Message, "#4");
  992. }
  993. } finally {
  994. File.SetAttributes (path2, FileAttributes.Normal);
  995. DeleteFile (path1);
  996. DeleteFile (path2);
  997. }
  998. }
  999. [Test]
  1000. public void Replace1_Source_FileNotFound ()
  1001. {
  1002. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  1003. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  1004. DeleteFile (path2);
  1005. try {
  1006. try {
  1007. File.Create (path2).Close ();
  1008. FileInfo info = new FileInfo (path1);
  1009. info.Replace (path2, null);
  1010. Assert.Fail ("#1");
  1011. } catch (FileNotFoundException ex) {
  1012. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1013. Assert.IsNull (ex.InnerException, "#3");
  1014. Assert.IsNotNull (ex.Message, "#4");
  1015. }
  1016. } finally {
  1017. DeleteFile (path2);
  1018. }
  1019. }
  1020. #endif
  1021. [Test]
  1022. public void Open ()
  1023. {
  1024. string path = TempFolder + DSC + "FIT.Open.Test";
  1025. DeleteFile (path);
  1026. FileStream stream = null;
  1027. try {
  1028. FileInfo info = new FileInfo (path);
  1029. stream = info.Open (FileMode.CreateNew);
  1030. Assert.IsTrue (stream.CanRead, "#A1");
  1031. Assert.IsTrue (stream.CanSeek, "#A2");
  1032. Assert.IsTrue (stream.CanWrite, "#A3");
  1033. stream.Close ();
  1034. stream = info.Open (FileMode.Open);
  1035. Assert.IsTrue (stream.CanRead, "#B1");
  1036. Assert.IsTrue (stream.CanSeek, "#B2");
  1037. Assert.IsTrue (stream.CanWrite, "#B3");
  1038. stream.Close ();
  1039. stream = info.Open (FileMode.Append, FileAccess.Write);
  1040. Assert.IsFalse (stream.CanRead, "#C1");
  1041. Assert.IsTrue (stream.CanSeek, "#C2");
  1042. Assert.IsTrue (stream.CanWrite, "#C3");
  1043. stream.Close ();
  1044. stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  1045. Assert.IsTrue (stream.CanRead, "#D1");
  1046. Assert.IsTrue (stream.CanSeek, "#D2");
  1047. Assert.IsTrue (stream.CanWrite, "#D3");
  1048. stream.Close ();
  1049. } finally {
  1050. if (stream != null)
  1051. stream.Close ();
  1052. DeleteFile (path);
  1053. }
  1054. }
  1055. [Test]
  1056. public void Open_FileDoesNotExist ()
  1057. {
  1058. string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
  1059. DeleteFile (path);
  1060. FileInfo info = new FileInfo (path);
  1061. try {
  1062. info.Open (FileMode.Open);
  1063. Assert.Fail ("#1");
  1064. } catch (FileNotFoundException ex) {
  1065. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1066. Assert.AreEqual (path, ex.FileName, "#3");
  1067. Assert.IsNull (ex.InnerException, "#4");
  1068. Assert.IsNotNull (ex.Message, "#5");
  1069. }
  1070. }
  1071. [Test]
  1072. public void OpenRead ()
  1073. {
  1074. string path = TempFolder + DSC + "FIT.OpenRead.Test";
  1075. DeleteFile (path);
  1076. FileStream stream = null;
  1077. try {
  1078. File.Create (path).Close ();
  1079. FileInfo info = new FileInfo (path);
  1080. stream = info.OpenRead ();
  1081. Assert.IsTrue (stream.CanRead, "#1");
  1082. Assert.IsTrue (stream.CanSeek, "#2");
  1083. Assert.IsFalse (stream.CanWrite, "#3");
  1084. stream.Close ();
  1085. } finally {
  1086. if (stream != null)
  1087. stream.Close ();
  1088. DeleteFile (path);
  1089. }
  1090. }
  1091. [Test]
  1092. public void OpenRead_FileLock ()
  1093. {
  1094. string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
  1095. DeleteFile (path);
  1096. FileStream stream = null;
  1097. try {
  1098. stream = File.Create (path);
  1099. FileInfo info = new FileInfo (path);
  1100. try {
  1101. info.OpenRead ();
  1102. Assert.Fail ("#1");
  1103. } catch (IOException ex) {
  1104. // The process cannot access the file because
  1105. // it is being used by another process
  1106. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  1107. Assert.IsNull (ex.InnerException, "#3");
  1108. Assert.IsNotNull (ex.Message, "#4");
  1109. }
  1110. } finally {
  1111. if (stream != null)
  1112. stream.Close ();
  1113. DeleteFile (path);
  1114. }
  1115. }
  1116. [Test]
  1117. public void OpenRead_Directory ()
  1118. {
  1119. FileInfo info = new FileInfo (TempFolder);
  1120. try {
  1121. info.OpenRead ();
  1122. Assert.Fail ("#1");
  1123. } catch (UnauthorizedAccessException ex) {
  1124. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  1125. Assert.IsNull (ex.InnerException, "#3");
  1126. Assert.IsNotNull (ex.Message, "#4");
  1127. }
  1128. }
  1129. [Test]
  1130. public void OpenText ()
  1131. {
  1132. string path = TempFolder + DSC + "FIT.OpenText.Test";
  1133. DeleteFile (path);
  1134. StreamReader reader = null;
  1135. try {
  1136. File.Create (path).Close ();
  1137. FileInfo info = new FileInfo (path);
  1138. reader = info.OpenText ();
  1139. Assert.AreEqual (-1, reader.Peek ());
  1140. } finally {
  1141. if (reader != null)
  1142. reader.Close ();
  1143. DeleteFile (path);
  1144. }
  1145. }
  1146. [Test]
  1147. public void OpenText_FileDoesNotExist ()
  1148. {
  1149. string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
  1150. DeleteFile (path);
  1151. FileInfo info = new FileInfo (path);
  1152. try {
  1153. info.OpenText ();
  1154. Assert.Fail ("#1");
  1155. } catch (FileNotFoundException ex) {
  1156. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1157. Assert.AreEqual (path, ex.FileName, "#3");
  1158. Assert.IsNull (ex.InnerException, "#4");
  1159. Assert.IsNotNull (ex.Message, "#5");
  1160. }
  1161. }
  1162. [Test]
  1163. public void OpenText_Directory ()
  1164. {
  1165. FileInfo info = new FileInfo (TempFolder);
  1166. try {
  1167. info.OpenText ();
  1168. Assert.Fail ("#1");
  1169. } catch (UnauthorizedAccessException ex) {
  1170. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  1171. Assert.IsNull (ex.InnerException, "#3");
  1172. Assert.IsNotNull (ex.Message, "#4");
  1173. }
  1174. }
  1175. [Test]
  1176. public void OpenWrite ()
  1177. {
  1178. string path = TempFolder + DSC + "FIT.OpenWrite.Test";
  1179. DeleteFile (path);
  1180. FileStream stream = null;
  1181. try {
  1182. File.Create (path).Close ();
  1183. FileInfo info = new FileInfo (path);
  1184. stream = info.OpenWrite ();
  1185. Assert.IsFalse (stream.CanRead, "#1");
  1186. Assert.IsTrue (stream.CanSeek, "#2");
  1187. Assert.IsTrue (stream.CanWrite, "#3");
  1188. } finally {
  1189. if (stream != null)
  1190. stream.Close ();
  1191. DeleteFile (path);
  1192. }
  1193. }
  1194. [Test]
  1195. public void OpenWrite_Directory ()
  1196. {
  1197. FileInfo info = new FileInfo (TempFolder);
  1198. try {
  1199. info.OpenWrite ();
  1200. Assert.Fail ("#1");
  1201. } catch (UnauthorizedAccessException ex) {
  1202. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  1203. Assert.IsNull (ex.InnerException, "#3");
  1204. Assert.IsNotNull (ex.Message, "#4");
  1205. }
  1206. }
  1207. #if !MOBILE
  1208. [Test]
  1209. public void Serialization ()
  1210. {
  1211. FileInfo info;
  1212. SerializationInfo si;
  1213. info = new FileInfo ("Test");
  1214. si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
  1215. info.GetObjectData (si, new StreamingContext ());
  1216. Assert.AreEqual (2, si.MemberCount, "#A1");
  1217. Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
  1218. Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
  1219. info = new FileInfo (TempFolder);
  1220. si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
  1221. info.GetObjectData (si, new StreamingContext ());
  1222. Assert.AreEqual (2, si.MemberCount, "#B1");
  1223. Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
  1224. Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
  1225. }
  1226. [Test]
  1227. public void Deserialization ()
  1228. {
  1229. FileInfo info = new FileInfo ("Test");
  1230. MemoryStream ms = new MemoryStream ();
  1231. BinaryFormatter bf = new BinaryFormatter ();
  1232. bf.Serialize (ms, info);
  1233. ms.Position = 0;
  1234. FileInfo clone = (FileInfo) bf.Deserialize (ms);
  1235. Assert.AreEqual (info.Name, clone.Name, "#1");
  1236. Assert.AreEqual (info.FullName, clone.FullName, "#2");
  1237. }
  1238. #endif
  1239. [Test]
  1240. [Category ("MobileNotWorking")]
  1241. public void ToStringVariety ()
  1242. {
  1243. Assert.AreEqual ("foo", new FileInfo ("foo").ToString ());
  1244. Assert.AreEqual ("c:/foo", new FileInfo ("c:/foo").ToString ());
  1245. Assert.AreEqual ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
  1246. Assert.AreEqual ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
  1247. Assert.AreEqual ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
  1248. Assert.AreEqual ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
  1249. Assert.AreEqual ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
  1250. Assert.AreEqual ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
  1251. }
  1252. void DeleteFile (string path)
  1253. {
  1254. if (File.Exists (path))
  1255. File.Delete (path);
  1256. }
  1257. void DeleteDirectory (string path)
  1258. {
  1259. if (Directory.Exists (path))
  1260. Directory.Delete (path, true);
  1261. }
  1262. }
  1263. }