DirectoryInfoTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo 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 DirectoryInfoTest
  14. {
  15. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  16. public DirectoryInfoTest()
  17. {
  18. if (Directory.Exists (TempFolder))
  19. Directory.Delete (TempFolder, true);
  20. Directory.CreateDirectory (TempFolder);
  21. }
  22. ~DirectoryInfoTest ()
  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 + "/DIT.Ctr.Test";
  38. DeleteDir (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", "DIT.Ctr.Test", info.Name);
  44. }
  45. [Test]
  46. [ExpectedException(typeof(ArgumentNullException))]
  47. public void CtorArgumentNullException ()
  48. {
  49. DirectoryInfo info = new DirectoryInfo (null);
  50. }
  51. [Test]
  52. [ExpectedException(typeof(ArgumentException))]
  53. public void CtorArgumentException1 ()
  54. {
  55. DirectoryInfo info = new DirectoryInfo ("");
  56. }
  57. [Test]
  58. [ExpectedException(typeof(ArgumentException))]
  59. public void CtorArgumentException2 ()
  60. {
  61. DirectoryInfo info = new DirectoryInfo (" ");
  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. DirectoryInfo info = new DirectoryInfo (path);
  72. }
  73. [Test]
  74. public void Exists ()
  75. {
  76. string path = TempFolder + "/DIT.Exists.Test";
  77. DeleteDir (path);
  78. try {
  79. DirectoryInfo info = new DirectoryInfo (path);
  80. Assertion.AssertEquals ("test#01", false, info.Exists);
  81. Directory.CreateDirectory (path);
  82. Assertion.AssertEquals ("test#02", false, info.Exists);
  83. info = new DirectoryInfo (path);
  84. Assertion.AssertEquals ("test#03", true, info.Exists);
  85. } finally {
  86. DeleteDir (path);
  87. }
  88. }
  89. [Test]
  90. public void Name ()
  91. {
  92. string path = TempFolder + "/DIT.Name.Test";
  93. DeleteDir (path);
  94. try {
  95. DirectoryInfo info = new DirectoryInfo (path);
  96. Assertion.AssertEquals ("test#01", "DIT.Name.Test", info.Name);
  97. info = Directory.CreateDirectory (path);
  98. Assertion.AssertEquals ("test#02", "DIT.Name.Test", info.Name);
  99. } finally {
  100. DeleteDir (path);
  101. }
  102. }
  103. [Test]
  104. public void Parent ()
  105. {
  106. string path = TempFolder + "/DIT.Parent.Test";
  107. DeleteDir (path);
  108. try {
  109. DirectoryInfo info = new DirectoryInfo (path);
  110. Assertion.AssertEquals ("test#01", "MonoTests.System.IO.Tests", info.Parent.Name);
  111. info = Directory.CreateDirectory (path);
  112. Assertion.AssertEquals ("test#02", "MonoTests.System.IO.Tests", info.Parent.Name);
  113. } finally {
  114. DeleteDir (path);
  115. }
  116. }
  117. [Test]
  118. public void Create ()
  119. {
  120. string path = TempFolder + "/DIT.Create.Test";
  121. DeleteDir (path);
  122. try {
  123. DirectoryInfo info = new DirectoryInfo (path);
  124. Assertion.AssertEquals ("test#01", false, info.Exists);
  125. info.Create ();
  126. Assertion.AssertEquals ("test#02", false, info.Exists);
  127. info = new DirectoryInfo (path);
  128. Assertion.AssertEquals ("test#03", true, info.Exists);
  129. } finally {
  130. DeleteDir (path);
  131. }
  132. }
  133. [Test]
  134. public void Delete1 ()
  135. {
  136. string path = TempFolder + "/DIT.Delete1.Test";
  137. DeleteDir (path);
  138. try {
  139. Directory.CreateDirectory (path);
  140. DirectoryInfo info = new DirectoryInfo (path);
  141. Assertion.AssertEquals ("test#01", true, info.Exists);
  142. info.Delete ();
  143. Assertion.AssertEquals ("test#02", true, info.Exists);
  144. info = new DirectoryInfo (path);
  145. Assertion.AssertEquals ("test#03", false, info.Exists);
  146. } finally {
  147. DeleteDir (path);
  148. }
  149. }
  150. [Test]
  151. public void Delete2 ()
  152. {
  153. string path = TempFolder + "/DIT.Delete2.Test";
  154. DeleteDir (path);
  155. try {
  156. Directory.CreateDirectory (path);
  157. File.Create (path + "/test").Close ();
  158. DirectoryInfo info = new DirectoryInfo (path);
  159. Assertion.AssertEquals ("test#01", true, info.Exists);
  160. info.Delete (true);
  161. Assertion.AssertEquals ("test#02", true, info.Exists);
  162. info = new DirectoryInfo (path);
  163. Assertion.AssertEquals ("test#03", false, info.Exists);
  164. } finally {
  165. DeleteDir (path);
  166. }
  167. }
  168. [Test]
  169. [ExpectedException (typeof (IOException))]
  170. public void DeleteIOException1 ()
  171. {
  172. string path = TempFolder + "/DIT.DeleteIOException1.Test";
  173. DeleteDir (path);
  174. try {
  175. Directory.CreateDirectory (path);
  176. File.Create (path + "/test").Close ();
  177. DirectoryInfo info = new DirectoryInfo (path);
  178. info.Delete ();
  179. } finally {
  180. DeleteDir (path);
  181. }
  182. }
  183. [Test]
  184. [ExpectedException (typeof (IOException))]
  185. public void DeleteIOException2 ()
  186. {
  187. string path = TempFolder + "/DIT.DeleteIOException2.Test";
  188. DeleteDir (path);
  189. try {
  190. Directory.CreateDirectory (path);
  191. File.Create (path + "/test").Close ();
  192. DirectoryInfo info = new DirectoryInfo (path);
  193. info.Delete (false);
  194. } finally {
  195. DeleteDir (path);
  196. }
  197. }
  198. [Test]
  199. public void GetDirectories1 ()
  200. {
  201. string path = TempFolder + "/DIT.GetDirectories1.Test";
  202. try {
  203. DirectoryInfo info = Directory.CreateDirectory (path);
  204. Assertion.AssertEquals ("test#01", 0, info.GetDirectories ().Length);
  205. Directory.CreateDirectory (path + "/" + "1");
  206. Directory.CreateDirectory (path + "/" + "2");
  207. File.Create (path + "/" + "filetest").Close ();
  208. Assertion.AssertEquals ("test#02", 2, info.GetDirectories ().Length);
  209. Directory.Delete (path + "/" + 2);
  210. Assertion.AssertEquals ("test#02", 1, info.GetDirectories ().Length);
  211. } finally {
  212. DeleteDir (path);
  213. }
  214. }
  215. [Test]
  216. public void GetDirectories2 ()
  217. {
  218. string path = TempFolder + "/DIT.GetDirectories2.Test";
  219. try {
  220. DirectoryInfo info = Directory.CreateDirectory (path);
  221. Assertion.AssertEquals ("test#01", 0, info.GetDirectories ("*").Length);
  222. Directory.CreateDirectory (path + "/" + "test120");
  223. Directory.CreateDirectory (path + "/" + "test210");
  224. Directory.CreateDirectory (path + "/" + "atest330");
  225. Directory.CreateDirectory (path + "/" + "test220");
  226. File.Create (path + "/" + "filetest").Close ();
  227. Assertion.AssertEquals ("test#02", 4, info.GetDirectories ("*").Length);
  228. Assertion.AssertEquals ("test#03", 3, info.GetDirectories ("test*").Length);
  229. Assertion.AssertEquals ("test#04", 2, info.GetDirectories ("test?20").Length);
  230. Assertion.AssertEquals ("test#05", 0, info.GetDirectories ("test?").Length);
  231. Assertion.AssertEquals ("test#06", 0, info.GetDirectories ("test[12]*").Length);
  232. Assertion.AssertEquals ("test#07", 2, info.GetDirectories ("test2*0").Length);
  233. Assertion.AssertEquals ("test#08", 4, info.GetDirectories ("*test*").Length);
  234. } finally {
  235. DeleteDir (path);
  236. }
  237. }
  238. [Test]
  239. [ExpectedException (typeof (DirectoryNotFoundException))]
  240. public void GetDirectoriesDirectoryNotFoundException1 ()
  241. {
  242. string path = TempFolder + "/DIT.GetDirectoriesDirectoryNotFoundException1.Test";
  243. DeleteDir (path);
  244. try {
  245. DirectoryInfo info = new DirectoryInfo (path);
  246. info.GetDirectories ();
  247. } finally {
  248. DeleteDir (path);
  249. }
  250. }
  251. [Test]
  252. [ExpectedException (typeof (DirectoryNotFoundException))]
  253. public void GetDirectoriesDirectoryNotFoundException2 ()
  254. {
  255. string path = TempFolder + "/DIT.GetDirectoriesDirectoryNotFoundException2.Test";
  256. DeleteDir (path);
  257. try {
  258. DirectoryInfo info = new DirectoryInfo (path);
  259. info.GetDirectories ("*");
  260. } finally {
  261. DeleteDir (path);
  262. }
  263. }
  264. [Test]
  265. [ExpectedException (typeof (ArgumentNullException))]
  266. public void GetDirectoriesArgumentNullException ()
  267. {
  268. string path = TempFolder + "/DIT.GetDirectoriesArgumentNullException.Test";
  269. DeleteDir (path);
  270. try {
  271. DirectoryInfo info = new DirectoryInfo (path);
  272. info.GetDirectories (null);
  273. } finally {
  274. DeleteDir (path);
  275. }
  276. }
  277. [Test]
  278. public void GetFiles1 ()
  279. {
  280. string path = TempFolder + "/DIT.GetFiles1.Test";
  281. DeleteDir (path);
  282. try {
  283. DirectoryInfo info = Directory.CreateDirectory (path);
  284. Assertion.AssertEquals ("test#01", 0, info.GetFiles ().Length);
  285. File.Create (path + "/" + "file1").Close ();
  286. File.Create (path + "/" + "file2").Close ();
  287. Directory.CreateDirectory (path + "/" + "directory1");
  288. Assertion.AssertEquals ("test#02", 2, info.GetFiles ().Length);
  289. } finally {
  290. DeleteDir (path);
  291. }
  292. }
  293. [Test]
  294. public void GetFiles2()
  295. {
  296. string path = TempFolder + "/DIT.GetFiles2.Test";
  297. DeleteDir (path);
  298. try {
  299. DirectoryInfo info = Directory.CreateDirectory (path);
  300. Assertion.AssertEquals ("test#01", 0, info.GetFiles ("*").Length);
  301. File.Create (path + "/" + "file120file").Close ();
  302. File.Create (path + "/" + "file220file").Close ();
  303. File.Create (path + "/" + "afile330file").Close ();
  304. File.Create (path + "/" + "test.abc").Close ();
  305. File.Create (path + "/" + "test.abcd").Close ();
  306. File.Create (path + "/" + "test.abcdef").Close ();
  307. Directory.CreateDirectory (path + "/" + "dir");
  308. Assertion.AssertEquals ("test#02", 6, info.GetFiles ("*").Length);
  309. Assertion.AssertEquals ("test#03", 2, info.GetFiles ("file*file").Length);
  310. Assertion.AssertEquals ("test#04", 3, info.GetFiles ("*file*").Length);
  311. Assertion.AssertEquals ("test#05", 2, info.GetFiles ("file?20file").Length);
  312. Assertion.AssertEquals ("test#07", 1, info.GetFiles ("*.abcd").Length);
  313. Assertion.AssertEquals ("test#08", 2, info.GetFiles ("*.abcd*").Length);
  314. } finally {
  315. DeleteDir (path);
  316. }
  317. }
  318. [Test]
  319. [ExpectedException (typeof (DirectoryNotFoundException))]
  320. public void GetFilesDirectoryNotFoundException1 ()
  321. {
  322. string path = TempFolder + "/DIT.GetFilesDirectoryNotFoundException1.Test";
  323. DeleteDir (path);
  324. try {
  325. DirectoryInfo info = new DirectoryInfo (path);
  326. info.GetFiles ();
  327. } finally {
  328. DeleteDir (path);
  329. }
  330. }
  331. [Test]
  332. [ExpectedException (typeof (DirectoryNotFoundException))]
  333. public void GetFilesDirectoryNotFoundException2 ()
  334. {
  335. string path = TempFolder + "/DIT.GetFilesDirectoryNotFoundException2.Test";
  336. DeleteDir (path);
  337. try {
  338. DirectoryInfo info = new DirectoryInfo (path);
  339. info.GetFiles ("*");
  340. } finally {
  341. DeleteDir (path);
  342. }
  343. }
  344. [Test]
  345. [ExpectedException (typeof (ArgumentNullException))]
  346. public void GetFilesArgumentNullException ()
  347. {
  348. string path = TempFolder + "/DIT.GetFilesArgumentNullException.Test";
  349. DeleteDir (path);
  350. try {
  351. DirectoryInfo info = new DirectoryInfo (path);
  352. info.GetFiles (null);
  353. } finally {
  354. DeleteDir (path);
  355. }
  356. }
  357. [Test]
  358. public void MoveTo ()
  359. {
  360. string path1 = TempFolder + "/DIT.MoveTo.Soucre.Test";
  361. string path2 = TempFolder + "/DIT.MoveTo.Dest.Test";
  362. DeleteDir (path1);
  363. DeleteDir (path2);
  364. try {
  365. DirectoryInfo info1 = Directory.CreateDirectory (path1);
  366. DirectoryInfo info2 = new DirectoryInfo (path2);
  367. Assertion.AssertEquals ("test#01", true, info1.Exists);
  368. Assertion.AssertEquals ("test#02", false, info2.Exists);
  369. info1.MoveTo (path2);
  370. Assertion.AssertEquals ("test#03", true, info1.Exists);
  371. Assertion.AssertEquals ("test#04", false, info2.Exists);
  372. info1 = new DirectoryInfo (path1);
  373. info2 = new DirectoryInfo (path2);
  374. Assertion.AssertEquals ("test#05", false, info1.Exists);
  375. Assertion.AssertEquals ("test#06", true, info2.Exists);
  376. } finally {
  377. DeleteDir (path1);
  378. DeleteDir (path2);
  379. }
  380. }
  381. [Test]
  382. [ExpectedException (typeof (ArgumentNullException))]
  383. public void MoveToArgumentNullException ()
  384. {
  385. string path = TempFolder + "/DIT.MoveToArgumentNullException.Test";
  386. DeleteDir (path);
  387. try {
  388. DirectoryInfo info = Directory.CreateDirectory (path);
  389. info.MoveTo (null);
  390. } finally {
  391. DeleteDir (path);
  392. }
  393. }
  394. [Test]
  395. [ExpectedException (typeof (IOException))]
  396. public void MoveToIOException1 ()
  397. {
  398. string path = TempFolder + "/DIT.MoveToIOException1.Test";
  399. DeleteDir (path);
  400. try {
  401. DirectoryInfo info = Directory.CreateDirectory (path);
  402. info.MoveTo (path);
  403. } finally {
  404. DeleteDir (path);
  405. }
  406. }
  407. [Test]
  408. [ExpectedException (typeof (ArgumentException))]
  409. public void MoveToArgumentException1 ()
  410. {
  411. string path = TempFolder + "/DIT.MoveToArgumentException1.Test";
  412. DeleteDir (path);
  413. try {
  414. DirectoryInfo info = Directory.CreateDirectory (path);
  415. info.MoveTo ("");
  416. } finally {
  417. DeleteDir (path);
  418. }
  419. }
  420. [Test]
  421. [ExpectedException (typeof (ArgumentException))]
  422. public void MoveToArgumentException2 ()
  423. {
  424. string path = TempFolder + "/DIT.MoveToArgumentException2.Test";
  425. DeleteDir (path);
  426. try {
  427. DirectoryInfo info = Directory.CreateDirectory (path);
  428. info.MoveTo (" ");
  429. } finally {
  430. DeleteDir (path);
  431. }
  432. }
  433. [Test]
  434. [ExpectedException (typeof (ArgumentException))]
  435. public void MoveToArgumentException3 ()
  436. {
  437. string path = TempFolder + "/DIT.MoveToArgumentException3.Test";
  438. DeleteDir (path);
  439. try {
  440. DirectoryInfo info = Directory.CreateDirectory (path);
  441. info.MoveTo (Path.InvalidPathChars [0].ToString ());
  442. } finally {
  443. DeleteDir (path);
  444. }
  445. }
  446. [Test]
  447. [ExpectedException (typeof (IOException))]
  448. public void MoveToIOException2 ()
  449. {
  450. string path = TempFolder + "/DIT.MoveToIOException2.Test";
  451. DeleteDir (path);
  452. try {
  453. DirectoryInfo info = new DirectoryInfo (path);
  454. info.MoveTo (path);
  455. } finally {
  456. DeleteDir (path);
  457. }
  458. }
  459. private void DeleteDir (string path)
  460. {
  461. if (Directory.Exists (path))
  462. Directory.Delete (path, true);
  463. }
  464. }
  465. }