FileNotFoundExceptionTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. Assert.AreEqual (fnf.GetType ().FullName + ": message",
  61. fnf.ToString (), "#7");
  62. }
  63. [Test]
  64. public void Constructor2_Message_Empty ()
  65. {
  66. FileNotFoundException fnf = new FileNotFoundException (string.Empty);
  67. #if NET_2_0
  68. Assert.IsNotNull (fnf.Data, "#1");
  69. #endif
  70. Assert.IsNull (fnf.FileName, "#2");
  71. Assert.IsNull (fnf.InnerException, "#3");
  72. Assert.IsNotNull (fnf.Message, "#4");
  73. Assert.AreEqual (string.Empty, fnf.Message, "#5");
  74. Assert.IsNull (fnf.FusionLog, "#6");
  75. Assert.AreEqual (fnf.GetType ().FullName + ": ",
  76. fnf.ToString (), "#7");
  77. }
  78. [Test]
  79. public void Constructor2_Message_Null ()
  80. {
  81. FileNotFoundException fnf = new FileNotFoundException ((string) null);
  82. #if NET_2_0
  83. Assert.IsNotNull (fnf.Data, "#1");
  84. #endif
  85. Assert.IsNull (fnf.FileName, "#2");
  86. Assert.IsNull (fnf.InnerException, "#3");
  87. #if NET_2_0
  88. Assert.IsNull (fnf.Message, "#4");
  89. #else
  90. Assert.IsNotNull (fnf.Message, "#4"); // File or assembly name (null), or ...
  91. #endif
  92. Assert.IsNull (fnf.FusionLog, "#5");
  93. #if NET_2_0
  94. Assert.AreEqual (fnf.GetType ().FullName + ": ",
  95. fnf.ToString (), "#6");
  96. #else
  97. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName), "#6");
  98. #endif
  99. }
  100. [Test]
  101. public void Constructor3 ()
  102. {
  103. ArithmeticException ame = new ArithmeticException ("something");
  104. FileNotFoundException fnf = new FileNotFoundException ("message",
  105. ame);
  106. #if NET_2_0
  107. Assert.IsNotNull (fnf.Data, "#1");
  108. #endif
  109. Assert.IsNull (fnf.FileName, "#2");
  110. Assert.IsNotNull (fnf.InnerException, "#3");
  111. Assert.AreSame (ame, fnf.InnerException, "#4");
  112. Assert.IsNotNull (fnf.Message, "#5");
  113. Assert.AreEqual ("message", fnf.Message, "#6");
  114. Assert.IsNull (fnf.FusionLog, "#7");
  115. Assert.AreEqual (fnf.GetType ().FullName + ": message ---> "
  116. + ame.GetType ().FullName + ": something", fnf.ToString (), "#8");
  117. }
  118. [Test]
  119. public void Constructor3_Message_Empty ()
  120. {
  121. ArithmeticException ame = new ArithmeticException ("something");
  122. FileNotFoundException fnf = new FileNotFoundException (string.Empty, ame);
  123. #if NET_2_0
  124. Assert.IsNotNull (fnf.Data, "#1");
  125. #endif
  126. Assert.IsNull (fnf.FileName, "#2");
  127. Assert.IsNotNull (fnf.InnerException, "#3");
  128. Assert.AreSame (ame, fnf.InnerException, "#4");
  129. Assert.IsNotNull (fnf.Message, "#5");
  130. Assert.AreEqual (string.Empty, fnf.Message, "#6");
  131. Assert.IsNull (fnf.FusionLog, "#7");
  132. Assert.AreEqual (fnf.GetType ().FullName + ": ---> "
  133. + ame.GetType ().FullName + ": something", fnf.ToString (), "#8");
  134. }
  135. [Test]
  136. public void Constructor3_Message_Null ()
  137. {
  138. ArithmeticException ame = new ArithmeticException ("something");
  139. FileNotFoundException fnf = new FileNotFoundException ((string) null, ame);
  140. #if NET_2_0
  141. Assert.IsNotNull (fnf.Data, "#1");
  142. #endif
  143. Assert.IsNull (fnf.FileName, "#2");
  144. Assert.IsNotNull (fnf.InnerException, "#3");
  145. Assert.AreSame (ame, fnf.InnerException, "#4");
  146. #if NET_2_0
  147. Assert.IsNull (fnf.Message, "#5");
  148. #else
  149. Assert.IsNotNull (fnf.Message, "#5"); // File or assembly name (null), or ...
  150. #endif
  151. Assert.IsNull (fnf.FusionLog, "#6");
  152. #if NET_2_0
  153. Assert.AreEqual (fnf.GetType ().FullName + ": ---> "
  154. + ame.GetType ().FullName + ": something", fnf.ToString (), "#7");
  155. #else
  156. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName), "#7");
  157. Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#9");
  158. #endif
  159. }
  160. [Test]
  161. public void Constructor3_InnerException_Null ()
  162. {
  163. FileNotFoundException fnf = new FileNotFoundException ("message",
  164. (Exception) null);
  165. #if NET_2_0
  166. Assert.IsNotNull (fnf.Data, "#1");
  167. #endif
  168. Assert.IsNull (fnf.FileName, "#2");
  169. Assert.IsNull (fnf.InnerException, "#3");
  170. Assert.IsNotNull (fnf.Message, "#4");
  171. Assert.AreEqual ("message", fnf.Message, "#5");
  172. Assert.IsNull (fnf.FusionLog, "#6");
  173. Assert.AreEqual (fnf.GetType ().FullName + ": message",
  174. fnf.ToString (), "#7");
  175. }
  176. [Test]
  177. public void Constructor4 ()
  178. {
  179. FileNotFoundException fnf = new FileNotFoundException ("message",
  180. "file.txt");
  181. #if NET_2_0
  182. Assert.IsNotNull (fnf.Data, "#1");
  183. #endif
  184. Assert.IsNotNull (fnf.FileName, "#2");
  185. Assert.AreEqual ("file.txt", fnf.FileName, "#3");
  186. Assert.IsNull (fnf.InnerException, "#4");
  187. Assert.IsNotNull (fnf.Message, "#5");
  188. Assert.AreEqual ("message", fnf.Message, "#6");
  189. Assert.IsNull (fnf.FusionLog, "#7");
  190. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  191. + ": message" + Environment.NewLine), "#8");
  192. #if NET_2_0
  193. Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
  194. Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#9");
  195. #else
  196. Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
  197. Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
  198. #endif
  199. }
  200. [Test]
  201. public void Constructor4_FileName_Empty ()
  202. {
  203. FileNotFoundException fnf = new FileNotFoundException ("message",
  204. string.Empty);
  205. #if NET_2_0
  206. Assert.IsNotNull (fnf.Data, "#1");
  207. #endif
  208. Assert.IsNotNull (fnf.FileName, "#2");
  209. Assert.AreEqual (string.Empty, fnf.FileName, "#3");
  210. Assert.IsNull (fnf.InnerException, "#4");
  211. Assert.IsNotNull (fnf.Message, "#5");
  212. Assert.AreEqual ("message", fnf.Message, "#6");
  213. Assert.IsNull (fnf.FusionLog, "#7");
  214. Assert.AreEqual (fnf.GetType ().FullName + ": message",
  215. fnf.ToString (), "#8");
  216. }
  217. [Test]
  218. public void Constructor4_FileName_Null ()
  219. {
  220. FileNotFoundException fnf = new FileNotFoundException ("message",
  221. (string) null);
  222. #if NET_2_0
  223. Assert.IsNotNull (fnf.Data, "#A1");
  224. #endif
  225. Assert.IsNull (fnf.FileName, "#A2");
  226. Assert.IsNull (fnf.InnerException, "#A3");
  227. Assert.IsNotNull (fnf.Message, "#A4");
  228. Assert.AreEqual ("message", fnf.Message, "#A5");
  229. Assert.IsNull (fnf.FusionLog, "#A6");
  230. Assert.AreEqual (fnf.GetType ().FullName + ": message",
  231. fnf.ToString (), "#A7");
  232. fnf = new FileNotFoundException (string.Empty, (string) null);
  233. #if NET_2_0
  234. Assert.IsNotNull (fnf.Data, "#B1");
  235. #endif
  236. Assert.IsNull (fnf.FileName, "#B2");
  237. Assert.IsNull (fnf.InnerException, "#B3");
  238. Assert.IsNotNull (fnf.Message, "#B4");
  239. Assert.AreEqual (string.Empty, fnf.Message, "#B5");
  240. Assert.IsNull (fnf.FusionLog, "#B6");
  241. Assert.AreEqual (fnf.GetType ().FullName + ": ",
  242. fnf.ToString (), "#B7");
  243. }
  244. [Test]
  245. public void Constructor4_FileNameAndMessage_Empty ()
  246. {
  247. FileNotFoundException fnf = new FileNotFoundException (string.Empty,
  248. string.Empty);
  249. #if NET_2_0
  250. Assert.IsNotNull (fnf.Data, "#1");
  251. #endif
  252. Assert.IsNotNull (fnf.FileName, "#2");
  253. Assert.AreEqual (string.Empty, fnf.FileName, "#3");
  254. Assert.IsNull (fnf.InnerException, "#4");
  255. Assert.IsNotNull (fnf.Message, "#5");
  256. Assert.AreEqual (string.Empty, fnf.Message, "#6");
  257. Assert.IsNull (fnf.FusionLog, "#7");
  258. Assert.AreEqual (fnf.GetType ().FullName + ": ", fnf.ToString (), "#8");
  259. }
  260. [Test]
  261. public void Constructor4_FileNameAndMessage_Null ()
  262. {
  263. FileNotFoundException fnf = new FileNotFoundException ((string) null,
  264. (string) null);
  265. #if NET_2_0
  266. Assert.IsNotNull (fnf.Data, "#1");
  267. #endif
  268. Assert.IsNull (fnf.FileName, "#2");
  269. Assert.IsNull (fnf.InnerException, "#3");
  270. #if NET_2_0
  271. Assert.IsNull (fnf.Message, "#4");
  272. #else
  273. Assert.IsNotNull (fnf.Message, "#4");
  274. #endif
  275. Assert.IsNull (fnf.FusionLog, "#5");
  276. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  277. + ": "), "#6");
  278. Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#7");
  279. Assert.IsFalse (fnf.ToString ().IndexOf ("''") != -1, "#8");
  280. }
  281. [Test]
  282. public void Constructor4_Message_Empty ()
  283. {
  284. FileNotFoundException fnf = null;
  285. fnf = new FileNotFoundException (string.Empty, "file.txt");
  286. #if NET_2_0
  287. Assert.IsNotNull (fnf.Data, "#1");
  288. #endif
  289. Assert.IsNotNull (fnf.FileName, "#2");
  290. Assert.AreEqual ("file.txt", fnf.FileName, "#3");
  291. Assert.IsNull (fnf.InnerException, "#4");
  292. Assert.IsNotNull (fnf.Message, "#5");
  293. Assert.AreEqual (string.Empty, fnf.Message, "#6");
  294. Assert.IsNull (fnf.FusionLog, "#7");
  295. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  296. + ": " + Environment.NewLine), "#8");
  297. #if NET_2_0
  298. Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
  299. Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
  300. #else
  301. Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
  302. Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
  303. #endif
  304. }
  305. [Test]
  306. public void Constructor4_Message_Null ()
  307. {
  308. FileNotFoundException fnf = null;
  309. fnf = new FileNotFoundException ((string) null, "file.txt");
  310. #if NET_2_0
  311. Assert.IsNotNull (fnf.Data, "#A1");
  312. #endif
  313. Assert.IsNotNull (fnf.FileName, "#A2");
  314. Assert.AreEqual ("file.txt", fnf.FileName, "#A3");
  315. Assert.IsNull (fnf.InnerException, "#A4");
  316. Assert.IsNotNull (fnf.Message, "#A5");
  317. Assert.IsNull (fnf.FusionLog, "#A6");
  318. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  319. + ": "), "#A7");
  320. Assert.IsTrue (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#A8");
  321. #if NET_2_0
  322. Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#A9");
  323. Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#A10");
  324. #else
  325. Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#A9");
  326. Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#A10");
  327. #endif
  328. fnf = new FileNotFoundException ((string) null, string.Empty);
  329. #if NET_2_0
  330. Assert.IsNotNull (fnf.Data, "#B1");
  331. #endif
  332. Assert.IsNotNull (fnf.FileName, "#B2");
  333. Assert.AreEqual (string.Empty, fnf.FileName, "#B3");
  334. Assert.IsNull (fnf.InnerException, "#B4");
  335. // .NET 1.1: File or assembly name , or one of its dependencies ...
  336. // .NET 2.0: Could not load file or assembly '' or one of its ...
  337. Assert.IsNotNull (fnf.Message, "#B5");
  338. Assert.IsNull (fnf.FusionLog, "#B6");
  339. Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
  340. + ": "), "#B7");
  341. Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#B8");
  342. #if NET_2_0
  343. Assert.IsTrue (fnf.ToString ().IndexOf ("''") != -1, "#B9");
  344. #else
  345. Assert.IsFalse (fnf.ToString ().IndexOf ("''") != -1, "#B9");
  346. Assert.IsFalse (fnf.ToString ().IndexOf ("\"\"") != -1, "#B10");
  347. #endif
  348. }
  349. }
  350. }