DirectoryInfoTest.cs 16 KB

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