2007-12-01 Olivier Dufour <olivier.duff@gmail.com>
[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
24         {
25                 string fileName;
26
27                 [SetUp]
28                 public void SetUp ()
29                 {
30                         fileName = Path.GetTempFileName ();
31                 }
32
33                 [TearDown]
34                 public void TearDown ()
35                 {
36                         File.Delete (fileName);
37                         
38 #if NET_2_0
39                         // This is totally out of place here, but since this test seem to be the last one run, and since nunit doesn't
40                         // offer any global TearDown method, it was the option I found.
41                         int c = Application.OpenForms.Count;
42                         if (c > 0) {
43                                 Console.WriteLine ("HEY!");
44                                 Console.WriteLine ("You created " + c.ToString () + " form(s) and you didn't dispose of them!");
45                                 Console.WriteLine ("Please modify your test to shut me up.");
46                         }
47 #endif
48                 }
49
50                 [Test]
51                 public void TestWriter ()
52                 {
53                         ResXResourceWriter w = new ResXResourceWriter (fileName);
54                         w.AddResource ("String", "hola");
55                         w.AddResource ("String2", (object) "hello");
56                         w.AddResource ("Int", 42);
57                         w.AddResource ("Enum", PlatformID.Win32NT);
58                         w.AddResource ("Convertible", new Point (43, 45));
59                         w.AddResource ("Serializable", new ArrayList (new byte [] { 1, 2, 3, 4 }));
60                         w.AddResource ("ByteArray", new byte [] { 12, 13, 14 });
61                         w.AddResource ("ByteArray2", (object) new byte [] { 15, 16, 17 });
62                         w.AddResource ("IntArray", new int [] { 1012, 1013, 1014 });
63                         w.AddResource ("StringArray", new string [] { "hello", "world" });
64                         w.AddResource ("Image", new Bitmap (1, 1));
65                         w.AddResource ("StrType", new MyStrType ("hello"));
66                         w.AddResource ("BinType", new MyBinType ("world"));
67
68                         try {
69                                 w.AddResource ("NonSerType", new MyNonSerializableType ());
70                                 Assert.Fail ("#0");
71                         } catch (InvalidOperationException) {
72                         }
73
74                         w.Generate ();
75                         w.Close ();
76
77                         ResXResourceReader r = new ResXResourceReader (fileName);
78                         Hashtable h = new Hashtable ();
79                         foreach (DictionaryEntry e in r) {
80                                 h.Add (e.Key, e.Value);
81                         }
82                         r.Close ();
83
84                         Assert.AreEqual ("hola", (string) h ["String"], "#1");
85                         Assert.AreEqual ("hello", (string) h ["String2"], "#2");
86                         Assert.AreEqual (42, (int) h ["Int"], "#3");
87                         Assert.AreEqual (PlatformID.Win32NT, (PlatformID) h ["Enum"], "#4");
88                         Assert.AreEqual (43, ((Point) h ["Convertible"]).X, "#5");
89                         Assert.AreEqual (2, (byte) ((ArrayList) h ["Serializable"]) [1], "#6");
90                         Assert.AreEqual (13, ((byte []) h ["ByteArray"]) [1], "#7");
91                         Assert.AreEqual (16, ((byte []) h ["ByteArray2"]) [1], "#8");
92                         Assert.AreEqual (1013, ((int []) h ["IntArray"]) [1], "#9");
93                         Assert.AreEqual ("world", ((string []) h ["StringArray"]) [1], "#10");
94                         Assert.AreEqual (typeof (Bitmap), h ["Image"].GetType (), "#11");
95                         Assert.AreEqual ("hello", ((MyStrType) h ["StrType"]).Value, "#12");
96                         Assert.AreEqual ("world", ((MyBinType) h ["BinType"]).Value, "#13");
97
98                         File.Delete (fileName);
99                 }
100         }
101
102         [Serializable]
103         [TypeConverter (typeof (MyStrTypeConverter))]
104         public class MyStrType
105         {
106                 public string Value;
107
108                 public MyStrType (string s)
109                 {
110                         Value = s;
111                 }
112         }
113
114         public class MyStrTypeConverter : TypeConverter
115         {
116                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
117                 {
118                         if (destinationType == typeof (string))
119                                 return true;
120                         return base.CanConvertTo (context, destinationType);
121                 }
122
123                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
124                 {
125                         if (sourceType == typeof (string))
126                                 return true;
127                         return base.CanConvertFrom (context, sourceType);
128                 }
129
130                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
131                 {
132                         if (destinationType == typeof (string))
133                                 return ((MyStrType) value).Value;
134                         return base.ConvertTo (context, culture, value, destinationType);
135                 }
136
137                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
138                 {
139                         if (value.GetType () == typeof (string))
140                                 return new MyStrType ((string) value);
141                         return base.ConvertFrom (context, culture, value);
142                 }
143         }
144
145         [Serializable]
146         [TypeConverter (typeof (MyBinTypeConverter))]
147         public class MyBinType
148         {
149                 public string Value;
150
151                 public MyBinType (string s)
152                 {
153                         Value = s;
154                 }
155         }
156
157         public class MyBinTypeConverter : TypeConverter
158         {
159                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
160                 {
161                         if (destinationType == typeof (byte []))
162                                 return true;
163                         return base.CanConvertTo (context, destinationType);
164                 }
165
166                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
167                 {
168                         if (sourceType == typeof (byte []))
169                                 return true;
170                         return base.CanConvertFrom (context, sourceType);
171                 }
172
173                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
174                 {
175                         if (destinationType == typeof (byte []))
176                                 return Encoding.Default.GetBytes (((MyBinType) value).Value);
177                         return base.ConvertTo (context, culture, value, destinationType);
178                 }
179
180                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
181                 {
182                         if (value.GetType () == typeof (byte []))
183                                 return new MyBinType (Encoding.Default.GetString ((byte []) value));
184                         return base.ConvertFrom (context, culture, value);
185                 }
186         }
187
188         [TypeConverter (typeof (MyNonSerializableTypeConverter))]
189         public class MyNonSerializableType
190         {
191                 public MyNonSerializableType ()
192                 {
193                 }
194         }
195
196         public class MyNonSerializableTypeConverter : TypeConverter
197         {
198                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
199                 {
200                         if (destinationType == typeof (byte []))
201                                 return true;
202                         return base.CanConvertTo (context, destinationType);
203                 }
204
205                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
206                 {
207                         if (sourceType == typeof (byte []))
208                                 return true;
209                         return base.CanConvertFrom (context, sourceType);
210                 }
211
212                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
213                 {
214                         if (destinationType == typeof (byte []))
215                                 return new byte [] { 0, 1, 2, 3 };
216                         return base.ConvertTo (context, culture, value, destinationType);
217                 }
218
219                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
220                 {
221                         if (value.GetType () == typeof (byte []))
222                                 return new MyNonSerializableType ();
223                         return base.ConvertFrom (context, culture, value);
224                 }
225         }
226 }