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 [ExpectedException (typeof (BadImageFormatException))]
94 public void ConstructorString_Bad ()
96 Assert.IsTrue (File.Exists (m_BadResourceFile));
97 new ResourceReader (m_BadResourceFile);
101 [Category ("MobileNotWorking")]
102 public void ConstructorString ()
104 if (!File.Exists (m_ResourceFile)) {
105 Assert.Fail ("Resource file is not where it should be:" + Path.Combine (Directory.GetCurrentDirectory (), m_ResourceFile));
107 ResourceReader r = new ResourceReader (m_ResourceFile);
108 Assert.IsNotNull (r, "ResourceReader");
113 public void ConstructorStream_Null ()
116 new ResourceReader ((Stream) null);
118 } catch (ArgumentNullException ex) {
119 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
120 Assert.IsNull (ex.InnerException, "#3");
121 Assert.IsNotNull (ex.Message, "#4");
122 Assert.IsNotNull (ex.ParamName, "#5");
123 Assert.AreEqual ("stream", ex.ParamName, "#6");
128 [Category ("MobileNotWorking")]
129 public void ConstructorStream_Closed ()
131 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
135 new ResourceReader (stream);
137 } catch (ArgumentException ex) {
138 // Stream was not readable
139 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
140 Assert.IsNull (ex.InnerException, "#3");
141 Assert.IsNotNull (ex.Message, "#4");
142 Assert.IsNull (ex.ParamName, "#5");
147 [Category ("MobileNotWorking")]
148 public void Stream ()
150 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
151 ResourceReader r = new ResourceReader (stream);
152 Assert.IsNotNull (r, "ResourceReader");
157 [Category ("MobileNotWorking")]
160 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
161 ResourceReader r = new ResourceReader (stream);
164 stream = new FileStream (m_ResourceFile, FileMode.Open);
165 Assert.IsNotNull (stream, "FileStream");
170 [Category ("MobileNotWorking")]
171 public void Enumerator ()
173 Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
174 ResourceReader reader = new ResourceReader (stream);
176 IDictionaryEnumerator en = reader.GetEnumerator ();
177 // Goes through the enumerator, printing out the key and value pairs.
178 while (en.MoveNext ()) {
179 DictionaryEntry de = (DictionaryEntry) en.Current;
180 Assert.IsTrue (String.Empty != (string) de.Key, "Current.Key should not be empty");
181 Assert.IsTrue (String.Empty != (string) de.Value, "Current.Value should not be empty");
182 Assert.IsTrue (String.Empty != (string) en.Key, "Entry.Key should not be empty");
183 Assert.IsTrue (String.Empty != (string) en.Value, "Entry.Value should not be empty");
189 public void ReadNullResource ()
191 MemoryStream stream = new MemoryStream ();
193 ResourceWriter rw = new ResourceWriter (stream);
194 rw.AddResource ("NullTest", value);
198 using (ResourceReader rr = new ResourceReader (stream)) {
200 foreach (DictionaryEntry de in rr) {
201 Assert.AreEqual ("NullTest", de.Key, "#1");
202 Assert.IsNull (de.Value, "#2");
203 Assert.AreEqual (0, entryCount, "#3");
206 Assert.AreEqual (1, entryCount, "#4");
211 public void ByteArray ()
213 byte [] content = new byte [] { 1, 2, 3, 4, 5, 6 };
215 Stream stream = null;
217 // we currently do not support writing v2 resource files
218 stream = new MemoryStream ();
219 stream.Write (byte_resource_v2, 0, byte_resource_v2.Length);
224 using (IResourceReader rr = new ResourceReader (stream)) {
225 foreach (DictionaryEntry de in rr) {
226 Assert.AreEqual ("byteArrayTest", de.Key, "#1");
227 Assert.AreEqual (content, de.Value, "#2");
231 Assert.AreEqual (1, entryCount, "#3");
236 [Category ("MobileNotWorking")]
237 public void GetResourceDataNullName ()
239 ResourceReader r = new ResourceReader ("Test/resources/StreamTest.resources");
244 r.GetResourceData (null, out type, out bytes);
246 } catch (ArgumentNullException ex) {
247 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
248 Assert.IsNull (ex.InnerException, "#3");
249 Assert.IsNotNull (ex.Message, "#4");
250 Assert.IsNotNull (ex.ParamName, "#5");
251 Assert.AreEqual ("resourceName", ex.ParamName, "#6");
258 [Category ("MobileNotWorking")]
259 public void GetResourceData ()
261 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};
262 byte [] t2 = new byte [] {0x0A, 0x73, 0x6F, 0x6D, 0x65, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67};
263 byte [] t3 = new byte [] {0x0E, 0x00, 0x00, 0x00, 0x73, 0x68, 0x61, 0x72, 0x64, 0x65, 0x6E, 0x66, 0x72, 0x65, 0x75, 0x64, 0x65, 0x0A};
265 ResourceReader r = new ResourceReader ("Test/resources/StreamTest.resources");
266 Hashtable items = new Hashtable ();
267 foreach (DictionaryEntry de in r) {
270 r.GetResourceData ((string) de.Key, out type, out bytes);
271 items [de.Key] = new DictionaryEntry (type, bytes);
274 DictionaryEntry p = (DictionaryEntry) items ["test"];
275 Assert.AreEqual ("ResourceTypeCode.Stream", p.Key as string, "#1-1");
276 Assert.AreEqual (t1, p.Value as byte [], "#1-2");
278 p = (DictionaryEntry) items ["test2"];
279 Assert.AreEqual ("ResourceTypeCode.String", p.Key as string, "#2-1");
280 Assert.AreEqual (t2, p.Value as byte [], "#2-2");
282 p = (DictionaryEntry) items ["test3"];
283 Assert.AreEqual ("ResourceTypeCode.ByteArray", p.Key as string, "#3-1");
284 Assert.AreEqual (t3, p.Value as byte [], "#3-2");
290 [Category ("MobileNotWorking")]
291 public void GetResourceData2 ()
293 byte [] expected = new byte [] {
294 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
295 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
296 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x51, 0x53,
297 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x44, 0x72,
298 0x61, 0x77, 0x69, 0x6E, 0x67, 0x2C, 0x20, 0x56,
299 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x32,
300 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2C, 0x20,
301 0x43, 0x75, 0x6C, 0x74, 0x75, 0x72, 0x65, 0x3D,
302 0x6E, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6C, 0x2C,
303 0x20, 0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x4B,
304 0x65, 0x79, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x3D,
305 0x62, 0x30, 0x33, 0x66, 0x35, 0x66, 0x37, 0x66,
306 0x31, 0x31, 0x64, 0x35, 0x30, 0x61, 0x33, 0x61,
307 0x05, 0x01, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79,
308 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x44, 0x72, 0x61,
309 0x77, 0x69, 0x6E, 0x67, 0x2E, 0x53, 0x69, 0x7A,
310 0x65, 0x02, 0x00, 0x00, 0x00, 0x05, 0x77, 0x69,
311 0x64, 0x74, 0x68, 0x06, 0x68, 0x65, 0x69, 0x67,
312 0x68, 0x74, 0x00, 0x00, 0x08, 0x08, 0x02, 0x00,
313 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00,
315 ResourceReader r = new ResourceReader ("Test/resources/bug81759.resources");
318 r.GetResourceData ("imageList.ImageSize", out type, out bytes);
319 // Note that const should not be used here.
320 Assert.AreEqual ("System.Drawing.Size, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", type, "#1");
321 Assert.AreEqual (expected, bytes, "#2");
325 // we currently do not support writing v2 resource files
326 private static readonly byte [] byte_resource_v2 = new byte [] {
327 0xce, 0xca, 0xef, 0xbe, 0x01, 0x00, 0x00, 0x00, 0x91, 0x00, 0x00,
328 0x00, 0x6c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x65,
329 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x52, 0x65, 0x73,
330 0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72,
331 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c,
332 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e,
333 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
334 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
335 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
336 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
337 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
338 0x23, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x65, 0x73,
339 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x52, 0x75, 0x6e, 0x74,
340 0x69, 0x6d, 0x65, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65,
341 0x53, 0x65, 0x74, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
342 0x00, 0x00, 0x00, 0x00, 0x50, 0x41, 0x44, 0x50, 0x41, 0x44, 0x50,
343 0x80, 0x88, 0x5a, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xdb, 0x00, 0x00,
344 0x00, 0x1a, 0x62, 0x00, 0x79, 0x00, 0x74, 0x00, 0x65, 0x00, 0x41,
345 0x00, 0x72, 0x00, 0x72, 0x00, 0x61, 0x00, 0x79, 0x00, 0x54, 0x00,
346 0x65, 0x00, 0x73, 0x00, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
347 0x06, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };