2 // ResXFileRefTest.cs: Unit Tests for ResXFileRef.
5 // Gert Driesen <drieseng@users.sourceforge.net>
9 using System.ComponentModel;
12 using System.Resources;
15 using NUnit.Framework;
17 namespace MonoTests.System.Resources
20 public class ResXFileRefTest
23 public void Constructor1 ()
25 ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
26 MonoTests.System.Windows.Forms.TestHelper.RemoveWarning (r);
28 Assert.AreEqual ("mono.bmp", r.FileName, "#1");
29 Assert.AreEqual ("Bitmap", r.TypeName, "#2");
34 public void Constructor1_FileName_Null ()
38 new ResXFileRef ((string) null, "Bitmap");
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");
48 ResXFileRef r = new ResXFileRef ((string) null, "Bitmap");
49 Assert.AreEqual (";Bitmap", r.ToString ());
54 public void Constructor1_TypeName_Null ()
58 new ResXFileRef ("mono.bmp", (string) null);
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");
68 ResXFileRef r = new ResXFileRef ("mono.bmp", (string) null);
69 Assert.AreEqual ("mono.bmp;", r.ToString ());
75 public void Constructor2 ()
77 Encoding utf8 = Encoding.UTF8;
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");
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");
91 public void Constructor2_FileName_Null ()
94 new ResXFileRef ((string) null, "Bitmap", Encoding.UTF8);
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");
106 public void Constructor2_TypeName_Null ()
109 new ResXFileRef ("mono.bmp", (string) null, Encoding.UTF8);
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");
122 public void ToStringTest ()
124 ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
125 Assert.AreEqual ("mono.bmp;Bitmap", r.ToString (), "#1");
128 r = new ResXFileRef ("mono.bmp", "Bitmap", Encoding.UTF8);
129 Assert.AreEqual ("mono.bmp;Bitmap;utf-8", r.ToString (), "#2");
131 r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
132 Assert.AreEqual ("mono.bmp;Bitmap", r.ToString (), "#3");
138 public class ResXFileRefConverterTest
143 _converter = new ResXFileRef.Converter ();
144 _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXResourceReaderTest");
145 if (!Directory.Exists (_tempDirectory)) {
146 Directory.CreateDirectory (_tempDirectory);
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");
154 public void TearDown ()
156 if (Directory.Exists (_tempDirectory)) {
157 Directory.Delete (_tempDirectory, true);
162 public void CanConvertFrom ()
164 Assert.IsTrue (_converter.CanConvertFrom (typeof (string)), "#1");
165 Assert.IsFalse (_converter.CanConvertFrom (typeof (byte [])), "#2");
169 public void CanConvertTo ()
171 Assert.IsTrue (_converter.CanConvertTo (typeof (string)), "#1");
172 Assert.IsFalse (_converter.CanConvertTo (typeof (MemoryStream)), "#2");
173 Assert.IsFalse (_converter.CanConvertTo (typeof (Bitmap)), "#3");
177 public void ConvertFrom_File_DoesNotExist ()
179 // file does not exist
180 string fileRef = "doesnotexist.txt;" + typeof (string).AssemblyQualifiedName;
182 _converter.ConvertFrom (fileRef);
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");
194 public void ConvertFrom_Type_NotSet ()
196 string fileRef = "doesnotexist.txt";
198 _converter.ConvertFrom (fileRef);
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");
209 } catch (IndexOutOfRangeException ex) {
210 Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#B2");
211 Assert.IsNull (ex.InnerException, "#B3");
212 Assert.IsNotNull (ex.Message, "#B4");
218 public void ConvertFrom_NotString ()
220 Assert.IsNull (_converter.ConvertFrom (null), "#G1");
221 Assert.IsNull (_converter.ConvertFrom (1), "#G2");
222 Assert.IsNull (_converter.ConvertFrom (true), "#G3");
226 public void ConvertFrom_Type_String ()
228 // read UTF-7 content without setting encoding
229 string fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName;
231 string result = _converter.ConvertFrom (fileRef) as string;
232 Assert.IsNotNull (result, "#A1");
233 Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#A2");
236 _converter.ConvertFrom (fileRef);
238 } catch (MissingMethodException) {
242 // read UTF-7 content using UTF-7 encoding
243 fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName + ";utf-7";
245 result = _converter.ConvertFrom (fileRef) as string;
246 Assert.IsNotNull (result, "#B1");
247 Assert.AreEqual ("\u0021\u0026\u002A\u003B", result, "#B2");
250 _converter.ConvertFrom (fileRef);
252 } catch (MissingMethodException) {
257 fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName + ";utf-99";
259 _converter.ConvertFrom (fileRef);
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");
271 } catch (MissingMethodException) {
277 public void ConvertFrom_Type_StreamReader ()
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");
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");
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");
305 public void ConvertFrom_Type_MemoryStream ()
307 string fileRef = _tempFileUTF7 + ";" + typeof (MemoryStream).AssemblyQualifiedName;
309 using (MemoryStream ms = (MemoryStream) _converter.ConvertFrom (fileRef)) {
310 Assert.IsTrue (ms.Length > 0);
314 _converter.ConvertFrom (fileRef);
316 } catch (MissingMethodException) {
322 public void ConvertTo ()
324 ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
325 Assert.AreEqual ("mono.bmp;Bitmap", (string) _converter.ConvertTo (
326 r, typeof (string)), "#1");
329 r = new ResXFileRef ("mono.bmp", "Bitmap", Encoding.UTF8);
330 Assert.AreEqual ("mono.bmp;Bitmap;utf-8", (string) _converter.ConvertTo (
331 r, typeof (string)), "#2");
333 r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
334 Assert.AreEqual ("mono.bmp;Bitmap", (string) _converter.ConvertTo (
335 r, typeof (string)), "#3");
339 private TypeConverter _converter;
340 private string _tempDirectory;
341 private string _tempFileUTF7;