FileTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // FileTest.cs: Test cases for System.IO.File
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. //
  6. // (C) 2002 Ximian, Inc. http://www.ximian.com
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.IO;
  11. namespace MonoTests.System.IO
  12. {
  13. public class FileTest : TestCase
  14. {
  15. protected override void SetUp ()
  16. {
  17. }
  18. protected override void TearDown ()
  19. {
  20. File.Delete ("resources" + Path.DirectorySeparatorChar + "baz");
  21. File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
  22. File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
  23. }
  24. public void TestExists ()
  25. {
  26. int i = 0;
  27. try {
  28. Assert ("null filename should not exist", !File.Exists (null));
  29. i++;
  30. Assert ("empty filename should not exist", !File.Exists (""));
  31. i++;
  32. Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
  33. i++;
  34. Assert ("File resources" + Path.DirectorySeparatorChar + "AFile.txt should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
  35. i++;
  36. Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "doesnotexist"));
  37. } catch (Exception e) {
  38. Fail ("Unexpected exception at i = " + i + ". e=" + e);
  39. }
  40. }
  41. public void TestCreate ()
  42. {
  43. FileStream stream;
  44. /* exception test: File.Create(null) */
  45. try {
  46. stream = File.Create (null);
  47. Fail ("File.Create(null) should throw ArgumentNullException");
  48. } catch (ArgumentNullException) {
  49. // do nothing, this is what we expect
  50. } catch (Exception e) {
  51. Fail ("File.Create(null) unexpected exception caught: e=" + e.ToString());
  52. }
  53. /* exception test: File.Create("") */
  54. try {
  55. stream = File.Create ("");
  56. Fail ("File.Create('') should throw ArgumentException");
  57. } catch (ArgumentException) {
  58. // do nothing, this is what we expect
  59. } catch (Exception e) {
  60. Fail ("File.Create('') unexpected exception caught: e=" + e.ToString());
  61. }
  62. /* exception test: File.Create(" ") */
  63. try {
  64. stream = File.Create (" ");
  65. Fail ("File.Create(' ') should throw ArgumentException");
  66. } catch (ArgumentException) {
  67. // do nothing, this is what we expect
  68. } catch (Exception e) {
  69. Fail ("File.Create(' ') unexpected exception caught: e=" + e.ToString());
  70. }
  71. /* exception test: File.Create(directory_not_found) */
  72. try {
  73. stream = File.Create ("directory_does_not_exist" + Path.DirectorySeparatorChar + "foo");
  74. Fail ("File.Create(directory_does_not_exist) should throw DirectoryNotFoundException");
  75. } catch (DirectoryNotFoundException) {
  76. // do nothing, this is what we expect
  77. } catch (Exception e) {
  78. Fail ("File.Create(directory_does_not_exist) unexpected exception caught: e=" + e.ToString());
  79. }
  80. /* positive test: create resources/foo */
  81. try {
  82. stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
  83. Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  84. stream.Close ();
  85. } catch (Exception e) {
  86. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  87. }
  88. /* positive test: repeat test above again to test for overwriting file */
  89. try {
  90. stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
  91. Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  92. stream.Close ();
  93. } catch (Exception e) {
  94. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  95. }
  96. }
  97. public void TestCopy ()
  98. {
  99. /* exception test: File.Copy(null, b) */
  100. try {
  101. File.Copy (null, "b");
  102. Fail ("File.Copy(null, 'b') should throw ArgumentNullException");
  103. } catch (ArgumentNullException) {
  104. // do nothing, this is what we expect
  105. } catch (Exception e) {
  106. Fail ("File.Copy(null, 'b') unexpected exception caught: e=" + e.ToString());
  107. }
  108. /* exception test: File.Copy(a, null) */
  109. try {
  110. File.Copy ("a", null);
  111. Fail ("File.Copy('a', null) should throw ArgumentNullException");
  112. } catch (ArgumentNullException) {
  113. // do nothing, this is what we expect
  114. } catch (Exception e) {
  115. Fail ("File.Copy('a', null) unexpected exception caught: e=" + e.ToString());
  116. }
  117. /* exception test: File.Copy("", b) */
  118. try {
  119. File.Copy ("", "b");
  120. Fail ("File.Copy('', 'b') should throw ArgumentException");
  121. } catch (ArgumentException) {
  122. // do nothing, this is what we expect
  123. } catch (Exception e) {
  124. Fail ("File.Copy('', 'b') unexpected exception caught: e=" + e.ToString());
  125. }
  126. /* exception test: File.Copy(a, "") */
  127. try {
  128. File.Copy ("a", "");
  129. Fail ("File.Copy('a', '') should throw ArgumentException");
  130. } catch (ArgumentException) {
  131. // do nothing, this is what we expect
  132. } catch (Exception e) {
  133. Fail ("File.Copy('a', '') unexpected exception caught: e=" + e.ToString());
  134. }
  135. /* exception test: File.Copy(" ", b) */
  136. try {
  137. File.Copy (" ", "b");
  138. Fail ("File.Copy(' ', 'b') should throw ArgumentException");
  139. } catch (ArgumentException) {
  140. // do nothing, this is what we expect
  141. } catch (Exception e) {
  142. Fail ("File.Copy(' ', 'b') unexpected exception caught: e=" + e.ToString());
  143. }
  144. /* exception test: File.Copy(a, " ") */
  145. try {
  146. File.Copy ("a", " ");
  147. Fail ("File.Copy('a', ' ') should throw ArgumentException");
  148. } catch (ArgumentException) {
  149. // do nothing, this is what we expect
  150. } catch (Exception e) {
  151. Fail ("File.Copy('a', ' ') unexpected exception caught: e=" + e.ToString());
  152. }
  153. /* exception test: File.Copy(doesnotexist, b) */
  154. try {
  155. File.Copy ("doesnotexist", "b");
  156. Fail ("File.Copy('doesnotexist', 'b') should throw FileNotFoundException");
  157. } catch (FileNotFoundException) {
  158. // do nothing, this is what we expect
  159. } catch (Exception e) {
  160. Fail ("File.Copy('doesnotexist', 'b') unexpected exception caught: e=" + e.ToString());
  161. }
  162. /* positive test: copy resources/AFile.txt to resources/bar */
  163. try {
  164. File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
  165. File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar");
  166. Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
  167. Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  168. } catch (Exception e) {
  169. Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
  170. }
  171. /* exception test: File.Copy(resources/AFile.txt, resources/bar) (default is overwrite == false) */
  172. try {
  173. File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar");
  174. Fail ("File.Copy('resources/AFile.txt', 'resources/bar') should throw IOException");
  175. } catch (IOException) {
  176. // do nothing, this is what we expect
  177. } catch (Exception e) {
  178. Fail ("#2 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
  179. }
  180. /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
  181. try {
  182. Assert ("File bar should exist before File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  183. File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar", true);
  184. Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
  185. Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  186. } catch (Exception e) {
  187. Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
  188. }
  189. }
  190. public void TestDelete ()
  191. {
  192. /* exception test: File.Delete(null) */
  193. try {
  194. File.Delete (null);
  195. Fail ("File.Delete(null) should throw ArgumentNullException");
  196. } catch (ArgumentNullException) {
  197. // do nothing, this is what we expect
  198. } catch (Exception e) {
  199. Fail ("File.Delete(null) unexpected exception caught: e=" + e.ToString());
  200. }
  201. /* exception test: File.Delete("") */
  202. try {
  203. File.Delete ("");
  204. Fail ("File.Delete('') should throw ArgumentException");
  205. } catch (ArgumentException) {
  206. // do nothing, this is what we expect
  207. } catch (Exception e) {
  208. Fail ("File.Delete('') unexpected exception caught: e=" + e.ToString());
  209. }
  210. /* exception test: File.Delete(" ") */
  211. try {
  212. File.Delete (" ");
  213. Fail ("File.Delete(' ') should throw ArgumentException");
  214. } catch (ArgumentException) {
  215. // do nothing, this is what we expect
  216. } catch (Exception e) {
  217. Fail ("File.Delete(' ') unexpected exception caught: e=" + e.ToString());
  218. }
  219. /* exception test: File.Delete(directory_not_found) */
  220. try {
  221. File.Delete ("directory_does_not_exist" + Path.DirectorySeparatorChar + "foo");
  222. Fail ("File.Delete(directory_does_not_exist) should throw DirectoryNotFoundException");
  223. } catch (DirectoryNotFoundException) {
  224. // do nothing, this is what we expect
  225. } catch (Exception e) {
  226. Fail ("File.Delete(directory_does_not_exist) unexpected exception caught: e=" + e.ToString());
  227. }
  228. if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "foo")) {
  229. FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "foo");
  230. f.Close();
  231. }
  232. Assert ("File resources" + Path.DirectorySeparatorChar + "foo should exist for TestDelete to succeed", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  233. try {
  234. File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
  235. } catch (Exception e) {
  236. Fail ("Unable to delete resources" + Path.DirectorySeparatorChar + "foo: e=" + e.ToString());
  237. }
  238. Assert ("File resources" + Path.DirectorySeparatorChar + "foo should not exist after File.Delete", !File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  239. }
  240. public void TestMove ()
  241. {
  242. /* exception test: File.Move(null, b) */
  243. try {
  244. File.Move (null, "b");
  245. Fail ("File.Move(null, 'b') should throw ArgumentNullException");
  246. } catch (ArgumentNullException) {
  247. // do nothing, this is what we expect
  248. } catch (Exception e) {
  249. Fail ("File.Move(null, 'b') unexpected exception caught: e=" + e.ToString());
  250. }
  251. /* exception test: File.Move(a, null) */
  252. try {
  253. File.Move ("a", null);
  254. Fail ("File.Move('a', null) should throw ArgumentNullException");
  255. } catch (ArgumentNullException) {
  256. // do nothing, this is what we expect
  257. } catch (Exception e) {
  258. Fail ("File.Move('a', null) unexpected exception caught: e=" + e.ToString());
  259. }
  260. /* exception test: File.Move("", b) */
  261. try {
  262. File.Move ("", "b");
  263. Fail ("File.Move('', 'b') should throw ArgumentException");
  264. } catch (ArgumentException) {
  265. // do nothing, this is what we expect
  266. } catch (Exception e) {
  267. Fail ("File.Move('', 'b') unexpected exception caught: e=" + e.ToString());
  268. }
  269. /* exception test: File.Move(a, "") */
  270. try {
  271. File.Move ("a", "");
  272. Fail ("File.Move('a', '') should throw ArgumentException");
  273. } catch (ArgumentException) {
  274. // do nothing, this is what we expect
  275. } catch (Exception e) {
  276. Fail ("File.Move('a', '') unexpected exception caught: e=" + e.ToString());
  277. }
  278. /* exception test: File.Move(" ", b) */
  279. try {
  280. File.Move (" ", "b");
  281. Fail ("File.Move(' ', 'b') should throw ArgumentException");
  282. } catch (ArgumentException) {
  283. // do nothing, this is what we expect
  284. } catch (Exception e) {
  285. Fail ("File.Move(' ', 'b') unexpected exception caught: e=" + e.ToString());
  286. }
  287. /* exception test: File.Move(a, " ") */
  288. try {
  289. File.Move ("a", " ");
  290. Fail ("File.Move('a', ' ') should throw ArgumentException");
  291. } catch (ArgumentException) {
  292. // do nothing, this is what we expect
  293. } catch (Exception e) {
  294. Fail ("File.Move('a', ' ') unexpected exception caught: e=" + e.ToString());
  295. }
  296. /* exception test: File.Move(doesnotexist, b) */
  297. try {
  298. File.Move ("doesnotexist", "b");
  299. Fail ("File.Move('doesnotexist', 'b') should throw FileNotFoundException");
  300. } catch (FileNotFoundException) {
  301. // do nothing, this is what we expect
  302. } catch (Exception e) {
  303. Fail ("File.Move('doesnotexist', 'b') unexpected exception caught: e=" + e.ToString());
  304. }
  305. /* exception test: File.Move(resources/foo, doesnotexist/b) */
  306. File.Copy("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "foo", true);
  307. try {
  308. File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "doesnotexist" + Path.DirectorySeparatorChar + "b");
  309. Fail ("File.Move('resources/foo', 'b') should throw DirectoryNotFoundException");
  310. } catch (DirectoryNotFoundException) {
  311. // do nothing, this is what we expect
  312. } catch (FileNotFoundException) {
  313. // LAMESPEC
  314. // do nothing, this is (kind of) what we expect
  315. } catch (Exception e) {
  316. Fail ("File.Move('resources/foo', 'doesnotexist/b') unexpected exception caught: e=" + e.ToString());
  317. }
  318. /* exception test: File.Move(doesnotexist/foo, b) */
  319. try {
  320. File.Move ("doesnotexist" + Path.DirectorySeparatorChar + "foo", "b");
  321. Fail ("File.Move('doesnotexist/foo', 'b') should throw DirectoryNotFoundException");
  322. } catch (DirectoryNotFoundException) {
  323. // do nothing, this is what we expect
  324. } catch (FileNotFoundException) {
  325. // LAMESPEC
  326. // do nothing, this is (kind of) what we expect
  327. } catch (Exception e) {
  328. Fail ("File.Move('doesnotexist/foo', 'b') unexpected exception caught: e=" + e.ToString());
  329. }
  330. /* exception test: File.Move(resources/foo, resources) */
  331. try {
  332. File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "resources");
  333. Fail ("File.Move('resources/foo', 'resources') should throw IOException");
  334. } catch (IOException) {
  335. // do nothing, this is what we expect
  336. } catch (Exception e) {
  337. Fail ("File.Move('resources/foo', 'resources') unexpected exception caught: e=" + e.ToString());
  338. }
  339. /* positive test: File.Move(a, a) shouldn't throw exception */
  340. try {
  341. File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "resources" + Path.DirectorySeparatorChar + "foo");
  342. } catch (Exception e) {
  343. Fail ("File.Move('doesnotexist/foo', 'b') unexpected exception caught: e=" + e.ToString());
  344. }
  345. if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "bar")) {
  346. FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "bar");
  347. f.Close();
  348. }
  349. Assert ("File resources" + Path.DirectorySeparatorChar + "bar should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  350. File.Move ("resources" + Path.DirectorySeparatorChar + "bar", "resources" + Path.DirectorySeparatorChar + "baz");
  351. Assert ("File resources" + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  352. Assert ("File resources" + Path.DirectorySeparatorChar + "baz should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "baz"));
  353. }
  354. public void TestOpen ()
  355. {
  356. try {
  357. FileStream stream = File.Open ("resources" + Path.DirectorySeparatorChar + "AFile.txt", FileMode.Open);
  358. stream.Close ();
  359. } catch (Exception e) {
  360. Fail ("Unable to open resources" + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
  361. }
  362. /* Exception tests */
  363. try {
  364. FileStream stream = File.Open ("filedoesnotexist", FileMode.Open);
  365. Fail ("File 'filedoesnotexist' should not exist");
  366. } catch (FileNotFoundException) {
  367. // do nothing, this is what we expect
  368. } catch (Exception e) {
  369. Fail ("Unexpect exception caught: e=" + e.ToString());
  370. }
  371. }
  372. }
  373. }