* docs.make, Makefile.am: Build mono-file-formats{.tree,.zip},
[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.Drawing;
12 using System.Globalization;
13 using System.IO;
14 using System.Resources;
15 using System.Text;
16 using System.Windows.Forms;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.System.Resources
21 {
22         [TestFixture]
23         public class WriterTest : MonoTests.System.Windows.Forms.TestHelper
24         {
25                 string fileName;
26
27                 [SetUp]
28                 protected override void SetUp ()
29                 {
30                         fileName = Path.GetTempFileName ();
31                         base.SetUp ();
32                 }
33
34                 [TearDown]
35                 protected override void TearDown ()
36                 {
37                         File.Delete (fileName);
38                         base.TearDown ();
39                 }
40
41                 [Test] // ctor (Stream)
42                 [NUnit.Framework.Category ("NotDotNet")]
43                 public void Constructor1_Stream_Null ()
44                 {
45                         try {
46                                 new ResXResourceWriter ((Stream) null);
47                                 Assert.Fail ("#1");
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");
53                         }
54                 }
55
56                 [Test] // ctor (Stream)
57                 [NUnit.Framework.Category ("NotDotNet")]
58                 public void Constructor1_Stream_NotWritable ()
59                 {
60                         MemoryStream ms = new MemoryStream (new byte [0], false);
61
62                         try {
63                                 new ResXResourceWriter (ms);
64                                 Assert.Fail ("#1");
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");
70                         }
71                 }
72
73                 [Test] // ctor (TextWriter)
74                 [NUnit.Framework.Category ("NotDotNet")]
75                 public void Constructor2_TextWriter_Null ()
76                 {
77                         try {
78                                 new ResXResourceWriter ((TextWriter) null);
79                                 Assert.Fail ("#1");
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");
85                         }
86                 }
87
88                 [Test] // ctor (String)
89                 [NUnit.Framework.Category ("NotDotNet")]
90                 public void Constructor3_FileName_Null ()
91                 {
92                         try {
93                                 new ResXResourceWriter ((string) null);
94                                 Assert.Fail ("#1");
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");
100                         }
101                 }
102
103                 [Test]
104                 public void TestWriter ()
105                 {
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"));
120
121                         try {
122                                 w.AddResource ("NonSerType", new MyNonSerializableType ());
123                                 Assert.Fail ("#0");
124                         } catch (InvalidOperationException) {
125                         }
126
127                         w.Generate ();
128                         w.Close ();
129
130                         ResXResourceReader r = new ResXResourceReader (fileName);
131                         Hashtable h = new Hashtable ();
132                         foreach (DictionaryEntry e in r) {
133                                 h.Add (e.Key, e.Value);
134                         }
135                         r.Close ();
136
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");
150
151                         File.Delete (fileName);
152                 }
153         }
154
155         [Serializable]
156         [TypeConverter (typeof (MyStrTypeConverter))]
157         public class MyStrType
158         {
159                 public string Value;
160
161                 public MyStrType (string s)
162                 {
163                         Value = s;
164                 }
165         }
166
167         public class MyStrTypeConverter : TypeConverter
168         {
169                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
170                 {
171                         if (destinationType == typeof (string))
172                                 return true;
173                         return base.CanConvertTo (context, destinationType);
174                 }
175
176                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
177                 {
178                         if (sourceType == typeof (string))
179                                 return true;
180                         return base.CanConvertFrom (context, sourceType);
181                 }
182
183                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
184                 {
185                         if (destinationType == typeof (string))
186                                 return ((MyStrType) value).Value;
187                         return base.ConvertTo (context, culture, value, destinationType);
188                 }
189
190                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
191                 {
192                         if (value.GetType () == typeof (string))
193                                 return new MyStrType ((string) value);
194                         return base.ConvertFrom (context, culture, value);
195                 }
196         }
197
198         [Serializable]
199         [TypeConverter (typeof (MyBinTypeConverter))]
200         public class MyBinType
201         {
202                 public string Value;
203
204                 public MyBinType (string s)
205                 {
206                         Value = s;
207                 }
208         }
209
210         public class MyBinTypeConverter : TypeConverter
211         {
212                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
213                 {
214                         if (destinationType == typeof (byte []))
215                                 return true;
216                         return base.CanConvertTo (context, destinationType);
217                 }
218
219                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
220                 {
221                         if (sourceType == typeof (byte []))
222                                 return true;
223                         return base.CanConvertFrom (context, sourceType);
224                 }
225
226                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
227                 {
228                         if (destinationType == typeof (byte []))
229                                 return Encoding.Default.GetBytes (((MyBinType) value).Value);
230                         return base.ConvertTo (context, culture, value, destinationType);
231                 }
232
233                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
234                 {
235                         if (value.GetType () == typeof (byte []))
236                                 return new MyBinType (Encoding.Default.GetString ((byte []) value));
237                         return base.ConvertFrom (context, culture, value);
238                 }
239         }
240
241         [TypeConverter (typeof (MyNonSerializableTypeConverter))]
242         public class MyNonSerializableType
243         {
244                 public MyNonSerializableType ()
245                 {
246                 }
247         }
248
249         public class MyNonSerializableTypeConverter : TypeConverter
250         {
251                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
252                 {
253                         if (destinationType == typeof (byte []))
254                                 return true;
255                         return base.CanConvertTo (context, destinationType);
256                 }
257
258                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
259                 {
260                         if (sourceType == typeof (byte []))
261                                 return true;
262                         return base.CanConvertFrom (context, sourceType);
263                 }
264
265                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
266                 {
267                         if (destinationType == typeof (byte []))
268                                 return new byte [] { 0, 1, 2, 3 };
269                         return base.ConvertTo (context, culture, value, destinationType);
270                 }
271
272                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
273                 {
274                         if (value.GetType () == typeof (byte []))
275                                 return new MyNonSerializableType ();
276                         return base.ConvertFrom (context, culture, value);
277                 }
278         }
279 }