FileNotFoundExceptionTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //
  2. // FileNotFoundExceptionTest.cs - Unit tests for
  3. // System.IO.FileNotFoundException
  4. //
  5. // Author:
  6. // Gert Driesen <[email protected]>
  7. //
  8. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.IO {
  33. [TestFixture]
  34. public class FileNotFoundExceptionTest {
  35. [Test]
  36. public void Constructor1 ()
  37. {
  38. FileNotFoundException fnf = new FileNotFoundException ();
  39. #if NET_2_0
  40. Assert.IsNotNull (fnf.Data, "#1");
  41. #endif
  42. Assert.IsNull (fnf.FileName, "#2");
  43. Assert.IsNull (fnf.InnerException, "#3");
  44. Assert.IsNotNull (fnf.Message, "#4"); // Unable to find the specified file
  45. Assert.IsNull (fnf.FusionLog, "#5");
  46. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType().FullName), "#6");
  47. }
  48. [Test]
  49. public void Constructor2 ()
  50. {
  51. FileNotFoundException fnf = new FileNotFoundException ("message");
  52. #if NET_2_0
  53. Assert.IsNotNull (fnf.Data, "#1");
  54. #endif
  55. Assert.IsNull (fnf.FileName, "#2");
  56. Assert.IsNull (fnf.InnerException, "#3");
  57. Assert.IsNotNull (fnf.Message, "#4");
  58. Assert.AreEqual ("message", fnf.Message, "#5");
  59. Assert.IsNull (fnf.FusionLog, "#6");
  60. #if TARGET_JVM
  61. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": message"),"#7");
  62. #else
  63. Assert.AreEqual (fnf.GetType ().FullName + ": message",
  64. fnf.ToString (), "#7");
  65. #endif
  66. }
  67. [Test]
  68. public void Constructor2_Message_Empty ()
  69. {
  70. FileNotFoundException fnf = new FileNotFoundException (string.Empty);
  71. #if NET_2_0
  72. Assert.IsNotNull (fnf.Data, "#1");
  73. #endif
  74. Assert.IsNull (fnf.FileName, "#2");
  75. Assert.IsNull (fnf.InnerException, "#3");
  76. Assert.IsNotNull (fnf.Message, "#4");
  77. Assert.AreEqual (string.Empty, fnf.Message, "#5");
  78. Assert.IsNull (fnf.FusionLog, "#6");
  79. #if TARGET_JVM
  80. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": "), "#7");
  81. #else
  82. Assert.AreEqual (fnf.GetType ().FullName + ": ",
  83. fnf.ToString (), "#7");
  84. #endif
  85. }
  86. [Test]
  87. public void Constructor2_Message_Null ()
  88. {
  89. FileNotFoundException fnf = new FileNotFoundException ((string) null);
  90. #if NET_2_0
  91. Assert.IsNotNull (fnf.Data, "#1");
  92. #endif
  93. Assert.IsNull (fnf.FileName, "#2");
  94. Assert.IsNull (fnf.InnerException, "#3");
  95. #if NET_2_0
  96. Assert.IsNull (fnf.Message, "#4");
  97. #else
  98. Assert.IsNotNull (fnf.Message, "#4"); // File or assembly name (null), or ...
  99. #endif
  100. Assert.IsNull (fnf.FusionLog, "#5");
  101. #if NET_2_0 && !TARGET_JVM
  102. Assert.AreEqual (fnf.GetType ().FullName + ": ",
  103. fnf.ToString (), "#6");
  104. #else
  105. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName), "#6");
  106. #endif
  107. }
  108. [Test]
  109. public void Constructor3 ()
  110. {
  111. ArithmeticException ame = new ArithmeticException ("something");
  112. FileNotFoundException fnf = new FileNotFoundException ("message",
  113. ame);
  114. #if NET_2_0
  115. Assert.IsNotNull (fnf.Data, "#1");
  116. #endif
  117. Assert.IsNull (fnf.FileName, "#2");
  118. Assert.IsNotNull (fnf.InnerException, "#3");
  119. Assert.AreSame (ame, fnf.InnerException, "#4");
  120. Assert.IsNotNull (fnf.Message, "#5");
  121. Assert.AreEqual ("message", fnf.Message, "#6");
  122. Assert.IsNull (fnf.FusionLog, "#7");
  123. #if TARGET_JVM
  124. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": message ---> "
  125. + ame.GetType().FullName + ": something"), "#8");
  126. #else
  127. Assert.AreEqual (fnf.GetType ().FullName + ": message ---> "
  128. + ame.GetType ().FullName + ": something", fnf.ToString (), "#8");
  129. #endif
  130. }
  131. [Test]
  132. public void Constructor3_Message_Empty ()
  133. {
  134. ArithmeticException ame = new ArithmeticException ("something");
  135. FileNotFoundException fnf = new FileNotFoundException (string.Empty, ame);
  136. #if NET_2_0
  137. Assert.IsNotNull (fnf.Data, "#1");
  138. #endif
  139. Assert.IsNull (fnf.FileName, "#2");
  140. Assert.IsNotNull (fnf.InnerException, "#3");
  141. Assert.AreSame (ame, fnf.InnerException, "#4");
  142. Assert.IsNotNull (fnf.Message, "#5");
  143. Assert.AreEqual (string.Empty, fnf.Message, "#6");
  144. Assert.IsNull (fnf.FusionLog, "#7");
  145. #if TARGET_JVM
  146. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": ---> "
  147. + ame.GetType().FullName + ": something"), "#8");
  148. #else
  149. Assert.AreEqual (fnf.GetType ().FullName + ": ---> "
  150. + ame.GetType ().FullName + ": something", fnf.ToString (), "#8");
  151. #endif
  152. }
  153. [Test]
  154. public void Constructor3_Message_Null ()
  155. {
  156. ArithmeticException ame = new ArithmeticException ("something");
  157. FileNotFoundException fnf = new FileNotFoundException ((string) null, ame);
  158. #if NET_2_0
  159. Assert.IsNotNull (fnf.Data, "#1");
  160. #endif
  161. Assert.IsNull (fnf.FileName, "#2");
  162. Assert.IsNotNull (fnf.InnerException, "#3");
  163. Assert.AreSame (ame, fnf.InnerException, "#4");
  164. #if NET_2_0
  165. Assert.IsNull (fnf.Message, "#5");
  166. #else
  167. Assert.IsNotNull (fnf.Message, "#5"); // File or assembly name (null), or ...
  168. #endif
  169. Assert.IsNull (fnf.FusionLog, "#6");
  170. #if NET_2_0
  171. #if TARGET_JVM
  172. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": ---> "
  173. + ame.GetType().FullName + ": something"), "#7");
  174. #else
  175. Assert.AreEqual (fnf.GetType ().FullName + ": ---> "
  176. + ame.GetType ().FullName + ": something", fnf.ToString (), "#7");
  177. #endif
  178. #else
  179. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName), "#7");
  180. Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#9");
  181. #endif
  182. }
  183. [Test]
  184. public void Constructor3_InnerException_Null ()
  185. {
  186. FileNotFoundException fnf = new FileNotFoundException ("message",
  187. (Exception) null);
  188. #if NET_2_0
  189. Assert.IsNotNull (fnf.Data, "#1");
  190. #endif
  191. Assert.IsNull (fnf.FileName, "#2");
  192. Assert.IsNull (fnf.InnerException, "#3");
  193. Assert.IsNotNull (fnf.Message, "#4");
  194. Assert.AreEqual ("message", fnf.Message, "#5");
  195. Assert.IsNull (fnf.FusionLog, "#6");
  196. #if TARGET_JVM
  197. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": message"), "#7");
  198. #else
  199. Assert.AreEqual (fnf.GetType ().FullName + ": message",
  200. fnf.ToString (), "#7");
  201. #endif
  202. }
  203. [Test]
  204. public void Constructor4 ()
  205. {
  206. FileNotFoundException fnf = new FileNotFoundException ("message",
  207. "file.txt");
  208. #if NET_2_0
  209. Assert.IsNotNull (fnf.Data, "#1");
  210. #endif
  211. Assert.IsNotNull (fnf.FileName, "#2");
  212. Assert.AreEqual ("file.txt", fnf.FileName, "#3");
  213. Assert.IsNull (fnf.InnerException, "#4");
  214. Assert.IsNotNull (fnf.Message, "#5");
  215. Assert.AreEqual ("message", fnf.Message, "#6");
  216. Assert.IsNull (fnf.FusionLog, "#7");
  217. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  218. + ": message" + Environment.NewLine), "#8");
  219. #if NET_2_0
  220. Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
  221. Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#9");
  222. #else
  223. Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
  224. Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
  225. #endif
  226. }
  227. [Test]
  228. public void Constructor4_FileName_Empty ()
  229. {
  230. FileNotFoundException fnf = new FileNotFoundException ("message",
  231. string.Empty);
  232. #if NET_2_0
  233. Assert.IsNotNull (fnf.Data, "#1");
  234. #endif
  235. Assert.IsNotNull (fnf.FileName, "#2");
  236. Assert.AreEqual (string.Empty, fnf.FileName, "#3");
  237. Assert.IsNull (fnf.InnerException, "#4");
  238. Assert.IsNotNull (fnf.Message, "#5");
  239. Assert.AreEqual ("message", fnf.Message, "#6");
  240. Assert.IsNull (fnf.FusionLog, "#7");
  241. #if TARGET_JVM
  242. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": message"), "#8");
  243. #else
  244. Assert.AreEqual (fnf.GetType ().FullName + ": message",
  245. fnf.ToString (), "#8");
  246. #endif
  247. }
  248. [Test]
  249. public void Constructor4_FileName_Null ()
  250. {
  251. FileNotFoundException fnf = new FileNotFoundException ("message",
  252. (string) null);
  253. #if NET_2_0
  254. Assert.IsNotNull (fnf.Data, "#A1");
  255. #endif
  256. Assert.IsNull (fnf.FileName, "#A2");
  257. Assert.IsNull (fnf.InnerException, "#A3");
  258. Assert.IsNotNull (fnf.Message, "#A4");
  259. Assert.AreEqual ("message", fnf.Message, "#A5");
  260. Assert.IsNull (fnf.FusionLog, "#A6");
  261. #if TARGET_JVM
  262. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": message"), "#A7");
  263. #else
  264. Assert.AreEqual (fnf.GetType ().FullName + ": message",
  265. fnf.ToString (), "#A7");
  266. #endif
  267. fnf = new FileNotFoundException (string.Empty, (string) null);
  268. #if NET_2_0
  269. Assert.IsNotNull (fnf.Data, "#B1");
  270. #endif
  271. Assert.IsNull (fnf.FileName, "#B2");
  272. Assert.IsNull (fnf.InnerException, "#B3");
  273. Assert.IsNotNull (fnf.Message, "#B4");
  274. Assert.AreEqual (string.Empty, fnf.Message, "#B5");
  275. Assert.IsNull (fnf.FusionLog, "#B6");
  276. #if TARGET_JVM
  277. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": "), "#B7");
  278. #else
  279. Assert.AreEqual (fnf.GetType ().FullName + ": ",
  280. fnf.ToString (), "#B7");
  281. #endif
  282. }
  283. [Test]
  284. public void Constructor4_FileNameAndMessage_Empty ()
  285. {
  286. FileNotFoundException fnf = new FileNotFoundException (string.Empty,
  287. string.Empty);
  288. #if NET_2_0
  289. Assert.IsNotNull (fnf.Data, "#1");
  290. #endif
  291. Assert.IsNotNull (fnf.FileName, "#2");
  292. Assert.AreEqual (string.Empty, fnf.FileName, "#3");
  293. Assert.IsNull (fnf.InnerException, "#4");
  294. Assert.IsNotNull (fnf.Message, "#5");
  295. Assert.AreEqual (string.Empty, fnf.Message, "#6");
  296. Assert.IsNull (fnf.FusionLog, "#7");
  297. #if TARGET_JVM
  298. Assert.IsTrue(fnf.ToString().StartsWith(fnf.GetType().FullName + ": "), "#8");
  299. #else
  300. Assert.AreEqual (fnf.GetType ().FullName + ": ", fnf.ToString (), "#8");
  301. #endif
  302. }
  303. [Test]
  304. public void Constructor4_FileNameAndMessage_Null ()
  305. {
  306. FileNotFoundException fnf = new FileNotFoundException ((string) null,
  307. (string) null);
  308. #if NET_2_0
  309. Assert.IsNotNull (fnf.Data, "#1");
  310. #endif
  311. Assert.IsNull (fnf.FileName, "#2");
  312. Assert.IsNull (fnf.InnerException, "#3");
  313. #if NET_2_0
  314. Assert.IsNull (fnf.Message, "#4");
  315. #else
  316. Assert.IsNotNull (fnf.Message, "#4");
  317. #endif
  318. Assert.IsNull (fnf.FusionLog, "#5");
  319. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  320. + ": "), "#6");
  321. #if !TARGET_JVM
  322. Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#7");
  323. #endif
  324. Assert.IsFalse (fnf.ToString ().IndexOf ("''") != -1, "#8");
  325. }
  326. [Test]
  327. public void Constructor4_Message_Empty ()
  328. {
  329. FileNotFoundException fnf = null;
  330. fnf = new FileNotFoundException (string.Empty, "file.txt");
  331. #if NET_2_0
  332. Assert.IsNotNull (fnf.Data, "#1");
  333. #endif
  334. Assert.IsNotNull (fnf.FileName, "#2");
  335. Assert.AreEqual ("file.txt", fnf.FileName, "#3");
  336. Assert.IsNull (fnf.InnerException, "#4");
  337. Assert.IsNotNull (fnf.Message, "#5");
  338. Assert.AreEqual (string.Empty, fnf.Message, "#6");
  339. Assert.IsNull (fnf.FusionLog, "#7");
  340. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  341. + ": " + Environment.NewLine), "#8");
  342. #if NET_2_0
  343. Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
  344. Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
  345. #else
  346. Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
  347. Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
  348. #endif
  349. }
  350. [Test]
  351. public void Constructor4_Message_Null ()
  352. {
  353. FileNotFoundException fnf = null;
  354. fnf = new FileNotFoundException ((string) null, "file.txt");
  355. #if NET_2_0
  356. Assert.IsNotNull (fnf.Data, "#A1");
  357. #endif
  358. Assert.IsNotNull (fnf.FileName, "#A2");
  359. Assert.AreEqual ("file.txt", fnf.FileName, "#A3");
  360. Assert.IsNull (fnf.InnerException, "#A4");
  361. Assert.IsNotNull (fnf.Message, "#A5");
  362. Assert.IsNull (fnf.FusionLog, "#A6");
  363. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  364. + ": "), "#A7");
  365. Assert.IsTrue (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#A8");
  366. #if NET_2_0
  367. Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#A9");
  368. Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#A10");
  369. #else
  370. Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#A9");
  371. Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#A10");
  372. #endif
  373. fnf = new FileNotFoundException ((string) null, string.Empty);
  374. #if NET_2_0
  375. Assert.IsNotNull (fnf.Data, "#B1");
  376. #endif
  377. Assert.IsNotNull (fnf.FileName, "#B2");
  378. Assert.AreEqual (string.Empty, fnf.FileName, "#B3");
  379. Assert.IsNull (fnf.InnerException, "#B4");
  380. // .NET 1.1: File or assembly name , or one of its dependencies ...
  381. // .NET 2.0: Could not load file or assembly '' or one of its ...
  382. Assert.IsNotNull (fnf.Message, "#B5");
  383. Assert.IsNull (fnf.FusionLog, "#B6");
  384. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  385. + ": "), "#B7");
  386. #if !TARGET_JVM
  387. Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#B8");
  388. #endif
  389. #if NET_2_0
  390. Assert.IsTrue (fnf.ToString ().IndexOf ("''") != -1, "#B9");
  391. #else
  392. Assert.IsFalse (fnf.ToString ().IndexOf ("''") != -1, "#B9");
  393. Assert.IsFalse (fnf.ToString ().IndexOf ("\"\"") != -1, "#B10");
  394. #endif
  395. }
  396. }
  397. }