2 // WriterTest.cs: Unit Tests for ResXResourceWriter.
5 // Robert Jordan <robertj@gmx.net>
9 using System.Collections;
10 using System.ComponentModel;
11 using System.ComponentModel.Design;
13 using System.Globalization;
15 using System.Resources;
17 using System.Windows.Forms;
19 using NUnit.Framework;
21 namespace MonoTests.System.Resources
24 public class WriterTest : MonoTests.System.Windows.Forms.TestHelper
29 protected override void SetUp ()
31 fileName = Path.GetTempFileName ();
36 protected override void TearDown ()
38 File.Delete (fileName);
42 [Test] // ctor (Stream)
43 [NUnit.Framework.Category ("NotDotNet")]
44 public void Constructor1_Stream_Null ()
47 new ResXResourceWriter ((Stream) null);
49 } catch (ArgumentNullException ex) {
50 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
51 Assert.IsNull (ex.InnerException, "#3");
52 Assert.IsNotNull (ex.Message, "#4");
53 Assert.AreEqual ("stream", ex.ParamName, "#5");
57 [Test] // ctor (Stream)
58 [NUnit.Framework.Category ("NotDotNet")]
59 public void Constructor1_Stream_NotWritable ()
61 MemoryStream ms = new MemoryStream (new byte [0], false);
64 new ResXResourceWriter (ms);
66 } catch (ArgumentException ex) {
67 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
68 Assert.IsNull (ex.InnerException, "#3");
69 Assert.IsNotNull (ex.Message, "#4");
70 Assert.AreEqual ("stream", ex.ParamName, "#5");
74 [Test] // ctor (TextWriter)
75 [NUnit.Framework.Category ("NotDotNet")]
76 public void Constructor2_TextWriter_Null ()
79 new ResXResourceWriter ((TextWriter) null);
81 } catch (ArgumentNullException ex) {
82 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
83 Assert.IsNull (ex.InnerException, "#3");
84 Assert.IsNotNull (ex.Message, "#4");
85 Assert.AreEqual ("textWriter", ex.ParamName, "#5");
89 [Test] // ctor (String)
90 [NUnit.Framework.Category ("NotDotNet")]
91 public void Constructor3_FileName_Null ()
94 new ResXResourceWriter ((string) null);
96 } catch (ArgumentNullException ex) {
97 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
98 Assert.IsNull (ex.InnerException, "#3");
99 Assert.IsNotNull (ex.Message, "#4");
100 Assert.AreEqual ("fileName", ex.ParamName, "#5");
105 public void AddResource_WithComment ()
107 ResXResourceWriter w = new ResXResourceWriter (fileName);
108 ResXDataNode node = new ResXDataNode ("key", "value");
109 node.Comment = "comment is preserved";
110 w.AddResource (node);
114 ResXResourceReader r = new ResXResourceReader (fileName);
115 ITypeResolutionService typeres = null;
116 r.UseResXDataNodes = true;
119 foreach (DictionaryEntry o in r)
121 string key = o.Key.ToString();
122 node = (ResXDataNode)o.Value;
123 string value = node.GetValue (typeres).ToString ();
124 string comment = node.Comment;
126 Assert.AreEqual ("key", key, "key");
127 Assert.AreEqual ("value", value, "value");
128 Assert.AreEqual ("comment is preserved", comment, "comment");
129 Assert.AreEqual (0, count, "too many nodes");
134 File.Delete (fileName);
138 public void TestWriter ()
140 ResXResourceWriter w = new ResXResourceWriter (fileName);
141 w.AddResource ("String", "hola");
142 w.AddResource ("String2", (object) "hello");
143 w.AddResource ("Int", 42);
144 w.AddResource ("Enum", PlatformID.Win32NT);
145 w.AddResource ("Convertible", new Point (43, 45));
146 w.AddResource ("Serializable", new ArrayList (new byte [] { 1, 2, 3, 4 }));
147 w.AddResource ("ByteArray", new byte [] { 12, 13, 14 });
148 w.AddResource ("ByteArray2", (object) new byte [] { 15, 16, 17 });
149 w.AddResource ("IntArray", new int [] { 1012, 1013, 1014 });
150 w.AddResource ("StringArray", new string [] { "hello", "world" });
151 w.AddResource ("Image", new Bitmap (1, 1));
152 w.AddResource ("StrType", new MyStrType ("hello"));
153 w.AddResource ("BinType", new MyBinType ("world"));
156 w.AddResource ("NonSerType", new MyNonSerializableType ());
158 } catch (InvalidOperationException) {
164 ResXResourceReader r = new ResXResourceReader (fileName);
165 Hashtable h = new Hashtable ();
166 foreach (DictionaryEntry e in r) {
167 h.Add (e.Key, e.Value);
171 Assert.AreEqual ("hola", (string) h ["String"], "#1");
172 Assert.AreEqual ("hello", (string) h ["String2"], "#2");
173 Assert.AreEqual (42, (int) h ["Int"], "#3");
174 Assert.AreEqual (PlatformID.Win32NT, (PlatformID) h ["Enum"], "#4");
175 Assert.AreEqual (43, ((Point) h ["Convertible"]).X, "#5");
176 Assert.AreEqual (2, (byte) ((ArrayList) h ["Serializable"]) [1], "#6");
177 Assert.AreEqual (13, ((byte []) h ["ByteArray"]) [1], "#7");
178 Assert.AreEqual (16, ((byte []) h ["ByteArray2"]) [1], "#8");
179 Assert.AreEqual (1013, ((int []) h ["IntArray"]) [1], "#9");
180 Assert.AreEqual ("world", ((string []) h ["StringArray"]) [1], "#10");
181 Assert.AreEqual (typeof (Bitmap), h ["Image"].GetType (), "#11");
182 Assert.AreEqual ("hello", ((MyStrType) h ["StrType"]).Value, "#12");
183 Assert.AreEqual ("world", ((MyBinType) h ["BinType"]).Value, "#13");
185 File.Delete (fileName);
190 [TypeConverter (typeof (MyStrTypeConverter))]
191 public class MyStrType
195 public MyStrType (string s)
201 public class MyStrTypeConverter : TypeConverter
203 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
205 if (destinationType == typeof (string))
207 return base.CanConvertTo (context, destinationType);
210 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
212 if (sourceType == typeof (string))
214 return base.CanConvertFrom (context, sourceType);
217 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
219 if (destinationType == typeof (string))
220 return ((MyStrType) value).Value;
221 return base.ConvertTo (context, culture, value, destinationType);
224 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
226 if (value.GetType () == typeof (string))
227 return new MyStrType ((string) value);
228 return base.ConvertFrom (context, culture, value);
233 [TypeConverter (typeof (MyBinTypeConverter))]
234 public class MyBinType
238 public MyBinType (string s)
244 public class MyBinTypeConverter : TypeConverter
246 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
248 if (destinationType == typeof (byte []))
250 return base.CanConvertTo (context, destinationType);
253 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
255 if (sourceType == typeof (byte []))
257 return base.CanConvertFrom (context, sourceType);
260 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
262 if (destinationType == typeof (byte []))
263 return Encoding.Default.GetBytes (((MyBinType) value).Value);
264 return base.ConvertTo (context, culture, value, destinationType);
267 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
269 if (value.GetType () == typeof (byte []))
270 return new MyBinType (Encoding.Default.GetString ((byte []) value));
271 return base.ConvertFrom (context, culture, value);
275 [TypeConverter (typeof (MyNonSerializableTypeConverter))]
276 public class MyNonSerializableType
278 public MyNonSerializableType ()
283 public class MyNonSerializableTypeConverter : TypeConverter
285 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
287 if (destinationType == typeof (byte []))
289 return base.CanConvertTo (context, destinationType);
292 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
294 if (sourceType == typeof (byte []))
296 return base.CanConvertFrom (context, sourceType);
299 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
301 if (destinationType == typeof (byte []))
302 return new byte [] { 0, 1, 2, 3 };
303 return base.ConvertTo (context, culture, value, destinationType);
306 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
308 if (value.GetType () == typeof (byte []))
309 return new MyNonSerializableType ();
310 return base.ConvertFrom (context, culture, value);