5e601d7e6ba15e4f1a28798f07f1c9df2db88471
[mono.git] / mcs / class / corlib / Test / System.IO / FileNotFoundExceptionTest.cs
1 //
2 // FileNotFoundExceptionTest.cs - Unit tests for
3 //      System.IO.FileNotFoundException
4 //
5 // Author:
6 //      Gert Driesen  <drieseng@users.sourceforge.net>
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
30 using System;
31 using System.IO;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.IO {
36         [TestFixture]
37         public class FileNotFoundExceptionTest {
38                 [Test]
39                 public void Constructor1 ()
40                 {
41                         FileNotFoundException fnf = new FileNotFoundException ();
42
43 #if NET_2_0
44                         Assert.IsNotNull (fnf.Data, "#1");
45 #endif
46                         Assert.IsNull (fnf.FileName, "#2");
47                         Assert.IsNull (fnf.InnerException, "#3");
48                         Assert.IsNotNull (fnf.Message, "#4"); // Unable to find the specified file
49                         Assert.IsNull (fnf.FusionLog, "#5");
50                         Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType().FullName), "#6");
51                 }
52
53                 [Test]
54                 public void Constructor2 ()
55                 {
56                         FileNotFoundException fnf = new FileNotFoundException ("message");
57
58 #if NET_2_0
59                         Assert.IsNotNull (fnf.Data, "#1");
60 #endif
61                         Assert.IsNull (fnf.FileName, "#2");
62                         Assert.IsNull (fnf.InnerException, "#3");
63                         Assert.IsNotNull (fnf.Message, "#4");
64                         Assert.AreEqual ("message", fnf.Message, "#5");
65                         Assert.IsNull (fnf.FusionLog, "#6");
66                         Assert.AreEqual (fnf.GetType ().FullName + ": message",
67                                 fnf.ToString (), "#7");
68                 }
69
70                 [Test]
71                 public void Constructor2_Message_Empty ()
72                 {
73                         FileNotFoundException fnf = new FileNotFoundException (string.Empty);
74
75 #if NET_2_0
76                         Assert.IsNotNull (fnf.Data, "#1");
77 #endif
78                         Assert.IsNull (fnf.FileName, "#2");
79                         Assert.IsNull (fnf.InnerException, "#3");
80                         Assert.IsNotNull (fnf.Message, "#4");
81                         Assert.AreEqual (string.Empty, fnf.Message, "#5");
82                         Assert.IsNull (fnf.FusionLog, "#6");
83                         Assert.AreEqual (fnf.GetType ().FullName + ": ",
84                                 fnf.ToString (), "#7");
85                 }
86
87                 [Test]
88                 public void Constructor2_Message_Null ()
89                 {
90                         FileNotFoundException fnf = new FileNotFoundException ((string) null);
91
92 #if NET_2_0
93                         Assert.IsNotNull (fnf.Data, "#1");
94 #endif
95                         Assert.IsNull (fnf.FileName, "#2");
96                         Assert.IsNull (fnf.InnerException, "#3");
97 #if NET_2_0
98                         Assert.IsNull (fnf.Message, "#4");
99 #else
100                         Assert.IsNotNull (fnf.Message, "#4"); // File or assembly name (null), or ...
101 #endif
102                         Assert.IsNull (fnf.FusionLog, "#5");
103 #if NET_2_0
104                         Assert.AreEqual (fnf.GetType ().FullName + ": ",
105                                 fnf.ToString (), "#6");
106 #else
107                         Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName), "#6");
108 #endif
109                 }
110
111                 [Test]
112                 public void Constructor3 ()
113                 {
114                         ArithmeticException ame = new ArithmeticException ("something");
115                         FileNotFoundException fnf = new FileNotFoundException ("message",
116                                 ame);
117
118 #if NET_2_0
119                         Assert.IsNotNull (fnf.Data, "#1");
120 #endif
121                         Assert.IsNull (fnf.FileName, "#2");
122                         Assert.IsNotNull (fnf.InnerException, "#3");
123                         Assert.AreSame (ame, fnf.InnerException, "#4");
124                         Assert.IsNotNull (fnf.Message, "#5");
125                         Assert.AreEqual ("message", fnf.Message, "#6");
126                         Assert.IsNull (fnf.FusionLog, "#7");
127                         Assert.AreEqual (fnf.GetType ().FullName + ": message ---> "
128                                 + ame.GetType ().FullName + ": something", fnf.ToString (), "#8");
129                 }
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
137 #if NET_2_0
138                         Assert.IsNotNull (fnf.Data, "#1");
139 #endif
140                         Assert.IsNull (fnf.FileName, "#2");
141                         Assert.IsNotNull (fnf.InnerException, "#3");
142                         Assert.AreSame (ame, fnf.InnerException, "#4");
143                         Assert.IsNotNull (fnf.Message, "#5");
144                         Assert.AreEqual (string.Empty, fnf.Message, "#6");
145                         Assert.IsNull (fnf.FusionLog, "#7");
146                         Assert.AreEqual (fnf.GetType ().FullName + ":  ---> "
147                                 + ame.GetType ().FullName + ": something", fnf.ToString (), "#8");
148                 }
149
150                 [Test]
151                 public void Constructor3_Message_Null ()
152                 {
153                         ArithmeticException ame = new ArithmeticException ("something");
154                         FileNotFoundException fnf = new FileNotFoundException ((string) null, ame);
155
156 #if NET_2_0
157                         Assert.IsNotNull (fnf.Data, "#1");
158 #endif
159                         Assert.IsNull (fnf.FileName, "#2");
160                         Assert.IsNotNull (fnf.InnerException, "#3");
161                         Assert.AreSame (ame, fnf.InnerException, "#4");
162 #if NET_2_0
163                         Assert.IsNull (fnf.Message, "#5");
164 #else
165                         Assert.IsNotNull (fnf.Message, "#5"); // File or assembly name (null), or ...
166 #endif
167                         Assert.IsNull (fnf.FusionLog, "#6");
168 #if NET_2_0
169                         Assert.AreEqual (fnf.GetType ().FullName + ":  ---> "
170                                 + ame.GetType ().FullName + ": something", fnf.ToString (), "#7");
171 #else
172                         Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName), "#7");
173                         Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#9");
174 #endif
175                 }
176
177                 [Test]
178                 public void Constructor3_InnerException_Null ()
179                 {
180                         FileNotFoundException fnf = new FileNotFoundException ("message",
181                                 (Exception) null);
182
183 #if NET_2_0
184                         Assert.IsNotNull (fnf.Data, "#1");
185 #endif
186                         Assert.IsNull (fnf.FileName, "#2");
187                         Assert.IsNull (fnf.InnerException, "#3");
188                         Assert.IsNotNull (fnf.Message, "#4");
189                         Assert.AreEqual ("message", fnf.Message, "#5");
190                         Assert.IsNull (fnf.FusionLog, "#6");
191                         Assert.AreEqual (fnf.GetType ().FullName + ": message",
192                                 fnf.ToString (), "#7");
193                 }
194
195                 [Test]
196                 public void Constructor4 ()
197                 {
198                         FileNotFoundException fnf = new FileNotFoundException ("message",
199                                 "file.txt");
200
201 #if NET_2_0
202                         Assert.IsNotNull (fnf.Data, "#1");
203 #endif
204                         Assert.IsNotNull (fnf.FileName, "#2");
205                         Assert.AreEqual ("file.txt", fnf.FileName, "#3");
206                         Assert.IsNull (fnf.InnerException, "#4");
207                         Assert.IsNotNull (fnf.Message, "#5");
208                         Assert.AreEqual ("message", fnf.Message, "#6");
209                         Assert.IsNull (fnf.FusionLog, "#7");
210                         Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
211                                 + ": message" + Environment.NewLine), "#8");
212 #if NET_2_0
213                         Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
214                         Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#9");
215 #else
216                         Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
217                         Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
218 #endif
219                 }
220
221                 [Test]
222                 public void Constructor4_FileName_Empty ()
223                 {
224                         FileNotFoundException fnf = new FileNotFoundException ("message",
225                                 string.Empty);
226
227 #if NET_2_0
228                         Assert.IsNotNull (fnf.Data, "#1");
229 #endif
230                         Assert.IsNotNull (fnf.FileName, "#2");
231                         Assert.AreEqual (string.Empty, fnf.FileName, "#3");
232                         Assert.IsNull (fnf.InnerException, "#4");
233                         Assert.IsNotNull (fnf.Message, "#5");
234                         Assert.AreEqual ("message", fnf.Message, "#6");
235                         Assert.IsNull (fnf.FusionLog, "#7");
236                         Assert.AreEqual (fnf.GetType ().FullName + ": message",
237                                 fnf.ToString (), "#8");
238                 }
239
240                 [Test]
241                 public void Constructor4_FileName_Null ()
242                 {
243                         FileNotFoundException fnf = new FileNotFoundException ("message",
244                                 (string) null);
245
246 #if NET_2_0
247                         Assert.IsNotNull (fnf.Data, "#A1");
248 #endif
249                         Assert.IsNull (fnf.FileName, "#A2");
250                         Assert.IsNull (fnf.InnerException, "#A3");
251                         Assert.IsNotNull (fnf.Message, "#A4");
252                         Assert.AreEqual ("message", fnf.Message, "#A5");
253                         Assert.IsNull (fnf.FusionLog, "#A6");
254                         Assert.AreEqual (fnf.GetType ().FullName + ": message",
255                                 fnf.ToString (), "#A7");
256
257                         fnf = new FileNotFoundException (string.Empty, (string) null);
258
259 #if NET_2_0
260                         Assert.IsNotNull (fnf.Data, "#B1");
261 #endif
262                         Assert.IsNull (fnf.FileName, "#B2");
263                         Assert.IsNull (fnf.InnerException, "#B3");
264                         Assert.IsNotNull (fnf.Message, "#B4");
265                         Assert.AreEqual (string.Empty, fnf.Message, "#B5");
266                         Assert.IsNull (fnf.FusionLog, "#B6");
267                         Assert.AreEqual (fnf.GetType ().FullName + ": ",
268                                 fnf.ToString (), "#B7");
269                 }
270
271                 [Test]
272                 public void Constructor4_FileNameAndMessage_Empty ()
273                 {
274                         FileNotFoundException fnf = new FileNotFoundException (string.Empty,
275                                 string.Empty);
276
277 #if NET_2_0
278                         Assert.IsNotNull (fnf.Data, "#1");
279 #endif
280                         Assert.IsNotNull (fnf.FileName, "#2");
281                         Assert.AreEqual (string.Empty, fnf.FileName, "#3");
282                         Assert.IsNull (fnf.InnerException, "#4");
283                         Assert.IsNotNull (fnf.Message, "#5");
284                         Assert.AreEqual (string.Empty, fnf.Message, "#6");
285                         Assert.IsNull (fnf.FusionLog, "#7");
286                         Assert.AreEqual (fnf.GetType ().FullName + ": ", fnf.ToString (), "#8");
287                 }
288
289                 [Test]
290                 public void Constructor4_FileNameAndMessage_Null ()
291                 {
292                         FileNotFoundException fnf = new FileNotFoundException ((string) null,
293                                 (string) null);
294
295 #if NET_2_0
296                         Assert.IsNotNull (fnf.Data, "#1");
297 #endif
298                         Assert.IsNull (fnf.FileName, "#2");
299                         Assert.IsNull (fnf.InnerException, "#3");
300 #if NET_2_0
301                         Assert.IsNull (fnf.Message, "#4");
302 #else
303                         Assert.IsNotNull (fnf.Message, "#4");
304 #endif
305                         Assert.IsNull (fnf.FusionLog, "#5");
306                         Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
307                                 + ": "), "#6");
308                         Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#7");
309                         Assert.IsFalse (fnf.ToString ().IndexOf ("''") != -1, "#8");
310                 }
311
312                 [Test]
313                 public void Constructor4_Message_Empty ()
314                 {
315                         FileNotFoundException fnf = null;
316                         
317                         fnf = new FileNotFoundException (string.Empty, "file.txt");
318
319 #if NET_2_0
320                         Assert.IsNotNull (fnf.Data, "#1");
321 #endif
322                         Assert.IsNotNull (fnf.FileName, "#2");
323                         Assert.AreEqual ("file.txt", fnf.FileName, "#3");
324                         Assert.IsNull (fnf.InnerException, "#4");
325                         Assert.IsNotNull (fnf.Message, "#5");
326                         Assert.AreEqual (string.Empty, fnf.Message, "#6");
327                         Assert.IsNull (fnf.FusionLog, "#7");
328                         Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
329                                 + ": " + Environment.NewLine), "#8");
330 #if NET_2_0
331                         Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
332                         Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
333 #else
334                         Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9");
335                         Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
336 #endif
337                 }
338
339                 [Test]
340                 public void Constructor4_Message_Null ()
341                 {
342                         FileNotFoundException fnf = null;
343                         
344                         fnf = new FileNotFoundException ((string) null, "file.txt");
345
346 #if NET_2_0
347                         Assert.IsNotNull (fnf.Data, "#A1");
348 #endif
349                         Assert.IsNotNull (fnf.FileName, "#A2");
350                         Assert.AreEqual ("file.txt", fnf.FileName, "#A3");
351                         Assert.IsNull (fnf.InnerException, "#A4");
352                         Assert.IsNotNull (fnf.Message, "#A5");
353                         Assert.IsNull (fnf.FusionLog, "#A6");
354                         Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
355                                 + ": "), "#A7");
356                         Assert.IsTrue (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#A8");
357 #if NET_2_0
358                         Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#A9");
359                         Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#A10");
360 #else
361                         Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#A9");
362                         Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#A10");
363 #endif
364
365                         fnf = new FileNotFoundException ((string) null, string.Empty);
366
367 #if NET_2_0
368                         Assert.IsNotNull (fnf.Data, "#B1");
369 #endif
370                         Assert.IsNotNull (fnf.FileName, "#B2");
371                         Assert.AreEqual (string.Empty, fnf.FileName, "#B3");
372                         Assert.IsNull (fnf.InnerException, "#B4");
373                         // .NET 1.1: File or assembly name , or one of its dependencies ...
374                         // .NET 2.0: Could not load file or assembly '' or one of its ...
375                         Assert.IsNotNull (fnf.Message, "#B5");
376                         Assert.IsNull (fnf.FusionLog, "#B6");
377                         Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName
378                                 + ": "), "#B7");
379                         Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#B8");
380 #if NET_2_0
381                         Assert.IsTrue (fnf.ToString ().IndexOf ("''") != -1, "#B9");
382 #else
383                         Assert.IsFalse (fnf.ToString ().IndexOf ("''") != -1, "#B9");
384                         Assert.IsFalse (fnf.ToString ().IndexOf ("\"\"") != -1, "#B10");
385 #endif
386                 }
387         }
388 }