* WriterTest.cs: Add a message if anyone forgets to close a form again.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Resources / ResXFileRefTest.cs
1 //
2 // ResXFileRefTest.cs: Unit Tests for ResXFileRef.
3 //
4 // Authors:
5 //     Gert Driesen <drieseng@users.sourceforge.net>
6 //
7
8 using System;
9 using System.ComponentModel;
10 using System.Drawing;
11 using System.IO;
12 using System.Resources;
13 using System.Text;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Resources
18 {
19         [TestFixture]
20         public class ResXFileRefTest
21         {
22                 [Test]
23                 public void Constructor1 ()
24                 {
25                         ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
26                         MonoTests.System.Windows.Forms.TestHelper.RemoveWarning (r);
27 #if NET_2_0
28                         Assert.AreEqual ("mono.bmp", r.FileName, "#1");
29                         Assert.AreEqual ("Bitmap", r.TypeName, "#2");
30 #endif
31                 }
32
33                 [Test]
34                 public void Constructor1_FileName_Null ()
35                 {
36 #if NET_2_0
37                         try {
38                                 new ResXFileRef ((string) null, "Bitmap");
39                                 Assert.Fail ("#1");
40                         } catch (ArgumentNullException ex) {
41                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
42                                 Assert.IsNotNull (ex.Message, "#3");
43                                 Assert.IsNotNull (ex.ParamName, "#4");
44                                 Assert.AreEqual ("fileName", ex.ParamName, "#5");
45                                 Assert.IsNull (ex.InnerException, "#6");
46                         }
47 #else
48                         ResXFileRef r = new ResXFileRef ((string) null, "Bitmap");
49                         Assert.AreEqual (";Bitmap", r.ToString ());
50 #endif
51                 }
52
53                 [Test]
54                 public void Constructor1_TypeName_Null ()
55                 {
56 #if NET_2_0
57                         try {
58                                 new ResXFileRef ("mono.bmp", (string) null);
59                                 Assert.Fail ("#1");
60                         } catch (ArgumentNullException ex) {
61                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
62                                 Assert.IsNotNull (ex.Message, "#3");
63                                 Assert.IsNotNull (ex.ParamName, "#4");
64                                 Assert.AreEqual ("typeName", ex.ParamName, "#5");
65                                 Assert.IsNull (ex.InnerException, "#6");
66                         }
67 #else
68                         ResXFileRef r = new ResXFileRef ("mono.bmp", (string) null);
69                         Assert.AreEqual ("mono.bmp;", r.ToString ());
70 #endif
71                 }
72
73 #if NET_2_0
74                 [Test]
75                 public void Constructor2 ()
76                 {
77                         Encoding utf8 = Encoding.UTF8;
78
79                         ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap", utf8);
80                         Assert.AreEqual ("mono.bmp", r.FileName, "#A1");
81                         Assert.AreSame (utf8, r.TextFileEncoding, "#A2");
82                         Assert.AreEqual ("Bitmap", r.TypeName, "#A3");
83
84                         r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
85                         Assert.AreEqual ("mono.bmp", r.FileName, "#B1");
86                         Assert.IsNull (r.TextFileEncoding, "#B2");
87                         Assert.AreEqual ("Bitmap", r.TypeName, "#B3");
88                 }
89
90                 [Test]
91                 public void Constructor2_FileName_Null ()
92                 {
93                         try {
94                                 new ResXFileRef ((string) null, "Bitmap", Encoding.UTF8);
95                                 Assert.Fail ("#1");
96                         } catch (ArgumentNullException ex) {
97                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
98                                 Assert.IsNotNull (ex.Message, "#3");
99                                 Assert.IsNotNull (ex.ParamName, "#4");
100                                 Assert.AreEqual ("fileName", ex.ParamName, "#5");
101                                 Assert.IsNull (ex.InnerException, "#6");
102                         }
103                 }
104
105                 [Test]
106                 public void Constructor2_TypeName_Null ()
107                 {
108                         try {
109                                 new ResXFileRef ("mono.bmp", (string) null, Encoding.UTF8);
110                                 Assert.Fail ("#1");
111                         } catch (ArgumentNullException ex) {
112                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
113                                 Assert.IsNotNull (ex.Message, "#3");
114                                 Assert.IsNotNull (ex.ParamName, "#4");
115                                 Assert.AreEqual ("typeName", ex.ParamName, "#5");
116                                 Assert.IsNull (ex.InnerException, "#6");
117                         }
118                 }
119 #endif
120
121                 [Test]
122                 public void ToStringTest ()
123                 {
124                         ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
125                         Assert.AreEqual ("mono.bmp;Bitmap", r.ToString (), "#1");
126
127 #if NET_2_0
128                         r = new ResXFileRef ("mono.bmp", "Bitmap", Encoding.UTF8);
129                         Assert.AreEqual ("mono.bmp;Bitmap;utf-8", r.ToString (), "#2");
130
131                         r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
132                         Assert.AreEqual ("mono.bmp;Bitmap", r.ToString (), "#3");
133 #endif
134                 }
135         }
136
137         [TestFixture]
138         public class ResXFileRefConverterTest
139         {
140                 [Test]
141                 public void SetUp ()
142                 {
143                         _converter = new ResXFileRef.Converter ();
144                         _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXResourceReaderTest");
145                         if (!Directory.Exists (_tempDirectory)) {
146                                 Directory.CreateDirectory (_tempDirectory);
147                         }
148                         _tempFileUTF7 = Path.Combine (_tempDirectory, "string_utf7.txt");
149                         using (StreamWriter sw = new StreamWriter (_tempFileUTF7, false, Encoding.UTF7)) {
150                                 sw.Write ("\u0021\u0026\u002A\u003B");
151                         }
152                 }
153
154                 public void TearDown ()
155                 {
156                         if (Directory.Exists (_tempDirectory)) {
157                                 Directory.Delete (_tempDirectory, true);
158                         }
159                 }
160
161                 [Test]
162                 public void CanConvertFrom ()
163                 {
164                         Assert.IsTrue (_converter.CanConvertFrom (typeof (string)), "#1");
165                         Assert.IsFalse (_converter.CanConvertFrom (typeof (byte [])), "#2");
166                 }
167
168                 [Test]
169                 public void CanConvertTo ()
170                 {
171                         Assert.IsTrue (_converter.CanConvertTo (typeof (string)), "#1");
172                         Assert.IsFalse (_converter.CanConvertTo (typeof (MemoryStream)), "#2");
173                         Assert.IsFalse (_converter.CanConvertTo (typeof (Bitmap)), "#3");
174                 }
175
176                 [Test]
177                 public void ConvertFrom_File_DoesNotExist ()
178                 {
179                         // file does not exist
180                         string fileRef = "doesnotexist.txt;" + typeof (string).AssemblyQualifiedName;
181                         try {
182                                 _converter.ConvertFrom (fileRef);
183                                 Assert.Fail ("#A1");
184                         } catch (FileNotFoundException ex) {
185                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
186                                 Assert.IsNull (ex.InnerException, "#A3");
187                                 Assert.IsNotNull (ex.FileName, "#A4");
188                                 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "doesnotexist.txt"), ex.FileName, "#A5");
189                                 Assert.IsNotNull (ex.Message, "#A6");
190                         }
191                 }
192
193                 [Test]
194                 public void ConvertFrom_Type_NotSet ()
195                 {
196                         string fileRef = "doesnotexist.txt";
197                         try {
198                                 _converter.ConvertFrom (fileRef);
199                                 Assert.Fail ("#B1");
200 #if NET_2_0
201                         } catch (ArgumentException ex) {
202                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
203                                 Assert.IsNull (ex.InnerException, "#B3");
204                                 Assert.IsNotNull (ex.Message, "#B4");
205                                 Assert.AreEqual ("value", ex.Message, "#B5");
206                                 Assert.IsNull (ex.ParamName, "#B6");
207                         }
208 #else
209                         } catch (IndexOutOfRangeException ex) {
210                                 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#B2");
211                                 Assert.IsNull (ex.InnerException, "#B3");
212                                 Assert.IsNotNull (ex.Message, "#B4");
213                         }
214 #endif
215                 }
216
217                 [Test]
218                 public void ConvertFrom_NotString ()
219                 {
220                         Assert.IsNull (_converter.ConvertFrom (null), "#G1");
221                         Assert.IsNull (_converter.ConvertFrom (1), "#G2");
222                         Assert.IsNull (_converter.ConvertFrom (true), "#G3");
223                 }
224
225                 [Test]
226                 public void ConvertFrom_Type_String ()
227                 {
228                         // read UTF-7 content without setting encoding
229                         string fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName;
230 #if NET_2_0
231                         string result = _converter.ConvertFrom (fileRef) as string;
232                         Assert.IsNotNull (result, "#A1");
233                         Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#A2");
234 #else
235                         try {
236                                 _converter.ConvertFrom (fileRef);
237                                 Assert.Fail ("#A");
238                         } catch (MissingMethodException) {
239                         }
240 #endif
241
242                         // read UTF-7 content using UTF-7 encoding
243                         fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName + ";utf-7";
244 #if NET_2_0
245                         result = _converter.ConvertFrom (fileRef) as string;
246                         Assert.IsNotNull (result, "#B1");
247                         Assert.AreEqual ("\u0021\u0026\u002A\u003B", result, "#B2");
248 #else
249                         try {
250                                 _converter.ConvertFrom (fileRef);
251                                 Assert.Fail ("#C");
252                         } catch (MissingMethodException) {
253                         }
254 #endif
255
256                         // invalid encoding
257                         fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName + ";utf-99";
258                         try {
259                                 _converter.ConvertFrom (fileRef);
260                                 Assert.Fail ("#D1");
261 #if NET_2_0
262                         } catch (ArgumentException ex) {
263                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
264                                 Assert.IsNull (ex.InnerException, "#D3");
265                                 Assert.IsNotNull (ex.Message, "#D4");
266                                 Assert.IsTrue (ex.Message.IndexOf ("'utf-99'") != -1, "#D5");
267                                 Assert.IsNotNull (ex.ParamName, "#D6");
268                                 Assert.AreEqual ("name", ex.ParamName, "#D7");
269                         }
270 #else
271                         } catch (MissingMethodException) {
272                         }
273 #endif
274                 }
275
276                 [Test]
277                 public void ConvertFrom_Type_StreamReader ()
278                 {
279                         // read UTF-7 content without setting encoding
280                         string fileRef = _tempFileUTF7 + ";" + typeof (StreamReader).AssemblyQualifiedName;
281                         using (StreamReader sr = (StreamReader) _converter.ConvertFrom (fileRef)) {
282                                 string result = sr.ReadToEnd ();
283                                 Assert.IsTrue (result.Length > 0, "#D1");
284                                 Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#D2");
285                         }
286
287                         // UTF-7 encoding is set, but not used
288                         fileRef = _tempFileUTF7 + ";" + typeof (StreamReader).AssemblyQualifiedName + ";utf-7";
289                         using (StreamReader sr = (StreamReader) _converter.ConvertFrom (fileRef)) {
290                                 string result = sr.ReadToEnd ();
291                                 Assert.IsTrue (result.Length > 0, "#F1");
292                                 Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#F2");
293                         }
294
295                         // invalid encoding is set, no error
296                         fileRef = _tempFileUTF7 + ";" + typeof (StreamReader).AssemblyQualifiedName + ";utf-99";
297                         using (StreamReader sr = (StreamReader) _converter.ConvertFrom (fileRef)) {
298                                 string result = sr.ReadToEnd ();
299                                 Assert.IsTrue (result.Length > 0, "#A1");
300                                 Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#A2");
301                         }
302                 }
303
304                 [Test]
305                 public void ConvertTo ()
306                 {
307                         ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
308                         Assert.AreEqual ("mono.bmp;Bitmap", (string) _converter.ConvertTo (
309                                 r, typeof (string)), "#1");
310
311 #if NET_2_0
312                         r = new ResXFileRef ("mono.bmp", "Bitmap", Encoding.UTF8);
313                         Assert.AreEqual ("mono.bmp;Bitmap;utf-8", (string) _converter.ConvertTo (
314                                 r, typeof (string)), "#2");
315
316                         r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
317                         Assert.AreEqual ("mono.bmp;Bitmap", (string) _converter.ConvertTo (
318                                 r, typeof (string)), "#3");
319 #endif
320                 }
321
322                 private TypeConverter _converter;
323                 private string _tempDirectory;
324                 private string _tempFileUTF7;
325         }
326 }