2 // MonoTests.System.Resources.ResourceReaderTest.cs
5 // Nick Drochak (ndrochak@gol.com)
7 // (C) 2001 Nick Drochak II
8 // Copyright (C) 2004 Novell (http://www.novell.com)
12 using System.Collections;
14 using System.Reflection;
15 using System.Resources;
17 using NUnit.Framework;
19 namespace MonoTests.System.Resources
22 public class ResourceReaderTest
24 internal static string m_ResourceFile;
25 private static string m_BadResourceFile;
26 private string _tempResourceFile;
29 public void FixtureSetUp ()
31 string base_path = Path.Combine (Directory.GetCurrentDirectory (), Path.Combine ("Test", "resources"));
32 m_ResourceFile = Path.Combine (base_path, "MyResources.resources");
33 m_BadResourceFile = Path.Combine (base_path, "Empty.resources");
39 _tempResourceFile = Path.GetTempFileName ();
43 public void TearDown ()
45 File.Delete (_tempResourceFile);
49 public void ConstructorString_Path_Null ()
52 new ResourceReader ((string) null);
54 } catch (ArgumentNullException ex) {
55 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
56 Assert.IsNull (ex.InnerException, "#3");
57 Assert.IsNotNull (ex.Message, "#4");
58 Assert.IsNotNull (ex.ParamName, "#5");
59 Assert.AreEqual ("path", ex.ParamName, "#6");
64 public void ConstructorString_Path_Empty ()
67 new ResourceReader (String.Empty);
69 } catch (ArgumentException ex) {
70 // Empty path name is not legal
71 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
72 Assert.IsNull (ex.InnerException, "#3");
73 Assert.IsNotNull (ex.Message, "#4");
74 Assert.IsNull (ex.ParamName, "#5");
79 [ExpectedException (typeof (FileNotFoundException))]
80 public void ConstructorString_NotFound ()
82 // use a file name that is *very* unlikely to exsist
83 new ResourceReader ("j38f8axvnn9h38hfa9nxn93f8hav4zvag87vvah32o");
87 [Category ("MobileNotWorking")]
88 [ExpectedException (typeof (BadImageFormatException))]
89 public void ConstructorString_Bad ()
91 Assert.IsTrue (File.Exists (m_BadResourceFile));
92 new ResourceReader (m_BadResourceFile);
96 [Category ("MobileNotWorking")]
97 public void ConstructorString ()
99 if (!File.Exists (m_ResourceFile)) {
100 Assert.Fail ("Resource file is not where it should be:" + Path.Combine (Directory.GetCurrentDirectory (), m_ResourceFile));
102 ResourceReader r = new ResourceReader (m_ResourceFile);
103 Assert.IsNotNull (r, "ResourceReader");
108 public void ConstructorStream_Null ()
111 new ResourceReader ((Stream) null);
113 } catch (ArgumentNullException ex) {
114 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
115 Assert.IsNull (ex.InnerException, "#3");
116 Assert.IsNotNull (ex.Message, "#4");
117 Assert.IsNotNull (ex.ParamName, "#5");
118 Assert.AreEqual ("stream", ex.ParamName, "#6");
123 [Category ("MobileNotWorking")]
124 public void ConstructorStream_Closed ()
126 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
130 new ResourceReader (stream);
132 } catch (ArgumentException ex) {
133 // Stream was not readable
134 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
135 Assert.IsNull (ex.InnerException, "#3");
136 Assert.IsNotNull (ex.Message, "#4");
137 Assert.IsNull (ex.ParamName, "#5");
142 [Category ("MobileNotWorking")]
143 public void Stream ()
145 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
146 ResourceReader r = new ResourceReader (stream);
147 Assert.IsNotNull (r, "ResourceReader");
152 [Category ("MobileNotWorking")]
155 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
156 ResourceReader r = new ResourceReader (stream);
159 stream = new FileStream (m_ResourceFile, FileMode.Open);
160 Assert.IsNotNull (stream, "FileStream");
165 [Category ("MobileNotWorking")]
166 public void Enumerator ()
168 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
169 ResourceReader reader = new ResourceReader (stream);
171 IDictionaryEnumerator en = reader.GetEnumerator ();
172 // Goes through the enumerator, printing out the key and value pairs.
173 while (en.MoveNext ()) {
174 DictionaryEntry de = (DictionaryEntry) en.Current;
175 Assert.IsTrue (String.Empty != (string) de.Key, "Current.Key should not be empty");
176 Assert.IsTrue (String.Empty != (string) de.Value, "Current.Value should not be empty");
177 Assert.IsTrue (String.Empty != (string) en.Key, "Entry.Key should not be empty");
178 Assert.IsTrue (String.Empty != (string) en.Value, "Entry.Value should not be empty");
184 public void ReadNullResource ()
186 MemoryStream stream = new MemoryStream ();
188 ResourceWriter rw = new ResourceWriter (stream);
189 rw.AddResource ("NullTest", value);
193 using (ResourceReader rr = new ResourceReader (stream)) {
195 foreach (DictionaryEntry de in rr) {
196 Assert.AreEqual ("NullTest", de.Key, "#1");
197 Assert.IsNull (de.Value, "#2");
198 Assert.AreEqual (0, entryCount, "#3");
201 Assert.AreEqual (1, entryCount, "#4");
206 public void ByteArray ()
208 byte [] content = new byte [] { 1, 2, 3, 4, 5, 6 };
210 Stream stream = null;
212 // we currently do not support writing v2 resource files
213 stream = new MemoryStream ();
214 stream.Write (byte_resource_v2, 0, byte_resource_v2.Length);
219 using (IResourceReader rr = new ResourceReader (stream)) {
220 foreach (DictionaryEntry de in rr) {
221 Assert.AreEqual ("byteArrayTest", de.Key, "#1");
222 Assert.AreEqual (content, de.Value, "#2");
226 Assert.AreEqual (1, entryCount, "#3");
231 [Category ("MobileNotWorking")]
232 public void GetResourceDataNullName ()
234 ResourceReader r = new ResourceReader ("Test/resources/StreamTest.resources");
239 r.GetResourceData (null, out type, out bytes);
241 } catch (ArgumentNullException ex) {
242 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
243 Assert.IsNull (ex.InnerException, "#3");
244 Assert.IsNotNull (ex.Message, "#4");
245 Assert.IsNotNull (ex.ParamName, "#5");
246 Assert.AreEqual ("resourceName", ex.ParamName, "#6");
253 [Category ("MobileNotWorking")]
254 public void GetResourceData ()
256 byte [] t1 = new byte [] {0x16, 0x00, 0x00, 0x00, 0x76, 0x65, 0x72, 0x69, 0x74, 0x61, 0x73, 0x20, 0x76, 0x6F, 0x73, 0x20, 0x6C, 0x69, 0x62, 0x65, 0x72, 0x61, 0x62, 0x69, 0x74, 0x0A};
257 byte [] t2 = new byte [] {0x0A, 0x73, 0x6F, 0x6D, 0x65, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67};
258 byte [] t3 = new byte [] {0x0E, 0x00, 0x00, 0x00, 0x73, 0x68, 0x61, 0x72, 0x64, 0x65, 0x6E, 0x66, 0x72, 0x65, 0x75, 0x64, 0x65, 0x0A};
260 ResourceReader r = new ResourceReader ("Test/resources/StreamTest.resources");
261 Hashtable items = new Hashtable ();
262 foreach (DictionaryEntry de in r) {
265 r.GetResourceData ((string) de.Key, out type, out bytes);
266 items [de.Key] = new DictionaryEntry (type, bytes);
269 DictionaryEntry p = (DictionaryEntry) items ["test"];
270 Assert.AreEqual ("ResourceTypeCode.Stream", p.Key as string, "#1-1");
271 Assert.AreEqual (t1, p.Value as byte [], "#1-2");
273 p = (DictionaryEntry) items ["test2"];
274 Assert.AreEqual ("ResourceTypeCode.String", p.Key as string, "#2-1");
275 Assert.AreEqual (t2, p.Value as byte [], "#2-2");
277 p = (DictionaryEntry) items ["test3"];
278 Assert.AreEqual ("ResourceTypeCode.ByteArray", p.Key as string, "#3-1");
279 Assert.AreEqual (t3, p.Value as byte [], "#3-2");
285 [Category ("MobileNotWorking")]
286 public void GetResourceData2 ()
288 byte [] expected = new byte [] {
289 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
290 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x51, 0x53,
292 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x44, 0x72,
293 0x61, 0x77, 0x69, 0x6E, 0x67, 0x2C, 0x20, 0x56,
294 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x32,
295 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2C, 0x20,
296 0x43, 0x75, 0x6C, 0x74, 0x75, 0x72, 0x65, 0x3D,
297 0x6E, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6C, 0x2C,
298 0x20, 0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x4B,
299 0x65, 0x79, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x3D,
300 0x62, 0x30, 0x33, 0x66, 0x35, 0x66, 0x37, 0x66,
301 0x31, 0x31, 0x64, 0x35, 0x30, 0x61, 0x33, 0x61,
302 0x05, 0x01, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79,
303 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x44, 0x72, 0x61,
304 0x77, 0x69, 0x6E, 0x67, 0x2E, 0x53, 0x69, 0x7A,
305 0x65, 0x02, 0x00, 0x00, 0x00, 0x05, 0x77, 0x69,
306 0x64, 0x74, 0x68, 0x06, 0x68, 0x65, 0x69, 0x67,
307 0x68, 0x74, 0x00, 0x00, 0x08, 0x08, 0x02, 0x00,
308 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00,
310 ResourceReader r = new ResourceReader ("Test/resources/bug81759.resources");
313 r.GetResourceData ("imageList.ImageSize", out type, out bytes);
314 // Note that const should not be used here.
315 Assert.AreEqual ("System.Drawing.Size, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", type, "#1");
316 Assert.AreEqual (expected, bytes, "#2");
320 // we currently do not support writing v2 resource files
321 private static readonly byte [] byte_resource_v2 = new byte [] {
322 0xce, 0xca, 0xef, 0xbe, 0x01, 0x00, 0x00, 0x00, 0x91, 0x00, 0x00,
323 0x00, 0x6c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x65,
324 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x52, 0x65, 0x73,
325 0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72,
326 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c,
327 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e,
328 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
329 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
330 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
331 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
332 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
333 0x23, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x65, 0x73,
334 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x52, 0x75, 0x6e, 0x74,
335 0x69, 0x6d, 0x65, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65,
336 0x53, 0x65, 0x74, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
337 0x00, 0x00, 0x00, 0x00, 0x50, 0x41, 0x44, 0x50, 0x41, 0x44, 0x50,
338 0x80, 0x88, 0x5a, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xdb, 0x00, 0x00,
339 0x00, 0x1a, 0x62, 0x00, 0x79, 0x00, 0x74, 0x00, 0x65, 0x00, 0x41,
340 0x00, 0x72, 0x00, 0x72, 0x00, 0x61, 0x00, 0x79, 0x00, 0x54, 0x00,
341 0x65, 0x00, 0x73, 0x00, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
342 0x06, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };