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 char ds = Path.DirectorySeparatorChar;
33 string base_path = Path.Combine (Directory.GetCurrentDirectory (), Path.Combine ("Test", "resources"));
34 m_ResourceFile = Path.Combine (base_path, "MyResources.resources");
35 m_BadResourceFile = Path.Combine (base_path, "Empty.resources");
37 m_ResourceFile = Path.Combine ("Test", Path.Combine ("resources", "MyResources.resources"));
38 m_BadResourceFile = "resources" + ds + "Empty.resources";
45 _tempResourceFile = Path.GetTempFileName ();
49 public void TearDown ()
51 File.Delete (_tempResourceFile);
55 public void ConstructorString_Path_Null ()
58 new ResourceReader ((string) null);
60 } catch (ArgumentNullException ex) {
61 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
62 Assert.IsNull (ex.InnerException, "#3");
63 Assert.IsNotNull (ex.Message, "#4");
64 Assert.IsNotNull (ex.ParamName, "#5");
65 Assert.AreEqual ("path", ex.ParamName, "#6");
70 public void ConstructorString_Path_Empty ()
73 new ResourceReader (String.Empty);
75 } catch (ArgumentException ex) {
76 // Empty path name is not legal
77 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
78 Assert.IsNull (ex.InnerException, "#3");
79 Assert.IsNotNull (ex.Message, "#4");
80 Assert.IsNull (ex.ParamName, "#5");
85 [ExpectedException (typeof (FileNotFoundException))]
86 public void ConstructorString_NotFound ()
88 // use a file name that is *very* unlikely to exsist
89 new ResourceReader ("j38f8axvnn9h38hfa9nxn93f8hav4zvag87vvah32o");
93 [Ignore ("Not covered in the docs, not sure what the correct behavior should be for this")]
94 [ExpectedException (typeof (DirectoryNotFoundException))]
95 public void ConstructorString_Bad ()
97 Assert.IsTrue (File.Exists (m_BadResourceFile));
98 new ResourceReader (m_BadResourceFile);
102 [Category ("MobileNotWorking")]
103 public void ConstructorString ()
105 if (!File.Exists (m_ResourceFile)) {
106 Assert.Fail ("Resource file is not where it should be:" + Path.Combine (Directory.GetCurrentDirectory (), m_ResourceFile));
108 ResourceReader r = new ResourceReader (m_ResourceFile);
109 Assert.IsNotNull (r, "ResourceReader");
114 public void ConstructorStream_Null ()
117 new ResourceReader ((Stream) null);
119 } catch (ArgumentNullException ex) {
120 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
121 Assert.IsNull (ex.InnerException, "#3");
122 Assert.IsNotNull (ex.Message, "#4");
123 Assert.IsNotNull (ex.ParamName, "#5");
124 Assert.AreEqual ("stream", ex.ParamName, "#6");
129 [Category ("MobileNotWorking")]
130 public void ConstructorStream_Closed ()
132 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
136 new ResourceReader (stream);
138 } catch (ArgumentException ex) {
139 // Stream was not readable
140 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
141 Assert.IsNull (ex.InnerException, "#3");
142 Assert.IsNotNull (ex.Message, "#4");
143 Assert.IsNull (ex.ParamName, "#5");
148 [Category ("MobileNotWorking")]
149 public void Stream ()
151 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
152 ResourceReader r = new ResourceReader (stream);
153 Assert.IsNotNull (r, "ResourceReader");
158 [Category ("MobileNotWorking")]
161 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
162 ResourceReader r = new ResourceReader (stream);
165 stream = new FileStream (m_ResourceFile, FileMode.Open);
166 Assert.IsNotNull (stream, "FileStream");
171 [Category ("MobileNotWorking")]
172 public void Enumerator ()
174 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
175 ResourceReader reader = new ResourceReader (stream);
177 IDictionaryEnumerator en = reader.GetEnumerator ();
178 // Goes through the enumerator, printing out the key and value pairs.
179 while (en.MoveNext ()) {
180 DictionaryEntry de = (DictionaryEntry) en.Current;
181 Assert.IsTrue (String.Empty != (string) de.Key, "Current.Key should not be empty");
182 Assert.IsTrue (String.Empty != (string) de.Value, "Current.Value should not be empty");
183 Assert.IsTrue (String.Empty != (string) en.Key, "Entry.Key should not be empty");
184 Assert.IsTrue (String.Empty != (string) en.Value, "Entry.Value should not be empty");
190 public void ReadNullResource ()
192 MemoryStream stream = new MemoryStream ();
194 ResourceWriter rw = new ResourceWriter (stream);
195 rw.AddResource ("NullTest", value);
199 using (ResourceReader rr = new ResourceReader (stream)) {
201 foreach (DictionaryEntry de in rr) {
202 Assert.AreEqual ("NullTest", de.Key, "#1");
203 Assert.IsNull (de.Value, "#2");
204 Assert.AreEqual (0, entryCount, "#3");
207 Assert.AreEqual (1, entryCount, "#4");
212 public void ByteArray ()
214 byte [] content = new byte [] { 1, 2, 3, 4, 5, 6 };
216 Stream stream = null;
218 // we currently do not support writing v2 resource files
219 stream = new MemoryStream ();
220 stream.Write (byte_resource_v2, 0, byte_resource_v2.Length);
225 using (IResourceReader rr = new ResourceReader (stream)) {
226 foreach (DictionaryEntry de in rr) {
227 Assert.AreEqual ("byteArrayTest", de.Key, "#1");
228 Assert.AreEqual (content, de.Value, "#2");
232 Assert.AreEqual (1, entryCount, "#3");
237 [Category ("MobileNotWorking")]
238 public void GetResourceDataNullName ()
240 ResourceReader r = new ResourceReader ("Test/resources/StreamTest.resources");
245 r.GetResourceData (null, out type, out bytes);
247 } catch (ArgumentNullException ex) {
248 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
249 Assert.IsNull (ex.InnerException, "#3");
250 Assert.IsNotNull (ex.Message, "#4");
251 Assert.IsNotNull (ex.ParamName, "#5");
252 Assert.AreEqual ("resourceName", ex.ParamName, "#6");
259 [Category ("MobileNotWorking")]
260 public void GetResourceData ()
262 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};
263 byte [] t2 = new byte [] {0x0A, 0x73, 0x6F, 0x6D, 0x65, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67};
264 byte [] t3 = new byte [] {0x0E, 0x00, 0x00, 0x00, 0x73, 0x68, 0x61, 0x72, 0x64, 0x65, 0x6E, 0x66, 0x72, 0x65, 0x75, 0x64, 0x65, 0x0A};
266 ResourceReader r = new ResourceReader ("Test/resources/StreamTest.resources");
267 Hashtable items = new Hashtable ();
268 foreach (DictionaryEntry de in r) {
271 r.GetResourceData ((string) de.Key, out type, out bytes);
272 items [de.Key] = new DictionaryEntry (type, bytes);
275 DictionaryEntry p = (DictionaryEntry) items ["test"];
276 Assert.AreEqual ("ResourceTypeCode.Stream", p.Key as string, "#1-1");
277 Assert.AreEqual (t1, p.Value as byte [], "#1-2");
279 p = (DictionaryEntry) items ["test2"];
280 Assert.AreEqual ("ResourceTypeCode.String", p.Key as string, "#2-1");
281 Assert.AreEqual (t2, p.Value as byte [], "#2-2");
283 p = (DictionaryEntry) items ["test3"];
284 Assert.AreEqual ("ResourceTypeCode.ByteArray", p.Key as string, "#3-1");
285 Assert.AreEqual (t3, p.Value as byte [], "#3-2");
291 [Category ("MobileNotWorking")]
292 public void GetResourceData2 ()
294 byte [] expected = new byte [] {
295 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
296 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
297 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x51, 0x53,
298 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x44, 0x72,
299 0x61, 0x77, 0x69, 0x6E, 0x67, 0x2C, 0x20, 0x56,
300 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x32,
301 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2C, 0x20,
302 0x43, 0x75, 0x6C, 0x74, 0x75, 0x72, 0x65, 0x3D,
303 0x6E, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6C, 0x2C,
304 0x20, 0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x4B,
305 0x65, 0x79, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x3D,
306 0x62, 0x30, 0x33, 0x66, 0x35, 0x66, 0x37, 0x66,
307 0x31, 0x31, 0x64, 0x35, 0x30, 0x61, 0x33, 0x61,
308 0x05, 0x01, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79,
309 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x44, 0x72, 0x61,
310 0x77, 0x69, 0x6E, 0x67, 0x2E, 0x53, 0x69, 0x7A,
311 0x65, 0x02, 0x00, 0x00, 0x00, 0x05, 0x77, 0x69,
312 0x64, 0x74, 0x68, 0x06, 0x68, 0x65, 0x69, 0x67,
313 0x68, 0x74, 0x00, 0x00, 0x08, 0x08, 0x02, 0x00,
314 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00,
316 ResourceReader r = new ResourceReader ("Test/resources/bug81759.resources");
319 r.GetResourceData ("imageList.ImageSize", out type, out bytes);
320 // Note that const should not be used here.
321 Assert.AreEqual ("System.Drawing.Size, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", type, "#1");
322 Assert.AreEqual (expected, bytes, "#2");
326 // we currently do not support writing v2 resource files
327 private static readonly byte [] byte_resource_v2 = new byte [] {
328 0xce, 0xca, 0xef, 0xbe, 0x01, 0x00, 0x00, 0x00, 0x91, 0x00, 0x00,
329 0x00, 0x6c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x65,
330 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x52, 0x65, 0x73,
331 0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72,
332 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c,
333 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e,
334 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
335 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
336 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
337 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
338 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
339 0x23, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x65, 0x73,
340 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x52, 0x75, 0x6e, 0x74,
341 0x69, 0x6d, 0x65, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65,
342 0x53, 0x65, 0x74, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
343 0x00, 0x00, 0x00, 0x00, 0x50, 0x41, 0x44, 0x50, 0x41, 0x44, 0x50,
344 0x80, 0x88, 0x5a, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xdb, 0x00, 0x00,
345 0x00, 0x1a, 0x62, 0x00, 0x79, 0x00, 0x74, 0x00, 0x65, 0x00, 0x41,
346 0x00, 0x72, 0x00, 0x72, 0x00, 0x61, 0x00, 0x79, 0x00, 0x54, 0x00,
347 0x65, 0x00, 0x73, 0x00, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
348 0x06, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };