2006-10-03 Robert Jordan <robertj@gmx.net>
[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 NUnit.Framework;
17
18 namespace MonoTests.System.Resources
19 {
20         [TestFixture]
21         public class WriterTest
22         {
23                 string fileName = Path.GetTempFileName ();
24
25                 [Test]
26                 public void TestWriter ()
27                 {
28                         ResXResourceWriter w = new ResXResourceWriter (fileName);
29                         w.AddResource ("String", "hola");
30                         w.AddResource ("String2", (object) "hello");
31                         w.AddResource ("Int", 42);
32                         w.AddResource ("Enum", PlatformID.Win32NT);
33                         w.AddResource ("Convertible", new Point (43, 45));
34                         w.AddResource ("Serializable", new ArrayList(new byte[] {1, 2, 3, 4}));
35                         w.AddResource ("ByteArray", new byte[] {12, 13, 14});
36                         w.AddResource ("ByteArray2", (object) new byte[] {15, 16, 17});
37                         w.AddResource ("IntArray", new int[] {1012, 1013, 1014});
38                         w.AddResource ("StringArray", new string[] {"hello", "world"});
39                         w.AddResource ("Image", new Bitmap (1, 1));
40                         w.AddResource ("StrType", new MyStrType ("hello"));
41                         w.AddResource ("BinType", new MyBinType ("world"));
42
43                         try {
44                                 w.AddResource ("NonSerType", new MyNonSerializableType ());
45                                 Assert.Fail ("#0");
46                         } catch (InvalidOperationException) {
47                         }
48
49                         w.Generate ();
50                         w.Close ();
51
52                         ResXResourceReader r = new ResXResourceReader (fileName);
53                         Hashtable h = new Hashtable();
54                         foreach (DictionaryEntry e in r) {
55                                 h.Add (e.Key, e.Value);
56                         }
57                         r.Close ();
58
59                         Assert.AreEqual ("hola", (string) h["String"], "#1");
60                         Assert.AreEqual ("hello", (string) h["String2"], "#2");
61                         Assert.AreEqual (42, (int) h["Int"], "#3");
62                         Assert.AreEqual (PlatformID.Win32NT, (PlatformID) h["Enum"], "#4");
63                         Assert.AreEqual (43, ((Point) h["Convertible"]).X, "#5");
64                         Assert.AreEqual (2, (byte) ((ArrayList) h["Serializable"])[1], "#6");
65                         Assert.AreEqual (13, ((byte[]) h["ByteArray"])[1], "#7");
66                         Assert.AreEqual (16, ((byte[]) h["ByteArray2"])[1], "#8");
67                         Assert.AreEqual (1013, ((int[]) h["IntArray"])[1], "#9");
68                         Assert.AreEqual ("world", ((string[]) h["StringArray"])[1], "#10");
69                         Assert.AreEqual (typeof (Bitmap), h["Image"].GetType (), "#11");
70                         Assert.AreEqual ("hello", ((MyStrType) h["StrType"]).Value, "#12");
71                         Assert.AreEqual ("world", ((MyBinType) h["BinType"]).Value, "#13");
72
73                         File.Delete (fileName);
74                 }
75         }
76
77         [Serializable]
78         [TypeConverter (typeof (MyStrTypeConverter))]
79         public class MyStrType
80         {
81                 public string Value;
82
83                 public MyStrType (string s)
84                 {
85                         Value = s;
86                 }
87         }
88
89         public class MyStrTypeConverter : TypeConverter
90         {
91                 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
92                 {
93                         if (destinationType == typeof (string)) 
94                                 return true;
95                         return base.CanConvertTo (context, destinationType);
96                 }
97
98                 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
99                 {
100                         if (sourceType == typeof (string)) 
101                                 return true;
102                         return base.CanConvertFrom (context, sourceType);
103                 }
104
105                 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
106                 {
107                         if (destinationType == typeof (string)) 
108                                 return ((MyStrType) value).Value;
109                         return base.ConvertTo (context, culture, value, destinationType);
110                 }
111
112                 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
113                 {
114                         if (value.GetType() == typeof (string))
115                                 return new MyStrType((string) value);
116                         return base.ConvertFrom (context, culture, value);
117                 }
118
119         }
120
121
122         [Serializable]
123         [TypeConverter (typeof (MyBinTypeConverter))]
124         public class MyBinType
125         {
126                 public string Value;
127
128                 public MyBinType (string s)
129                 {
130                         Value = s;
131                 }
132         }
133
134         public class MyBinTypeConverter : TypeConverter
135         {
136                 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
137                 {
138                         if (destinationType == typeof (byte[])) 
139                                 return true;
140                         return base.CanConvertTo (context, destinationType);
141                 }
142
143                 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
144                 {
145                         if (sourceType == typeof (byte[])) 
146                                 return true;
147                         return base.CanConvertFrom (context, sourceType);
148                 }
149
150                 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
151                 {
152                         if (destinationType == typeof (byte[])) 
153                                 return Encoding.Default.GetBytes (((MyBinType) value).Value);
154                         return base.ConvertTo (context, culture, value, destinationType);
155                 }
156
157                 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
158                 {
159                         if (value.GetType() == typeof (byte[]))
160                                 return new MyBinType (Encoding.Default.GetString ((byte[]) value));
161                         return base.ConvertFrom (context, culture, value);
162                 }
163
164         }
165
166
167         [TypeConverter (typeof (MyNonSerializableTypeConverter))]
168         public class MyNonSerializableType
169         {
170                 public MyNonSerializableType ()
171                 {
172                 }
173         }
174
175         public class MyNonSerializableTypeConverter : TypeConverter
176         {
177                 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
178                 {
179                         if (destinationType == typeof (byte[])) 
180                                 return true;
181                         return base.CanConvertTo (context, destinationType);
182                 }
183
184                 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
185                 {
186                         if (sourceType == typeof (byte[])) 
187                                 return true;
188                         return base.CanConvertFrom (context, sourceType);
189                 }
190
191                 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
192                 {
193                         if (destinationType == typeof (byte[])) 
194                                 return new byte[] {0, 1, 2, 3};
195                         return base.ConvertTo (context, culture, value, destinationType);
196                 }
197
198                 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
199                 {
200                         if (value.GetType() == typeof (byte[]))
201                                 return new MyNonSerializableType ();
202                         return base.ConvertFrom (context, culture, value);
203                 }
204
205         }
206 }