Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Resources / WriterTest.cs
1 //
2 // WriterTest.cs: Unit Tests for ResXResourceWriter.
3 //
4 // Authors:
5 //     Robert Jordan <robertj@gmx.net>
6 //
7
8 using System;
9 using System.Collections;
10 using System.ComponentModel;
11 using System.ComponentModel.Design;
12 using System.Drawing;
13 using System.Globalization;
14 using System.IO;
15 using System.Resources;
16 using System.Text;
17 using System.Windows.Forms;
18
19 using NUnit.Framework;
20
21 namespace MonoTests.System.Resources
22 {
23         [TestFixture]
24         public class WriterTest : MonoTests.System.Windows.Forms.TestHelper
25         {
26                 string fileName;
27
28                 [SetUp]
29                 protected override void SetUp ()
30                 {
31                         fileName = Path.GetTempFileName ();
32                         base.SetUp ();
33                 }
34
35                 [TearDown]
36                 protected override void TearDown ()
37                 {
38                         File.Delete (fileName);
39                         base.TearDown ();
40                 }
41
42                 [Test] // ctor (Stream)
43                 [NUnit.Framework.Category ("NotDotNet")]
44                 public void Constructor1_Stream_Null ()
45                 {
46                         try {
47                                 new ResXResourceWriter ((Stream) null);
48                                 Assert.Fail ("#1");
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");
54                         }
55                 }
56
57                 [Test] // ctor (Stream)
58                 [NUnit.Framework.Category ("NotDotNet")]
59                 public void Constructor1_Stream_NotWritable ()
60                 {
61                         MemoryStream ms = new MemoryStream (new byte [0], false);
62
63                         try {
64                                 new ResXResourceWriter (ms);
65                                 Assert.Fail ("#1");
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");
71                         }
72                 }
73
74                 [Test] // ctor (TextWriter)
75                 [NUnit.Framework.Category ("NotDotNet")]
76                 public void Constructor2_TextWriter_Null ()
77                 {
78                         try {
79                                 new ResXResourceWriter ((TextWriter) null);
80                                 Assert.Fail ("#1");
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");
86                         }
87                 }
88
89                 [Test] // ctor (String)
90                 [NUnit.Framework.Category ("NotDotNet")]
91                 public void Constructor3_FileName_Null ()
92                 {
93                         try {
94                                 new ResXResourceWriter ((string) null);
95                                 Assert.Fail ("#1");
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");
101                         }
102                 }
103
104                 [Test]
105                 public void AddResource_WithComment ()
106                 {
107                         ResXResourceWriter w = new ResXResourceWriter (fileName);
108                         ResXDataNode node = new ResXDataNode ("key", "value");
109                         node.Comment = "comment is preserved";
110                         w.AddResource (node);
111                         w.Generate ();
112                         w.Close ();
113
114                         ResXResourceReader r = new ResXResourceReader (fileName);
115                         ITypeResolutionService typeres = null;
116                         r.UseResXDataNodes = true;
117                         
118                         int count = 0;
119                         foreach (DictionaryEntry o in r)
120                         {
121                                 string key = o.Key.ToString();
122                                 node = (ResXDataNode)o.Value;
123                                 string value = node.GetValue (typeres).ToString ();
124                                 string comment = node.Comment;
125
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");
130                                 count++;
131                         }
132                         r.Close ();
133
134                         File.Delete (fileName);
135                 }
136
137                 [Test]
138                 public void TestWriter ()
139                 {
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"));
154
155                         try {
156                                 w.AddResource ("NonSerType", new MyNonSerializableType ());
157                                 Assert.Fail ("#0");
158                         } catch (InvalidOperationException) {
159                         }
160
161                         w.Generate ();
162                         w.Close ();
163
164                         ResXResourceReader r = new ResXResourceReader (fileName);
165                         Hashtable h = new Hashtable ();
166                         foreach (DictionaryEntry e in r) {
167                                 h.Add (e.Key, e.Value);
168                         }
169                         r.Close ();
170
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");
184
185                         File.Delete (fileName);
186                 }
187         }
188
189         [Serializable]
190         [TypeConverter (typeof (MyStrTypeConverter))]
191         public class MyStrType
192         {
193                 public string Value;
194
195                 public MyStrType (string s)
196                 {
197                         Value = s;
198                 }
199         }
200
201         public class MyStrTypeConverter : TypeConverter
202         {
203                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
204                 {
205                         if (destinationType == typeof (string))
206                                 return true;
207                         return base.CanConvertTo (context, destinationType);
208                 }
209
210                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
211                 {
212                         if (sourceType == typeof (string))
213                                 return true;
214                         return base.CanConvertFrom (context, sourceType);
215                 }
216
217                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
218                 {
219                         if (destinationType == typeof (string))
220                                 return ((MyStrType) value).Value;
221                         return base.ConvertTo (context, culture, value, destinationType);
222                 }
223
224                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
225                 {
226                         if (value.GetType () == typeof (string))
227                                 return new MyStrType ((string) value);
228                         return base.ConvertFrom (context, culture, value);
229                 }
230         }
231
232         [Serializable]
233         [TypeConverter (typeof (MyBinTypeConverter))]
234         public class MyBinType
235         {
236                 public string Value;
237
238                 public MyBinType (string s)
239                 {
240                         Value = s;
241                 }
242         }
243
244         public class MyBinTypeConverter : TypeConverter
245         {
246                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
247                 {
248                         if (destinationType == typeof (byte []))
249                                 return true;
250                         return base.CanConvertTo (context, destinationType);
251                 }
252
253                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
254                 {
255                         if (sourceType == typeof (byte []))
256                                 return true;
257                         return base.CanConvertFrom (context, sourceType);
258                 }
259
260                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
261                 {
262                         if (destinationType == typeof (byte []))
263                                 return Encoding.Default.GetBytes (((MyBinType) value).Value);
264                         return base.ConvertTo (context, culture, value, destinationType);
265                 }
266
267                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
268                 {
269                         if (value.GetType () == typeof (byte []))
270                                 return new MyBinType (Encoding.Default.GetString ((byte []) value));
271                         return base.ConvertFrom (context, culture, value);
272                 }
273         }
274
275         [TypeConverter (typeof (MyNonSerializableTypeConverter))]
276         public class MyNonSerializableType
277         {
278                 public MyNonSerializableType ()
279                 {
280                 }
281         }
282
283         public class MyNonSerializableTypeConverter : TypeConverter
284         {
285                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
286                 {
287                         if (destinationType == typeof (byte []))
288                                 return true;
289                         return base.CanConvertTo (context, destinationType);
290                 }
291
292                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
293                 {
294                         if (sourceType == typeof (byte []))
295                                 return true;
296                         return base.CanConvertFrom (context, sourceType);
297                 }
298
299                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
300                 {
301                         if (destinationType == typeof (byte []))
302                                 return new byte [] { 0, 1, 2, 3 };
303                         return base.ConvertTo (context, culture, value, destinationType);
304                 }
305
306                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
307                 {
308                         if (value.GetType () == typeof (byte []))
309                                 return new MyNonSerializableType ();
310                         return base.ConvertFrom (context, culture, value);
311                 }
312         }
313 }