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