2 // WriterTest.cs: Unit Tests for ResXResourceWriter.
5 // Robert Jordan <robertj@gmx.net>
9 using System.Collections;
10 using System.ComponentModel;
12 using System.Globalization;
14 using System.Resources;
16 using System.Windows.Forms;
18 using NUnit.Framework;
20 namespace MonoTests.System.Resources
23 public class WriterTest : MonoTests.System.Windows.Forms.TestHelper
28 protected override void SetUp ()
30 fileName = Path.GetTempFileName ();
35 protected override void TearDown ()
37 File.Delete (fileName);
41 [Test] // ctor (Stream)
42 [NUnit.Framework.Category ("NotDotNet")]
43 public void Constructor1_Stream_Null ()
46 new ResXResourceWriter ((Stream) null);
48 } catch (ArgumentNullException ex) {
49 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
50 Assert.IsNull (ex.InnerException, "#3");
51 Assert.IsNotNull (ex.Message, "#4");
52 Assert.AreEqual ("stream", ex.ParamName, "#5");
56 [Test] // ctor (Stream)
57 [NUnit.Framework.Category ("NotDotNet")]
58 public void Constructor1_Stream_NotWritable ()
60 MemoryStream ms = new MemoryStream (new byte [0], false);
63 new ResXResourceWriter (ms);
65 } catch (ArgumentException ex) {
66 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
67 Assert.IsNull (ex.InnerException, "#3");
68 Assert.IsNotNull (ex.Message, "#4");
69 Assert.AreEqual ("stream", ex.ParamName, "#5");
73 [Test] // ctor (TextWriter)
74 [NUnit.Framework.Category ("NotDotNet")]
75 public void Constructor2_TextWriter_Null ()
78 new ResXResourceWriter ((TextWriter) null);
80 } catch (ArgumentNullException ex) {
81 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
82 Assert.IsNull (ex.InnerException, "#3");
83 Assert.IsNotNull (ex.Message, "#4");
84 Assert.AreEqual ("textWriter", ex.ParamName, "#5");
88 [Test] // ctor (String)
89 [NUnit.Framework.Category ("NotDotNet")]
90 public void Constructor3_FileName_Null ()
93 new ResXResourceWriter ((string) null);
95 } catch (ArgumentNullException ex) {
96 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
97 Assert.IsNull (ex.InnerException, "#3");
98 Assert.IsNotNull (ex.Message, "#4");
99 Assert.AreEqual ("fileName", ex.ParamName, "#5");
104 public void TestWriter ()
106 ResXResourceWriter w = new ResXResourceWriter (fileName);
107 w.AddResource ("String", "hola");
108 w.AddResource ("String2", (object) "hello");
109 w.AddResource ("Int", 42);
110 w.AddResource ("Enum", PlatformID.Win32NT);
111 w.AddResource ("Convertible", new Point (43, 45));
112 w.AddResource ("Serializable", new ArrayList (new byte [] { 1, 2, 3, 4 }));
113 w.AddResource ("ByteArray", new byte [] { 12, 13, 14 });
114 w.AddResource ("ByteArray2", (object) new byte [] { 15, 16, 17 });
115 w.AddResource ("IntArray", new int [] { 1012, 1013, 1014 });
116 w.AddResource ("StringArray", new string [] { "hello", "world" });
117 w.AddResource ("Image", new Bitmap (1, 1));
118 w.AddResource ("StrType", new MyStrType ("hello"));
119 w.AddResource ("BinType", new MyBinType ("world"));
122 w.AddResource ("NonSerType", new MyNonSerializableType ());
124 } catch (InvalidOperationException) {
130 ResXResourceReader r = new ResXResourceReader (fileName);
131 Hashtable h = new Hashtable ();
132 foreach (DictionaryEntry e in r) {
133 h.Add (e.Key, e.Value);
137 Assert.AreEqual ("hola", (string) h ["String"], "#1");
138 Assert.AreEqual ("hello", (string) h ["String2"], "#2");
139 Assert.AreEqual (42, (int) h ["Int"], "#3");
140 Assert.AreEqual (PlatformID.Win32NT, (PlatformID) h ["Enum"], "#4");
141 Assert.AreEqual (43, ((Point) h ["Convertible"]).X, "#5");
142 Assert.AreEqual (2, (byte) ((ArrayList) h ["Serializable"]) [1], "#6");
143 Assert.AreEqual (13, ((byte []) h ["ByteArray"]) [1], "#7");
144 Assert.AreEqual (16, ((byte []) h ["ByteArray2"]) [1], "#8");
145 Assert.AreEqual (1013, ((int []) h ["IntArray"]) [1], "#9");
146 Assert.AreEqual ("world", ((string []) h ["StringArray"]) [1], "#10");
147 Assert.AreEqual (typeof (Bitmap), h ["Image"].GetType (), "#11");
148 Assert.AreEqual ("hello", ((MyStrType) h ["StrType"]).Value, "#12");
149 Assert.AreEqual ("world", ((MyBinType) h ["BinType"]).Value, "#13");
151 File.Delete (fileName);
156 [TypeConverter (typeof (MyStrTypeConverter))]
157 public class MyStrType
161 public MyStrType (string s)
167 public class MyStrTypeConverter : TypeConverter
169 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
171 if (destinationType == typeof (string))
173 return base.CanConvertTo (context, destinationType);
176 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
178 if (sourceType == typeof (string))
180 return base.CanConvertFrom (context, sourceType);
183 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
185 if (destinationType == typeof (string))
186 return ((MyStrType) value).Value;
187 return base.ConvertTo (context, culture, value, destinationType);
190 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
192 if (value.GetType () == typeof (string))
193 return new MyStrType ((string) value);
194 return base.ConvertFrom (context, culture, value);
199 [TypeConverter (typeof (MyBinTypeConverter))]
200 public class MyBinType
204 public MyBinType (string s)
210 public class MyBinTypeConverter : TypeConverter
212 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
214 if (destinationType == typeof (byte []))
216 return base.CanConvertTo (context, destinationType);
219 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
221 if (sourceType == typeof (byte []))
223 return base.CanConvertFrom (context, sourceType);
226 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
228 if (destinationType == typeof (byte []))
229 return Encoding.Default.GetBytes (((MyBinType) value).Value);
230 return base.ConvertTo (context, culture, value, destinationType);
233 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
235 if (value.GetType () == typeof (byte []))
236 return new MyBinType (Encoding.Default.GetString ((byte []) value));
237 return base.ConvertFrom (context, culture, value);
241 [TypeConverter (typeof (MyNonSerializableTypeConverter))]
242 public class MyNonSerializableType
244 public MyNonSerializableType ()
249 public class MyNonSerializableTypeConverter : TypeConverter
251 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
253 if (destinationType == typeof (byte []))
255 return base.CanConvertTo (context, destinationType);
258 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
260 if (sourceType == typeof (byte []))
262 return base.CanConvertFrom (context, sourceType);
265 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
267 if (destinationType == typeof (byte []))
268 return new byte [] { 0, 1, 2, 3 };
269 return base.ConvertTo (context, culture, value, destinationType);
272 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
274 if (value.GetType () == typeof (byte []))
275 return new MyNonSerializableType ();
276 return base.ConvertFrom (context, culture, value);